@kehto/paja 0.30.0 → 0.31.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.
@@ -14787,6 +14787,66 @@ function installPajaResourceSettings() {
14787
14787
  return { getServers: settings.getServers, dispose: () => form?.removeEventListener("submit", submit) };
14788
14788
  }
14789
14789
 
14790
+ // src/browser-security-settings.ts
14791
+ var STORAGE_KEY2 = "kehto:paja:nested-context-lock";
14792
+ function isSetting(value) {
14793
+ return value === "auto" || value === "on" || value === "off";
14794
+ }
14795
+ function readStoredNestedContextLockSetting(getStorage = () => localStorage) {
14796
+ try {
14797
+ const stored = getStorage().getItem(STORAGE_KEY2);
14798
+ return isSetting(stored) ? stored : null;
14799
+ } catch {
14800
+ return null;
14801
+ }
14802
+ }
14803
+ function createPajaSecuritySettings(getStorage = () => localStorage) {
14804
+ let setting = readStoredNestedContextLockSetting(getStorage);
14805
+ let status = setting ? `Frame-creation lock forced ${setting} for this host.` : "Frame-creation lock follows the host default.";
14806
+ return {
14807
+ getSetting: () => setting,
14808
+ getStatus: () => status,
14809
+ save(next) {
14810
+ if (next !== "" && !isSetting(next)) {
14811
+ return { ok: false, message: "Choose host default, auto, on, or off." };
14812
+ }
14813
+ try {
14814
+ const storage = getStorage();
14815
+ if (next === "") {
14816
+ storage.removeItem(STORAGE_KEY2);
14817
+ setting = null;
14818
+ status = "Frame-creation lock follows the host default.";
14819
+ } else {
14820
+ storage.setItem(STORAGE_KEY2, next);
14821
+ setting = next;
14822
+ status = `Frame-creation lock forced ${next}; applies on the next napplet load.`;
14823
+ }
14824
+ } catch {
14825
+ setting = next === "" ? null : next;
14826
+ status = "Applied for this session only; storage is unavailable, so the choice will not persist.";
14827
+ }
14828
+ return { ok: true, message: status };
14829
+ }
14830
+ };
14831
+ }
14832
+ function installPajaSecuritySettings() {
14833
+ const settings = createPajaSecuritySettings();
14834
+ const select2 = document.getElementById("paja-nested-context-lock");
14835
+ const status = document.getElementById("paja-nested-context-lock-status");
14836
+ if (!select2 || !status) return { getSetting: settings.getSetting, dispose: () => {
14837
+ } };
14838
+ select2.value = settings.getSetting() ?? "";
14839
+ status.textContent = settings.getStatus();
14840
+ const onChange = () => {
14841
+ const result2 = settings.save(select2.value);
14842
+ select2.setAttribute("aria-invalid", String(!result2.ok));
14843
+ status.textContent = result2.message;
14844
+ if (!result2.ok) select2.value = settings.getSetting() ?? "";
14845
+ };
14846
+ select2.addEventListener("change", onChange);
14847
+ return { getSetting: settings.getSetting, dispose: () => select2.removeEventListener("change", onChange) };
14848
+ }
14849
+
14790
14850
  // src/browser-llm-models.ts
14791
14851
  var PajaLlmModelsError = class extends Error {
14792
14852
  constructor(failure4, message3) {
@@ -16294,7 +16354,7 @@ var sessionRegistry = {
16294
16354
  }
16295
16355
  };
16296
16356
  var nappKeyRegistry = sessionRegistry;
16297
- var STORAGE_KEY2 = "napplet:acl";
16357
+ var STORAGE_KEY3 = "napplet:acl";
16298
16358
  var DEFAULT_STATE_QUOTA = 512 * 1024;
16299
16359
  function aclKey(_pubkey, dTag, aggregateHash) {
16300
16360
  return `${dTag}:${aggregateHash}`;
@@ -16461,14 +16521,14 @@ var aclStore = {
16461
16521
  stateQuota: val.stateQuota
16462
16522
  }
16463
16523
  ]);
16464
- localStorage.setItem(STORAGE_KEY2, JSON.stringify(entries));
16524
+ localStorage.setItem(STORAGE_KEY3, JSON.stringify(entries));
16465
16525
  } catch {
16466
16526
  }
16467
16527
  },
16468
16528
  /** Load the ACL store from localStorage. Migrates old 3-segment keys to 2-segment format. */
