mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	refactor: one authoritave source for allowed html tags
This commit is contained in:
		| @@ -1,5 +1,6 @@ | ||||
| import OptionsWidget from "../options_widget.js"; | ||||
| import { t } from "../../../../services/i18n.js"; | ||||
| import { DEFAULT_ALLOWED_TAGS } from "../../../../services/html_sanitizer.js"; | ||||
|  | ||||
| const TPL = ` | ||||
| <div class="options-section"> | ||||
| @@ -21,19 +22,6 @@ const TPL = ` | ||||
|     </div> | ||||
| </div>`; | ||||
|  | ||||
| const defaultTags = [ | ||||
|     'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', | ||||
|     'li', 'b', 'i', 'strong', 'em', 'strike', 's', 'del', 'abbr', 'code', 'hr', 'br', 'div', | ||||
|     'table', 'thead', 'caption', 'tbody', 'tfoot', 'tr', 'th', 'td', 'pre', 'section', 'img', | ||||
|     'figure', 'figcaption', 'span', 'label', 'input', 'details', 'summary', 'address', 'aside', 'footer', | ||||
|     'header', 'hgroup', 'main', 'nav', 'dl', 'dt', 'menu', 'bdi', 'bdo', 'dfn', 'kbd', 'mark', 'q', 'time', | ||||
|     'var', 'wbr', 'area', 'map', 'track', 'video', 'audio', 'picture', 'del', 'ins', | ||||
|     'en-media', | ||||
|     'acronym', 'article', 'big', 'button', 'cite', 'col', 'colgroup', 'data', 'dd', | ||||
|     'fieldset', 'form', 'legend', 'meter', 'noscript', 'option', 'progress', 'rp', | ||||
|     'samp', 'small', 'sub', 'sup', 'template', 'textarea', 'tt' | ||||
| ]; | ||||
|  | ||||
| export default class HtmlImportTagsOptions extends OptionsWidget { | ||||
|     doRender() { | ||||
|         this.$widget = $(TPL); | ||||
| @@ -56,13 +44,13 @@ export default class HtmlImportTagsOptions extends OptionsWidget { | ||||
|                 this.$allowedTags.val(tags.join(' ')); | ||||
|             } else { | ||||
|                 // If no tags are set, show the defaults | ||||
|                 this.$allowedTags.val(defaultTags.join(' ')); | ||||
|                 this.$allowedTags.val(DEFAULT_ALLOWED_TAGS.join(' ')); | ||||
|             } | ||||
|         } | ||||
|         catch (e) { | ||||
|             console.error('Could not load HTML tags:', e); | ||||
|             // On error, show the defaults | ||||
|             this.$allowedTags.val(defaultTags.join(' ')); | ||||
|             this.$allowedTags.val(DEFAULT_ALLOWED_TAGS.join(' ')); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -76,7 +64,7 @@ export default class HtmlImportTagsOptions extends OptionsWidget { | ||||
|     } | ||||
|  | ||||
|     async resetToDefault() { | ||||
|         this.$allowedTags.val(defaultTags.join('\n')); // Use actual newline | ||||
|         this.$allowedTags.val(DEFAULT_ALLOWED_TAGS.join('\n')); // Use actual newline | ||||
|         await this.saveTags(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -2,6 +2,21 @@ import sanitizeHtml from "sanitize-html"; | ||||
| import sanitizeUrl from "@braintree/sanitize-url"; | ||||
| import optionService from "./options.js"; | ||||
|  | ||||
| // Default list of allowed HTML tags | ||||
| export const DEFAULT_ALLOWED_TAGS = [ | ||||
|     'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', | ||||
|     'li', 'b', 'i', 'strong', 'em', 'strike', 's', 'del', 'abbr', 'code', 'hr', 'br', 'div', | ||||
|     'table', 'thead', 'caption', 'tbody', 'tfoot', 'tr', 'th', 'td', 'pre', 'section', 'img', | ||||
|     'figure', 'figcaption', 'span', 'label', 'input', 'details', 'summary', 'address', 'aside', 'footer', | ||||
|     'header', 'hgroup', 'main', 'nav', 'dl', 'dt', 'menu', 'bdi', 'bdo', 'dfn', 'kbd', 'mark', 'q', 'time', | ||||
|     'var', 'wbr', 'area', 'map', 'track', 'video', 'audio', 'picture', 'del', 'ins', | ||||
|     'en-media', // for ENEX import | ||||
|     // Additional tags (https://github.com/TriliumNext/Notes/issues/567) | ||||
|     'acronym', 'article', 'big', 'button', 'cite', 'col', 'colgroup', 'data', 'dd', | ||||
|     'fieldset', 'form', 'legend', 'meter', 'noscript', 'option', 'progress', 'rp', | ||||
|     'samp', 'small', 'sub', 'sup', 'template', 'textarea', 'tt' | ||||
| ] as const; | ||||
|  | ||||
| // intended mainly as protection against XSS via import | ||||
| // secondarily, it (partly) protects against "CSS takeover" | ||||
| // sanitize also note titles, label values etc. - there are so many usages which make it difficult | ||||
| @@ -30,19 +45,7 @@ function sanitize(dirtyHtml: string) { | ||||
|         allowedTags = JSON.parse(optionService.getOption('allowedHtmlTags')); | ||||
|     } catch (e) { | ||||
|         // Fallback to default list if option doesn't exist or is invalid | ||||
|         allowedTags = [ | ||||
|             'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', | ||||
|             'li', 'b', 'i', 'strong', 'em', 'strike', 's', 'del', 'abbr', 'code', 'hr', 'br', 'div', | ||||
|             'table', 'thead', 'caption', 'tbody', 'tfoot', 'tr', 'th', 'td', 'pre', 'section', 'img', | ||||
|             'figure', 'figcaption', 'span', 'label', 'input', 'details', 'summary', 'address', 'aside', 'footer', | ||||
|             'header', 'hgroup', 'main', 'nav', 'dl', 'dt', 'menu', 'bdi', 'bdo', 'dfn', 'kbd', 'mark', 'q', 'time', | ||||
|             'var', 'wbr', 'area', 'map', 'track', 'video', 'audio', 'picture', 'del', 'ins', | ||||
|             'en-media', // for ENEX import | ||||
|             // Additional tags (https://github.com/TriliumNext/Notes/issues/567) | ||||
|             'acronym', 'article', 'big', 'button', 'cite', 'col', 'colgroup', 'data', 'dd', | ||||
|             'fieldset', 'form', 'legend', 'meter', 'noscript', 'option', 'progress', 'rp', | ||||
|             'samp', 'small', 'sub', 'sup', 'template', 'textarea', 'tt' | ||||
|         ]; | ||||
|         allowedTags = DEFAULT_ALLOWED_TAGS; | ||||
|     } | ||||
|  | ||||
|     // to minimize document changes, compress H | ||||
|   | ||||
		Reference in New Issue
	
	Block a user