@bquery/bquery 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +527 -501
  2. package/dist/{batch-4LAvfLE7.js → batch-x7b2eZST.js} +2 -2
  3. package/dist/{batch-4LAvfLE7.js.map → batch-x7b2eZST.js.map} +1 -1
  4. package/dist/component.es.mjs +1 -1
  5. package/dist/core/collection.d.ts +19 -3
  6. package/dist/core/collection.d.ts.map +1 -1
  7. package/dist/core/element.d.ts +23 -4
  8. package/dist/core/element.d.ts.map +1 -1
  9. package/dist/core/index.d.ts +1 -0
  10. package/dist/core/index.d.ts.map +1 -1
  11. package/dist/core/utils/function.d.ts +21 -4
  12. package/dist/core/utils/function.d.ts.map +1 -1
  13. package/dist/{core-COenAZjD.js → core-BhpuvPhy.js} +62 -37
  14. package/dist/core-BhpuvPhy.js.map +1 -0
  15. package/dist/core.es.mjs +174 -131
  16. package/dist/core.es.mjs.map +1 -1
  17. package/dist/full.es.mjs +7 -7
  18. package/dist/full.iife.js +2 -2
  19. package/dist/full.iife.js.map +1 -1
  20. package/dist/full.umd.js +2 -2
  21. package/dist/full.umd.js.map +1 -1
  22. package/dist/index.es.mjs +7 -7
  23. package/dist/motion.es.mjs.map +1 -1
  24. package/dist/{persisted-Dz_ryNuC.js → persisted-DHoi3uEs.js} +4 -4
  25. package/dist/{persisted-Dz_ryNuC.js.map → persisted-DHoi3uEs.js.map} +1 -1
  26. package/dist/platform/storage.d.ts.map +1 -1
  27. package/dist/platform.es.mjs +12 -7
  28. package/dist/platform.es.mjs.map +1 -1
  29. package/dist/reactive/core.d.ts +12 -0
  30. package/dist/reactive/core.d.ts.map +1 -1
  31. package/dist/reactive/effect.d.ts.map +1 -1
  32. package/dist/reactive/internals.d.ts +6 -0
  33. package/dist/reactive/internals.d.ts.map +1 -1
  34. package/dist/reactive.es.mjs +6 -6
  35. package/dist/router.es.mjs +1 -1
  36. package/dist/{sanitize-1FBEPAFH.js → sanitize-Cxvxa-DX.js} +50 -39
  37. package/dist/sanitize-Cxvxa-DX.js.map +1 -0
  38. package/dist/security/sanitize-core.d.ts.map +1 -1
  39. package/dist/security.es.mjs +2 -2
  40. package/dist/store.es.mjs +2 -2
  41. package/dist/type-guards-BdKlYYlS.js +32 -0
  42. package/dist/type-guards-BdKlYYlS.js.map +1 -0
  43. package/dist/untrack-DNnnqdlR.js +6 -0
  44. package/dist/{untrack-BuEQKH7_.js.map → untrack-DNnnqdlR.js.map} +1 -1
  45. package/dist/view/evaluate.d.ts.map +1 -1
  46. package/dist/view.es.mjs +157 -151
  47. package/dist/view.es.mjs.map +1 -1
  48. package/dist/{watch-CXyaBC_9.js → watch-DXXv3iAI.js} +3 -3
  49. package/dist/{watch-CXyaBC_9.js.map → watch-DXXv3iAI.js.map} +1 -1
  50. package/package.json +132 -132
  51. package/src/core/collection.ts +628 -588
  52. package/src/core/element.ts +774 -746
  53. package/src/core/index.ts +48 -47
  54. package/src/core/utils/function.ts +151 -110
  55. package/src/motion/animate.ts +113 -113
  56. package/src/motion/flip.ts +176 -176
  57. package/src/motion/scroll.ts +57 -57
  58. package/src/motion/spring.ts +150 -150
  59. package/src/motion/timeline.ts +246 -246
  60. package/src/motion/transition.ts +51 -51
  61. package/src/platform/storage.ts +215 -208
  62. package/src/reactive/core.ts +114 -93
  63. package/src/reactive/effect.ts +54 -43
  64. package/src/reactive/internals.ts +122 -105
  65. package/src/security/sanitize-core.ts +364 -343
  66. package/src/view/evaluate.ts +290 -274
  67. package/dist/core-COenAZjD.js.map +0 -1
  68. package/dist/sanitize-1FBEPAFH.js.map +0 -1
  69. package/dist/type-guards-DRma3-Kc.js +0 -16
  70. package/dist/type-guards-DRma3-Kc.js.map +0 -1
  71. package/dist/untrack-BuEQKH7_.js +0 -6