16469
16529
  load() {
16470
16530
  try {
16471
- const raw = localStorage.getItem(STORAGE_KEY2);
16531
+ const raw = localStorage.getItem(STORAGE_KEY3);
16472
16532
  if (!raw) return;
16473
16533
  let entries = JSON.parse(raw);
16474
16534
  const hasOldKeys = entries.some(([key]) => key.split(":").length === 3);
@@ -16496,7 +16556,7 @@ var aclStore = {
16496
16556
  stateQuota: entry.quota
16497
16557
  }];
16498
16558
  });
16499
- localStorage.setItem(STORAGE_KEY2, JSON.stringify(entries));
16559
+ localStorage.setItem(STORAGE_KEY3, JSON.stringify(entries));
16500
16560
  }
16501
16561
  }
16502
16562
  store.clear();
@@ -16532,7 +16592,7 @@ var aclStore = {
16532
16592
  clear() {
16533
16593
  store.clear();
16534
16594
  try {
16535
- localStorage.removeItem(STORAGE_KEY2);
16595
+ localStorage.removeItem(STORAGE_KEY3);
16536
16596
  } catch {
16537
16597
  }
16538
16598
  }
@@ -17330,7 +17390,76 @@ function scriptJson(value) {
17330
17390
  }
17331
17391
  function renderNappletNamespacePrelude(options) {
17332
17392
  const domains = uniqueBareDomains(options.domains);
17333
- return `<script data-kehto-nip5d-injection>(${nappletNamespacePrelude.toString()})(${scriptJson(domains)}, (${createIntentUriNormalizer.toString()})());<\/script>`;
17393
+ const frameLock = options.experimental?.nestedContextLock ? nappletFrameLock.toString() : "(function(){})";
17394
+ return `<script data-kehto-nip5d-injection>(${nappletNamespacePrelude.toString()})(${scriptJson(domains)}, (${createIntentUriNormalizer.toString()})(), ${frameLock});<\/script>`;
17395
+ }
17396
+ function nappletFrameLock(win) {
17397
+ const FRAME_ELEMENTS2 = /* @__PURE__ */ new Set([
17398
+ "iframe",
17399
+ "frame",
17400
+ "frameset",
17401
+ "object",
17402
+ "embed",
17403
+ "portal",
17404
+ "fencedframe"
17405
+ ]);
17406
+ const isFrameTag = (value) => FRAME_ELEMENTS2.has(String(value).toLowerCase());
17407
+ const FRAME_TAG2 = /<\/?(?:iframe|frame|frameset|object|embed|portal|fencedframe)(?=[\s/>]|$)/i;
17408
+ const FRAME_TAG_PREFIX = /<[a-zA-Z]*$/;
17409
+ const { trustedTypes } = win;
17410
+ if (trustedTypes) {
17411
+ try {
17412
+ trustedTypes.createPolicy("default", {
17413
+ createHTML: (input, ...rest) => {
17414
+ const sink = rest.length >= 2 ? String(rest[1]) : "";
17415
+ const blocked = /write/i.test(sink) ? FRAME_TAG2.test(input) || FRAME_TAG_PREFIX.test(input) : FRAME_TAG2.test(input);
17416
+ if (blocked) throw new TypeError("kehto: napplets may not create nested browsing contexts");
17417
+ return input;
17418
+ },
17419
+ createScript: (input) => input,
17420
+ createScriptURL: (input) => input
17421
+ });
17422
+ } catch {
17423
+ }
17424
+ }
17425
+ const pin = (object, name, value) => {
17426
+ try {
17427
+ Object.defineProperty(object, name, {
17428
+ value,
17429
+ writable: false,
17430
+ configurable: false,
17431
+ enumerable: false
17432
+ });
17433
+ } catch {
17434
+ }
17435
+ };
17436
+ const localName = (qualified) => String(qualified ?? "").split(":").pop().toLowerCase();
17437
+ const HTML_NS = "http://www.w3.org/1999/xhtml";
17438
+ const docProto = win.Document?.prototype;
17439
+ if (docProto) {
17440
+ const createElement = docProto.createElement;
17441
+ pin(docProto, "createElement", function(tagName, ...rest) {
17442
+ if (isFrameTag(tagName)) throw new TypeError("kehto: napplets may not create nested browsing contexts");
17443
+ return createElement.call(this, tagName, ...rest);
17444
+ });
17445
+ const createElementNS = docProto.createElementNS;
17446
+ pin(docProto, "createElementNS", function(namespace, qualifiedName, ...rest) {
17447
+ if (namespace === HTML_NS && FRAME_ELEMENTS2.has(localName(qualifiedName))) {
17448
+ throw new TypeError("kehto: napplets may not create nested browsing contexts");
17449
+ }
17450
+ return createElementNS.call(this, namespace, qualifiedName, ...rest);
17451
+ });
17452
+ }
17453
+ const domImpl = win.DOMImplementation?.prototype;
17454
+ if (domImpl) {
17455
+ const createDocument = domImpl.createDocument;
17456
+ pin(domImpl, "createDocument", function(namespace, qualifiedName, ...rest) {
17457
+ if (namespace === HTML_NS && FRAME_ELEMENTS2.has(localName(qualifiedName))) {
17458
+ throw new TypeError("kehto: napplets may not create nested browsing contexts");
17459
+ }
17460
+ return createDocument.call(this, namespace, qualifiedName, ...rest);
17461
+ });
17462
+ }
17334
17463
  }
17335
17464
  function injectNappletNamespacePrelude(html, options) {
17336
17465
  const prelude = renderNappletNamespacePrelude(options);
@@ -17346,7 +17475,7 @@ function injectNappletNamespacePrelude(html, options) {
17346
17475
  }
17347
17476
  return `${prelude}${html}`;
17348
17477
  }
17349
- function nappletNamespacePrelude(domains, normalizeIntentUri2) {
17478
+ function nappletNamespacePrelude(domains, normalizeIntentUri2, frameLock) {
17350
17479
  const target = window;
17351
17480
  const allowed2 = new Set(domains);
17352
17481
  const requestTimeoutMs = 3e4;
@@ -17372,6 +17501,7 @@ function nappletNamespacePrelude(domains, normalizeIntentUri2) {
17372
17501
  } catch {
17373
17502
  }
17374
17503
  }
17504
+ frameLock(target);
17375
17505
  function id() {
17376
17506
  if (target.crypto?.randomUUID) return target.crypto.randomUUID();
17377
17507
  return `napplet-${Date.now()}-${Math.random().toString(16).slice(2)}`;
@@ -19046,7 +19176,7 @@ function requireBootstrapScripts(directive, sources2) {
19046
19176
  throw new Error(`CSP ${directive} must allow the shell's inline namespace bootstrap`);
19047
19177
  }
19048
19178
  }
19049
- function buildNappletCsp(options = {}) {
19179
+ function buildNappletCsp(options = {}, lockTrustedTypes = false) {
19050
19180
  const grants = [...new Set((options.connectOrigins ?? []).map(connectionOrigin))].sort();
19051
19181
  const policy = {
19052
19182
  ...DEFAULT_DIRECTIVES,
@@ -19068,16 +19198,60 @@ function buildNappletCsp(options = {}) {
19068
19198
  }
19069
19199
  requireBootstrapScripts("script-src", policy["script-src"]);
19070
19200
  if (policy["script-src-elem"]) requireBootstrapScripts("script-src-elem", policy["script-src-elem"]);
19071
- return Object.entries(policy).map(([name, sources2]) => `${name} ${sources2.join(" ")}`).join("; ");
19201
+ const sourceLists = Object.entries(policy).map(([name, sources2]) => `${name} ${sources2.join(" ")}`);
19202
+ if (lockTrustedTypes) sourceLists.push(TRUSTED_TYPES_DIRECTIVES);
19203
+ return sourceLists.join("; ");
19072
19204
  }
19073
- function renderNappletCspMeta(options = {}) {
19074
- const policy = buildNappletCsp(options).replace(/&/g, "&amp;").replace(/"/g, "&quot;");
19205
+ var TRUSTED_TYPES_DIRECTIVES = "require-trusted-types-for 'script'; trusted-types default";
19206
+ function renderNappletCspMeta(options = {}, lockTrustedTypes = false) {
19207
+ const policy = buildNappletCsp(options, lockTrustedTypes).replace(/&/g, "&amp;").replace(/"/g, "&quot;");
19075
19208
  return `<meta http-equiv="Content-Security-Policy" content="${policy}">`;
19076
19209
  }
19077
19210
  function prepareNappletSrcdoc(html, options) {
19078
- const meta = renderNappletCspMeta(options.csp);
19211
+ const lockNestedContexts = options.experimental?.nestedContextLock === true;
19212
+ const meta = renderNappletCspMeta(options.csp, lockNestedContexts);
19079
19213
  return insertNappletHead(html, meta + renderNappletNamespacePrelude(options));
19080
19214
  }
19215
+ var FRAME_ELEMENTS = /* @__PURE__ */ new Set([
19216
+ "iframe",
19217
+ "frame",
19218
+ "frameset",
19219
+ "object",
19220
+ "embed",
19221
+ "portal",
19222
+ "fencedframe"
19223
+ ]);
19224
+ var FRAME_TAG = /<\/?(?:iframe|frame|frameset|object|embed|portal|fencedframe)(?=[\s/>]|$)/i;
19225
+ function subtreeHasFrame(root) {
19226
+ for (const element2 of Array.from(root.querySelectorAll("*"))) {
19227
+ const local = (element2.localName || "").toLowerCase();
19228
+ if (element2.namespaceURI === "http://www.w3.org/1999/xhtml" && FRAME_ELEMENTS.has(local)) {
19229
+ return true;
19230
+ }
19231
+ const template = element2;
19232
+ if (local === "template" && template.content && subtreeHasFrame(template.content)) return true;
19233
+ if (element2.shadowRoot && subtreeHasFrame(element2.shadowRoot)) return true;
19234
+ }
19235
+ return false;
19236
+ }
19237
+ function nappletMarkupHasFrame(html) {
19238
+ if (typeof html !== "string") return false;
19239
+ const DOMParser = globalThis.DOMParser;
19240
+ if (DOMParser) {
19241
+ try {
19242
+ return subtreeHasFrame(new DOMParser().parseFromString(html, "text/html"));
19243
+ } catch {
19244
+ }
19245
+ }
19246
+ return FRAME_TAG.test(html);
19247
+ }
19248
+ function assertNappletHasNoFrame(html) {
19249
+ if (nappletMarkupHasFrame(html)) {
19250
+ throw new Error(
19251
+ "kehto: napplet markup declares a nested browsing context (iframe/frame/object/embed); rejected before load"
19252
+ );
19253
+ }
19254
+ }
19081
19255
 
19082
19256
  // src/browser-console-panel.ts
19083
19257
  var PAJA_CONSOLE_PANEL_STORAGE_KEY = "kehto:paja:console:v1";
@@ -25340,8 +25514,14 @@ async function navigateFrame(frame, config, generation2, adapter, resolvedTarget
25340
25514
  console.warn(`[paja] Napplet requires unsupported NAP capabilities: ${missing.join(", ")}; full functionality may be unavailable.`);
25341
25515
  }
25342
25516
  }
25517
+ const lockSetting = readStoredNestedContextLockSetting() ?? config.experimental?.nestedContextLock ?? "auto";
25518
+ const nestedContextLock = resolveNestedContextLock(lockSetting, domains);
25519
+ if (nestedContextLock) {
25520
+ assertNappletHasNoFrame(resolvedTarget.indexHtml);
25521
+ }
25343
25522
  const srcdoc = prepareNappletSrcdoc(resolvedTarget.indexHtml, {
25344
25523
  domains,
25524
+ ...nestedContextLock ? { experimental: { nestedContextLock: true } } : {},
25345
25525
  csp: {
25346
25526
  ...config.csp,
25347
25527
  connectOrigins: [
@@ -25367,6 +25547,12 @@ async function navigateFrame(frame, config, generation2, adapter, resolvedTarget
25367
25547
  );
25368
25548
  return registeredWindowId;
25369
25549
  }
25550
+ var PRIVACY_SENSITIVE_DOMAINS = /* @__PURE__ */ new Set(["dm"]);
25551
+ function resolveNestedContextLock(setting, grantedDomains) {
25552
+ if (setting === "on") return true;
25553
+ if (setting === "off") return false;
25554
+ return grantedDomains.some((domain) => PRIVACY_SENSITIVE_DOMAINS.has(domain));
25555
+ }
25370
25556
  function renderTargetErrorHtml(error) {
25371
25557
  const message3 = error instanceof Error ? error.message : String(error);
25372
25558
  return `<!doctype html><html><body><pre>${escapeHtml(message3)}</pre></body></html>`;
@@ -29777,6 +29963,8 @@ async function installPajaHost() {
29777
29963
  const disposeSidebarSections = installPajaSidebarSections();
29778
29964
  window.addEventListener("pagehide", disposeSidebarSections, { once: true });
29779
29965
  const resourceSettings = installPajaResourceSettings();
29966
+ const securitySettings = installPajaSecuritySettings();
29967
+ window.addEventListener("pagehide", securitySettings.dispose, { once: true });
29780
29968
  const config = await readLatestPajaHostConfig(readPajaHostConfig());
29781
29969
  const stage = getStage();
29782
29970
  const frame = config.target.mode === "runtime-pointer" ? null : getFrame();