mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 18:50:41 +01:00
safe import implementation
This commit is contained in:
@@ -5,13 +5,14 @@ const sql = require('./sql');
|
||||
const utils = require('./utils');
|
||||
const Attribute = require('../entities/attribute');
|
||||
|
||||
const ATTRIBUTE_TYPES = [ 'label', 'label-definition', 'relation', 'relation-definition' ];
|
||||
|
||||
const BUILTIN_ATTRIBUTES = [
|
||||
// label names
|
||||
{ type: 'label', name: 'disableVersioning' },
|
||||
{ type: 'label', name: 'calendarRoot' },
|
||||
{ type: 'label', name: 'archived' },
|
||||
{ type: 'label', name: 'excludeFromExport' },
|
||||
{ type: 'label', name: 'run' },
|
||||
{ type: 'label', name: 'manualTransactionHandling' },
|
||||
{ type: 'label', name: 'disableInclusion' },
|
||||
{ type: 'label', name: 'appCss' },
|
||||
@@ -19,19 +20,20 @@ const BUILTIN_ATTRIBUTES = [
|
||||
{ type: 'label', name: 'hideChildrenOverview' },
|
||||
{ type: 'label', name: 'hidePromotedAttributes' },
|
||||
{ type: 'label', name: 'readOnly' },
|
||||
{ type: 'label', name: 'customRequestHandler' },
|
||||
{ type: 'label', name: 'customResourceProvider' },
|
||||
{ type: 'label', name: 'run', isDangerous: true },
|
||||
{ type: 'label', name: 'customRequestHandler', isDangerous: true },
|
||||
{ type: 'label', name: 'customResourceProvider', isDangerous: true },
|
||||
|
||||
// relation names
|
||||
{ type: 'relation', name: 'runOnNoteView' },
|
||||
{ type: 'relation', name: 'runOnNoteCreation' },
|
||||
{ type: 'relation', name: 'runOnNoteTitleChange' },
|
||||
{ type: 'relation', name: 'runOnNoteChange' },
|
||||
{ type: 'relation', name: 'runOnChildNoteCreation' },
|
||||
{ type: 'relation', name: 'runOnAttributeCreation' },
|
||||
{ type: 'relation', name: 'runOnAttributeChange' },
|
||||
{ type: 'relation', name: 'runOnNoteView', isDangerous: true },
|
||||
{ type: 'relation', name: 'runOnNoteCreation', isDangerous: true },
|
||||
{ type: 'relation', name: 'runOnNoteTitleChange', isDangerous: true },
|
||||
{ type: 'relation', name: 'runOnNoteChange', isDangerous: true },
|
||||
{ type: 'relation', name: 'runOnChildNoteCreation', isDangerous: true },
|
||||
{ type: 'relation', name: 'runOnAttributeCreation', isDangerous: true },
|
||||
{ type: 'relation', name: 'runOnAttributeChange', isDangerous: true },
|
||||
{ type: 'relation', name: 'template' },
|
||||
{ type: 'relation', name: 'renderNote' }
|
||||
{ type: 'relation', name: 'renderNote', isDangerous: true }
|
||||
];
|
||||
|
||||
async function getNotesWithLabel(name, value) {
|
||||
@@ -94,11 +96,25 @@ async function getAttributeNames(type, nameLike) {
|
||||
return names;
|
||||
}
|
||||
|
||||
function isAttributeType(type) {
|
||||
return ATTRIBUTE_TYPES.includes(type);
|
||||
}
|
||||
|
||||
function isAttributeDangerous(type, name) {
|
||||
return BUILTIN_ATTRIBUTES.some(attr =>
|
||||
attr.type === attr.type &&
|
||||
attr.name.toLowerCase() === name.trim().toLowerCase() &&
|
||||
attr.isDangerous
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNotesWithLabel,
|
||||
getNotesWithLabels,
|
||||
getNoteWithLabel,
|
||||
createLabel,
|
||||
createAttribute,
|
||||
getAttributeNames
|
||||
getAttributeNames,
|
||||
isAttributeType,
|
||||
isAttributeDangerous
|
||||
};
|
||||
@@ -6,6 +6,7 @@ const utils = require('../../services/utils');
|
||||
const log = require('../../services/log');
|
||||
const repository = require('../../services/repository');
|
||||
const noteService = require('../../services/notes');
|
||||
const attributeService = require('../../services/attributes');
|
||||
const Branch = require('../../entities/branch');
|
||||
const tar = require('tar-stream');
|
||||
const stream = require('stream');
|
||||
@@ -153,10 +154,19 @@ async function importTar(importContext, fileBuffer, importRootNote) {
|
||||
for (const attr of noteMeta.attributes) {
|
||||
attr.noteId = note.noteId;
|
||||
|
||||
if (!attributeService.isAttributeType(attr.type)) {
|
||||
log.error("Unrecognized attribute type " + attr.type);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (attr.type === 'relation') {
|
||||
attr.value = getNewNoteId(attr.value);
|
||||
}
|
||||
|
||||
if (importContext.safeImport && attributeService.isAttributeDangerous(attr.type, attr.name)) {
|
||||
attr.name = 'disabled-' + attr.name;
|
||||
}
|
||||
|
||||
attributes.push(attr);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user