@@ -1,343 +1,364 @@
1
- /**
2
- * Core HTML sanitization logic.
3
- *
4
- * @module bquery/security
5
- * @internal
6
- */
7
-
8
- import {
9
- DANGEROUS_ATTR_PREFIXES,
10
- DANGEROUS_PROTOCOLS,
11
- DANGEROUS_TAGS,
12
- DEFAULT_ALLOWED_ATTRIBUTES,
13
- DEFAULT_ALLOWED_TAGS,
14
- RESERVED_IDS,
15
- } from './constants';
16
- import type { SanitizeOptions } from './types';
17
-
18
- /**
19
- * Check if an attribute name is allowed.
20
- * @internal
21
- */
22
- const isAllowedAttribute = (
23
- name: string,
24
- allowedSet: Set<string>,
25
- allowDataAttrs: boolean
26
- ): boolean => {
27
- const lowerName = name.toLowerCase();
28
-
29
- // Check dangerous prefixes
30
- for (const prefix of DANGEROUS_ATTR_PREFIXES) {
31
- if (lowerName.startsWith(prefix)) return false;
32
- }
33
-
34
- // Check data attributes
35
- if (allowDataAttrs && lowerName.startsWith('data-')) return true;
36
-
37
- // Check aria attributes (allowed by default)
38
- if (lowerName.startsWith('aria-')) return true;
39
-
40
- // Check explicit allow list
41
- return allowedSet.has(lowerName);
42
- };
43
-
44
- /**
45
- * Check if an ID/name value could cause DOM clobbering.
46
- * @internal
47
- */
48
- const isSafeIdOrName = (value: string): boolean => {
49
- const lowerValue = value.toLowerCase().trim();
50
- return !RESERVED_IDS.has(lowerValue);
51
- };
52
-
53
- /**
54
- * Normalize URL by removing control characters, whitespace, and Unicode tricks.
55
- * Enhanced to prevent various bypass techniques.
56
- * @internal
57
- */
58
- const normalizeUrl = (value: string): string =>
59
- value
60
- // Remove null bytes and control characters
61
- .replace(/[\u0000-\u001F\u007F]+/g, '')
62
- // Remove zero-width characters that could hide malicious content
63
- .replace(/[\u200B-\u200D\uFEFF\u2028\u2029]+/g, '')
64
- // Remove escaped Unicode sequences
65
- .replace(/\\u[\da-fA-F]{4}/g, '')
66
- // Remove whitespace
67
- .replace(/\s+/g, '')
68
- // Normalize case
69
- .toLowerCase();
70
-
71
- /**
72
- * Check if a URL value is safe.
73
- * @internal
74
- */
75
- const isSafeUrl = (value: string): boolean => {
76
- const normalized = normalizeUrl(value);
77
- for (const protocol of DANGEROUS_PROTOCOLS) {
78
- if (normalized.startsWith(protocol)) return false;
79
- }
80
- return true;
81
- };
82
-
83
- /**
84
- * Check if a URL is external (different origin).
85
- * @internal
86
- */
87
- const isExternalUrl = (url: string): boolean => {
88
- try {
89
- // Normalize URL by trimming whitespace
90
- const trimmedUrl = url.trim();
91
-
92
- // Protocol-relative URLs (//example.com) are always external.
93
- // CRITICAL: This check must run before the relative-URL check below;
94
- // otherwise, a protocol-relative URL like "//evil.com" would be treated
95
- // as a non-http(s) relative URL and incorrectly classified as same-origin.
96
- // Handling them up front guarantees correct security classification.
97
- if (trimmedUrl.startsWith('//')) {
98
- return true;
99
- }
100
-
101
- // Normalize URL for case-insensitive protocol checks
102
- const lowerUrl = trimmedUrl.toLowerCase();
103
-
104
- // Check for non-http(s) protocols which are considered external/special
105
- // (mailto:, tel:, ftp:, etc.)
106
- const hasProtocol = /^[a-z][a-z0-9+.-]*:/i.test(trimmedUrl);
107
- if (hasProtocol && !lowerUrl.startsWith('http://') && !lowerUrl.startsWith('https://')) {
108
- // These are special protocols, not traditional "external" links
109
- // but we treat them as external for security consistency
110
- return true;
111
- }
112
-
113
- // Relative URLs are not external
114
- if (!lowerUrl.startsWith('http://') && !lowerUrl.startsWith('https://')) {
115
- return false;
116
- }
117
-
118
- // In non-browser environments (e.g., Node.js), treat all absolute URLs as external
119
- if (typeof window === 'undefined' || !window.location) {
120
- return true;
121
- }
122
-
123
- const urlObj = new URL(trimmedUrl, window.location.href);
124
- return urlObj.origin !== window.location.origin;
125
- } catch {
126
- // If URL parsing fails, treat as potentially external for safety
127
- return true;
128
- }
129
- };
130
-
131
- /**
132
- * Parse an HTML string into a Document using DOMParser.
133
- * This helper is intentionally separated to make the control-flow around HTML parsing
134
- * explicit for static analysis tools. It should ONLY be called when the input is
135
- * known to contain HTML syntax (angle brackets).
136
- *
137
- * DOMParser creates an inert document where scripts don't execute, making it safe
138
- * for parsing untrusted HTML that will subsequently be sanitized.
139
- *
140
- * @param htmlContent - A string that is known to contain HTML markup (has < or >)
141
- * @returns The parsed Document
142
- * @internal
143
- */
144
- const parseHtmlDocument = (htmlContent: string): Document => {
145
- const parser = new DOMParser();
146
- // Parse as a full HTML document in an inert context; scripts won't execute
147
- return parser.parseFromString(htmlContent, 'text/html');
148
- };
149
-
150
- /**
151
- * Safely parse HTML string into a DocumentFragment using DOMParser.
152
- * DOMParser is preferred over innerHTML for security as it creates an inert document
153
- * where scripts don't execute and provides better static analysis recognition.
154
- *
155
- * This function includes input normalization to satisfy static analysis tools:
156
- * - Coerces input to string and trims whitespace
157
- * - For plain text (no HTML tags), creates a Text node directly without parsing
158
- * - Only invokes DOMParser for actual HTML-like content via parseHtmlDocument
159
- *
160
- * The separation between plain text handling and HTML parsing is intentional:
161
- * DOM text that contains no HTML syntax is never fed into an HTML parser,
162
- * preventing "DOM text reinterpreted as HTML" issues.
163
- *
164
- * @internal
165
- */
166
- const parseHtmlSafely = (html: string): DocumentFragment => {
167
- // Step 1: Normalize input - coerce to string and trim
168
- // This defensive check handles edge cases even though TypeScript says it's a string
169
- const normalizedHtml = (typeof html === 'string' ? html : String(html ?? '')).trim();
170
-
171
- // Step 2: Create the fragment that will hold our result
172
- const fragment = document.createDocumentFragment();
173
-
174
- // Step 3: Early return for empty input
175
- if (normalizedHtml.length === 0) {
176
- return fragment;
177
- }
178
-
179
- // Step 4: If input contains no angle brackets, it's plain text - no HTML parsing needed.
180
- // Plain text is handled as a Text node, never passed to an HTML parser.
181
- // This explicitly prevents "DOM text reinterpreted as HTML" for purely textual inputs.
182
- const containsHtmlSyntax = normalizedHtml.includes('<') || normalizedHtml.includes('>');
183
- if (!containsHtmlSyntax) {
184
- fragment.appendChild(document.createTextNode(normalizedHtml));
185
- return fragment;
186
- }
187
-
188
- // Step 5: Input contains HTML syntax - parse it via the dedicated HTML parsing helper.
189
- // This separation makes the data-flow explicit: only strings with HTML syntax
190
- // are passed to DOMParser, satisfying static analysis requirements.
191
- const doc = parseHtmlDocument(normalizedHtml);
192
-
193
- // Move all children from the document body into the fragment.
194
- // This avoids interpolating untrusted HTML into an outer wrapper string.
195
- const body = doc.body;
196
-
197
- if (!body) {
198
- return fragment;
199
- }
200
-
201
- while (body.firstChild) {
202
- fragment.appendChild(body.firstChild);
203
- }
204
-
205
- return fragment;
206
- };
207
-
208
- /**
209
- * Core sanitization logic (without Trusted Types wrapper).
210
- * @internal
211
- */
212
- export const sanitizeHtmlCore = (html: string, options: SanitizeOptions = {}): string => {
213
- const {
214
- allowTags = [],
215
- allowAttributes = [],
216
- allowDataAttributes = true,
217
- stripAllTags = false,
218
- } = options;
219
-
220
- // Build combined allow sets (excluding dangerous tags even if specified)
221
- const allowedTags = new Set(
222
- [...DEFAULT_ALLOWED_TAGS, ...allowTags.map((t) => t.toLowerCase())].filter(
223
- (tag) => !DANGEROUS_TAGS.has(tag)
224
- )
225
- );
226
- const allowedAttrs = new Set([
227
- ...DEFAULT_ALLOWED_ATTRIBUTES,
228
- ...allowAttributes.map((a) => a.toLowerCase()),
229
- ]);
230
-
231
- // Use DOMParser for safe HTML parsing (inert context, no script execution)
232
- const fragment = parseHtmlSafely(html);
233
-
234
- if (stripAllTags) {
235
- return fragment.textContent ?? '';
236
- }
237
-
238
- // Walk the DOM tree
239
- const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_ELEMENT);
240
-
241
- const toRemove: Element[] = [];
242
-
243
- while (walker.nextNode()) {
244
- const el = walker.currentNode as Element;
245
- const tagName = el.tagName.toLowerCase();
246
-
247
- // Remove explicitly dangerous tags even if in allow list
248
- if (DANGEROUS_TAGS.has(tagName)) {
249
- toRemove.push(el);
250
- continue;
251
- }
252
-
253
- // Remove disallowed tags entirely
254
- if (!allowedTags.has(tagName)) {
255
- toRemove.push(el);
256
- continue;
257
- }
258
-
259
- // Process attributes
260
- const attrsToRemove: string[] = [];
261
- for (const attr of Array.from(el.attributes)) {
262
- const attrName = attr.name.toLowerCase();
263
-
264
- // Check if attribute is allowed
265
- if (!isAllowedAttribute(attrName, allowedAttrs, allowDataAttributes)) {
266
- attrsToRemove.push(attr.name);
267
- continue;
268
- }
269
-
270
- // Check for DOM clobbering on id and name attributes
271
- if ((attrName === 'id' || attrName === 'name') && !isSafeIdOrName(attr.value)) {
272
- attrsToRemove.push(attr.name);
273
- continue;
274
- }
275
-
276
- // Validate URL attributes
277
- if (
278
- (attrName === 'href' || attrName === 'src' || attrName === 'srcset') &&
279
- !isSafeUrl(attr.value)
280
- ) {
281
- attrsToRemove.push(attr.name);
282
- }
283
- }
284
-
285
- // Remove disallowed attributes
286
- for (const attrName of attrsToRemove) {
287
- el.removeAttribute(attrName);
288
- }
289
-
290
- // Add rel="noopener noreferrer" to external links for security
291
- if (tagName === 'a') {
292
- const href = el.getAttribute('href');
293
- const target = el.getAttribute('target');
294
- const hasTargetBlank = target?.toLowerCase() === '_blank';
295
- const isExternal = href && isExternalUrl(href);
296
-
297
- // Add security attributes to links opening in new window or external links
298
- if (hasTargetBlank || isExternal) {
299
- const existingRel = el.getAttribute('rel');
300
- const relValues = new Set(existingRel ? existingRel.split(/\s+/).filter(Boolean) : []);
301
-
302
- // Add noopener and noreferrer
303
- relValues.add('noopener');
304
- relValues.add('noreferrer');
305
-
306
- el.setAttribute('rel', Array.from(relValues).join(' '));
307
- }
308
- }
309
- }
310
-
311
- // Remove disallowed elements
312
- for (const el of toRemove) {
313
- el.remove();
314
- }
315
-
316
- // Serialize the sanitized fragment to HTML string.
317
- // We use a temporary container to get the innerHTML of the fragment.
318
- const serializeFragment = (frag: DocumentFragment): string => {
319
- const container = document.createElement('div');
320
- container.appendChild(frag.cloneNode(true));
321
- return container.innerHTML;
322
- };
323
-
324
- // Double-parse to prevent mutation XSS (mXSS).
325
- // Browsers may normalize HTML during serialization in ways that could create
326
- // new dangerous content when re-parsed. By re-parsing the sanitized output
327
- // and verifying stability, we ensure the final HTML is safe.
328
- const firstPass = serializeFragment(fragment);
329
-
330
- // Re-parse through DOMParser for mXSS detection.
331
- // Using DOMParser instead of innerHTML for security.
332
- const verifyFragment = parseHtmlSafely(firstPass);
333
- const secondPass = serializeFragment(verifyFragment);
334
-
335
- // Verify stability: if content mutates between parses, it indicates mXSS attempt
336
- if (firstPass !== secondPass) {
337
- // Content mutated during re-parse - potential mXSS detected.
338
- // Return safely escaped text content as fallback.
339
- return fragment.textContent ?? '';
340
- }
341
-
342
- return secondPass;
343
- };
1
+ /**
2
+ * Core HTML sanitization logic.
3
+ *
4
+ * @module bquery/security
5
+ * @internal
6
+ */
7
+
8
+ import {
9
+ DANGEROUS_ATTR_PREFIXES,
10
+ DANGEROUS_PROTOCOLS,
11
+ DANGEROUS_TAGS,
12
+ DEFAULT_ALLOWED_ATTRIBUTES,
13
+ DEFAULT_ALLOWED_TAGS,
14
+ RESERVED_IDS,
15
+ } from './constants';
16
+ import type { SanitizeOptions } from './types';
17
+
18
+ /**
19
+ * Check if an attribute name is allowed.
20
+ * @internal
21
+ */
22
+ const isAllowedAttribute = (
23
+ name: string,
24
+ allowedSet: Set<string>,
25
+ allowDataAttrs: boolean
26
+ ): boolean => {
27
+ const lowerName = name.toLowerCase();
28
+
29
+ // Check dangerous prefixes
30
+ for (const prefix of DANGEROUS_ATTR_PREFIXES) {
31
+ if (lowerName.startsWith(prefix)) return false;
32
+ }
33
+
34
+ // Check data attributes
35
+ if (allowDataAttrs && lowerName.startsWith('data-')) return true;
36
+
37
+ // Check aria attributes (allowed by default)
38
+ if (lowerName.startsWith('aria-')) return true;
39
+
40
+ // Check explicit allow list
41
+ return allowedSet.has(lowerName);
42
+ };
43
+
44
+ /**
45
+ * Check if an ID/name value could cause DOM clobbering.
46
+ * @internal
47
+ */
48
+ const isSafeIdOrName = (value: string): boolean => {
49
+ const lowerValue = value.toLowerCase().trim();
50
+ return !RESERVED_IDS.has(lowerValue);
51
+ };
52
+
53
+ /**
54
+ * Normalize URL by removing control characters, whitespace, and Unicode tricks.
55
+ * Enhanced to prevent various bypass techniques.
56
+ * @internal
57
+ */
58
+ const normalizeUrl = (value: string): string =>
59
+ value
60
+ // Remove null bytes and control characters
61
+ .replace(/[\u0000-\u001F\u007F]+/g, '')
62
+ // Remove zero-width characters that could hide malicious content
63
+ .replace(/[\u200B-\u200D\uFEFF\u2028\u2029]+/g, '')
64
+ // Remove escaped Unicode sequences
65
+ .replace(/\\u[\da-fA-F]{4}/g, '')
66
+ // Remove whitespace
67
+ .replace(/\s+/g, '')
68
+ // Normalize case
69
+ .toLowerCase();
70
+
71
+ /**
72
+ * Check if a URL value is safe.
73
+ * @internal
74
+ */
75
+ const isSafeUrl = (value: string): boolean => {
76
+ const normalized = normalizeUrl(value);
77
+ for (const protocol of DANGEROUS_PROTOCOLS) {
78
+ if (normalized.startsWith(protocol)) return false;
79
+ }
80
+ return true;
81
+ };
82
+
83
+ /**
84
+ * Check if a srcset attribute value is safe.
85
+ * srcset contains comma-separated entries of "url [descriptor]".
86
+ * Each individual URL must be validated.
87
+ * @internal
88
+ */
89
+ const isSafeSrcset = (value: string): boolean => {
90
+ const entries = value.split(',');
91
+ for (const entry of entries) {
92
+ const url = entry.trim().split(/\s+/)[0];
93
+ if (url && !isSafeUrl(url)) return false;
94
+ }
95
+ return true;
96
+ };
97
+
98
+ /**
99
+ * Check if a URL is external (different origin).
100
+ * @internal
101
+ */
102
+ const isExternalUrl = (url: string): boolean => {
103
+ try {
104
+ // Normalize URL by trimming whitespace
105
+ const trimmedUrl = url.trim();
106
+
107
+ // Protocol-relative URLs (//example.com) are always external.
108
+ // CRITICAL: This check must run before the relative-URL check below;
109
+ // otherwise, a protocol-relative URL like "//evil.com" would be treated
110
+ // as a non-http(s) relative URL and incorrectly classified as same-origin.
111
+ // Handling them up front guarantees correct security classification.
112
+ if (trimmedUrl.startsWith('//')) {
113
+ return true;
114
+ }
115
+
116
+ // Normalize URL for case-insensitive protocol checks
117
+ const lowerUrl = trimmedUrl.toLowerCase();
118
+
119
+ // Check for non-http(s) protocols which are considered external/special
120
+ // (mailto:, tel:, ftp:, etc.)
121
+ const hasProtocol = /^[a-z][a-z0-9+.-]*:/i.test(trimmedUrl);
122
+ if (hasProtocol && !lowerUrl.startsWith('http://') && !lowerUrl.startsWith('https://')) {
123
+ // These are special protocols, not traditional "external" links
124
+ // but we treat them as external for security consistency
125
+ return true;
126
+ }
127
+
128
+ // Relative URLs are not external
129
+ if (!lowerUrl.startsWith('http://') && !lowerUrl.startsWith('https://')) {
130
+ return false;
131
+ }
132
+
133
+ // In non-browser environments (e.g., Node.js), treat all absolute URLs as external
134
+ if (typeof window === 'undefined' || !window.location) {
135
+ return true;
136
+ }
137
+
138
+ const urlObj = new URL(trimmedUrl, window.location.href);
139
+ return urlObj.origin !== window.location.origin;
140
+ } catch {
141
+ // If URL parsing fails, treat as potentially external for safety
142
+ return true;
143
+ }
144
+ };
145
+
146
+ /**
147
+ * Parse an HTML string into a Document using DOMParser.
148
+ * This helper is intentionally separated to make the control-flow around HTML parsing
149
+ * explicit for static analysis tools. It should ONLY be called when the input is
150
+ * known to contain HTML syntax (angle brackets).
151
+ *
152
+ * DOMParser creates an inert document where scripts don't execute, making it safe
153
+ * for parsing untrusted HTML that will subsequently be sanitized.
154
+ *
155
+ * @param htmlContent - A string that is known to contain HTML markup (has < or >)
156
+ * @returns The parsed Document
157
+ * @internal
158
+ */
159
+ const parseHtmlDocument = (htmlContent: string): Document => {
160
+ const parser = new DOMParser();
161
+ // Parse as a full HTML document in an inert context; scripts won't execute
162
+ return parser.parseFromString(htmlContent, 'text/html');
163
+ };
164
+
165
+ /**
166
+ * Safely parse HTML string into a DocumentFragment using DOMParser.
167
+ * DOMParser is preferred over innerHTML for security as it creates an inert document
168
+ * where scripts don't execute and provides better static analysis recognition.
169
+ *
170
+ * This function includes input normalization to satisfy static analysis tools:
171
+ * - Coerces input to string and trims whitespace
172
+ * - For plain text (no HTML tags), creates a Text node directly without parsing
173
+ * - Only invokes DOMParser for actual HTML-like content via parseHtmlDocument
174
+ *
175
+ * The separation between plain text handling and HTML parsing is intentional:
176
+ * DOM text that contains no HTML syntax is never fed into an HTML parser,
177
+ * preventing "DOM text reinterpreted as HTML" issues.
178
+ *
179
+ * @internal
180
+ */
181
+ const parseHtmlSafely = (html: string): DocumentFragment => {
182
+ // Step 1: Normalize input - coerce to string and trim
183
+ // This defensive check handles edge cases even though TypeScript says it's a string
184
+ const normalizedHtml = (typeof html === 'string' ? html : String(html ?? '')).trim();
185
+
186
+ // Step 2: Create the fragment that will hold our result
187
+ const fragment = document.createDocumentFragment();
188
+
189
+ // Step 3: Early return for empty input
190
+ if (normalizedHtml.length === 0) {
191
+ return fragment;
192
+ }
193
+
194
+ // Step 4: If input contains no angle brackets, it's plain text - no HTML parsing needed.
195
+ // Plain text is handled as a Text node, never passed to an HTML parser.
196
+ // This explicitly prevents "DOM text reinterpreted as HTML" for purely textual inputs.
197
+ const containsHtmlSyntax = normalizedHtml.includes('<') || normalizedHtml.includes('>');
198
+ if (!containsHtmlSyntax) {
199
+ fragment.appendChild(document.createTextNode(normalizedHtml));
200
+ return fragment;
201
+ }
202
+
203
+ // Step 5: Input contains HTML syntax - parse it via the dedicated HTML parsing helper.
204
+ // This separation makes the data-flow explicit: only strings with HTML syntax
205
+ // are passed to DOMParser, satisfying static analysis requirements.
206
+ const doc = parseHtmlDocument(normalizedHtml);
207
+
208
+ // Move all children from the document body into the fragment.
209
+ // This avoids interpolating untrusted HTML into an outer wrapper string.
210
+ const body = doc.body;
211
+
212
+ if (!body) {
213
+ return fragment;
214
+ }
215
+
216
+ while (body.firstChild) {
217
+ fragment.appendChild(body.firstChild);
218
+ }
219
+
220
+ return fragment;
221
+ };
222
+
223
+ /**
224
+ * Core sanitization logic (without Trusted Types wrapper).
225
+ * @internal
226
+ */
227
+ export const sanitizeHtmlCore = (html: string, options: SanitizeOptions = {}): string => {
228
+ const {
229
+ allowTags = [],
230
+ allowAttributes = [],
231
+ allowDataAttributes = true,
232
+ stripAllTags = false,
233
+ } = options;
234
+
235
+ // Build combined allow sets (excluding dangerous tags even if specified)
236
+ const allowedTags = new Set(
237
+ [...DEFAULT_ALLOWED_TAGS, ...allowTags.map((t) => t.toLowerCase())].filter(
238
+ (tag) => !DANGEROUS_TAGS.has(tag)
239
+ )
240
+ );
241
+ const allowedAttrs = new Set([
242
+ ...DEFAULT_ALLOWED_ATTRIBUTES,
243
+ ...allowAttributes.map((a) => a.toLowerCase()),
244
+ ]);
245
+
246
+ // Use DOMParser for safe HTML parsing (inert context, no script execution)
247
+ const fragment = parseHtmlSafely(html);
248
+
249
+ if (stripAllTags) {
250
+ return fragment.textContent ?? '';
251
+ }
252
+
253
+ // Walk the DOM tree
254
+ const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_ELEMENT);
255
+
256
+ const toRemove: Element[] = [];
257
+
258
+ while (walker.nextNode()) {
259
+ const el = walker.currentNode as Element;
260
+ const tagName = el.tagName.toLowerCase();
261
+
262
+ // Remove explicitly dangerous tags even if in allow list
263
+ if (DANGEROUS_TAGS.has(tagName)) {
264
+ toRemove.push(el);
265
+ continue;
266
+ }
267
+
268
+ // Remove disallowed tags entirely
269
+ if (!allowedTags.has(tagName)) {
270
+ toRemove.push(el);
271
+ continue;
272
+ }
273
+
274
+ // Process attributes
275
+ const attrsToRemove: string[] = [];
276
+ for (const attr of Array.from(el.attributes)) {
277
+ const attrName = attr.name.toLowerCase();
278
+
279
+ // Check if attribute is allowed
280
+ if (!isAllowedAttribute(attrName, allowedAttrs, allowDataAttributes)) {
281
+ attrsToRemove.push(attr.name);
282
+ continue;
283
+ }
284
+
285
+ // Check for DOM clobbering on id and name attributes
286
+ if ((attrName === 'id' || attrName === 'name') && !isSafeIdOrName(attr.value)) {
287
+ attrsToRemove.push(attr.name);
288
+ continue;
289
+ }
290
+
291
+ // Validate URL attributes
292
+ if (
293
+ (attrName === 'href' || attrName === 'src' || attrName === 'action') &&
294
+ !isSafeUrl(attr.value)
295
+ ) {
296
+ attrsToRemove.push(attr.name);
297
+ continue;
298
+ }
299
+
300
+ // Validate srcset URLs individually
301
+ if (attrName === 'srcset' && !isSafeSrcset(attr.value)) {
302
+ attrsToRemove.push(attr.name);
303
+ }
304
+ }
305
+
306
+ // Remove disallowed attributes
307
+ for (const attrName of attrsToRemove) {
308
+ el.removeAttribute(attrName);
309
+ }
310
+
311
+ // Add rel="noopener noreferrer" to external links for security
312
+ if (tagName === 'a') {
313
+ const href = el.getAttribute('href');
314
+ const target = el.getAttribute('target');
315
+ const hasTargetBlank = target?.toLowerCase() === '_blank';
316
+ const isExternal = href && isExternalUrl(href);
317
+
318
+ // Add security attributes to links opening in new window or external links
319
+ if (hasTargetBlank || isExternal) {
320
+ const existingRel = el.getAttribute('rel');
321
+ const relValues = new Set(existingRel ? existingRel.split(/\s+/).filter(Boolean) : []);
322
+
323
+ // Add noopener and noreferrer
324
+ relValues.add('noopener');
325
+ relValues.add('noreferrer');
326
+
327
+ el.setAttribute('rel', Array.from(relValues).join(' '));
328
+ }
329
+ }
330
+ }
331
+
332
+ // Remove disallowed elements
333
+ for (const el of toRemove) {
334
+ el.remove();
335
+ }
336
+
337
+ // Serialize the sanitized fragment to HTML string.
338
+ // We use a temporary container to get the innerHTML of the fragment.
339
+ const serializeFragment = (frag: DocumentFragment): string => {
340
+ const container = document.createElement('div');
341
+ container.appendChild(frag.cloneNode(true));
342
+ return container.innerHTML;
343
+ };
344
+
345
+ // Double-parse to prevent mutation XSS (mXSS).
346
+ // Browsers may normalize HTML during serialization in ways that could create
347
+ // new dangerous content when re-parsed. By re-parsing the sanitized output
348
+ // and verifying stability, we ensure the final HTML is safe.
349
+ const firstPass = serializeFragment(fragment);
350
+
351
+ // Re-parse through DOMParser for mXSS detection.
352
+ // Using DOMParser instead of innerHTML for security.
353
+ const verifyFragment = parseHtmlSafely(firstPass);
354
+ const secondPass = serializeFragment(verifyFragment);
355
+
356
+ // Verify stability: if content mutates between parses, it indicates mXSS attempt
357
+ if (firstPass !== secondPass) {
358
+ // Content mutated during re-parse - potential mXSS detected.
359
+ // Return safely escaped text content as fallback.
360
+ return fragment.textContent ?? '';
361
+ }
362
+
363
+ return secondPass;
364
+ };