@ghostlytools/sdk 1.12.0 → 1.13.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.
@@ -14,7 +14,14 @@ import type { HeaderList } from "./frames.js";
14
14
  * refuses `javascript:` URLs (its own Trusted Types default policy: one passes every other string on as it is);
15
15
  * - refuses a declarative shadow root the page's script would make (`setHTMLUnsafe`, `Document.parseHTMLUnsafe`,
16
16
  * `document.write`, a write split in two too): its `srcdoc` frames would be out of the guard's sight;
17
- * - listens for frames' loads again after `document.open()`, which takes every listener of the document away.
17
+ * - listens for frames' loads again after `document.open()`, which takes every listener of the document away;
18
+ * - starts a worker made from a `blob:` URL of the page's own (`URL.createObjectURL` in that window) from a script of its
19
+ * own that makes a pass-through Trusted Types `default` policy there first and then runs the page's (`importScripts`;
20
+ * for a module worker, a static `import` of the policy's module, then of the page's): such a worker takes the page's
21
+ * policy, Trusted Types included, and no guard runs in it to make one. A `SharedWorker` made twice from one blob gets
22
+ * the same script, so it is the same worker; the page's revoke of the blob's URL drops the guard's own a minute later.
23
+ * A `data:` worker, and a blob made in another window, in a worker or already revoked, run as they were. Once per
24
+ * window, whichever guard runs first (`ghostlyWorkerGuard`, `CLIPBOARD_GUARD_SCRIPT` does the same).
18
25
  * It uses nothing the page can change after it ran (the page's own code runs later, in the same realm).
19
26
  *
20
27
  * What a script put in the page cannot do, so where the viewer does not run it per document this is not a guarantee:
@@ -23,9 +30,9 @@ import type { HeaderList } from "./frames.js";
23
30
  * guard is there.
24
31
  * No backslash, `${` or backtick in it: Desktop's copy (`SERVICE_GUARD`, apps/desktop/src/viewer.rs) is the same text.
25
32
  */
26
- export declare const VIEWER_GUARD_SCRIPT = "(function noWebRtc(w) {\n \"use strict\";\n try {\n const apply = Reflect.apply;\n const own = \"<script>(\" + noWebRtc + \")(window)<\" + \"/script>\";\n for (const name of Object.getOwnPropertyNames(w)) {\n if (/^(webkit)?RTC/.test(name)) { try { delete w[name]; } catch (e) {} }\n }\n if (w.trustedTypes && !w.trustedTypes.defaultPolicy) {\n try {\n w.trustedTypes.createPolicy(\"default\", {\n createHTML: (input) => input,\n createScript: (input, type, sink) => (sink === \"Location href\" ? null : input),\n createScriptURL: (input) => input,\n });\n } catch (e) {}\n }\n const open = w.open;\n const opener = function (url, target) {\n apply(open, this, [url === undefined ? \"\" : url, target === undefined ? \"_blank\" : target, \"noopener\"]);\n return null;\n };\n w.open = opener;\n const describe = Object.getOwnPropertyDescriptor;\n const define = Object.defineProperty;\n const shut = (inner) => {\n try { if (inner && inner !== w && (\"RTCPeerConnection\" in inner || \"webkitRTCPeerConnection\" in inner)) noWebRtc(inner); } catch (e) {}\n };\n const count = describe(w, \"length\").get;\n const view = describe(w.Document.prototype, \"defaultView\").get;\n const slice = String.prototype.slice;\n const sweep = () => {\n try { for (let i = apply(count, w, []); i-- > 0;) shut(w[i]); } catch (e) {}\n };\n const listen = w.EventTarget.prototype.addEventListener;\n const home = w.document;\n const reopen = w.Document.prototype.open;\n w.Document.prototype.open = function () {\n if (arguments.length > 2) return apply(opener, w, [arguments[0], arguments[1]]);\n const got = apply(reopen, this, arguments);\n if (this === home) { try { apply(listen, home, [\"load\", sweep, true]); } catch (e) {} }\n return got;\n };\n const text = String;\n const lower = String.prototype.toLowerCase;\n const index = String.prototype.indexOf;\n const Refused = w.TypeError;\n const shadowed = (html) => apply(index, apply(lower, html, []), [\"shadowrootmode\"]) >= 0;\n const refuse = () => { throw new Refused(\"A declarative shadow root is not allowed here\"); };\n for (const [owner, name] of [[w.Element.prototype, \"setHTMLUnsafe\"], [w.ShadowRoot && w.ShadowRoot.prototype, \"setHTMLUnsafe\"], [w.Document, \"parseHTMLUnsafe\"]]) {\n const real = owner && owner[name];\n if (typeof real !== \"function\") continue;\n owner[name] = function (html) {\n const checked = text(html);\n if (shadowed(checked)) refuse();\n return apply(real, this, arguments.length > 1 ? [checked, arguments[1]] : [checked]);\n };\n }\n let tail = \"\";\n for (const name of [\"write\", \"writeln\"]) {\n const real = w.Document.prototype[name];\n if (typeof real !== \"function\") continue;\n w.Document.prototype[name] = function () {\n let checked = \"\";\n for (let i = 0; i < arguments.length; i++) checked += text(arguments[i]);\n if (shadowed(tail + checked)) refuse();\n tail = apply(slice, tail + checked, [-16]);\n return apply(real, this, [checked]);\n };\n }\n for (const kind of [\"HTMLIFrameElement\", \"HTMLFrameElement\", \"HTMLObjectElement\", \"HTMLEmbedElement\"]) {\n const proto = w[kind] && w[kind].prototype;\n for (const name of [\"contentWindow\", \"contentDocument\", \"getSVGDocument\"]) {\n const found = proto && describe(proto, name);\n const key = found && found.get ? \"get\" : \"value\";\n if (!found || typeof found[key] !== \"function\") continue;\n const read = found[key];\n found[key] = function () {\n const got = apply(read, this, arguments);\n try { shut(name === \"contentWindow\" ? got : apply(view, got, [])); } catch (e) {}\n return got;\n };\n try { define(proto, name, found); } catch (e) {}\n }\n }\n const { getAttribute, setAttribute, removeAttribute, attachShadow } = w.Element.prototype;\n const size = describe(w.NodeList.prototype, \"length\").get;\n const first = (frame) => {\n const doc = apply(getAttribute, frame, [\"srcdoc\"]);\n if (doc === null || apply(slice, doc, [0, own.length]) === own) return;\n try { apply(setAttribute, frame, [\"srcdoc\", own + doc]); } catch (e) { apply(removeAttribute, frame, [\"srcdoc\"]); }\n };\n const Observer = w.MutationObserver;\n const observe = Observer.prototype.observe;\n const watch = (root, all) => {\n const scan = () => {\n try {\n const frames = apply(all, root, [\"iframe[srcdoc]\"]);\n for (let i = apply(size, frames, []); i-- > 0;) first(frames[i]);\n } catch (e) {}\n sweep();\n };\n apply(observe, new Observer(scan), [root, { subtree: true, childList: true, attributes: true, attributeFilter: [\"srcdoc\"] }]);\n scan();\n return root;\n };\n const inShadow = w.DocumentFragment.prototype.querySelectorAll;\n if (attachShadow) {\n w.Element.prototype.attachShadow = function () {\n const root = apply(attachShadow, this, arguments);\n try { watch(root, inShadow); } catch (e) {}\n return root;\n };\n }\n apply(listen, home, [\"load\", sweep, true]);\n watch(home, w.Document.prototype.querySelectorAll);\n } catch (e) {}\n})(window)";
33
+ export declare const VIEWER_GUARD_SCRIPT = "(function noWebRtc(w) {\n \"use strict\";\n try {\n const apply = Reflect.apply;\n const own = \"<script>(\" + noWebRtc + \")(window)<\" + \"/script>\";\n for (const name of Object.getOwnPropertyNames(w)) {\n if (/^(webkit)?RTC/.test(name)) { try { delete w[name]; } catch (e) {} }\n }\n if (w.trustedTypes && !w.trustedTypes.defaultPolicy) {\n try {\n w.trustedTypes.createPolicy(\"default\", {\n createHTML: (input) => input,\n createScript: (input, type, sink) => (sink === \"Location href\" ? null : input),\n createScriptURL: (input) => input,\n });\n } catch (e) {}\n }\n const open = w.open;\n const opener = function (url, target) {\n apply(open, this, [url === undefined ? \"\" : url, target === undefined ? \"_blank\" : target, \"noopener\"]);\n return null;\n };\n w.open = opener;\n const describe = Object.getOwnPropertyDescriptor;\n const define = Object.defineProperty;\n const shut = (inner) => {\n try { if (inner && inner !== w && (\"RTCPeerConnection\" in inner || \"webkitRTCPeerConnection\" in inner)) noWebRtc(inner); } catch (e) {}\n };\n const count = describe(w, \"length\").get;\n const view = describe(w.Document.prototype, \"defaultView\").get;\n const slice = String.prototype.slice;\n const sweep = () => {\n try { for (let i = apply(count, w, []); i-- > 0;) shut(w[i]); } catch (e) {}\n };\n const listen = w.EventTarget.prototype.addEventListener;\n const home = w.document;\n const reopen = w.Document.prototype.open;\n w.Document.prototype.open = function () {\n if (arguments.length > 2) return apply(opener, w, [arguments[0], arguments[1]]);\n const got = apply(reopen, this, arguments);\n if (this === home) { try { apply(listen, home, [\"load\", sweep, true]); } catch (e) {} }\n return got;\n };\n const text = String;\n const lower = String.prototype.toLowerCase;\n const index = String.prototype.indexOf;\n const Refused = w.TypeError;\n const shadowed = (html) => apply(index, apply(lower, html, []), [\"shadowrootmode\"]) >= 0;\n const refuse = () => { throw new Refused(\"A declarative shadow root is not allowed here\"); };\n for (const [owner, name] of [[w.Element.prototype, \"setHTMLUnsafe\"], [w.ShadowRoot && w.ShadowRoot.prototype, \"setHTMLUnsafe\"], [w.Document, \"parseHTMLUnsafe\"]]) {\n const real = owner && owner[name];\n if (typeof real !== \"function\") continue;\n owner[name] = function (html) {\n const checked = text(html);\n if (shadowed(checked)) refuse();\n return apply(real, this, arguments.length > 1 ? [checked, arguments[1]] : [checked]);\n };\n }\n let tail = \"\";\n for (const name of [\"write\", \"writeln\"]) {\n const real = w.Document.prototype[name];\n if (typeof real !== \"function\") continue;\n w.Document.prototype[name] = function () {\n let checked = \"\";\n for (let i = 0; i < arguments.length; i++) checked += text(arguments[i]);\n if (shadowed(tail + checked)) refuse();\n tail = apply(slice, tail + checked, [-16]);\n return apply(real, this, [checked]);\n };\n }\n for (const kind of [\"HTMLIFrameElement\", \"HTMLFrameElement\", \"HTMLObjectElement\", \"HTMLEmbedElement\"]) {\n const proto = w[kind] && w[kind].prototype;\n for (const name of [\"contentWindow\", \"contentDocument\", \"getSVGDocument\"]) {\n const found = proto && describe(proto, name);\n const key = found && found.get ? \"get\" : \"value\";\n if (!found || typeof found[key] !== \"function\") continue;\n const read = found[key];\n found[key] = function () {\n const got = apply(read, this, arguments);\n try { shut(name === \"contentWindow\" ? got : apply(view, got, [])); } catch (e) {}\n return got;\n };\n try { define(proto, name, found); } catch (e) {}\n }\n }\n const { getAttribute, setAttribute, removeAttribute, attachShadow } = w.Element.prototype;\n const size = describe(w.NodeList.prototype, \"length\").get;\n const first = (frame) => {\n const doc = apply(getAttribute, frame, [\"srcdoc\"]);\n if (doc === null || apply(slice, doc, [0, own.length]) === own) return;\n try { apply(setAttribute, frame, [\"srcdoc\", own + doc]); } catch (e) { apply(removeAttribute, frame, [\"srcdoc\"]); }\n };\n const Observer = w.MutationObserver;\n const observe = Observer.prototype.observe;\n const watch = (root, all) => {\n const scan = () => {\n try {\n const frames = apply(all, root, [\"iframe[srcdoc]\"]);\n for (let i = apply(size, frames, []); i-- > 0;) first(frames[i]);\n } catch (e) {}\n sweep();\n };\n apply(observe, new Observer(scan), [root, { subtree: true, childList: true, attributes: true, attributeFilter: [\"srcdoc\"] }]);\n scan();\n return root;\n };\n const inShadow = w.DocumentFragment.prototype.querySelectorAll;\n if (attachShadow) {\n w.Element.prototype.attachShadow = function () {\n const root = apply(attachShadow, this, arguments);\n try { watch(root, inShadow); } catch (e) {}\n return root;\n };\n }\n if (w.trustedTypes && w.URL && w.Blob && !describe(w, \"ghostlyWorkerGuard\")) {\n try {\n define(w, \"ghostlyWorkerGuard\", { value: true });\n const construct = Reflect.construct;\n const json = JSON.stringify;\n const later = w.setTimeout;\n const Blob = w.Blob;\n const sized = describe(Blob.prototype, \"size\").get;\n const make = w.URL.createObjectURL;\n const revoke = w.URL.revokeObjectURL;\n const policy = \"if (self.trustedTypes && !self.trustedTypes.defaultPolicy) { try { self.trustedTypes.createPolicy('default', { createHTML: (s) => s, createScript: (s) => s, createScriptURL: (s) => s }); } catch (e) {} }\";\n const script = (code) => apply(make, null, [new Blob([code], { type: \"text/javascript\" })]);\n const known = { __proto__: null };\n let first = null;\n w.URL.createObjectURL = function createObjectURL(object) {\n const made = apply(make, this, arguments);\n try { apply(sized, object, []); known[made] = { blob: object, inner: null, classic: null, module: null }; } catch (e) {}\n return made;\n };\n w.URL.revokeObjectURL = function revokeObjectURL(url) {\n try {\n const key = text(url);\n const entry = known[key];\n if (entry) {\n delete known[key];\n const ours = [entry.inner, entry.classic, entry.module];\n apply(later, w, [() => { for (let i = 0; i < 3; i++) { if (ours[i] !== null) apply(revoke, null, [ours[i]]); } }, 60000]);\n }\n } catch (e) {}\n return apply(revoke, this, arguments);\n };\n const start = (url, options) => {\n const entry = known[text(url)];\n if (!entry) return url;\n const kind = options && typeof options === \"object\" && options.type === \"module\" ? \"module\" : \"classic\";\n if (entry[kind] === null) {\n if (entry.inner === null) entry.inner = apply(make, null, [entry.blob]);\n if (kind === \"module\" && first === null) first = script(policy);\n entry[kind] = script(kind === \"module\" ? \"import \" + json(first) + \"; import \" + json(entry.inner) + \";\" : policy + \" importScripts(\" + json(entry.inner) + \");\");\n }\n return entry[kind];\n };\n for (const name of [\"Worker\", \"SharedWorker\"]) {\n const Real = w[name];\n if (typeof Real !== \"function\") continue;\n const started = function (url) {\n if (new.target === undefined) return apply(Real, this, arguments);\n const args = [];\n for (let i = 0; i < arguments.length; i++) args[i] = arguments[i];\n if (args.length > 0) { try { args[0] = start(url, args[1]); } catch (e) {} }\n return construct(Real, args, new.target);\n };\n define(started, \"name\", { value: name });\n started.prototype = Real.prototype;\n define(Real.prototype, \"constructor\", { value: started, writable: true, enumerable: false, configurable: true });\n w[name] = started;\n }\n } catch (e) {}\n }\n apply(listen, home, [\"load\", sweep, true]);\n watch(home, w.Document.prototype.querySelectorAll);\n } catch (e) {}\n})(window)";
27
34
  /** `VIEWER_GUARD_SCRIPT`'s hash, as a CSP source: a contact's own policy that would refuse it is given it (`allowingViewerGuard`). */
28
- export declare const VIEWER_GUARD_HASH = "'sha256-P3jYgJwfoJ/cAS31rQz7Tk99bBwsSeN2wDDqJubDMZw='";
35
+ export declare const VIEWER_GUARD_HASH = "'sha256-DweV0mr/eyHFWESxtsRrzk0i6VjF4GSCokjozAiXkbQ='";
29
36
  /**
30
37
  * With the guard, where it is put in the page and not run by the viewer in every frame: frames only from the app's own
31
38
  * origin (a `data:` or `blob:` frame would run before the guard could), and Trusted Types, which hands each
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ghostlytools/sdk",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "type": "module",
5
5
  "description": "Build a wallet source, an identity proof, a mini-app or a client for Ghostly without reading its internals: the adapter contracts, their fakes and contract test suites, the plugin registry, and the mini-app API types (Apps run in Ghostly 1.2 and later).",
6
6
  "license": "MIT",