@pie-qti/item-player 0.1.9 → 0.1.10
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.
- package/README.md +17 -0
- package/dist/catalog/applyGlossaryTriggers.d.ts +13 -2
- package/dist/catalog/applyGlossaryTriggers.d.ts.map +1 -1
- package/dist/catalog/glossaryAction.d.ts +4 -2
- package/dist/catalog/glossaryAction.d.ts.map +1 -1
- package/dist/components/utils/index.d.ts +1 -2
- package/dist/components/utils/index.d.ts.map +1 -1
- package/dist/components/utils/index.js +1 -2
- package/dist/core/ComponentRegistry.d.ts +10 -1
- package/dist/core/ComponentRegistry.d.ts.map +1 -1
- package/dist/core/ComponentRegistry.js +42 -1
- package/dist/core/Player.d.ts +10 -2
- package/dist/core/Player.d.ts.map +1 -1
- package/dist/core/Player.js +54 -46
- package/dist/document/AssessmentItemDocument.d.ts +1 -1
- package/dist/element.d.ts +15 -0
- package/dist/element.js +1817 -1625
- package/dist/element.js.map +1 -1
- package/dist/extraction/ExtractionRegistry.d.ts +1 -1
- package/dist/extraction/createContext.d.ts +1 -1
- package/dist/extraction/index.d.ts +2 -2
- package/dist/extraction/index.d.ts.map +1 -1
- package/dist/extraction/index.js +1 -1
- package/dist/extraction/interactionExtractionPipeline.d.ts +1 -1
- package/dist/extraction/interactionSecurity.d.ts +1 -1
- package/dist/extraction/types.d.ts +1 -1
- package/dist/presentation/itemPresentationPlan.d.ts +54 -0
- package/dist/presentation/itemPresentationPlan.d.ts.map +1 -0
- package/dist/presentation/itemPresentationPlan.js +98 -0
- package/dist/types/index.d.ts +17 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +0 -3
- package/dist/web-components/QTIInteractionBase.d.ts +1 -1
- package/dist/web-components/QTIInteractionElement.d.ts +1 -1
- package/package.json +7 -14
- package/src/__tests__/catalog.test.ts +0 -267
- package/src/__tests__/fixtures/pci/echo-pci.js +0 -49
- package/src/__tests__/pci.test.ts +0 -353
- package/src/__tests__/qti3-attribute-extraction.test.ts +0 -174
- package/src/__tests__/qti3-debug.test.ts +0 -140
- package/src/__tests__/qti3-extraction.test.ts +0 -309
- package/src/catalog/applyGlossaryTriggers.ts +0 -372
- package/src/catalog/catalogExtractor.ts +0 -130
- package/src/catalog/catalogLookup.ts +0 -53
- package/src/catalog/glossaryAction.ts +0 -38
- package/src/catalog/types.ts +0 -37
- package/src/components/ItemBody.svelte +0 -363
- package/src/components/ItemPlayer.svelte +0 -163
- package/src/components/ModalFeedbackDisplay.svelte +0 -97
- package/src/components/actions/typesetAction.ts +0 -74
- package/src/components/index.ts +0 -22
- package/src/components/utils/assignProps.ts +0 -2
- package/src/components/utils/feedbackUtils.ts +0 -140
- package/src/components/utils/index.ts +0 -13
- package/src/components/utils/stimulusRender.ts +0 -93
- package/src/components/utils/stylesheetRender.ts +0 -58
- package/src/core/ComponentRegistry.ts +0 -248
- package/src/core/Player.ts +0 -1618
- package/src/core/Plugin.ts +0 -123
- package/src/core/PluginManager.ts +0 -182
- package/src/core/constants.ts +0 -117
- package/src/core/declarations.ts +0 -171
- package/src/core/parsingLimits.ts +0 -62
- package/src/core/random.ts +0 -24
- package/src/core/rolePolicy.ts +0 -34
- package/src/core/sanitizer.ts +0 -305
- package/src/core/trustedTypes.ts +0 -63
- package/src/core/urlPolicy.ts +0 -98
- package/src/document/AssessmentItemDocument.ts +0 -247
- package/src/element.ts +0 -128
- package/src/extensions/MathLiveEditor.ts +0 -313
- package/src/extraction/ExtractionRegistry.ts +0 -335
- package/src/extraction/createContext.ts +0 -52
- package/src/extraction/extractors/associateExtractor.ts +0 -1
- package/src/extraction/extractors/choiceExtractor.ts +0 -1
- package/src/extraction/extractors/conformance-qti3-advanced.test.ts +0 -239
- package/src/extraction/extractors/conformance-qti3-basic.test.ts +0 -82
- package/src/extraction/extractors/conformance.test.ts +0 -208
- package/src/extraction/extractors/customExtractor.ts +0 -1
- package/src/extraction/extractors/drawingExtractor.ts +0 -1
- package/src/extraction/extractors/endAttemptExtractor.ts +0 -1
- package/src/extraction/extractors/extendedTextExtractor.ts +0 -1
- package/src/extraction/extractors/gapMatchExtractor.ts +0 -1
- package/src/extraction/extractors/graphicAssociateExtractor.ts +0 -1
- package/src/extraction/extractors/graphicGapMatchExtractor.ts +0 -1
- package/src/extraction/extractors/graphicOrderExtractor.ts +0 -1
- package/src/extraction/extractors/hotspotExtractor.ts +0 -1
- package/src/extraction/extractors/hottextExtractor.ts +0 -1
- package/src/extraction/extractors/index.ts +0 -9
- package/src/extraction/extractors/inlineChoiceExtractor.ts +0 -1
- package/src/extraction/extractors/matchExtractor.ts +0 -1
- package/src/extraction/extractors/mediaExtractor.ts +0 -1
- package/src/extraction/extractors/orderExtractor.ts +0 -1
- package/src/extraction/extractors/portableCustomExtractor.ts +0 -1
- package/src/extraction/extractors/positionObjectExtractor.ts +0 -1
- package/src/extraction/extractors/selectPointExtractor.ts +0 -1
- package/src/extraction/extractors/sliderExtractor.ts +0 -1
- package/src/extraction/extractors/textEntryExtractor.ts +0 -1
- package/src/extraction/extractors/uploadExtractor.ts +0 -1
- package/src/extraction/index.ts +0 -108
- package/src/extraction/interactionExtractionPipeline.ts +0 -70
- package/src/extraction/interactionSecurity.ts +0 -60
- package/src/extraction/types.ts +0 -246
- package/src/extraction/utils.ts +0 -243
- package/src/iframe/IFramePlayerHost.ts +0 -265
- package/src/iframe/index.ts +0 -11
- package/src/iframe/protocol.ts +0 -137
- package/src/index.ts +0 -101
- package/src/interactions/associate/extractor.ts +0 -133
- package/src/interactions/associate/index.ts +0 -2
- package/src/interactions/associate/types.ts +0 -1
- package/src/interactions/choice/extractor.ts +0 -121
- package/src/interactions/choice/index.ts +0 -2
- package/src/interactions/choice/types.ts +0 -1
- package/src/interactions/custom/extractor.ts +0 -72
- package/src/interactions/custom/index.ts +0 -2
- package/src/interactions/custom/types.ts +0 -1
- package/src/interactions/drawing/extractor.ts +0 -113
- package/src/interactions/drawing/index.ts +0 -2
- package/src/interactions/drawing/types.ts +0 -1
- package/src/interactions/end-attempt/extractor.ts +0 -74
- package/src/interactions/end-attempt/index.ts +0 -2
- package/src/interactions/end-attempt/types.ts +0 -1
- package/src/interactions/extended-text/extractor.ts +0 -92
- package/src/interactions/extended-text/index.ts +0 -2
- package/src/interactions/extended-text/types.ts +0 -1
- package/src/interactions/gap-match/extractor.ts +0 -193
- package/src/interactions/gap-match/index.ts +0 -2
- package/src/interactions/gap-match/types.ts +0 -1
- package/src/interactions/graphic-associate/extractor.ts +0 -192
- package/src/interactions/graphic-associate/index.ts +0 -2
- package/src/interactions/graphic-associate/types.ts +0 -1
- package/src/interactions/graphic-gap-match/extractor.ts +0 -257
- package/src/interactions/graphic-gap-match/index.ts +0 -2
- package/src/interactions/graphic-gap-match/types.ts +0 -1
- package/src/interactions/graphic-order/extractor.ts +0 -162
- package/src/interactions/graphic-order/index.ts +0 -2
- package/src/interactions/graphic-order/types.ts +0 -1
- package/src/interactions/hotspot/extractor.ts +0 -208
- package/src/interactions/hotspot/index.ts +0 -2
- package/src/interactions/hotspot/types.ts +0 -1
- package/src/interactions/hottext/extractor.ts +0 -163
- package/src/interactions/hottext/index.ts +0 -2
- package/src/interactions/hottext/types.ts +0 -1
- package/src/interactions/index.ts +0 -52
- package/src/interactions/inline/index.ts +0 -1
- package/src/interactions/inline/render-plan.ts +0 -105
- package/src/interactions/inline-choice/InlineChoice.svelte +0 -57
- package/src/interactions/inline-choice/extractor.ts +0 -105
- package/src/interactions/inline-choice/index.ts +0 -2
- package/src/interactions/inline-choice/types.ts +0 -1
- package/src/interactions/match/extractor.ts +0 -187
- package/src/interactions/match/index.ts +0 -2
- package/src/interactions/match/types.ts +0 -1
- package/src/interactions/media/extractor.ts +0 -168
- package/src/interactions/media/index.ts +0 -2
- package/src/interactions/media/types.ts +0 -1
- package/src/interactions/modules.ts +0 -111
- package/src/interactions/order/extractor.ts +0 -120
- package/src/interactions/order/index.ts +0 -2
- package/src/interactions/order/types.ts +0 -1
- package/src/interactions/portable-custom/extractor.ts +0 -122
- package/src/interactions/portable-custom/index.ts +0 -2
- package/src/interactions/portable-custom/types.ts +0 -1
- package/src/interactions/position-object/extractor.ts +0 -243
- package/src/interactions/position-object/index.ts +0 -2
- package/src/interactions/position-object/types.ts +0 -1
- package/src/interactions/select-point/extractor.ts +0 -143
- package/src/interactions/select-point/index.ts +0 -2
- package/src/interactions/select-point/types.ts +0 -1
- package/src/interactions/shared/index.ts +0 -1
- package/src/interactions/shared/types.ts +0 -430
- package/src/interactions/slider/extractor.ts +0 -102
- package/src/interactions/slider/index.ts +0 -2
- package/src/interactions/slider/types.ts +0 -1
- package/src/interactions/text-entry/InlineTextEntry.svelte +0 -75
- package/src/interactions/text-entry/extractor.ts +0 -79
- package/src/interactions/text-entry/index.ts +0 -2
- package/src/interactions/text-entry/types.ts +0 -1
- package/src/interactions/upload/extractor.ts +0 -81
- package/src/interactions/upload/index.ts +0 -2
- package/src/interactions/upload/types.ts +0 -1
- package/src/pci/PciHost.ts +0 -138
- package/src/pci/types.ts +0 -70
- package/src/pnp/applyPnp.ts +0 -29
- package/src/pnp/parsePnpXml.ts +0 -215
- package/src/pnp/types.ts +0 -75
- package/src/server.ts +0 -14
- package/src/types/index.ts +0 -388
- package/src/types/interactions.ts +0 -8
- package/src/types/responseValidation.ts +0 -29
- package/src/types/xmldom.d.ts +0 -12
- package/src/utils/PerformanceMonitor.ts +0 -335
- package/src/utils/drawingUtils.ts +0 -73
- package/src/utils/responseUtils.ts +0 -51
- package/src/utils/touchDragHelper.ts +0 -1
- package/src/web-components/QTIInteractionBase.ts +0 -303
- package/src/web-components/QTIInteractionElement.ts +0 -125
- package/src/web-components/index.ts +0 -22
package/src/core/rolePolicy.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { QTIRole } from '../types/index.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Product policy layered on top of QTI role/view semantics.
|
|
5
|
-
*
|
|
6
|
-
* QTI defines these roles as audience markers for view-controlled content
|
|
7
|
-
* (for example rubric/outcome visibility). It does not define a universal
|
|
8
|
-
* interaction UI behavior matrix. The player policy below keeps runtime
|
|
9
|
-
* behavior explicit and consistent across integrations.
|
|
10
|
-
*/
|
|
11
|
-
export interface RoleCapabilities {
|
|
12
|
-
isCandidate: boolean;
|
|
13
|
-
isReadOnly: boolean;
|
|
14
|
-
canViewCorrectResponses: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const ROLES_WITH_CORRECT_RESPONSE_ACCESS: ReadonlySet<QTIRole> = new Set([
|
|
18
|
-
'scorer',
|
|
19
|
-
'author',
|
|
20
|
-
'tutor',
|
|
21
|
-
'testConstructor',
|
|
22
|
-
]);
|
|
23
|
-
|
|
24
|
-
export function getRoleCapabilities(role: QTIRole): RoleCapabilities {
|
|
25
|
-
const isCandidate = role === 'candidate';
|
|
26
|
-
const canViewCorrectResponses = ROLES_WITH_CORRECT_RESPONSE_ACCESS.has(role);
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
isCandidate,
|
|
30
|
-
isReadOnly: !isCandidate,
|
|
31
|
-
canViewCorrectResponses,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
package/src/core/sanitizer.ts
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTML Sanitizer for QTI Content
|
|
3
|
-
*
|
|
4
|
-
* Removes dangerous HTML/scripts while preserving safe educational content.
|
|
5
|
-
* Protects against XSS attacks via:
|
|
6
|
-
* - Script tags
|
|
7
|
-
* - Event handlers (onclick, onerror, onload, etc.)
|
|
8
|
-
* - Dangerous protocols (javascript:, data:text/html)
|
|
9
|
-
* - Unsafe iframe/object sources
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { type HTMLElement, parse } from 'node-html-parser';
|
|
13
|
-
import type { PlayerSecurityConfig, UrlPolicyConfig } from '../types/index.js';
|
|
14
|
-
import { normalizeParsingLimits } from './parsingLimits.js';
|
|
15
|
-
import { sanitizeResourceUrl } from './urlPolicy.js';
|
|
16
|
-
|
|
17
|
-
export interface SanitizeHtmlOptions {
|
|
18
|
-
/**
|
|
19
|
-
* Security config (URL policy and tag allowances).
|
|
20
|
-
* Defaults are conservative for same-DOM embedding.
|
|
21
|
-
*/
|
|
22
|
-
security?: PlayerSecurityConfig;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
class HtmlLimitExceeded extends Error {
|
|
26
|
-
constructor(message: string) {
|
|
27
|
-
super(message);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function policyFromOptions(options?: SanitizeHtmlOptions): UrlPolicyConfig | undefined {
|
|
32
|
-
return options?.security?.urlPolicy;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function allowObjectEmbeds(options?: SanitizeHtmlOptions): boolean {
|
|
36
|
-
return options?.security?.allowObjectEmbeds === true;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function allowIframes(options?: SanitizeHtmlOptions): boolean {
|
|
40
|
-
return options?.security?.allowIframes === true;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function escapeAttr(value: string): string {
|
|
44
|
-
return value
|
|
45
|
-
.replace(/&/g, '&')
|
|
46
|
-
.replace(/"/g, '"')
|
|
47
|
-
.replace(/</g, '<')
|
|
48
|
-
.replace(/>/g, '>');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function getOptionalAttr(element: HTMLElement, attrName: string): string {
|
|
52
|
-
const value = element.getAttribute(attrName);
|
|
53
|
-
return value ? ` ${attrName}="${escapeAttr(value)}"` : '';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function sanitizeUrlAttr(value: string, policy: UrlPolicyConfig | undefined, kind: 'img' | 'media' | 'object' | 'link' | 'any'): string {
|
|
57
|
-
return sanitizeResourceUrl(value, policy, kind) ?? '#';
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function sanitizeSrcset(value: string, policy: UrlPolicyConfig | undefined): string {
|
|
61
|
-
// srcset grammar is complex; we implement a conservative filter:
|
|
62
|
-
// split by commas into candidate strings; sanitize the URL portion of each candidate.
|
|
63
|
-
const parts = value
|
|
64
|
-
.split(',')
|
|
65
|
-
.map((p) => p.trim())
|
|
66
|
-
.filter(Boolean);
|
|
67
|
-
const out: string[] = [];
|
|
68
|
-
for (const part of parts) {
|
|
69
|
-
// URL is the first token; the rest are descriptors (e.g., 1x, 320w).
|
|
70
|
-
const tokens = part.split(/\s+/).filter(Boolean);
|
|
71
|
-
if (tokens.length === 0) continue;
|
|
72
|
-
const url = tokens[0]!;
|
|
73
|
-
const sanitized = sanitizeResourceUrl(url, policy, 'img');
|
|
74
|
-
if (!sanitized) continue;
|
|
75
|
-
out.push([sanitized, ...tokens.slice(1)].join(' '));
|
|
76
|
-
}
|
|
77
|
-
return out.join(', ');
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Recursively sanitize an HTML element and its children
|
|
82
|
-
*/
|
|
83
|
-
function sanitizeElement(
|
|
84
|
-
element: HTMLElement,
|
|
85
|
-
options: SanitizeHtmlOptions | undefined,
|
|
86
|
-
limitsState: { enabled: boolean; nodes: number; maxNodes: number; maxDepth: number },
|
|
87
|
-
depth: number
|
|
88
|
-
): void {
|
|
89
|
-
if (limitsState.enabled) {
|
|
90
|
-
if (depth > limitsState.maxDepth) {
|
|
91
|
-
throw new HtmlLimitExceeded(`HTML depth exceeds maxHtmlDepth (${depth} > ${limitsState.maxDepth})`);
|
|
92
|
-
}
|
|
93
|
-
limitsState.nodes++;
|
|
94
|
-
if (limitsState.nodes > limitsState.maxNodes) {
|
|
95
|
-
throw new HtmlLimitExceeded(
|
|
96
|
-
`HTML node count exceeds maxHtmlNodes (${limitsState.nodes} > ${limitsState.maxNodes})`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const tagName = (element as any).rawTagName?.toLowerCase();
|
|
102
|
-
|
|
103
|
-
// Remove script tags entirely
|
|
104
|
-
if (tagName === 'script') {
|
|
105
|
-
element.remove();
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Remove high-risk elements by default (same-DOM embedding).
|
|
110
|
-
if (tagName === 'iframe' && !allowIframes(options)) {
|
|
111
|
-
element.remove();
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
if ((tagName === 'object' || tagName === 'embed') && !allowObjectEmbeds(options)) {
|
|
115
|
-
const replacementHtml = getSafeObjectReplacementHtml(element, policyFromOptions(options));
|
|
116
|
-
if (replacementHtml) {
|
|
117
|
-
element.replaceWith(replacementHtml);
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
element.remove();
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
if (tagName === 'base' || tagName === 'meta' || tagName === 'link' || tagName === 'style' || tagName === 'foreignobject') {
|
|
124
|
-
element.remove();
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Remove dangerous event handler attributes (case-insensitive).
|
|
129
|
-
const attrs = element.attributes || {};
|
|
130
|
-
for (const attrName of Object.keys(attrs)) {
|
|
131
|
-
if (attrName.toLowerCase().startsWith('on')) {
|
|
132
|
-
element.removeAttribute(attrName);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Sanitize URL attributes
|
|
137
|
-
const policy = policyFromOptions(options);
|
|
138
|
-
for (const attrName of Object.keys(attrs)) {
|
|
139
|
-
const lower = attrName.toLowerCase();
|
|
140
|
-
const value = element.getAttribute(attrName);
|
|
141
|
-
if (!value) continue;
|
|
142
|
-
|
|
143
|
-
// Block srcdoc everywhere (HTML document string).
|
|
144
|
-
if (lower === 'srcdoc') {
|
|
145
|
-
element.removeAttribute(attrName);
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (lower === 'srcset') {
|
|
150
|
-
const sanitized = sanitizeSrcset(value, policy);
|
|
151
|
-
if (!sanitized) element.removeAttribute(attrName);
|
|
152
|
-
else if (sanitized !== value) element.setAttribute(attrName, sanitized);
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// URL-bearing attributes (include SVG xlink:href).
|
|
157
|
-
if (
|
|
158
|
-
lower === 'href' ||
|
|
159
|
-
lower === 'xlink:href' ||
|
|
160
|
-
lower === 'src' ||
|
|
161
|
-
lower === 'data' ||
|
|
162
|
-
lower === 'action' ||
|
|
163
|
-
lower === 'formaction' ||
|
|
164
|
-
lower === 'poster'
|
|
165
|
-
) {
|
|
166
|
-
const kind =
|
|
167
|
-
tagName === 'img' ? 'img' :
|
|
168
|
-
tagName === 'audio' || tagName === 'video' || tagName === 'source' ? 'media' :
|
|
169
|
-
tagName === 'object' || tagName === 'embed' ? 'object' :
|
|
170
|
-
tagName === 'a' ? 'link' :
|
|
171
|
-
'any';
|
|
172
|
-
const sanitized = sanitizeUrlAttr(value, policy, kind);
|
|
173
|
-
if (sanitized !== value) element.setAttribute(attrName, sanitized);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Recursively sanitize children
|
|
178
|
-
const children = element.childNodes.filter((n) => (n as any).rawTagName) as HTMLElement[];
|
|
179
|
-
for (const child of children) {
|
|
180
|
-
sanitizeElement(child, options, limitsState, depth + 1);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Sanitize HTML string by removing XSS vectors
|
|
186
|
-
*
|
|
187
|
-
* @param html - HTML string to sanitize
|
|
188
|
-
* @returns Sanitized HTML string safe for rendering
|
|
189
|
-
*/
|
|
190
|
-
export function sanitizeHtml(html: string, options?: SanitizeHtmlOptions): string {
|
|
191
|
-
if (!html || typeof html !== 'string') {
|
|
192
|
-
return '';
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
try {
|
|
196
|
-
const limits = normalizeParsingLimits(options?.security);
|
|
197
|
-
if (limits.enabled) {
|
|
198
|
-
const TE: typeof TextEncoder | undefined = (globalThis as any).TextEncoder;
|
|
199
|
-
const bytes = TE ? new TE().encode(html).byteLength : html.length * 2;
|
|
200
|
-
if (bytes > limits.maxHtmlBytes) {
|
|
201
|
-
return '';
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Parse HTML
|
|
206
|
-
const root = parse(html, {
|
|
207
|
-
comment: false, // Remove comments (potential XSS vector)
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
// Sanitize all elements
|
|
211
|
-
const elements = root.childNodes.filter((n) => (n as any).rawTagName) as HTMLElement[];
|
|
212
|
-
const limitsState = {
|
|
213
|
-
enabled: limits.enabled,
|
|
214
|
-
nodes: 0,
|
|
215
|
-
maxNodes: limits.maxHtmlNodes,
|
|
216
|
-
maxDepth: limits.maxHtmlDepth,
|
|
217
|
-
};
|
|
218
|
-
for (const element of elements) {
|
|
219
|
-
sanitizeElement(element, options, limitsState, 1);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Return sanitized HTML
|
|
223
|
-
return root.toString();
|
|
224
|
-
} catch (error) {
|
|
225
|
-
if (error instanceof HtmlLimitExceeded) {
|
|
226
|
-
return '';
|
|
227
|
-
}
|
|
228
|
-
// If parsing fails, return empty string (safer than returning potentially malicious content)
|
|
229
|
-
console.error('HTML sanitization failed:', error);
|
|
230
|
-
return '';
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Sanitize text content in choice labels, prompts, etc.
|
|
236
|
-
* This is for content that gets serialized to JSON and displayed in UI.
|
|
237
|
-
*
|
|
238
|
-
* @param text - Text content that may contain HTML
|
|
239
|
-
* @returns Sanitized text
|
|
240
|
-
*/
|
|
241
|
-
export function sanitizeTextContent(text: string, options?: SanitizeHtmlOptions): string {
|
|
242
|
-
if (!text || typeof text !== 'string') {
|
|
243
|
-
return '';
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// If it looks like HTML (contains tags), sanitize it
|
|
247
|
-
if (/<[^>]+>/.test(text)) {
|
|
248
|
-
return sanitizeHtml(text, options);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Plain text - return as-is
|
|
252
|
-
return text;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function getSafeObjectReplacementHtml(element: HTMLElement, policy: UrlPolicyConfig | undefined): string | null {
|
|
256
|
-
if ((element as any).rawTagName?.toLowerCase() !== 'object') {
|
|
257
|
-
return null;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const rawType = element.getAttribute('type')?.trim().toLowerCase() ?? '';
|
|
261
|
-
const rawData = element.getAttribute('data')?.trim() ?? '';
|
|
262
|
-
if (!rawType || !rawData) {
|
|
263
|
-
return null;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const dimensions = `${getOptionalAttr(element, 'width')}${getOptionalAttr(element, 'height')}`;
|
|
267
|
-
const classes = getOptionalAttr(element, 'class');
|
|
268
|
-
const id = getOptionalAttr(element, 'id');
|
|
269
|
-
|
|
270
|
-
if (rawType.startsWith('image/')) {
|
|
271
|
-
const src = sanitizeResourceUrl(rawData, policy, 'img');
|
|
272
|
-
if (!src) {
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
275
|
-
const alt =
|
|
276
|
-
element.getAttribute('aria-label') ??
|
|
277
|
-
element.getAttribute('title') ??
|
|
278
|
-
element.textContent?.trim() ??
|
|
279
|
-
'';
|
|
280
|
-
return `<img src="${escapeAttr(src)}" alt="${escapeAttr(alt)}"${dimensions}${classes}${id}>`;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
if (rawType.startsWith('audio/')) {
|
|
284
|
-
const src = sanitizeResourceUrl(rawData, policy, 'media');
|
|
285
|
-
if (!src) {
|
|
286
|
-
return null;
|
|
287
|
-
}
|
|
288
|
-
return `<audio controls${classes}${id}><source src="${escapeAttr(src)}" type="${escapeAttr(rawType)}"></audio>`;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (rawType.startsWith('video/')) {
|
|
292
|
-
const src = sanitizeResourceUrl(rawData, policy, 'media');
|
|
293
|
-
if (!src) {
|
|
294
|
-
return null;
|
|
295
|
-
}
|
|
296
|
-
const rawPoster = element.getAttribute('poster')?.trim() ?? '';
|
|
297
|
-
const poster = rawPoster
|
|
298
|
-
? sanitizeResourceUrl(rawPoster, policy, 'img')
|
|
299
|
-
: null;
|
|
300
|
-
const posterAttr = poster ? ` poster="${escapeAttr(poster)}"` : '';
|
|
301
|
-
return `<video controls${dimensions}${classes}${id}${posterAttr}><source src="${escapeAttr(src)}" type="${escapeAttr(rawType)}"></video>`;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
return null;
|
|
305
|
-
}
|
package/src/core/trustedTypes.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import type { HtmlContent, TrustedTypesHtml } from '../types/index.js';
|
|
2
|
-
|
|
3
|
-
type TrustedTypesPolicyLike = {
|
|
4
|
-
createHTML: (input: string) => TrustedTypesHtml;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
function getTrustedTypesGlobal(): any {
|
|
8
|
-
return (globalThis as any).trustedTypes;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const policyCache = new Map<string, TrustedTypesPolicyLike>();
|
|
12
|
-
|
|
13
|
-
function getOrCreatePolicy(policyName: string): TrustedTypesPolicyLike | null {
|
|
14
|
-
const existing = policyCache.get(policyName);
|
|
15
|
-
if (existing) return existing;
|
|
16
|
-
|
|
17
|
-
const tt = getTrustedTypesGlobal();
|
|
18
|
-
if (!tt?.createPolicy) return null;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
const policy = tt.createPolicy(policyName, {
|
|
22
|
-
createHTML: (s: string) => s,
|
|
23
|
-
});
|
|
24
|
-
// Policy may be blocked/throw depending on CSP allowlist.
|
|
25
|
-
if (!policy?.createHTML) return null;
|
|
26
|
-
policyCache.set(policyName, policy as TrustedTypesPolicyLike);
|
|
27
|
-
return policy as TrustedTypesPolicyLike;
|
|
28
|
-
} catch {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Convert a sanitized HTML string into a Trusted Types HTML value when:
|
|
35
|
-
* - the host configured a policy name, and
|
|
36
|
-
* - the browser supports Trusted Types, and
|
|
37
|
-
* - CSP allows creating/using that policy.
|
|
38
|
-
*
|
|
39
|
-
* Otherwise returns the original string.
|
|
40
|
-
*/
|
|
41
|
-
export function toTrustedHtml(html: string, policyName?: string): HtmlContent {
|
|
42
|
-
if (!policyName) return html;
|
|
43
|
-
if (!html) return '';
|
|
44
|
-
|
|
45
|
-
const policy = getOrCreatePolicy(policyName);
|
|
46
|
-
if (!policy) return html;
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
return policy.createHTML(html);
|
|
50
|
-
} catch {
|
|
51
|
-
return html;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Normalize HtmlContent back to a string.
|
|
57
|
-
* Useful when you need to perform string operations (replace/substr/etc) before re-wrapping.
|
|
58
|
-
*/
|
|
59
|
-
export function htmlToString(html: HtmlContent): string {
|
|
60
|
-
return typeof html === 'string' ? html : String(html as any);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
package/src/core/urlPolicy.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import type { UrlPolicyConfig } from '../types/index.js';
|
|
2
|
-
|
|
3
|
-
export type UrlKind = 'img' | 'media' | 'object' | 'link' | 'any';
|
|
4
|
-
|
|
5
|
-
const DEFAULT_POLICY: Required<Pick<
|
|
6
|
-
UrlPolicyConfig,
|
|
7
|
-
| 'allowHttps'
|
|
8
|
-
| 'allowHttp'
|
|
9
|
-
| 'allowProtocolRelative'
|
|
10
|
-
| 'allowDataImages'
|
|
11
|
-
| 'allowSvgDataImages'
|
|
12
|
-
>> = {
|
|
13
|
-
allowHttps: true,
|
|
14
|
-
allowHttp: false,
|
|
15
|
-
allowProtocolRelative: false,
|
|
16
|
-
allowDataImages: true,
|
|
17
|
-
allowSvgDataImages: false,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
function normalizePolicy(policy?: UrlPolicyConfig): UrlPolicyConfig & typeof DEFAULT_POLICY {
|
|
21
|
-
return { ...DEFAULT_POLICY, ...(policy ?? {}) };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function isDangerousScheme(u: string): boolean {
|
|
25
|
-
const s = u.toLowerCase().trim();
|
|
26
|
-
return s.startsWith('javascript:') || s.startsWith('vbscript:') || s.startsWith('data:text/html');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function allowHost(hostname: string, policy: UrlPolicyConfig): boolean {
|
|
30
|
-
const allow = policy.allowedHosts;
|
|
31
|
-
if (!allow || allow.length === 0) return true;
|
|
32
|
-
return allow.includes(hostname);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function sanitizeDataUrl(raw: string, kind: UrlKind, policy: UrlPolicyConfig & typeof DEFAULT_POLICY): string | null {
|
|
36
|
-
const s = raw.toLowerCase().trim();
|
|
37
|
-
if (!s.startsWith('data:')) return null;
|
|
38
|
-
if (kind !== 'img') return null;
|
|
39
|
-
if (!policy.allowDataImages) return null;
|
|
40
|
-
if (!s.startsWith('data:image/')) return null;
|
|
41
|
-
if (!policy.allowSvgDataImages && s.startsWith('data:image/svg+xml')) return null;
|
|
42
|
-
return raw.trim();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Sanitize a URL used for embedding resources (img/audio/video/object/link).
|
|
47
|
-
* Returns null when the URL should be blocked.
|
|
48
|
-
*/
|
|
49
|
-
export function sanitizeResourceUrl(raw: string, policy?: UrlPolicyConfig, kind: UrlKind = 'any'): string | null {
|
|
50
|
-
const p = normalizePolicy(policy);
|
|
51
|
-
const s = String(raw ?? '').trim();
|
|
52
|
-
if (!s) return null;
|
|
53
|
-
|
|
54
|
-
if (isDangerousScheme(s)) return null;
|
|
55
|
-
|
|
56
|
-
// data: URLs (context-sensitive)
|
|
57
|
-
const dataOk = sanitizeDataUrl(s, kind, p);
|
|
58
|
-
if (dataOk) return dataOk;
|
|
59
|
-
if (s.toLowerCase().trim().startsWith('data:')) return null;
|
|
60
|
-
|
|
61
|
-
// Protocol-relative URLs are treated as network loads and blocked by default.
|
|
62
|
-
if (s.startsWith('//')) return p.allowProtocolRelative ? `https:${s}` : null;
|
|
63
|
-
|
|
64
|
-
// Relative/absolute-path URLs: allow and optionally resolve.
|
|
65
|
-
// Bare relative paths (e.g. "images/foo.png" from IMS CP manifests) are treated
|
|
66
|
-
// the same as "./" paths — they have no scheme and are safe to pass through.
|
|
67
|
-
if (s.startsWith('/') || s.startsWith('./') || s.startsWith('../') || !/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(s)) {
|
|
68
|
-
if (p.assetBaseUrl) {
|
|
69
|
-
try {
|
|
70
|
-
// Strip leading '/' to make URL constructor resolve relative to base path, not origin
|
|
71
|
-
const relativePath = s.startsWith('/') ? s.slice(1) : s;
|
|
72
|
-
return new URL(relativePath, p.assetBaseUrl).toString();
|
|
73
|
-
} catch {
|
|
74
|
-
// assetBaseUrl resolution failed; return the raw relative path as-is
|
|
75
|
-
return s;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return s;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Absolute URLs: allow only http/https (by policy) and optional host allowlist.
|
|
82
|
-
let u: URL;
|
|
83
|
-
try {
|
|
84
|
-
u = new URL(s);
|
|
85
|
-
} catch {
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const proto = u.protocol.toLowerCase();
|
|
90
|
-
if (proto === 'https:' && !p.allowHttps) return null;
|
|
91
|
-
if (proto === 'http:' && !p.allowHttp) return null;
|
|
92
|
-
if (proto !== 'https:' && proto !== 'http:') return null;
|
|
93
|
-
|
|
94
|
-
if (!allowHost(u.hostname, p)) return null;
|
|
95
|
-
return u.toString();
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|