@pie-qti/item-player 0.1.21 → 0.1.23

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 CHANGED
@@ -188,6 +188,28 @@ host origin.
188
188
  Portable Custom Interaction execution is disabled unless the host supplies a trusted
189
189
  `pci.moduleResolver`. Authored module paths are never imported directly by default.
190
190
 
191
+ `createAllowlistPciModuleResolver` builds a resolver that imports only from allow-listed origins
192
+ and/or absolute URL prefixes, refusing non-http(s) schemes outright. Passing it is still the host's
193
+ trust decision; it exists so the security-critical check does not have to be rewritten per host.
194
+
195
+ ```ts
196
+ import { createAllowlistPciModuleResolver } from '@pie-qti/item-player';
197
+
198
+ const pci = {
199
+ // Authored relative paths resolve against this, so it must itself be allow-listed.
200
+ baseUrl: 'https://cdn.example.com/pci/item-1/',
201
+ moduleResolver: createAllowlistPciModuleResolver({
202
+ allowedOrigins: ['https://cdn.example.com'],
203
+ allowedPathPrefixes: ['https://cdn.example.com/pci/'],
204
+ }),
205
+ };
206
+ ```
207
+
208
+ Prefixes are matched against the normalized URL, so an authored `../../secrets/token.js` resolves to
209
+ `https://cdn.example.com/secrets/token.js` and is refused. Traversal that stays inside the prefix is
210
+ not a traversal: normalization is the whole check, so keep the prefix as narrow as the deployment
211
+ allows.
212
+
191
213
  ## Server scoring
192
214
 
193
215
  `@pie-qti/item-player/server` exports a DOM-free definition/session interface with the same ownership
@@ -73,10 +73,15 @@ export declare class Player {
73
73
  */
74
74
  createPciHost(data: ExtractedPci): PciHost;
75
75
  /**
76
- * Push a response value into the PCI identified by responseIdentifier.
77
- * Called from setResponses() so session restore reaches PCI modules.
76
+ * Offer a response value to the PCI identified by responseIdentifier. A
77
+ * mounted module that has already reported a response keeps it.
78
78
  */
79
- setPciResponse(responseIdentifier: string, value: unknown): void;
79
+ offerPciResponse(responseIdentifier: string, value: unknown): void;
80
+ /**
81
+ * Authoritatively replace a PCI's response, discarding in-progress candidate
82
+ * state. Session restore and reset-to-default use this.
83
+ */
84
+ restorePciResponse(responseIdentifier: string, value: unknown): void;
80
85
  /**
81
86
  * Disable/enable all PCI hosts based on role.
82
87
  * 'candidate' = enabled; any other role = disabled.
@@ -88,7 +93,21 @@ export declare class Player {
88
93
  destroyPciHosts(): void;
89
94
  /** Breaking-change API: returns the typed declaration map */
90
95
  getDeclarations(): DeclarationMap;
96
+ /**
97
+ * Apply candidate response values to the declaration context.
98
+ *
99
+ * Every candidate interaction reaches this method, including a PCI reporting
100
+ * its own change, so a mounted PCI module is offered the value rather than
101
+ * force-fed it. Use `restoreResponses()` when the caller's value must replace
102
+ * in-progress module state.
103
+ */
91
104
  setResponses(responses: Record<string, unknown>): void;
105
+ /**
106
+ * Apply response values authoritatively, discarding in-progress PCI candidate
107
+ * state. State replacement and explicit host overrides use this.
108
+ */
109
+ restoreResponses(responses: Record<string, unknown>): void;
110
+ private applyResponses;
92
111
  /**
93
112
  * Destroy all resources owned by this player instance (PCI modules, etc.).
94
113
  * Call when unmounting to prevent memory leaks.
@@ -284,11 +284,18 @@ export class Player {
284
284
  return host;
285
285
  }
286
286
  /**
287
- * Push a response value into the PCI identified by responseIdentifier.
288
- * Called from setResponses() so session restore reaches PCI modules.
287
+ * Offer a response value to the PCI identified by responseIdentifier. A
288
+ * mounted module that has already reported a response keeps it.
289
289
  */
290
- setPciResponse(responseIdentifier, value) {
291
- this._pciHosts.get(responseIdentifier)?.setResponse(value);
290
+ offerPciResponse(responseIdentifier, value) {
291
+ this._pciHosts.get(responseIdentifier)?.offerResponse(value);
292
+ }
293
+ /**
294
+ * Authoritatively replace a PCI's response, discarding in-progress candidate
295
+ * state. Session restore and reset-to-default use this.
296
+ */
297
+ restorePciResponse(responseIdentifier, value) {
298
+ this._pciHosts.get(responseIdentifier)?.restore(value);
292
299
  }
293
300
  /**
294
301
  * Disable/enable all PCI hosts based on role.
@@ -316,14 +323,31 @@ export class Player {
316
323
  getDeclarations() {
317
324
  return this.decls;
318
325
  }
326
+ /**
327
+ * Apply candidate response values to the declaration context.
328
+ *
329
+ * Every candidate interaction reaches this method, including a PCI reporting
330
+ * its own change, so a mounted PCI module is offered the value rather than
331
+ * force-fed it. Use `restoreResponses()` when the caller's value must replace
332
+ * in-progress module state.
333
+ */
319
334
  setResponses(responses) {
335
+ this.applyResponses(responses, (id, value) => this.offerPciResponse(id, value));
336
+ }
337
+ /**
338
+ * Apply response values authoritatively, discarding in-progress PCI candidate
339
+ * state. State replacement and explicit host overrides use this.
340
+ */
341
+ restoreResponses(responses) {
342
+ this.applyResponses(responses, (id, value) => this.restorePciResponse(id, value));
343
+ }
344
+ applyResponses(responses, applyToPci) {
320
345
  for (const [id, raw] of Object.entries(responses)) {
321
346
  const d = this.decls[id];
322
347
  if (!d)
323
348
  continue;
324
349
  d.value = this.coerceToDeclarationValue(d.baseType, d.cardinality, raw, d.identifier);
325
- // Push restored responses into any mounted PCI modules
326
- this.setPciResponse(id, this.qtiValueToPublic(d.value));
350
+ applyToPci(id, this.qtiValueToPublic(d.value));
327
351
  }
328
352
  }
329
353
  /**
@@ -544,7 +568,7 @@ export class Player {
544
568
  if (d.__kind !== 'response')
545
569
  continue;
546
570
  this.ctx.resetToDefault(d.identifier);
547
- this.setPciResponse(d.identifier, d.value.kind === 'value' ? d.value.value : null);
571
+ this.restorePciResponse(d.identifier, d.value.kind === 'value' ? d.value.value : null);
548
572
  }
549
573
  }
550
574
  resetTemplatesToDefault() {
@@ -1275,7 +1299,7 @@ export class Player {
1275
1299
  continue;
1276
1300
  d.value = this.coerceToDeclarationValue(d.baseType, d.cardinality, v.value, d.identifier);
1277
1301
  if (d.__kind === 'response') {
1278
- this.setPciResponse(d.identifier, this.qtiValueToPublic(d.value));
1302
+ this.restorePciResponse(d.identifier, this.qtiValueToPublic(d.value));
1279
1303
  }
1280
1304
  }
1281
1305
  }