dsh-builtin-browser 1.0.3 → 1.0.4
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.en.md +89 -0
- package/browser-extension/constants.mjs +20 -0
- package/browser-extension/controller.mjs +293 -0
- package/browser-extension/locales.mjs +15 -0
- package/browser-extension/manifest.json +12 -0
- package/browser-extension/options.html +14 -0
- package/browser-extension/options.mjs +9 -0
- package/browser-extension/permission-guard.mjs +13 -0
- package/browser-extension/worker.mjs +74 -0
- package/package.json +20 -2
- package/scripts/build-client.mjs +305 -0
- package/scripts/build.mjs +69 -0
- package/scripts/bundle-host.mjs +28 -0
- package/scripts/fix-exports.mjs +40 -0
- package/scripts/install-electron.mjs +101 -0
- package/scripts/pack.mjs +99 -0
- package/scripts/publish-manifest.mjs +56 -0
- package/scripts/verify-exports.mjs +58 -0
- package/scripts/verify-preview-sdk.mjs +93 -0
- package/skills/dsh-builtin-browser/SKILL.md +98 -0
- package/skills/dsh-builtin-browser/references/batch.md +68 -0
- package/skills/dsh-builtin-browser/references/deliver.md +31 -0
- package/skills/dsh-builtin-browser/references/locate.md +11 -0
- package/skills/dsh-builtin-browser/references/recover.md +11 -0
- package/skills/dsh-builtin-browser/references/verify.md +13 -0
- package/skills/dsh-debug-host/SKILL.md +56 -0
- package/skills/dsh-debug-self/SKILL.md +50 -0
- package/src/bridge-compatibility.ts +39 -0
- package/src/browser/anti-bot.ts +251 -0
- package/src/browser/deep-dom.ts +40 -0
- package/src/browser/handles.ts +89 -0
- package/src/browser/runtime.ts +578 -0
- package/src/browser/takeover-target.ts +45 -0
- package/src/browser/types.ts +870 -0
- package/src/browser-electron/authenticated-route.ts +109 -0
- package/src/browser-electron/automatic-maintenance.ts +736 -0
- package/src/browser-electron/bridge-auto.ts +159 -0
- package/src/browser-electron/bridge-connection.ts +386 -0
- package/src/browser-electron/bridge-endpoint.ts +129 -0
- package/src/browser-electron/bridge-install.ts +290 -0
- package/src/browser-electron/bridge-maintenance.ts +189 -0
- package/src/browser-electron/bridge-status.ts +386 -0
- package/src/browser-electron/desktop-bridge-host.ts +253 -0
- package/src/browser-electron/desktop-identity.ts +353 -0
- package/src/browser-electron/desktop-shell.ts +100 -0
- package/src/browser-electron/document-readiness.ts +45 -0
- package/src/browser-electron/electron-provision.ts +347 -0
- package/src/browser-electron/entry.ts +1378 -0
- package/src/browser-electron/ephemeral-profile.ts +189 -0
- package/src/browser-electron/extension-browser.ts +230 -0
- package/src/browser-electron/extension-setup.ts +67 -0
- package/src/browser-electron/file-write.ts +31 -0
- package/src/browser-electron/foreground-watch.ts +142 -0
- package/src/browser-electron/guest-cookie-jar.ts +177 -0
- package/src/browser-electron/guest-cookie-store.ts +59 -0
- package/src/browser-electron/guest-css-runtime.ts +76 -0
- package/src/browser-electron/guest-css.ts +68 -0
- package/src/browser-electron/guest-route.ts +716 -0
- package/src/browser-electron/guest-runtime.ts +408 -0
- package/src/browser-electron/guest-storage-runtime.ts +54 -0
- package/src/browser-electron/guest-storage.ts +120 -0
- package/src/browser-electron/history-store.ts +224 -0
- package/src/browser-electron/host-main.ts +1385 -0
- package/src/browser-electron/host-patch-maintenance.ts +121 -0
- package/src/browser-electron/http-body.ts +65 -0
- package/src/browser-electron/json-reply.ts +47 -0
- package/src/browser-electron/maintenance-broker.ts +166 -0
- package/src/browser-electron/maintenance-lifecycle.ts +91 -0
- package/src/browser-electron/maintenance-route.ts +68 -0
- package/src/browser-electron/missing-system-browser.ts +114 -0
- package/src/browser-electron/open-slot.ts +49 -0
- package/src/browser-electron/paths.ts +25 -0
- package/src/browser-electron/preferred-browser.ts +130 -0
- package/src/browser-electron/preview-route.ts +215 -0
- package/src/browser-electron/process-ownership.ts +174 -0
- package/src/browser-electron/process.ts +23 -0
- package/src/browser-electron/profile-checkpoint.ts +187 -0
- package/src/browser-electron/profile-command.ts +47 -0
- package/src/browser-electron/profile-extension.ts +129 -0
- package/src/browser-electron/profile-lease.ts +211 -0
- package/src/browser-electron/profile-residue.ts +49 -0
- package/src/browser-electron/provider-core.ts +1107 -0
- package/src/browser-electron/provider-interaction.ts +1034 -0
- package/src/browser-electron/provider-media.ts +589 -0
- package/src/browser-electron/provider-navigation.ts +323 -0
- package/src/browser-electron/provider-observation.ts +664 -0
- package/src/browser-electron/provider-tabs.ts +372 -0
- package/src/browser-electron/provider.ts +666 -0
- package/src/browser-electron/reference-route.ts +598 -0
- package/src/browser-electron/remote-host.ts +1538 -0
- package/src/browser-electron/rpc-input.ts +13 -0
- package/src/browser-electron/rpc-ops.ts +41 -0
- package/src/browser-electron/runtime-diagnostics.ts +38 -0
- package/src/browser-electron/runtime-route.ts +105 -0
- package/src/browser-electron/runtime-status.ts +309 -0
- package/src/browser-electron/settings-route.ts +64 -0
- package/src/browser-electron/settings-store.ts +593 -0
- package/src/browser-electron/shortcuts-route.ts +112 -0
- package/src/browser-electron/sidebar-only-host.ts +300 -0
- package/src/browser-electron/startpage-shortcuts.ts +213 -0
- package/src/browser-electron/system-browser.ts +1850 -0
- package/src/browser-electron/tauri-host.ts +194 -0
- package/src/browser-electron/timing.ts +16 -0
- package/src/browser-electron/virtual-cursor.ts +162 -0
- package/src/browser-electron/windows-system.ts +24 -0
- package/src/browser-reference/delivery.ts +384 -0
- package/src/browser-reference/entry.ts +631 -0
- package/src/browser-reference/export.ts +339 -0
- package/src/browser-reference/import.ts +303 -0
- package/src/browser-reference/page-script.ts +1537 -0
- package/src/browser-reference/service.ts +1346 -0
- package/src/browser-reference/store.ts +172 -0
- package/src/browser-reference/text-quote.ts +219 -0
- package/src/browser-reference/types.ts +195 -0
- package/src/browser-reference/visible.ts +367 -0
- package/src/browser-reference/web-sources.ts +106 -0
- package/src/client/apply-guard.ts +22 -0
- package/src/client/browser/BrowserController.ts +693 -0
- package/src/client/browser/BrowserFrame.ts +78 -0
- package/src/client/browser/BrowserNavigation.ts +169 -0
- package/src/client/browser/BrowserPage.ts +23 -0
- package/src/client/browser/BrowserPersistence.ts +44 -0
- package/src/client/browser/IframeImpl.ts +182 -0
- package/src/client/browser/child-tabs.ts +14 -0
- package/src/client/browser/frame-history.ts +125 -0
- package/src/client/browser/frame-state.ts +27 -0
- package/src/client/browser/iframe-reference.ts +67 -0
- package/src/client/browser/initialization.ts +262 -0
- package/src/client/browser/omnibox.ts +243 -0
- package/src/client/browser/store.ts +38 -0
- package/src/client/browser/takeover-channel.ts +72 -0
- package/src/client/browser/url.ts +46 -0
- package/src/client/browser/web-reference-source.ts +56 -0
- package/src/client/css-modules.d.ts +4 -0
- package/src/client/definition.tsx +36 -0
- package/src/client/electron/ElectronWebViewImpl.ts +371 -0
- package/src/client/electron/ElectronWebviewPresentation.ts +140 -0
- package/src/client/electron/NativeChildPresentation.ts +53 -0
- package/src/client/electron/NativeViewPage.ts +265 -0
- package/src/client/electron/pages.ts +23 -0
- package/src/client/electron/reference-overlay.ts +53 -0
- package/src/client/electron/workspace.ts +33 -0
- package/src/client/index.ts +333 -0
- package/src/client/locales.ts +860 -0
- package/src/client/pages.ts +28 -0
- package/src/client/reference/client/BrowserRefLink.tsx +142 -0
- package/src/client/reference/client/draft-reference.ts +167 -0
- package/src/client/reference/client/index.ts +7 -0
- package/src/client/reference/client/ordered-submission.ts +108 -0
- package/src/client/reference/client/pending-delivery.ts +216 -0
- package/src/client/reference/client/plugin.ts +208 -0
- package/src/client/reference/client/production/ReferenceCapsules.tsx +345 -0
- package/src/client/reference/client/production/ReferenceDock.module.css +193 -0
- package/src/client/reference/client/production/ReferenceDock.tsx +6 -0
- package/src/client/reference/client/production/ReferenceFeedback.tsx +15 -0
- package/src/client/reference/client/production/ReferenceMath.tsx +44 -0
- package/src/client/reference/client/production/ReferenceText.tsx +36 -0
- package/src/client/reference/client/production/assembly.ts +447 -0
- package/src/client/reference/client/production/asset-store.ts +144 -0
- package/src/client/reference/client/production/availability.ts +72 -0
- package/src/client/reference/client/production/bytes.ts +36 -0
- package/src/client/reference/client/production/composer-attachments.ts +44 -0
- package/src/client/reference/client/production/delivery-client.ts +211 -0
- package/src/client/reference/client/production/draft-adapter.ts +512 -0
- package/src/client/reference/client/production/http-transport.ts +212 -0
- package/src/client/reference/client/production/production-submission.ts +70 -0
- package/src/client/reference/client/production/protocol.ts +234 -0
- package/src/client/reference/client/production/reference-card.ts +240 -0
- package/src/client/reference/client/production/reference-labels.ts +29 -0
- package/src/client/reference/client/production/reply-link-bridge.ts +222 -0
- package/src/client/reference/client/production/reveal.ts +215 -0
- package/src/client/reference/client/production/runtime.ts +1056 -0
- package/src/client/reference/client/production/snapshot-adapter.ts +154 -0
- package/src/client/reference/client/production/source-marker.ts +99 -0
- package/src/client/reference/client/production/source-opener.ts +55 -0
- package/src/client/reference/client/production/styles.ts +104 -0
- package/src/client/reference/client/production/user-text-rule.ts +67 -0
- package/src/client/reference/client/reveal.ts +109 -0
- package/src/client/reference/client/scheme.ts +114 -0
- package/src/client/reference/client/session-schedule.ts +36 -0
- package/src/client/reference/client/types.ts +168 -0
- package/src/client/reference/css-modules.d.ts +4 -0
- package/src/client/reference/index.ts +11 -0
- package/src/client/reference-capture-owner.ts +162 -0
- package/src/client/reference-intents.ts +36 -0
- package/src/client/settings/index.ts +42 -0
- package/src/client/settings/mount-guard.ts +31 -0
- package/src/client/settings/panel.ts +2768 -0
- package/src/client/shortcut-open.ts +30 -0
- package/src/client/startpage/binding.ts +99 -0
- package/src/client/startpage/controller.ts +300 -0
- package/src/client/startpage/model.ts +279 -0
- package/src/client/view/Browser.module.css +751 -0
- package/src/client/view/BrowserBody.tsx +315 -0
- package/src/client/view/BrowserPresentation.ts +9 -0
- package/src/client/view/BrowserTitle.tsx +17 -0
- package/src/client/view/IframePresentation.ts +365 -0
- package/src/client/view/StartPage.tsx +598 -0
- package/src/client/view/TakeoverBanner.tsx +21 -0
- package/src/compat/host-features.ts +92 -0
- package/src/index.ts +110 -0
- package/src/reference-owner.ts +5 -0
- package/src/reference-page.ts +9 -0
- package/src/reference-source.ts +3 -0
- package/src/reference-text.ts +219 -0
- package/src/startpage-settings.ts +103 -0
- package/src/startpage.ts +71 -0
- package/src/takeover-protocol.ts +28 -0
- package/src/tool-browser/block-recovery.ts +85 -0
- package/src/tool-browser/code-tool.ts +555 -0
- package/src/tool-browser/code-worker.ts +725 -0
- package/src/tool-browser/element-fields.ts +167 -0
- package/src/tool-browser/errors.ts +40 -0
- package/src/tool-browser/index.ts +265 -0
- package/src/tool-browser/page-probe.ts +675 -0
- package/src/tool-browser/panel-page.ts +126 -0
- package/src/tool-browser/probe-client.ts +183 -0
- package/src/tool-browser/screenshot-annotations.ts +131 -0
- package/src/tool-browser/screenshot-image.ts +49 -0
- package/src/tool-browser/state.ts +375 -0
- package/src/tool-browser/takeover.ts +199 -0
- package/src/tool-browser/tools-debug.ts +806 -0
- package/src/tool-browser/tools-interaction.ts +956 -0
- package/src/tool-browser/tools-navigation.ts +786 -0
- package/src/tool-browser/tools-observation.ts +474 -0
- package/src/tool-browser/tools-reference.ts +294 -0
- package/src/types/electron-shim.d.ts +144 -0
- package/src/types.ts +85 -0
- package/src/web-guest.ts +35 -0
- package/tools/accept-desktop.mjs +148 -0
- package/dist/client.d.cts +0 -516
- package/dist/client.d.mts +0 -516
- package/dist/index.cjs +0 -92
- package/dist/index.d.mts +0 -2960
- package/dist/index.mjs +0 -24
- package/dsh-builtin-browser-1.0.3-publish.tgz +0 -0
- package/dsh-builtin-browser-1.0.3.tgz +0 -0
- package/dsh-host-patch/sources/main/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/manifest.json +0 -218
- package/dsh-host-patch/sources/rc3/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/series.json +0 -260
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { guestReferenceHandlerScript } from '../browser-reference/page-script.ts'
|
|
2
|
+
import { guestStorageScript, type GuestStorageConfig } from './guest-storage-runtime.ts'
|
|
3
|
+
import { guestCssRuntimeScript } from './guest-css-runtime.ts'
|
|
4
|
+
import { GUEST_ROUTE } from '../../assets/browser-policy.mjs'
|
|
5
|
+
|
|
6
|
+
/** Early, self-contained interception for documents served from the guest proxy. */
|
|
7
|
+
export function guestRuntimeScript(pageUrl: string, storage?: GuestStorageConfig, referrerPolicy = ''): string {
|
|
8
|
+
return `(() => {
|
|
9
|
+
if (window.__dshGuestRuntime) return;
|
|
10
|
+
window.__dshGuestRuntime = true;
|
|
11
|
+
let real = ${JSON.stringify(pageUrl).replace(/</gu, '\\u003c')};
|
|
12
|
+
const route = ${JSON.stringify(GUEST_ROUTE)};
|
|
13
|
+
const attempt = fn => { try { return fn(); } catch {} };
|
|
14
|
+
attempt(() => {
|
|
15
|
+
const cache = { match: async () => undefined, matchAll: async () => [], keys: async () => [],
|
|
16
|
+
add: async () => {}, addAll: async () => {}, put: async () => {}, delete: async () => false };
|
|
17
|
+
const caches = { open: async () => cache, match: async () => undefined, has: async () => false,
|
|
18
|
+
delete: async () => false, keys: async () => [] };
|
|
19
|
+
Object.defineProperty(window, 'caches', { configurable: true, enumerable: true, get: () => caches });
|
|
20
|
+
});
|
|
21
|
+
${storage === undefined ? '' : 'attempt(() => { ' + guestStorageScript(storage) + ' });'}
|
|
22
|
+
const storageContext = ${JSON.stringify(storage?.context ?? '')};
|
|
23
|
+
const contextual = value => {
|
|
24
|
+
if (!storageContext) return value;
|
|
25
|
+
const url = new URL(value, location.href);
|
|
26
|
+
url.searchParams.set('storage-context', storageContext);
|
|
27
|
+
return value.startsWith('/') ? url.pathname + url.search + url.hash : url.href;
|
|
28
|
+
};
|
|
29
|
+
const realURL = value => {
|
|
30
|
+
const raw = String(value);
|
|
31
|
+
const proxyURL = new URL(raw, location.origin + '/');
|
|
32
|
+
if (proxyURL.origin === location.origin && proxyURL.pathname === route) {
|
|
33
|
+
const inner = proxyURL.searchParams.get('u');
|
|
34
|
+
if (inner) return new URL(inner);
|
|
35
|
+
}
|
|
36
|
+
const own = raw === '' || /^[?#]/.test(raw) ? undefined : new URL(raw, location.href);
|
|
37
|
+
if (own && own.origin === location.origin && own.pathname === route && !own.searchParams.has('u')) {
|
|
38
|
+
const url = new URL(real);
|
|
39
|
+
url.search = own.search;
|
|
40
|
+
url.hash = own.hash;
|
|
41
|
+
return url;
|
|
42
|
+
}
|
|
43
|
+
let url = new URL(raw, real);
|
|
44
|
+
if (url.origin === location.origin) {
|
|
45
|
+
url = new URL(url.pathname + url.search + url.hash, new URL(real).origin);
|
|
46
|
+
}
|
|
47
|
+
return url;
|
|
48
|
+
};
|
|
49
|
+
const toProxy = value => {
|
|
50
|
+
try {
|
|
51
|
+
const raw = String(value);
|
|
52
|
+
const hostURL = new URL(raw, location.origin + '/');
|
|
53
|
+
if (hostURL.origin === location.origin && hostURL.pathname === route) return contextual(raw);
|
|
54
|
+
const url = realURL(raw);
|
|
55
|
+
return /^https?:$/.test(url.protocol) ? contextual(route + '?u=' + encodeURIComponent(url.href)) : raw;
|
|
56
|
+
} catch { return value; }
|
|
57
|
+
};
|
|
58
|
+
window.__dshGuestToProxy = toProxy;
|
|
59
|
+
const policies = ['no-referrer', 'no-referrer-when-downgrade', 'origin', 'origin-when-cross-origin', 'same-origin', 'strict-origin', 'strict-origin-when-cross-origin', 'unsafe-url'];
|
|
60
|
+
const referrerPolicy = override => {
|
|
61
|
+
if (policies.includes(override)) return override;
|
|
62
|
+
const metas = attempt(() => [...document.querySelectorAll('meta[name="referrer" i]')]) || [];
|
|
63
|
+
const last = value => String(value || '').split(',').map(item => item.trim().toLowerCase()).reverse().find(item => policies.includes(item));
|
|
64
|
+
for (const meta of metas.reverse()) {
|
|
65
|
+
const token = last(meta.content);
|
|
66
|
+
if (token) return token;
|
|
67
|
+
}
|
|
68
|
+
return last(${JSON.stringify(referrerPolicy).replace(/</gu, '\\u003c')}) || '';
|
|
69
|
+
};
|
|
70
|
+
const requestInit = ['body', 'cache', 'credentials', 'duplex', 'headers', 'integrity', 'keepalive', 'method', 'mode', 'priority', 'redirect', 'referrer', 'referrerPolicy', 'signal', 'window'];
|
|
71
|
+
const refererHeaders = (set, override) => {
|
|
72
|
+
set('x-dsh-guest-referer', real);
|
|
73
|
+
const policy = referrerPolicy(override);
|
|
74
|
+
if (policy) set('x-dsh-guest-referrer-policy', policy);
|
|
75
|
+
};
|
|
76
|
+
const nativeFetch = window.fetch && window.fetch.bind(window);
|
|
77
|
+
window.__dshGuestFetch = nativeFetch;
|
|
78
|
+
attempt(() => {
|
|
79
|
+
window.fetch = (input, init) => {
|
|
80
|
+
try {
|
|
81
|
+
const isRequest = input instanceof Request;
|
|
82
|
+
const url = toProxy(isRequest ? input.url : input);
|
|
83
|
+
if (!/^https?:$/.test(new URL(url, location.href).protocol)) return nativeFetch(input, init);
|
|
84
|
+
const headers = new Headers(init && init.headers || (isRequest ? input.headers : undefined));
|
|
85
|
+
headers.set('x-dsh-guest-headers', [...headers.keys()].filter(name => !name.startsWith('x-dsh-')).join(','));
|
|
86
|
+
headers.set('x-dsh-guest-sub', '1');
|
|
87
|
+
const source = init && requestInit.some(name => init[name] !== undefined) ? init : isRequest ? input : {};
|
|
88
|
+
refererHeaders((name, value) => headers.set(name, value), source.referrer === '' ? 'no-referrer' : source.referrerPolicy || '');
|
|
89
|
+
const options = Object.assign({}, init, { headers, mode: 'cors', credentials: 'omit' });
|
|
90
|
+
return nativeFetch(isRequest ? new Request(new URL(url, location.href), input) : url, options);
|
|
91
|
+
} catch (error) { return Promise.reject(error); }
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
attempt(() => {
|
|
95
|
+
const open = XMLHttpRequest.prototype.open;
|
|
96
|
+
const send = XMLHttpRequest.prototype.send;
|
|
97
|
+
const header = XMLHttpRequest.prototype.setRequestHeader;
|
|
98
|
+
const names = new WeakMap();
|
|
99
|
+
XMLHttpRequest.prototype.open = function(method, url, ...args) {
|
|
100
|
+
const result = open.call(this, method, toProxy(url), ...args);
|
|
101
|
+
names.set(this, new Set());
|
|
102
|
+
attempt(() => this.setRequestHeader('x-dsh-guest-sub', '1'));
|
|
103
|
+
attempt(() => refererHeaders((name, value) => this.setRequestHeader(name, value)));
|
|
104
|
+
return result;
|
|
105
|
+
};
|
|
106
|
+
XMLHttpRequest.prototype.setRequestHeader = function(name, value) {
|
|
107
|
+
const result = header.call(this, name, value);
|
|
108
|
+
const key = String(name).toLowerCase();
|
|
109
|
+
if (!key.startsWith('x-dsh-')) names.get(this)?.add(key);
|
|
110
|
+
return result;
|
|
111
|
+
};
|
|
112
|
+
XMLHttpRequest.prototype.send = function(...args) {
|
|
113
|
+
this.withCredentials = false;
|
|
114
|
+
header.call(this, 'x-dsh-guest-headers', [...(names.get(this) || [])].join(','));
|
|
115
|
+
return send.apply(this, args);
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
attempt(() => {
|
|
119
|
+
navigator.sendBeacon = (url, data) => {
|
|
120
|
+
try {
|
|
121
|
+
const headers = { 'x-dsh-guest-sub': '1' };
|
|
122
|
+
refererHeaders((name, value) => { headers[name] = value; });
|
|
123
|
+
nativeFetch(toProxy(url), { method: 'POST', body: data, keepalive: true, headers }).catch(() => {});
|
|
124
|
+
return true;
|
|
125
|
+
} catch { return false; }
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
let childRequestId = 0;
|
|
129
|
+
const childError = code => {
|
|
130
|
+
const error = new Error(code + ': this sidebar supports URL-only tabs, not native child windows or opener relationships');
|
|
131
|
+
error.name = 'NotSupportedError'; error.code = code;
|
|
132
|
+
return error;
|
|
133
|
+
};
|
|
134
|
+
const childRequest = (mode, url) => {
|
|
135
|
+
if (loadId === undefined) throw childError('sidebar-tab-owner-not-ready');
|
|
136
|
+
parent.postMessage({ type: 'dsh.browser.guest.child.request', loadId, requestId: ++childRequestId, mode, url }, '*');
|
|
137
|
+
};
|
|
138
|
+
const nativeChild = url => {
|
|
139
|
+
attempt(() => childRequest('native-opener', url));
|
|
140
|
+
throw childError('BROWSER_NATIVE_CHILD_UNAVAILABLE');
|
|
141
|
+
};
|
|
142
|
+
const detached = features => String(features || '').split(',').some(feature => {
|
|
143
|
+
const [key, value] = feature.trim().toLowerCase().split(/\\s*=\\s*/);
|
|
144
|
+
return ['noopener', 'noreferrer'].includes(key) && !['0', 'no', 'false'].includes(value);
|
|
145
|
+
});
|
|
146
|
+
attempt(() => { window.open = (url, target = '_blank', features = '') => {
|
|
147
|
+
const name = String(target || '_blank').toLowerCase();
|
|
148
|
+
if (name !== '_self' && (name !== '_blank' || !detached(features))) return nativeChild(String(url || ''));
|
|
149
|
+
const next = realURL(url == null || String(url) === '' ? 'about:blank' : url);
|
|
150
|
+
if (!/^https?:$/.test(next.protocol)) throw childError('invalid-child-url');
|
|
151
|
+
if (name === '_self') { location.assign(toProxy(next.href)); return window; }
|
|
152
|
+
childRequest('url-only', next.href);
|
|
153
|
+
return null;
|
|
154
|
+
}; });
|
|
155
|
+
for (const name of ['pushState', 'replaceState']) attempt(() => {
|
|
156
|
+
const original = history[name];
|
|
157
|
+
const reason = name === 'pushState' ? 'push' : 'replace';
|
|
158
|
+
history[name] = function(state, title, url) {
|
|
159
|
+
if (url == null) {
|
|
160
|
+
const result = original.call(this, state, title, url);
|
|
161
|
+
if (name === 'pushState') report(reason);
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
const next = attempt(() => realURL(url));
|
|
165
|
+
const result = original.call(this, state, title, toProxy(url));
|
|
166
|
+
if (next && /^https?:$/.test(next.protocol)) real = next.href;
|
|
167
|
+
report(reason);
|
|
168
|
+
return result;
|
|
169
|
+
};
|
|
170
|
+
});
|
|
171
|
+
let loadId;
|
|
172
|
+
const documentId = String(Date.now()) + ':' + String(Math.random());
|
|
173
|
+
let referenceRequestId = 0;
|
|
174
|
+
const reference = ${guestReferenceHandlerScript()};
|
|
175
|
+
const navType = () => {
|
|
176
|
+
const entry = attempt(() => performance.getEntriesByType && performance.getEntriesByType('navigation')[0]);
|
|
177
|
+
return entry && typeof entry.type === 'string' ? entry.type : undefined;
|
|
178
|
+
};
|
|
179
|
+
const report = (reason) => attempt(() => {
|
|
180
|
+
const navigationType = navType();
|
|
181
|
+
parent.postMessage({
|
|
182
|
+
type: 'dsh.browser.guest.navigation',
|
|
183
|
+
url: real,
|
|
184
|
+
depth: history.length,
|
|
185
|
+
loadId,
|
|
186
|
+
documentId,
|
|
187
|
+
ready: document.readyState !== 'loading',
|
|
188
|
+
...(typeof reason === 'string' && reason ? { reason } : {}),
|
|
189
|
+
...(navigationType ? { navigationType } : {}),
|
|
190
|
+
}, '*');
|
|
191
|
+
});
|
|
192
|
+
attempt(() => window.addEventListener('message', event => {
|
|
193
|
+
if (event.source !== parent || !event.data) return;
|
|
194
|
+
if (event.data.type === 'dsh.browser.guest.navigation.request') {
|
|
195
|
+
if (!Number.isSafeInteger(event.data.loadId) || event.data.loadId <= 0) return;
|
|
196
|
+
loadId = event.data.loadId;
|
|
197
|
+
report('sync');
|
|
198
|
+
} else if (event.data.type === 'dsh.browser.guest.reference.request') {
|
|
199
|
+
const data = event.data;
|
|
200
|
+
if (event.origin !== location.origin || data.loadId !== loadId || loadId === undefined || data.documentId !== documentId
|
|
201
|
+
|| !Number.isSafeInteger(data.requestId) || data.requestId <= referenceRequestId) return;
|
|
202
|
+
referenceRequestId = data.requestId;
|
|
203
|
+
const ack = { type: 'dsh.browser.guest.reference.ack', requestId: data.requestId, loadId, documentId, url: real };
|
|
204
|
+
try {
|
|
205
|
+
const current = new URL(real), source = new URL(data.url);
|
|
206
|
+
current.hash = ''; source.hash = '';
|
|
207
|
+
if (current.href !== source.href || document.readyState === 'loading') throw new Error('source document is not ready');
|
|
208
|
+
if (!data.spec || typeof data.spec !== 'object' || Array.isArray(data.spec) || !['text', 'highlight', 'element', 'summary'].includes(data.operation)) throw new Error('invalid reference command');
|
|
209
|
+
if (data.operation !== 'element' && (!Number.isSafeInteger(data.spec.maxChars) || data.spec.maxChars <= 0
|
|
210
|
+
|| data.spec.maxChars > 2000000 || typeof data.spec.legacy !== 'boolean')) throw new Error('invalid text specification');
|
|
211
|
+
if (data.operation === 'element' && (!['image', 'table'].includes(data.spec.kind)
|
|
212
|
+
|| !data.spec.anchor || typeof data.spec.anchor !== 'object' || Array.isArray(data.spec.anchor))) throw new Error('invalid element specification');
|
|
213
|
+
if (data.operation === 'highlight' && (!Number.isSafeInteger(data.spec.start) || !Number.isSafeInteger(data.spec.end)
|
|
214
|
+
|| data.spec.start < 0 || data.spec.end <= data.spec.start)) throw new Error('invalid text positions');
|
|
215
|
+
const value = data.operation === 'summary'
|
|
216
|
+
? { url: real, title: String(document.title || '').slice(0, 1000), snapshotHint: reference('text', { maxChars: 1500, legacy: false }).text, fresh: true }
|
|
217
|
+
: reference(data.operation, data.spec);
|
|
218
|
+
parent.postMessage({ ...ack, ok: true, value }, event.origin);
|
|
219
|
+
} catch (error) { parent.postMessage({ ...ack, ok: false, reason: String(error).slice(0, 200) }, event.origin); }
|
|
220
|
+
} else if (event.data.type === 'dsh.browser.guest.history') {
|
|
221
|
+
if (event.data.delta === -1) history.back();
|
|
222
|
+
else if (event.data.delta === 1) history.forward();
|
|
223
|
+
}
|
|
224
|
+
}));
|
|
225
|
+
attempt(() => window.addEventListener('DOMContentLoaded', () => report('ready')));
|
|
226
|
+
attempt(() => window.addEventListener('pageshow', event => { if (event && event.persisted) report('pageshow'); }));
|
|
227
|
+
attempt(() => window.addEventListener('popstate', () => {
|
|
228
|
+
const next = attempt(() => realURL(location.href));
|
|
229
|
+
if (next) real = next.href;
|
|
230
|
+
report('pop');
|
|
231
|
+
}));
|
|
232
|
+
attempt(() => {
|
|
233
|
+
const proxyResource = value => {
|
|
234
|
+
if (/^(#|data:|blob:|about:|javascript:)/i.test(value.trim())) return value;
|
|
235
|
+
return toProxy(value);
|
|
236
|
+
};
|
|
237
|
+
${guestCssRuntimeScript()}
|
|
238
|
+
const proxySrcset = value => {
|
|
239
|
+
let result = '', position = 0;
|
|
240
|
+
while (position < value.length) {
|
|
241
|
+
const start = position;
|
|
242
|
+
while (position < value.length && /[\\s,]/.test(value[position])) position++;
|
|
243
|
+
result += value.slice(start, position);
|
|
244
|
+
if (position === value.length) break;
|
|
245
|
+
const urlStart = position;
|
|
246
|
+
while (position < value.length && !/\\s/.test(value[position])) position++;
|
|
247
|
+
let urlEnd = position;
|
|
248
|
+
while (urlEnd > urlStart && value[urlEnd - 1] === ',') urlEnd--;
|
|
249
|
+
result += proxyResource(value.slice(urlStart, urlEnd)) + value.slice(urlEnd, position);
|
|
250
|
+
if (urlEnd < position) continue;
|
|
251
|
+
const descriptors = position;
|
|
252
|
+
while (position < value.length && value[position] !== ',') position++;
|
|
253
|
+
result += value.slice(descriptors, position);
|
|
254
|
+
}
|
|
255
|
+
return result;
|
|
256
|
+
};
|
|
257
|
+
const resourceAttribute = (name, value) => name.toLowerCase() === 'style' ? proxyCss(value)
|
|
258
|
+
: name.toLowerCase() === 'srcset' ? proxySrcset(String(value)) : proxyResource(String(value));
|
|
259
|
+
const set = Element.prototype.setAttribute;
|
|
260
|
+
Element.prototype.setAttribute = function(name, value) {
|
|
261
|
+
if (this.tagName === 'LINK' && /^integrity$/i.test(name)) {
|
|
262
|
+
this.removeAttribute('integrity');
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
return set.call(this, name, /^(src|href|action|poster|srcset|style)$/i.test(name) ? resourceAttribute(name, value) : value);
|
|
266
|
+
};
|
|
267
|
+
for (const [name, prop] of [['HTMLImageElement', 'src'], ['HTMLScriptElement', 'src'],
|
|
268
|
+
['HTMLIFrameElement', 'src'], ['HTMLLinkElement', 'href'], ['HTMLSourceElement', 'src'],
|
|
269
|
+
['HTMLImageElement', 'srcset'], ['HTMLSourceElement', 'srcset'], ['HTMLVideoElement', 'poster']]) attempt(() => {
|
|
270
|
+
const proto = window[name].prototype;
|
|
271
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, prop);
|
|
272
|
+
if (!descriptor || !descriptor.set) return;
|
|
273
|
+
Object.defineProperty(proto, prop, Object.assign({}, descriptor, {
|
|
274
|
+
set(value) { descriptor.set.call(this, resourceAttribute(prop, value)); }
|
|
275
|
+
}));
|
|
276
|
+
});
|
|
277
|
+
const rewriteResource = node => {
|
|
278
|
+
if (!node || node.nodeType !== 1) return;
|
|
279
|
+
if (node.tagName === 'LINK' && node.hasAttribute('integrity')) node.removeAttribute('integrity');
|
|
280
|
+
rewriteStyleText(node);
|
|
281
|
+
const attributes = /^(A|FORM)$/.test(node.tagName) ? ['style'] : ['src', 'href', 'srcset', 'poster', 'style'];
|
|
282
|
+
for (const attribute of attributes) attempt(() => {
|
|
283
|
+
const value = node.getAttribute(attribute);
|
|
284
|
+
if (!value || !value.trim()) return;
|
|
285
|
+
const next = resourceAttribute(attribute, value);
|
|
286
|
+
if (next !== value) node.setAttribute(attribute, next);
|
|
287
|
+
});
|
|
288
|
+
};
|
|
289
|
+
const rewriteTree = node => {
|
|
290
|
+
rewriteResource(node);
|
|
291
|
+
if (node && node.querySelectorAll) {
|
|
292
|
+
for (const descendant of node.querySelectorAll('[src],[href],[srcset],[poster],[style],[integrity],style')) rewriteResource(descendant);
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
for (const name of ['Node', 'Element', 'Document', 'DocumentFragment']) {
|
|
296
|
+
for (const method of ['appendChild', 'insertBefore', 'replaceChild', 'append', 'prepend', 'before', 'after', 'replaceWith', 'replaceChildren', 'insertAdjacentElement']) attempt(() => {
|
|
297
|
+
const proto = window[name].prototype;
|
|
298
|
+
if (!Object.prototype.hasOwnProperty.call(proto, method)) return;
|
|
299
|
+
const original = proto[method];
|
|
300
|
+
proto[method] = function(...args) {
|
|
301
|
+
if (isStyle(this)) args = args.map(value => {
|
|
302
|
+
if (typeof value === 'string') return proxyCss(value);
|
|
303
|
+
if (value && value.nodeType === 3) value.textContent = proxyCss(value.textContent);
|
|
304
|
+
return value;
|
|
305
|
+
});
|
|
306
|
+
for (const node of args) if (node && typeof node === 'object') rewriteTree(node);
|
|
307
|
+
return original.apply(this, args);
|
|
308
|
+
};
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
const html = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML');
|
|
312
|
+
let inert;
|
|
313
|
+
const rewriteMarkup = (value, context) => {
|
|
314
|
+
if (!inert) inert = document.implementation.createHTMLDocument('');
|
|
315
|
+
const container = context && context.nodeType === 1
|
|
316
|
+
? inert.createElementNS(context.namespaceURI, context.localName) : inert.createElement('div');
|
|
317
|
+
html.set.call(container, value);
|
|
318
|
+
rewriteTree(container.content || container);
|
|
319
|
+
return html.get.call(container);
|
|
320
|
+
};
|
|
321
|
+
for (const prop of ['innerHTML', 'outerHTML']) attempt(() => {
|
|
322
|
+
const descriptor = Object.getOwnPropertyDescriptor(Element.prototype, prop);
|
|
323
|
+
Object.defineProperty(Element.prototype, prop, Object.assign({}, descriptor, {
|
|
324
|
+
set(value) { descriptor.set.call(this, rewriteMarkup(value, prop === 'innerHTML' ? this : this.parentElement)); }
|
|
325
|
+
}));
|
|
326
|
+
});
|
|
327
|
+
attempt(() => {
|
|
328
|
+
const original = Element.prototype.insertAdjacentHTML;
|
|
329
|
+
Element.prototype.insertAdjacentHTML = function(position, value) {
|
|
330
|
+
const outside = /^(beforebegin|afterend)$/i.test(position);
|
|
331
|
+
return original.call(this, position, rewriteMarkup(value, outside ? this.parentElement : this));
|
|
332
|
+
};
|
|
333
|
+
});
|
|
334
|
+
attempt(() => {
|
|
335
|
+
const original = window.Range.prototype.createContextualFragment;
|
|
336
|
+
window.Range.prototype.createContextualFragment = function(value) {
|
|
337
|
+
const context = this.startContainer.nodeType === 1 ? this.startContainer : this.startContainer.parentElement;
|
|
338
|
+
return original.call(this, rewriteMarkup(value, context));
|
|
339
|
+
};
|
|
340
|
+
});
|
|
341
|
+
const observer = new MutationObserver(records => {
|
|
342
|
+
for (const record of records) attempt(() => {
|
|
343
|
+
if (record.type === 'attributes') rewriteResource(record.target);
|
|
344
|
+
else if (record.type === 'characterData') rewriteStyleText(record.target.parentNode);
|
|
345
|
+
else for (const node of record.addedNodes) rewriteTree(node);
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
observer.observe(document, { childList: true, subtree: true, attributes: true, characterData: true,
|
|
349
|
+
attributeFilter: ['src', 'href', 'srcset', 'poster', 'action', 'style', 'integrity'] });
|
|
350
|
+
rewriteTree(document.documentElement);
|
|
351
|
+
});
|
|
352
|
+
attempt(() => document.addEventListener('click', event => {
|
|
353
|
+
attempt(() => {
|
|
354
|
+
let node = event.target;
|
|
355
|
+
while (node && node.tagName !== 'A') node = node.parentNode;
|
|
356
|
+
if (!node || !node.hasAttribute('href') || node.hasAttribute('download')) return;
|
|
357
|
+
const href = node.getAttribute('href');
|
|
358
|
+
const url = realURL(href);
|
|
359
|
+
if (!/^https?:$/.test(url.protocol)) return;
|
|
360
|
+
event.preventDefault();
|
|
361
|
+
const target = (node.getAttribute('target') || '_self').toLowerCase();
|
|
362
|
+
if (target !== '_self') {
|
|
363
|
+
const opener = /(?:^|\\s)opener(?:\\s|$)/i.test(node.getAttribute('rel') || '');
|
|
364
|
+
try { if (target !== '_blank' || opener) nativeChild(url.href); else childRequest('url-only', url.href); }
|
|
365
|
+
catch (error) { console.error('[dsh-browser]', error); }
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
location.assign(toProxy(href));
|
|
369
|
+
});
|
|
370
|
+
}, true));
|
|
371
|
+
const prepareForm = (form, submitter) => {
|
|
372
|
+
const action = submitter && submitter.getAttribute('formaction') || form.getAttribute('action') || real;
|
|
373
|
+
const method = (submitter && submitter.getAttribute('formmethod') || form.getAttribute('method') || 'GET').toUpperCase();
|
|
374
|
+
const url = realURL(action);
|
|
375
|
+
if (!/^https?:$/.test(url.protocol)) return false;
|
|
376
|
+
const target = (submitter && submitter.getAttribute('formtarget') || form.getAttribute('target') || '_self').toLowerCase();
|
|
377
|
+
if (target !== '_self' && (target !== '_blank' || method !== 'GET'
|
|
378
|
+
|| /(?:^|\\s)opener(?:\\s|$)/i.test(form.getAttribute('rel') || ''))) {
|
|
379
|
+
try { nativeChild(url.href); } catch (error) { console.error('[dsh-browser]', error); }
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
if (method === 'GET') {
|
|
383
|
+
const params = new URLSearchParams();
|
|
384
|
+
const data = submitter ? new FormData(form, submitter) : new FormData(form);
|
|
385
|
+
for (const [key, value] of data) params.append(key, typeof value === 'string' ? value : value.name);
|
|
386
|
+
url.search = params.toString();
|
|
387
|
+
if (target === '_blank') {
|
|
388
|
+
try { childRequest('url-only', url.href); } catch (error) { console.error('[dsh-browser]', error); }
|
|
389
|
+
} else location.assign(toProxy(url.href));
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
form.setAttribute('action', toProxy(url.href));
|
|
393
|
+
form.setAttribute('target', '_self');
|
|
394
|
+
if (submitter && submitter.hasAttribute('formaction')) submitter.setAttribute('formaction', toProxy(url.href));
|
|
395
|
+
if (submitter && submitter.hasAttribute('formtarget')) submitter.setAttribute('formtarget', '_self');
|
|
396
|
+
return false;
|
|
397
|
+
};
|
|
398
|
+
attempt(() => document.addEventListener('submit', event => {
|
|
399
|
+
attempt(() => { if (event.target && event.target.tagName === 'FORM' && prepareForm(event.target, event.submitter)) event.preventDefault(); });
|
|
400
|
+
}, true));
|
|
401
|
+
attempt(() => {
|
|
402
|
+
const submit = HTMLFormElement.prototype.submit;
|
|
403
|
+
HTMLFormElement.prototype.submit = function() {
|
|
404
|
+
if (!attempt(() => prepareForm(this))) return submit.call(this);
|
|
405
|
+
};
|
|
406
|
+
});
|
|
407
|
+
})();`;
|
|
408
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface GuestStorageConfig { readonly token: string; readonly context: string; readonly cookies?: string }
|
|
2
|
+
|
|
3
|
+
/** Synchronous Web Storage remains isolated from the application's real origin. */
|
|
4
|
+
export function guestStorageScript(config: GuestStorageConfig): string {
|
|
5
|
+
return `(() => {
|
|
6
|
+
const config = ${JSON.stringify(config).replace(/</gu, '\\u003c')};
|
|
7
|
+
const XHR = XMLHttpRequest, open = XHR.prototype.open, send = XHR.prototype.send, header = XHR.prototype.setRequestHeader;
|
|
8
|
+
const rpc = (area, op, key, value) => {
|
|
9
|
+
const endpoint = location.pathname + '?u=' + encodeURIComponent(real) + '&storage-token=' + encodeURIComponent(config.token);
|
|
10
|
+
const xhr = new XHR();
|
|
11
|
+
open.call(xhr, 'POST', endpoint, false);
|
|
12
|
+
header.call(xhr, 'content-type', 'text/plain;charset=UTF-8');
|
|
13
|
+
send.call(xhr, JSON.stringify({ area, op, key, value }));
|
|
14
|
+
let response;
|
|
15
|
+
try { response = JSON.parse(xhr.responseText); } catch { throw new DOMException('Guest storage is unavailable', 'UnknownError'); }
|
|
16
|
+
if (xhr.status !== 200 || !response.ok) throw new DOMException('Guest storage request failed', response.name || 'UnknownError');
|
|
17
|
+
return response.value;
|
|
18
|
+
};
|
|
19
|
+
const cookieSnapshots = new Map([[real, config.cookies || '']]);
|
|
20
|
+
Object.defineProperty(Document.prototype, 'cookie', { configurable: true, enumerable: true,
|
|
21
|
+
get: function cookie() {
|
|
22
|
+
if (this !== document) throw new TypeError('Illegal invocation');
|
|
23
|
+
try { const value = rpc('cookies', 'get'); cookieSnapshots.set(real, value); return value; }
|
|
24
|
+
catch (error) { if (error.name === 'NetworkError') return cookieSnapshots.get(real) || ''; throw error; }
|
|
25
|
+
},
|
|
26
|
+
set: function cookie(value) {
|
|
27
|
+
if (this !== document) throw new TypeError('Illegal invocation');
|
|
28
|
+
cookieSnapshots.set(real, rpc('cookies', 'set', undefined, String(value)));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
for (const area of ['local', 'session']) {
|
|
32
|
+
const storage = Object.create(Storage.prototype);
|
|
33
|
+
const entries = () => rpc(area, 'entries');
|
|
34
|
+
Object.defineProperties(storage, {
|
|
35
|
+
getItem: { configurable: true, value: function(key) { if (!arguments.length) throw new TypeError('A key is required'); return rpc(area, 'get', String(key)); } },
|
|
36
|
+
setItem: { configurable: true, value: function(key, value) { if (arguments.length < 2) throw new TypeError('A key and value are required'); rpc(area, 'set', String(key), String(value)); } },
|
|
37
|
+
removeItem: { configurable: true, value: function(key) { if (!arguments.length) throw new TypeError('A key is required'); rpc(area, 'remove', String(key)); } },
|
|
38
|
+
clear: { configurable: true, value: () => { rpc(area, 'clear'); } },
|
|
39
|
+
key: { configurable: true, value: function(index) { if (!arguments.length) throw new TypeError('An index is required'); return entries()[Number(index) >>> 0]?.[0] ?? null; } },
|
|
40
|
+
length: { configurable: true, get: () => entries().length }
|
|
41
|
+
});
|
|
42
|
+
const proxy = new Proxy(storage, {
|
|
43
|
+
get: (target, key) => typeof key !== 'string' || key in target ? Reflect.get(target, key) : rpc(area, 'get', key) ?? undefined,
|
|
44
|
+
set: (_, key, value) => { if (typeof key !== 'string') return false; rpc(area, 'set', key, String(value)); return true; },
|
|
45
|
+
deleteProperty: (_, key) => { if (typeof key !== 'string') return false; rpc(area, 'remove', key); return true; },
|
|
46
|
+
has: (target, key) => key in target || (typeof key === 'string' && rpc(area, 'get', key) !== null),
|
|
47
|
+
ownKeys: target => [...Reflect.ownKeys(target), ...entries().map(pair => pair[0]).filter(key => !Object.hasOwn(target, key))],
|
|
48
|
+
getOwnPropertyDescriptor: (target, key) => Object.getOwnPropertyDescriptor(target, key)
|
|
49
|
+
|| (typeof key === 'string' && rpc(area, 'get', key) !== null ? { configurable: true, enumerable: true, writable: true, value: rpc(area, 'get', key) } : undefined)
|
|
50
|
+
});
|
|
51
|
+
Object.defineProperty(window, area + 'Storage', { configurable: true, get: () => proxy });
|
|
52
|
+
}
|
|
53
|
+
})();`
|
|
54
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { BROWSER_PROFILE_DIR } from '../../assets/browser-policy.mjs'
|
|
2
|
+
import { createHmac, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto'
|
|
3
|
+
import { closeSync, constants, fchmodSync, fstatSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { dshHome } from './paths.ts'
|
|
6
|
+
|
|
7
|
+
type Entries = Map<string, string>
|
|
8
|
+
type Partitions = Map<string, Entries>
|
|
9
|
+
const MAX_STORE_BYTES = 64 * 1024 * 1024
|
|
10
|
+
const serialized = (partitions: Partitions): string => JSON.stringify([...partitions].map(([origin, entries]) => [origin, [...entries]]))
|
|
11
|
+
|
|
12
|
+
function fail(name: string): never { throw Object.assign(new Error(name), { name }) }
|
|
13
|
+
|
|
14
|
+
/** Storage capabilities are issued only in browser navigation responses, never fetchable HTML. */
|
|
15
|
+
export class GuestStorage {
|
|
16
|
+
private readonly secret = randomBytes(32)
|
|
17
|
+
private local: Partitions = new Map()
|
|
18
|
+
private session: Partitions = new Map()
|
|
19
|
+
private readonly directory?: string
|
|
20
|
+
private readonly quota = 5 * 1024 * 1024
|
|
21
|
+
constructor(directory?: string, quota = 5 * 1024 * 1024) {
|
|
22
|
+
this.directory = directory
|
|
23
|
+
this.quota = quota
|
|
24
|
+
if (directory === undefined) return
|
|
25
|
+
let fd: number | undefined
|
|
26
|
+
try {
|
|
27
|
+
fd = openSync(join(directory, 'storage.json'), constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0))
|
|
28
|
+
if (!fstatSync(fd).isFile() || fstatSync(fd).size > MAX_STORE_BYTES) return
|
|
29
|
+
fchmodSync(fd, 0o600)
|
|
30
|
+
const saved: unknown = JSON.parse(readFileSync(fd, 'utf8'))
|
|
31
|
+
if (!saved || typeof saved !== 'object' || !('version' in saved) || saved.version !== 1
|
|
32
|
+
|| !('origins' in saved) || !Array.isArray(saved.origins)) return
|
|
33
|
+
for (const item of saved.origins) {
|
|
34
|
+
if (!Array.isArray(item) || item.length !== 2 || typeof item[0] !== 'string' || !Array.isArray(item[1])) continue
|
|
35
|
+
try { if (new URL(item[0]).origin !== item[0] || !/^https?:/u.test(item[0])) continue } catch { continue }
|
|
36
|
+
const entries: Entries = new Map()
|
|
37
|
+
for (const pair of item[1]) {
|
|
38
|
+
if (Array.isArray(pair) && pair.length === 2 && pair.every(value => typeof value === 'string')) entries.set(pair[0], pair[1])
|
|
39
|
+
}
|
|
40
|
+
if (this.size(entries) <= quota) this.local.set(item[0], entries)
|
|
41
|
+
}
|
|
42
|
+
} catch {} finally { if (fd !== undefined) closeSync(fd) }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
issue(origin: string, context: string): string {
|
|
46
|
+
const payload = Buffer.from(JSON.stringify({ origin, context })).toString('base64url')
|
|
47
|
+
return payload + '.' + createHmac('sha256', this.secret).update(payload).digest('base64url')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
authorize(token: string, origin: string): { readonly context: string } {
|
|
51
|
+
if (token.length > 8192) fail('SecurityError')
|
|
52
|
+
const [payload, signature, extra] = token.split('.')
|
|
53
|
+
const expected = createHmac('sha256', this.secret).update(payload ?? '').digest()
|
|
54
|
+
const received = Buffer.from(signature ?? '', 'base64url')
|
|
55
|
+
if (extra !== undefined || received.length !== expected.length || !timingSafeEqual(received, expected)) fail('SecurityError')
|
|
56
|
+
let scope: { origin?: unknown; context?: unknown }
|
|
57
|
+
try { scope = JSON.parse(Buffer.from(payload!, 'base64url').toString('utf8')) } catch { fail('SecurityError') }
|
|
58
|
+
if (scope?.origin !== origin || typeof scope.context !== 'string') fail('SecurityError')
|
|
59
|
+
return { context: scope.context }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
execute(token: string, origin: string, input: unknown): unknown {
|
|
63
|
+
const scope = this.authorize(token, origin)
|
|
64
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) fail('TypeError')
|
|
65
|
+
const { area, op, key, value } = input as Record<string, unknown>
|
|
66
|
+
if (area !== 'local' && area !== 'session') fail('TypeError')
|
|
67
|
+
if (!['entries', 'get', 'set', 'remove', 'clear'].includes(String(op))) fail('TypeError')
|
|
68
|
+
if (op !== 'entries' && op !== 'clear' && typeof key !== 'string') fail('TypeError')
|
|
69
|
+
if (op === 'set' && typeof value !== 'string') fail('TypeError')
|
|
70
|
+
const partitions = area === 'local' ? this.local : this.session
|
|
71
|
+
const partition = area === 'local' ? origin : JSON.stringify([scope.context, origin])
|
|
72
|
+
const current = partitions.get(partition) ?? new Map<string, string>()
|
|
73
|
+
if (op === 'entries') return [...current]
|
|
74
|
+
if (op === 'get') return current.get(key as string) ?? null
|
|
75
|
+
const next = new Map(current)
|
|
76
|
+
if (op === 'clear') next.clear()
|
|
77
|
+
else if (op === 'remove') next.delete(key as string)
|
|
78
|
+
else next.set(key as string, value as string)
|
|
79
|
+
if (this.size(next) > this.quota) fail('QuotaExceededError')
|
|
80
|
+
const updated = new Map(partitions)
|
|
81
|
+
if (next.size === 0) updated.delete(partition)
|
|
82
|
+
else updated.set(partition, next)
|
|
83
|
+
if (Buffer.byteLength(serialized(updated), 'utf8') > MAX_STORE_BYTES) fail('QuotaExceededError')
|
|
84
|
+
if (area === 'local') {
|
|
85
|
+
this.persist(updated)
|
|
86
|
+
this.local = updated
|
|
87
|
+
} else this.session = updated
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private size(entries: Entries): number {
|
|
92
|
+
return [...entries].reduce((size, [key, value]) => size + 2 * (key.length + value.length), 0)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private persist(origins: Partitions): void {
|
|
96
|
+
if (this.directory === undefined) return
|
|
97
|
+
const file = join(this.directory, 'storage.json'), temporary = file + '.' + randomUUID() + '.tmp'
|
|
98
|
+
let fd: number | undefined
|
|
99
|
+
try {
|
|
100
|
+
mkdirSync(this.directory, { recursive: true, mode: 0o700 })
|
|
101
|
+
fd = openSync(temporary, 'wx', 0o600)
|
|
102
|
+
fchmodSync(fd, 0o600)
|
|
103
|
+
writeFileSync(fd, '{"version":1,"origins":' + serialized(origins) + '}')
|
|
104
|
+
fsyncSync(fd)
|
|
105
|
+
closeSync(fd); fd = undefined
|
|
106
|
+
renameSync(temporary, file)
|
|
107
|
+
} catch { fail('UnknownError') } finally {
|
|
108
|
+
if (fd !== undefined) closeSync(fd)
|
|
109
|
+
try { rmSync(temporary, { force: true }) } catch {}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const stores = new Map<string, GuestStorage>()
|
|
115
|
+
export function localGuestStorage(): GuestStorage {
|
|
116
|
+
const directory = join(dshHome(), BROWSER_PROFILE_DIR, 'web-storage')
|
|
117
|
+
let store = stores.get(directory)
|
|
118
|
+
if (store === undefined) { store = new GuestStorage(directory); stores.set(directory, store) }
|
|
119
|
+
return store
|
|
120
|
+
}
|