generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitize.js
157 lines (133 loc) · 4.5 KB
/
sanitize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { sanitizer as sanitizerConfig } from '@aegisjsproject/sanitizer/config/html.js';
import { convertConfig, EVENT_ATTRS } from '@aegisjsproject/sanitizer/config-utils.js';
import { createPolicy } from '@aegisjsproject/sanitizer/trust.js';
const LINK_ATTRS = new Set(['href', 'src' , 'action', 'xlink:href']);
const ILLEGAL_PROTOCOLS = new Set(['javascript:', 'data:', 'file:', 'ftp:']);
const policy = createPolicy('aegis-sanitizer#html', { createHTML: input => input });
export function setHTML(el, content, {
elements = sanitizerConfig.elements,
attributes = sanitizerConfig.attributes,
comments = sanitizerConfig.comments,
dataAttributes = sanitizerConfig.dataAttributes,
sanitizer,
...rest
} = sanitizerConfig, { allowInsecure = false } = {}) {
const tmp = document.createElement('template');
tmp.setHTMLUnsafe(policy.createHTML(content));
sanitize(tmp.content, { elements, attributes, comments, dataAttributes, sanitizer, ...rest }, allowInsecure);
el.replaceChildren(tmp.content);
}
export function parseHTML(content, {
elements = sanitizerConfig.elements,
attributes = sanitizerConfig.attributes,
comments = sanitizerConfig.comments,
dataAttributes = sanitizerConfig.dataAttributes,
sanitizer,
...rest
} = sanitizerConfig, { allowInsecure = false } = {}) {
const doc = new DOMParser().parseFromString(policy.createHTML(content), 'text/html');
sanitize(doc, { elements, attributes, comments, dataAttributes, sanitizer, ...rest }, allowInsecure);
return doc;
}
export function sanitize(node, config = sanitizerConfig, allowInsecure = false) {
if (! (node instanceof Node)) {
throw new TypeError('Not a node.');
} else {
const converted = convertConfig(config);
return sanitizeNode(node, converted, allowInsecure);
}
}
function isAllowedElement(el, config) {
return el.namespaceURI in config.elements
&& config.elements[el.namespaceURI].some(opt => opt.name === el.localName);
}
function isIllegalURLAttr(attr) {
if (! LINK_ATTRS.has(attr.name)) {
return false;
} else if (! URL.canParse(attr.value)) {
return false;
} else {
const { protocol } = new URL(attr.value);
return ILLEGAL_PROTOCOLS.has(protocol);
}
}
const isScriptAttr = 'trustedTypes' in globalThis
? attr => trustedTypes.getAttributeType(
attr.ownerElement.tagName, attr.localName,
attr.ownerElement.namespaceURI, attr.namespaceURI,
) !== null
: attr => EVENT_ATTRS.has(attr.localName);
function isAllowedAttr(attr, config, allowInsecure = false) {
const ns = attr.namespaceURI || '';
return (
(! ('dataAttributes' in config) || config.dataAttributes)
&& attr.name.startsWith('data-')
) || (
ns in config.attributes
&& config.attributes[ns].some(opt => opt.name === attr.localName)
&& ! (allowInsecure || isScriptAttr(attr))
&& ! (allowInsecure || isIllegalURLAttr(attr))
);
}
function sanitizeNode(node, config, allowInsecure = false) {
switch(node.nodeType) {
case Node.ELEMENT_NODE:
sanitizeElement(node, config, allowInsecure);
break;
case Node.DOCUMENT_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
sanitizeFragOrDoc(node, config, allowInsecure);
break;
case Node.COMMENT_NODE:
sanitizeComment(node, config, allowInsecure);
break;
case Node.ATTRIBUTE_NODE:
sanitizeAttr(node, config, allowInsecure);
break;
case Node.TEXT_NODE:
case Node.DOCUMENT_TYPE_NODE:
break;
default:
if (node.ownerElement instanceof Element) {
node.ownerElement.removeChild(node);
}
}
}
function sanitizeElement(el, config, allowInsecure) {
if (! isAllowedElement(el, config)) {
el.remove();
} else {
if (el.hasAttributes()) {
const attrs = el.attributes;
for (let i = attrs.length - 1; i !== -1; i--) {
sanitizeAttr(attrs[i], config, allowInsecure);
}
}
if (el.tagName === 'TEMPLATE') {
sanitizeFragOrDoc(el.content, config, allowInsecure);
} else if (el.hasChildNodes()) {
const childNodes = el.childNodes;
for (let i = childNodes.length - 1; i !== -1; i--) {
sanitizeNode(childNodes[i], config, allowInsecure);
}
}
}
}
function sanitizeAttr(attr, config, allowInsecure = false) {
if (! isAllowedAttr(attr, config, allowInsecure) && attr.ownerElement instanceof Element) {
attr.ownerElement.removeAttributeNode(attr);
}
}
function sanitizeFragOrDoc(node, config, allowInsecure = false) {
if (node.hasChildNodes()) {
const childNodes = node.childNodes;
for (let i = childNodes.length - 1; i !== -1; i--) {
sanitizeNode(childNodes[i], config, allowInsecure);
}
}
}
function sanitizeComment(node, config) {
if (! config.comments) {
node.remove();
}
}