generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trust.js
189 lines (165 loc) · 4.52 KB
/
trust.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @copyright 2023-2024 Chris Zuber <admin@kernvalley.us>
*/
export const SUPPORTS_TRUSTED_TYPES = 'trustedTypes' in globalThis;
export function setProp(el, prop, val, {
policy,
} = {}) {
switch(getPropertyType(el.tagName, prop)) {
case 'TrustedScript':
el[prop] = createScript(val, { policy });
break;
case 'TrustedScriptURL':
el[prop] = createScriptURL(val, { policy });
break;
case 'TrustedHTML':
el[prop] = createHTML(val, { policy });
break;
default:
el[prop] = val;
}
}
export function setAttr(el, attr, val, {
elementNs,
policy,
} = {}) {
switch(getAttributeType(el.tagName, attr, elementNs)) {
case 'TrustedScriptURL':
if (typeof elementNs === 'string') {
el.setAttributeNs(elementNs, attr, createScriptURL(val, { policy }));
} else {
el.setAttribute(attr, createScriptURL(val, { policy }));
}
break;
case 'TrustedScript':
if (typeof elementNs === 'string') {
el.setAttributeNS(elementNs, attr, createScript(val, { policy }));
} else {
el.setAttribute(attr, createScript(val, { policy }));
}
break;
case 'TrustedHTML':
if (typeof elementNs === 'string') {
el.setAttributeNS(elementNs, attr, createHTML(val, { policy }));
} else {
el.setAttribute(attr, createHTML(val, { policy }));
}
break;
default:
if (typeof elementNs === 'string') {
el.setAttributeNS(elementNs, attr, val);
} else {
el.setAttribute(attr, val);
}
}
}
export function supported() {
return 'trustedTypes' in globalThis && trustedTypes.createPolicy instanceof Function;
}
export function isTrustPolicy(policy) {
if ('TrustedTypePolicy' in globalThis && policy instanceof TrustedTypePolicy) {
return true;
} else {
return policy != null && policy.createHTML instanceof Function;
}
}
export function hasDefaultPolicy() {
return supported() && isTrustPolicy(trustedTypes.defaultPolicy);
}
export function getAttributeType(tagName, attribute, elementNs) {
if (supported()) {
return trustedTypes.getAttributeType(tagName.toLowerCase(), attribute, elementNs);
} else {
return null;
}
}
export function getPropertyType(tagName, property) {
if (supported()) {
return trustedTypes.getPropertyType(tagName.toLowerCase(), property);
} else {
return null;
}
}
export function isHTML(input) {
if (supported()) {
return trustedTypes.isHTML(input);
} else {
return typeof input === 'string';
}
}
export function isScript(input) {
if (supported()) {
return trustedTypes.isScript(input);
} else {
return typeof input === 'string';
}
}
export function isScriptURL(input) {
if (supported()) {
return trustedTypes.isScriptURL(input);
} else {
return typeof input === 'string' || input instanceof URL;
}
}
export function isTrustedType(input) {
if (supported()) {
return trustedTypes.isHTML(input) || trustedTypes.isScript(input) || trustedTypes.isScriptURL(input);
} else {
return true;
}
}
export function createHTML(input, { policy = getDefaultPolicy() } = {}) {
if (isTrustPolicy(policy) && ! isHTML(input)) {
return policy.createHTML(input);
} else {
return input;
}
}
export function createScript(input, { policy = getDefaultPolicy() } = {}) {
if (isTrustPolicy(policy) && ! isScript(input)) {
return policy.createScript(input);
} else {
return input;
}
}
export function createScriptURL(input, { policy = getDefaultPolicy() } = {}) {
if (isTrustPolicy(policy) && ! isScriptURL(input)) {
return policy.createScriptURL(input);
} else {
return input;
}
}
export function createPolicy(name, {
createHTML = () => {
throw new TypeError('This policy does not provide `createHTML()`');
},
createScript = () => {
throw new TypeError('This policy does not provide `createScript()`');
},
createScriptURL = () => {
throw new TypeError('This policy does not provide `createScriptURL()`');
},
}) {
if (supported()) {
return trustedTypes.createPolicy(name, { createHTML, createScript, createScriptURL });
} else {
return Object.freeze({
name,
createHTML: (input, ...args) => createHTML(input.toString(), ...args),
createScript: (input, ...args) => createScript(input.toString(), ...args),
createScriptURL: (input, ...args) => createScriptURL(input.toString(), ...args),
});
}
}
export function createSanitizerPolicy(name = 'aegis#html') {
return createPolicy(name, {
createHTML(input, sanitizer) {
const el = document.createElement('div');
el.setHTML(input, { sanitizer });
return el.innerHTML;
}
});
}
export function getDefaultPolicy() {
return 'trustedTypes' in globalThis ? trustedTypes.defaultPolicy : null;
}