2018-10-23 09:34:00 +02:00
|
|
|
/**
|
|
|
|
* CodeX Sanitizer
|
|
|
|
*
|
|
|
|
* @module Sanitizer
|
|
|
|
* Clears HTML from taint tags
|
|
|
|
*
|
|
|
|
* @version 2.0.0
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* Module can be used within two ways:
|
|
|
|
* 1) When you have an instance
|
|
|
|
* - this.Editor.Sanitizer.clean(yourTaintString);
|
|
|
|
* 2) As static method
|
2019-02-28 12:01:32 +01:00
|
|
|
* - EditorJS.Sanitizer.clean(yourTaintString, yourCustomConfiguration);
|
2018-10-23 09:34:00 +02:00
|
|
|
*
|
|
|
|
* {@link SanitizerConfig}
|
|
|
|
*/
|
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
import Module from '../__module';
|
2019-11-30 21:42:39 +01:00
|
|
|
import * as _ from '../utils';
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} SanitizerConfig
|
|
|
|
* @property {Object} tags - define tags restrictions
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* tags : {
|
|
|
|
* p: true,
|
|
|
|
* a: {
|
|
|
|
* href: true,
|
|
|
|
* rel: "nofollow",
|
|
|
|
* target: "_blank"
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
import HTMLJanitor from 'html-janitor';
|
2018-11-23 17:11:50 +01:00
|
|
|
import {BlockToolData, InlineToolConstructable, SanitizerConfig} from '../../../types';
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
export default class Sanitizer extends Module {
|
|
|
|
/**
|
|
|
|
* Memoize tools config
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
private configCache: {[toolName: string]: SanitizerConfig} = {};
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cached inline tools config
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
private inlineToolsConfigCache: SanitizerConfig | null = null;
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize Blocks
|
|
|
|
*
|
|
|
|
* Enumerate blocks and clean data
|
|
|
|
*
|
2018-11-23 17:11:50 +01:00
|
|
|
* @param {{tool, data: BlockToolData}[]} blocksData[]
|
2018-10-23 09:34:00 +02:00
|
|
|
*/
|
|
|
|
public sanitizeBlocks(
|
2018-11-23 17:11:50 +01:00
|
|
|
blocksData: Array<{tool: string, data: BlockToolData}>,
|
|
|
|
): Array<{tool: string, data: BlockToolData}> {
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
return blocksData.map((block) => {
|
|
|
|
const toolConfig = this.composeToolConfig(block.tool);
|
|
|
|
|
2018-10-23 14:33:03 +02:00
|
|
|
if (_.isEmpty(toolConfig)) {
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2018-10-23 09:34:00 +02:00
|
|
|
block.data = this.deepSanitize(block.data, toolConfig);
|
|
|
|
|
|
|
|
return block;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method recursively reduces Block's data and cleans with passed rules
|
|
|
|
*
|
2018-11-23 17:11:50 +01:00
|
|
|
* @param {BlockToolData|object|*} dataToSanitize - taint string or object/array that contains taint string
|
|
|
|
* @param {SanitizerConfig} rules - object with sanitizer rules
|
2018-10-23 09:34:00 +02:00
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
public deepSanitize(dataToSanitize: any, rules: SanitizerConfig): any {
|
2018-10-23 09:34:00 +02:00
|
|
|
/**
|
|
|
|
* BlockData It may contain 3 types:
|
|
|
|
* - Array
|
|
|
|
* - Object
|
|
|
|
* - Primitive
|
|
|
|
*/
|
|
|
|
if (Array.isArray(dataToSanitize)) {
|
|
|
|
/**
|
|
|
|
* Array: call sanitize for each item
|
|
|
|
*/
|
|
|
|
return this.cleanArray(dataToSanitize, rules);
|
|
|
|
} else if (typeof dataToSanitize === 'object') {
|
|
|
|
/**
|
|
|
|
* Objects: just clean object deeper.
|
|
|
|
*/
|
|
|
|
return this.cleanObject(dataToSanitize, rules);
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Primitives (number|string|boolean): clean this item
|
2018-11-09 18:15:49 +01:00
|
|
|
*
|
|
|
|
* Clean only strings
|
2018-10-23 09:34:00 +02:00
|
|
|
*/
|
2018-11-09 18:15:49 +01:00
|
|
|
if (typeof dataToSanitize === 'string') {
|
|
|
|
return this.cleanOneItem(dataToSanitize, rules);
|
|
|
|
}
|
|
|
|
return dataToSanitize;
|
2018-10-23 09:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans string from unwanted tags
|
|
|
|
* Method allows to use default config
|
|
|
|
*
|
2018-11-09 18:15:49 +01:00
|
|
|
* @param {string} taintString - taint string
|
2018-10-23 09:34:00 +02:00
|
|
|
* @param {SanitizerConfig} customConfig - allowed tags
|
|
|
|
*
|
2018-11-09 18:15:49 +01:00
|
|
|
* @return {string} clean HTML
|
2018-10-23 09:34:00 +02:00
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
public clean(taintString: string, customConfig: SanitizerConfig = {} as SanitizerConfig): string {
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
const sanitizerConfig = {
|
|
|
|
tags: customConfig,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API client can use custom config to manage sanitize process
|
|
|
|
*/
|
|
|
|
const sanitizerInstance = this.createHTMLJanitorInstance(sanitizerConfig);
|
|
|
|
return sanitizerInstance.clean(taintString);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge with inline tool config
|
|
|
|
*
|
|
|
|
* @param {string} toolName
|
2018-11-23 17:11:50 +01:00
|
|
|
* @param {SanitizerConfig} toolRules
|
|
|
|
* @return {SanitizerConfig}
|
2018-10-23 09:34:00 +02:00
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
public composeToolConfig(toolName: string): SanitizerConfig {
|
2018-10-23 09:34:00 +02:00
|
|
|
/**
|
|
|
|
* If cache is empty, then compose tool config and put it to the cache object
|
|
|
|
*/
|
|
|
|
if (this.configCache[toolName]) {
|
|
|
|
return this.configCache[toolName];
|
|
|
|
}
|
|
|
|
|
2019-09-09 12:03:18 +02:00
|
|
|
const sanitizeGetter = this.Editor.Tools.INTERNAL_SETTINGS.SANITIZE_CONFIG;
|
2018-10-23 09:34:00 +02:00
|
|
|
const toolClass = this.Editor.Tools.available[toolName];
|
2018-10-23 14:33:03 +02:00
|
|
|
const baseConfig = this.getInlineToolsConfig(toolName);
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If Tools doesn't provide sanitizer config or it is empty
|
|
|
|
*/
|
|
|
|
if (!toolClass.sanitize || (toolClass[sanitizeGetter] && _.isEmpty(toolClass[sanitizeGetter]))) {
|
2018-10-23 14:33:03 +02:00
|
|
|
return baseConfig;
|
2018-10-23 09:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const toolRules = toolClass.sanitize;
|
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
const toolConfig = {} as SanitizerConfig;
|
2018-10-23 09:34:00 +02:00
|
|
|
for (const fieldName in toolRules) {
|
|
|
|
if (toolRules.hasOwnProperty(fieldName)) {
|
|
|
|
const rule = toolRules[fieldName];
|
|
|
|
if (typeof rule === 'object') {
|
|
|
|
toolConfig[fieldName] = Object.assign({}, baseConfig, rule);
|
|
|
|
} else {
|
|
|
|
toolConfig[fieldName] = rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.configCache[toolName] = toolConfig;
|
|
|
|
|
|
|
|
return toolConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns Sanitizer config
|
|
|
|
* When Tool's "inlineToolbar" value is True, get all sanitizer rules from all tools,
|
|
|
|
* otherwise get only enabled
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
public getInlineToolsConfig(name: string): SanitizerConfig {
|
2018-10-23 09:34:00 +02:00
|
|
|
const {Tools} = this.Editor;
|
2018-11-30 19:57:30 +01:00
|
|
|
const toolsConfig = Tools.getToolSettings(name);
|
|
|
|
const enableInlineTools = toolsConfig.inlineToolbar || [];
|
2018-10-23 09:34:00 +02:00
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
let config = {} as SanitizerConfig;
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
if (typeof enableInlineTools === 'boolean' && enableInlineTools) {
|
|
|
|
/**
|
|
|
|
* getting all tools sanitizer rule
|
|
|
|
*/
|
|
|
|
config = this.getAllInlineToolsConfig();
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* getting only enabled
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
(enableInlineTools as string[]).map( (inlineToolName) => {
|
|
|
|
config = Object.assign(
|
|
|
|
config,
|
2019-09-09 12:03:18 +02:00
|
|
|
Tools.inline[inlineToolName][Tools.INTERNAL_SETTINGS.SANITIZE_CONFIG],
|
2018-11-23 17:11:50 +01:00
|
|
|
) as SanitizerConfig;
|
2018-10-23 09:34:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return general config for all inline tools
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
public getAllInlineToolsConfig(): SanitizerConfig {
|
2018-10-23 09:34:00 +02:00
|
|
|
const {Tools} = this.Editor;
|
|
|
|
|
|
|
|
if (this.inlineToolsConfigCache) {
|
|
|
|
return this.inlineToolsConfigCache;
|
|
|
|
}
|
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
const config: SanitizerConfig = {} as SanitizerConfig;
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
Object.entries(Tools.inline)
|
2018-11-23 17:11:50 +01:00
|
|
|
.forEach( ([name, inlineTool]: [string, InlineToolConstructable]) => {
|
2019-09-09 12:03:18 +02:00
|
|
|
Object.assign(config, inlineTool[Tools.INTERNAL_SETTINGS.SANITIZE_CONFIG]);
|
2018-11-23 17:11:50 +01:00
|
|
|
});
|
2018-10-23 09:34:00 +02:00
|
|
|
|
|
|
|
this.inlineToolsConfigCache = config;
|
|
|
|
|
|
|
|
return this.inlineToolsConfigCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean array
|
|
|
|
* @param {array} array - [1, 2, {}, []]
|
|
|
|
* @param {object} ruleForItem
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
private cleanArray(array: any[], ruleForItem: SanitizerConfig): any[] {
|
2018-10-23 09:34:00 +02:00
|
|
|
return array.map( (arrayItem) => this.deepSanitize(arrayItem, ruleForItem));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean object
|
|
|
|
* @param {object} object - {level: 0, text: 'adada', items: [1,2,3]}}
|
|
|
|
* @param {object} rules - { b: true } or true|false
|
|
|
|
* @return {object}
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
private cleanObject(object: any, rules: SanitizerConfig|{[field: string]: SanitizerConfig}): any {
|
2018-10-23 09:34:00 +02:00
|
|
|
const cleanData = {};
|
|
|
|
|
|
|
|
for (const fieldName in object) {
|
|
|
|
if (!object.hasOwnProperty(fieldName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentIterationItem = object[fieldName];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get object from config by field name
|
|
|
|
* - if it is a HTML Janitor rule, call with this rule
|
|
|
|
* - otherwise, call with parent's config
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
const ruleForItem = this.isRule(rules[fieldName] as SanitizerConfig) ? rules[fieldName] : rules;
|
2018-10-23 09:34:00 +02:00
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
cleanData[fieldName] = this.deepSanitize(currentIterationItem, ruleForItem as SanitizerConfig);
|
2018-10-23 09:34:00 +02:00
|
|
|
}
|
|
|
|
return cleanData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} taintString
|
2018-11-23 17:11:50 +01:00
|
|
|
* @param {SanitizerConfig|boolean} rule
|
2018-10-23 09:34:00 +02:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
private cleanOneItem(taintString: string, rule: SanitizerConfig|boolean): string {
|
2018-10-23 09:34:00 +02:00
|
|
|
if (typeof rule === 'object') {
|
|
|
|
return this.clean(taintString, rule);
|
|
|
|
} else if (rule === false) {
|
2018-11-23 17:11:50 +01:00
|
|
|
return this.clean(taintString, {} as SanitizerConfig);
|
2018-10-23 09:34:00 +02:00
|
|
|
} else {
|
|
|
|
return taintString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if passed item is a HTML Janitor rule:
|
|
|
|
* { a : true }, {}, false, true, function(){} — correct rules
|
|
|
|
* undefined, null, 0, 1, 2 — not a rules
|
|
|
|
* @param config
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
private isRule(config: SanitizerConfig): boolean {
|
2018-10-23 09:34:00 +02:00
|
|
|
return typeof config === 'object' || typeof config === 'boolean' || typeof config === 'function';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If developer uses editor's API, then he can customize sanitize restrictions.
|
|
|
|
* Or, sanitizing config can be defined globally in editors initialization. That config will be used everywhere
|
|
|
|
* At least, if there is no config overrides, that API uses Default configuration
|
|
|
|
*
|
|
|
|
* @uses https://www.npmjs.com/package/html-janitor
|
2018-11-29 10:16:06 +01:00
|
|
|
* @license https://github.com/guardian/html-janitor/blob/master/LICENSE
|
2018-10-23 09:34:00 +02:00
|
|
|
*
|
|
|
|
* @param {SanitizerConfig} config - sanitizer extension
|
|
|
|
*/
|
2018-11-23 17:11:50 +01:00
|
|
|
private createHTMLJanitorInstance(config: {tags: SanitizerConfig}): HTMLJanitor|null {
|
2018-10-23 09:34:00 +02:00
|
|
|
if (config) {
|
|
|
|
return new HTMLJanitor(config);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|