@blackbelt-technology/pi-agent-dashboard 0.10.0 → 0.11.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.
- package/AGENTS.md +2 -2
- package/README.md +47 -6
- package/docs/architecture.md +1369 -63
- package/package.json +14 -9
- package/packages/extension/package.json +5 -3
- package/packages/extension/src/bridge-handlers/dispatch.ts +57 -3
- package/packages/extension/src/bridge-handlers/instance.ts +10 -1
- package/packages/extension/src/bridge-handlers/lifecycle.ts +65 -26
- package/packages/extension/src/bridge-ticket-client.ts +61 -7
- package/packages/extension/src/bridge-tls-checks.ts +79 -0
- package/packages/extension/src/bridge.ts +41 -28
- package/packages/extension/src/canvas-tool.ts +2 -0
- package/packages/extension/src/command-handler.ts +80 -6
- package/packages/extension/src/connect-target.ts +23 -0
- package/packages/extension/src/connection.ts +30 -7
- package/packages/extension/src/dashboard-http-origin.ts +88 -0
- package/packages/extension/src/dev-build.ts +11 -1
- package/packages/extension/src/git-link-builder.ts +11 -2
- package/packages/extension/src/inbound-limits.ts +138 -0
- package/packages/extension/src/local-token-header.ts +77 -6
- package/packages/extension/src/model-refresh.ts +69 -0
- package/packages/extension/src/pinned-endpoint.ts +44 -0
- package/packages/extension/src/project-trust-live.ts +143 -0
- package/packages/extension/src/project-trust.ts +159 -20
- package/packages/extension/src/provider-register.ts +59 -18
- package/packages/extension/src/remote-registration-gate.ts +21 -21
- package/packages/extension/src/remote-upgrade-preparer.ts +104 -0
- package/packages/extension/src/role-model-tools.ts +47 -6
- package/packages/extension/src/server-auto-start.ts +1 -1
- package/packages/extension/src/server-proof-probe.ts +117 -0
- package/packages/extension/src/session-sync.ts +42 -12
- package/packages/extension/src/vault-delivery.ts +11 -1
- package/packages/server/package.json +8 -8
- package/packages/server/src/access/access-grants.ts +61 -8
- package/packages/server/src/access/access-health.ts +10 -0
- package/packages/server/src/access/access-plane.ts +5 -1
- package/packages/server/src/access/containment-gate.ts +55 -17
- package/packages/server/src/access/cwd-verdict.ts +3 -3
- package/packages/server/src/access/pi-trust-live.ts +244 -0
- package/packages/server/src/access/pi-trust-mirror-service.ts +44 -11
- package/packages/server/src/access/planes.ts +106 -1
- package/packages/server/src/access/preview-egress-store.ts +153 -0
- package/packages/server/src/access/prompt-channel.ts +5 -0
- package/packages/server/src/access/provisioning-wiring.ts +113 -0
- package/packages/server/src/access/provisioning.ts +763 -0
- package/packages/server/src/access/yolo-session.ts +54 -2
- package/packages/server/src/acp-bridge/acp-session-adapter.ts +8 -3
- package/packages/server/src/agent-events/agent-event-hub.ts +158 -0
- package/packages/server/src/auth/audit-log.ts +80 -0
- package/packages/server/src/auth/auth-plugin.ts +44 -25
- package/packages/server/src/auth/auth.ts +24 -4
- package/packages/server/src/auth/bearer-auth.ts +52 -12
- package/packages/server/src/auth/cors-origin.ts +76 -29
- package/packages/server/src/auth/csp-report-paths.ts +31 -0
- package/packages/server/src/auth/csp-report-sink.ts +302 -0
- package/packages/server/src/auth/csp-report-store.ts +406 -0
- package/packages/server/src/auth/csp.ts +105 -9
- package/packages/server/src/auth/dashboard-credential-store.ts +4 -0
- package/packages/server/src/auth/dashboard-state-caller.ts +29 -0
- package/packages/server/src/auth/local-proof-refusal-log.ts +89 -0
- package/packages/server/src/auth/local-proof.ts +69 -18
- package/packages/server/src/auth/localhost-guard.ts +48 -10
- package/packages/server/src/auth/nav-intent.ts +72 -0
- package/packages/server/src/auth/passkey/passkey-assets.ts +99 -0
- package/packages/server/src/auth/passkey/passkey-pages.ts +17 -81
- package/packages/server/src/auth/passkey/passkey-routes.ts +8 -0
- package/packages/server/src/auth/pi-legacy-credentials.ts +3 -0
- package/packages/server/src/auth/plugin-server-trust.ts +29 -0
- package/packages/server/src/auth/plugin-trust-store.ts +103 -0
- package/packages/server/src/auth/proven-tls.ts +45 -0
- package/packages/server/src/auth/provider-auth-storage.ts +6 -1
- package/packages/server/src/auth/radius-mcp.ts +1 -1
- package/packages/server/src/auth/request-principal.ts +32 -0
- package/packages/server/src/auth/secret-rotation.ts +246 -0
- package/packages/server/src/auth/secure-origin.ts +56 -0
- package/packages/server/src/auth/serve-app-host.ts +111 -11
- package/packages/server/src/auth/ws-ticket.ts +39 -1
- package/packages/server/src/auth/ws-upgrade-reject-log.ts +4 -1
- package/packages/server/src/browser-handlers/directory-handler.ts +23 -0
- package/packages/server/src/browser-handlers/folder-gate.ts +55 -0
- package/packages/server/src/browser-handlers/handler-context.ts +25 -2
- package/packages/server/src/browser-handlers/session-action-handler.ts +9 -1
- package/packages/server/src/browser-handlers/subscription-handler.ts +100 -2
- package/packages/server/src/browser-handlers/terminal-handler.ts +108 -10
- package/packages/server/src/canvas/canvas-accumulator.ts +35 -7
- package/packages/server/src/cli.ts +210 -39
- package/packages/server/src/config-api.ts +23 -1
- package/packages/server/src/config-snapshot.ts +48 -1
- package/packages/server/src/directory-service.ts +160 -54
- package/packages/server/src/event-wiring.ts +143 -108
- package/packages/server/src/git-worktree/git-operations.ts +44 -10
- package/packages/server/src/git-worktree/redact-git-text.ts +124 -0
- package/packages/server/src/git-worktree/ref-arg.ts +39 -0
- package/packages/server/src/git-worktree/worktree-init-trust.ts +43 -1
- package/packages/server/src/identity/http-road-classification.ts +13 -1
- package/packages/server/src/identity/ws-message-scope.ts +1 -0
- package/packages/server/src/identity/ws-road-classification.ts +1 -0
- package/packages/server/src/known-server-moves.ts +36 -15
- package/packages/server/src/lib/browse-roots.ts +118 -17
- package/packages/server/src/lib/client-dist.ts +36 -21
- package/packages/server/src/lib/dashboard-state-guard.ts +230 -0
- package/packages/server/src/lib/diagram-render.ts +4 -6
- package/packages/server/src/lib/eml.ts +3 -8
- package/packages/server/src/lib/file-cwd-admission.ts +145 -0
- package/packages/server/src/lib/grep-worker.ts +23 -0
- package/packages/server/src/lib/grep.ts +93 -7
- package/packages/server/src/lib/index-html.ts +81 -0
- package/packages/server/src/lib/office-preview.ts +2 -3
- package/packages/server/src/lib/path-containment.ts +7 -0
- package/packages/server/src/lib/resolve-file-mention.ts +11 -9
- package/packages/server/src/lib/route-admission.ts +80 -0
- package/packages/server/src/lib/writable-md-target.ts +4 -0
- package/packages/server/src/lifecycle/recovery-server.ts +65 -18
- package/packages/server/src/live-server/capability-redact.ts +15 -0
- package/packages/server/src/live-server/live-browser-gate.ts +22 -0
- package/packages/server/src/live-server/live-capability.ts +224 -17
- package/packages/server/src/live-server/live-csp-report.ts +63 -0
- package/packages/server/src/live-server/live-csp.ts +93 -0
- package/packages/server/src/live-server/live-egress.ts +435 -0
- package/packages/server/src/live-server/live-exposure.ts +149 -0
- package/packages/server/src/live-server/live-log.ts +21 -0
- package/packages/server/src/live-server/live-origin-scan.ts +298 -0
- package/packages/server/src/live-server/live-server-manager.ts +209 -33
- package/packages/server/src/live-server/live-server-proxy.ts +162 -17
- package/packages/server/src/live-server/live-static.ts +228 -0
- package/packages/server/src/openspec/dependencies-file.ts +122 -0
- package/packages/server/src/openspec/openspec-change-watcher.ts +3 -1
- package/packages/server/src/openspec/openspec-dependency-inputs.ts +281 -0
- package/packages/server/src/openspec/openspec-poll-fs-helpers.ts +72 -26
- package/packages/server/src/openspec/openspec-poll-worker-pool.ts +48 -12
- package/packages/server/src/openspec/openspec-poll-worker.ts +95 -9
- package/packages/server/src/openspec/workflow-schema-status.ts +379 -0
- package/packages/server/src/openspec/workflow-schema.ts +141 -267
- package/packages/server/src/package/npm-search-proxy.ts +5 -2
- package/packages/server/src/package/provider-probe.ts +8 -2
- package/packages/server/src/pairing/browser-gateway.ts +188 -12
- package/packages/server/src/pairing/device-token-services.ts +26 -0
- package/packages/server/src/pairing/paired-devices.ts +335 -22
- package/packages/server/src/pairing/pairing.ts +34 -4
- package/packages/server/src/pairing/socket-locality.ts +26 -0
- package/packages/server/src/persistence/preferences-store.ts +9 -0
- package/packages/server/src/pi/bridge-channel.ts +33 -0
- package/packages/server/src/pi/bridge-upgrade-auth.ts +18 -3
- package/packages/server/src/pi/gateway-listeners.ts +9 -3
- package/packages/server/src/pi/host-project-trust.ts +14 -6
- package/packages/server/src/pi/pi-gateway.ts +191 -21
- package/packages/server/src/pi/resource-toggle-trust.ts +1 -1
- package/packages/server/src/plugin-discovery-wiring.ts +142 -0
- package/packages/server/src/plugin-event-seam.ts +116 -4
- package/packages/server/src/plugin-runtime-clients.ts +89 -0
- package/packages/server/src/routes/access-routes.ts +159 -9
- package/packages/server/src/routes/agent-event-stream-routes.ts +137 -0
- package/packages/server/src/routes/canvas-types-routes.ts +20 -6
- package/packages/server/src/routes/csp-violations-routes.ts +162 -0
- package/packages/server/src/routes/file-routes.ts +329 -209
- package/packages/server/src/routes/file-write-binary-routes.ts +77 -20
- package/packages/server/src/routes/folder-project-routes.ts +18 -12
- package/packages/server/src/routes/git-routes.ts +319 -203
- package/packages/server/src/routes/grep-routes.ts +38 -14
- package/packages/server/src/routes/known-servers-routes.ts +189 -91
- package/packages/server/src/routes/live-server-routes.ts +378 -15
- package/packages/server/src/routes/local-proof-routes.ts +14 -0
- package/packages/server/src/routes/nav-intent-routes.ts +46 -0
- package/packages/server/src/routes/openspec-group-routes.ts +53 -44
- package/packages/server/src/routes/openspec-routes.ts +379 -28
- package/packages/server/src/routes/package-routes.ts +58 -10
- package/packages/server/src/routes/pairing-routes.ts +146 -10
- package/packages/server/src/routes/pi-trust-query-routes.ts +330 -0
- package/packages/server/src/routes/plugin-activation-routes.ts +59 -0
- package/packages/server/src/routes/plugin-runtime-client-routes.ts +149 -0
- package/packages/server/src/routes/provider-auth-routes.ts +15 -2
- package/packages/server/src/routes/resource-activation-routes.ts +18 -4
- package/packages/server/src/routes/session-routes.ts +51 -4
- package/packages/server/src/routes/system-routes.ts +84 -15
- package/packages/server/src/routes/ui-link-base.ts +88 -0
- package/packages/server/src/runtime-overlay/runtime-io.ts +2 -9
- package/packages/server/src/server.ts +648 -137
- package/packages/server/src/session/hydration-state.ts +148 -0
- package/packages/server/src/session/memory-session-manager.ts +7 -0
- package/packages/server/src/session/replay-exit-controller.ts +221 -0
- package/packages/server/src/session/retained-transcript.ts +9 -2
- package/packages/server/src/session/session-load-worker.ts +11 -2
- package/packages/server/src/spawn-process/headless-pid-registry.ts +14 -9
- package/packages/server/src/terminal/terminal-manager.ts +54 -46
- package/packages/server/src/tls/tls-bridge-route.ts +61 -0
- package/packages/server/src/tls/tls-config-validation.ts +2 -0
- package/packages/server/src/vault/device-expiry-sweep.ts +36 -0
- package/packages/server/src/vault/vault-delivery-hub.ts +155 -25
- package/packages/server/src/watch-files-handler.ts +66 -0
- package/packages/shared/package.json +1 -1
- package/packages/shared/src/browser-protocol.ts +148 -2
- package/packages/shared/src/canvas-declare.ts +27 -0
- package/packages/shared/src/config.ts +372 -53
- package/packages/shared/src/containment-guard.ts +35 -0
- package/packages/shared/src/dashboard-plugin/anthropic-peer-names.ts +9 -0
- package/packages/shared/src/dashboard-plugin/event-type-owners.ts +50 -0
- package/packages/shared/src/dashboard-plugin/file-handler.ts +2 -0
- package/packages/shared/src/dashboard-plugin/host-api.ts +61 -0
- package/packages/shared/src/dashboard-plugin/host-fx-classes.ts +23 -0
- package/packages/shared/src/dashboard-plugin/host-import-map.ts +38 -0
- package/packages/shared/src/dashboard-plugin/index.ts +2 -0
- package/packages/shared/src/dashboard-plugin/manifest-types.ts +54 -1
- package/packages/shared/src/dashboard-plugin/plugin-status.ts +54 -0
- package/packages/shared/src/dashboard-plugin/slot-props.ts +45 -3
- package/packages/shared/src/dashboard-plugin/slot-types.ts +8 -0
- package/packages/shared/src/dashboard-plugin/subagent-types.ts +219 -0
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +28 -0
- package/packages/shared/src/display-prefs.ts +13 -0
- package/packages/shared/src/doctor-core.ts +94 -27
- package/packages/shared/src/live-server.ts +141 -0
- package/packages/shared/src/local-proof-signal.ts +16 -0
- package/packages/shared/src/loopback-host.ts +26 -0
- package/packages/shared/src/mcp/verb-tiers.generated.ts +178 -0
- package/packages/shared/src/mdns-discovery.ts +25 -6
- package/packages/shared/src/nav-intent.ts +29 -0
- package/packages/shared/src/openspec-dependencies.ts +760 -0
- package/packages/shared/src/openspec-poller.ts +3 -2
- package/packages/shared/src/pairing-knock-client.ts +44 -4
- package/packages/shared/src/pi-trust-live-query.ts +93 -0
- package/packages/shared/src/pi-trust-mirror-snapshot.ts +5 -7
- package/packages/shared/src/pi-trust-mirror-types.ts +36 -3
- package/packages/shared/src/pi-trust-mirror.ts +29 -3
- package/packages/shared/src/pi-trust-resource-digest.ts +255 -0
- package/packages/shared/src/platform/ensure-bundled-git.ts +57 -34
- package/packages/shared/src/platform/git-source.ts +88 -26
- package/packages/shared/src/platform/git.ts +14 -8
- package/packages/shared/src/platform/process.ts +48 -4
- package/packages/shared/src/platform/resolve-pi-shell.ts +76 -0
- package/packages/shared/src/platform/select-git-source.ts +7 -0
- package/packages/shared/src/plugin-route-cwd.ts +95 -0
- package/packages/shared/src/preview-egress.ts +138 -0
- package/packages/shared/src/project-root.ts +16 -0
- package/packages/shared/src/protocol.ts +40 -1
- package/packages/shared/src/recommended-extensions.ts +18 -0
- package/packages/shared/src/recovery-timing.ts +27 -0
- package/packages/shared/src/remote-credentials.ts +63 -0
- package/packages/shared/src/rest-api.ts +55 -8
- package/packages/shared/src/route-tiers.ts +55 -1
- package/packages/shared/src/runtime-overlay/archive-safety.d.mts +2 -0
- package/packages/shared/src/runtime-overlay/archive-safety.mjs +33 -0
- package/packages/shared/src/runtime-overlay/manifest.d.mts +7 -0
- package/packages/shared/src/runtime-overlay/manifest.mjs +22 -2
- package/packages/shared/src/safe-local-path.ts +24 -0
- package/packages/shared/src/sanitize-profiles.ts +98 -0
- package/packages/shared/src/server-launcher.ts +9 -1
- package/packages/shared/src/server-pinning.ts +55 -2
- package/packages/shared/src/server-routes.ts +462 -0
- package/packages/shared/src/session-ui-path.ts +11 -0
- package/packages/shared/src/tls-only-token.ts +16 -0
- package/packages/shared/src/tool-registry/definitions.ts +5 -5
- package/packages/shared/src/tool-registry/strategies.ts +12 -11
- package/packages/shared/src/types.ts +66 -0
- package/packages/shared/src/url-policy.ts +96 -0
- package/packages/shared/src/vault-delivery.ts +15 -0
- package/packages/shared/src/ws-message-tiers.ts +1 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-token preview egress state: which external origins a restricted preview
|
|
3
|
+
* wants (found in markup by a lazy scan of the HTML it served, or reported by the
|
|
4
|
+
* browser's CSP violation reports), which are allowed (project grant / the
|
|
5
|
+
* viewer's allow-once set), and the CSP `<G>` built from the allowed ones.
|
|
6
|
+
*
|
|
7
|
+
* - The blocked set is per TOKEN and bounded at 64. Eviction removes the oldest
|
|
8
|
+
* `runtime` entries first and never a markup-detected or an allowed one.
|
|
9
|
+
* - A `runtime` origin comes from a report the PAGE itself can forge, so it is
|
|
10
|
+
* labelled as such and is never project-grantable (the route layer enforces it).
|
|
11
|
+
* - The CSP report sink is rate limited per token (20/s, 600/h) and reads only
|
|
12
|
+
* `blocked-uri`; the capability never reaches a log line (`liveLog` redacts).
|
|
13
|
+
*
|
|
14
|
+
* See change: improve-canvas-html-and-url-ux (design D6, D7; tasks 2.7-2.9, 2.11, 3.3).
|
|
15
|
+
*/
|
|
16
|
+
import type { StaticTarget } from "@blackbelt-technology/pi-dashboard-shared/live-server.js";
|
|
17
|
+
import { canonicalHttpsOrigin, isUngrantableEgressOrigin } from "@blackbelt-technology/pi-dashboard-shared/preview-egress.js";
|
|
18
|
+
import type { LiveCapabilityGrant } from "./live-capability.js";
|
|
19
|
+
import { type ScanOutcome, scanHtmlFile, scanStillValid } from "./live-origin-scan.js";
|
|
20
|
+
|
|
21
|
+
/** The per-token blocked-set bound (markup + runtime). */
|
|
22
|
+
export const MAX_BLOCKED_ORIGINS = 64;
|
|
23
|
+
/** HTML documents remembered per token for scanning. */
|
|
24
|
+
const MAX_SERVED_DOCS = 8;
|
|
25
|
+
/** Token states kept (oldest dropped). */
|
|
26
|
+
const MAX_TOKEN_STATES = 512;
|
|
27
|
+
const MAX_SCAN_CACHE = 64;
|
|
28
|
+
/** CSP report limits per token (decision C4). */
|
|
29
|
+
export const CSP_REPORT_MAX_BYTES = 64 * 1024;
|
|
30
|
+
const CSP_REPORTS_PER_SECOND = 20;
|
|
31
|
+
const CSP_REPORTS_PER_HOUR = 600;
|
|
32
|
+
/** Reports read from one body (a Reporting API batch). */
|
|
33
|
+
const MAX_REPORTS_PER_BODY = 20;
|
|
34
|
+
|
|
35
|
+
/** Hosts that serve content anyone can publish: a grant for one trusts every tenant. */
|
|
36
|
+
const SHARED_HOSTING = [
|
|
37
|
+
"github.io",
|
|
38
|
+
"pages.dev",
|
|
39
|
+
"netlify.app",
|
|
40
|
+
"vercel.app",
|
|
41
|
+
"workers.dev",
|
|
42
|
+
"glitch.me",
|
|
43
|
+
"repl.co",
|
|
44
|
+
"trycloudflare.com",
|
|
45
|
+
"ngrok-free.app",
|
|
46
|
+
"ngrok.io",
|
|
47
|
+
"onrender.com",
|
|
48
|
+
"fly.dev",
|
|
49
|
+
"surge.sh",
|
|
50
|
+
"share.zrok.io",
|
|
51
|
+
] as const;
|
|
52
|
+
|
|
53
|
+
const isSharedHostingOrigin = (origin: string): boolean => {
|
|
54
|
+
let host: string;
|
|
55
|
+
try {
|
|
56
|
+
host = new URL(origin).hostname;
|
|
57
|
+
} catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return SHARED_HOSTING.some((d) => host === d || host.endsWith(`.${d}`));
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
interface EgressOriginView {
|
|
64
|
+
origin: string;
|
|
65
|
+
source: "markup" | "runtime";
|
|
66
|
+
state: "blocked" | "allowed-once" | "allowed-project";
|
|
67
|
+
shared?: boolean;
|
|
68
|
+
ungrantable?: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface EgressView {
|
|
72
|
+
origins: EgressOriginView[];
|
|
73
|
+
scanIncomplete: boolean;
|
|
74
|
+
rootAbsolute: boolean;
|
|
75
|
+
runtimeChannel: boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface PreviewEgressDeps {
|
|
79
|
+
getStatic(id: string): StaticTarget | undefined;
|
|
80
|
+
/** The cwd of the session that opened an in-memory preset (port) target, when still known. */
|
|
81
|
+
presetCwdOf?(id: string): string | undefined;
|
|
82
|
+
/** The project a working directory belongs to (git common root, else cwd). Cached here. */
|
|
83
|
+
projectKeyOf(cwd: string): string;
|
|
84
|
+
/** Origins granted for a project (read live: a revoke applies on the next call). */
|
|
85
|
+
projectGrants(projectKey: string): readonly string[];
|
|
86
|
+
/** The viewer's allow-once set. */
|
|
87
|
+
allowOnce(viewerId: string | undefined): readonly string[];
|
|
88
|
+
/** The dashboard's own origins (tunnels, public base URLs, …): never grantable. */
|
|
89
|
+
ownOrigins?(): readonly string[];
|
|
90
|
+
now?(): number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface TokenState {
|
|
94
|
+
/** Real paths of HTML documents served under this token, oldest first. */
|
|
95
|
+
served: string[];
|
|
96
|
+
/** Runtime-reported origins, insertion-ordered (oldest first). */
|
|
97
|
+
runtime: Set<string>;
|
|
98
|
+
reports: number;
|
|
99
|
+
/** Timestamps (ms) of accepted reports in the last second. */
|
|
100
|
+
recent: number[];
|
|
101
|
+
hourStart: number;
|
|
102
|
+
hourCount: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
type ReportOutcome = "ok" | "rate-limited";
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Pull the blocked URLs out of a CSP report body: the legacy
|
|
109
|
+
* `{"csp-report":{"blocked-uri":…}}` (`application/csp-report`) and the Reporting API
|
|
110
|
+
* array `[{type:"csp-violation",body:{blockedURL:…}}]` (`application/reports+json`).
|
|
111
|
+
* Nothing else in the body is read.
|
|
112
|
+
*/
|
|
113
|
+
function blockedUrisOf(body: unknown): string[] {
|
|
114
|
+
const out: string[] = [];
|
|
115
|
+
const one = (item: unknown): void => {
|
|
116
|
+
if (!item || typeof item !== "object") return;
|
|
117
|
+
const rec = item as Record<string, unknown>;
|
|
118
|
+
const inner = (rec["csp-report"] ?? rec.body ?? rec) as Record<string, unknown> | undefined;
|
|
119
|
+
if (!inner || typeof inner !== "object") return;
|
|
120
|
+
const uri = inner["blocked-uri"] ?? inner.blockedURL ?? inner["blocked-url"];
|
|
121
|
+
if (typeof uri === "string" && uri.length > 0 && uri.length <= 2048) out.push(uri);
|
|
122
|
+
};
|
|
123
|
+
if (Array.isArray(body)) for (const item of body.slice(0, MAX_REPORTS_PER_BODY)) one(item);
|
|
124
|
+
else one(body);
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A blocked URL as a listable origin (`https://…` canonical, `http://…` as-is), or `null` (inline, data, own, …). */
|
|
129
|
+
function listedOriginOf(blockedUri: string): string | null {
|
|
130
|
+
let u: URL;
|
|
131
|
+
try {
|
|
132
|
+
u = new URL(blockedUri);
|
|
133
|
+
} catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
if (u.protocol === "https:") return canonicalHttpsOrigin(u.origin);
|
|
137
|
+
if (u.protocol === "http:") return u.origin;
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The dashboard origin a request reached us on (`scheme://Host`), as far as this hop can tell. */
|
|
142
|
+
export function requestOriginsOf(request: { protocol?: string; headers: Record<string, unknown> }): string[] {
|
|
143
|
+
const host = request.headers.host;
|
|
144
|
+
if (typeof host !== "string" || !/^[A-Za-z0-9.\-[\]:]+$/.test(host)) return [];
|
|
145
|
+
const scheme = request.protocol === "https" ? "https" : "http";
|
|
146
|
+
return [`${scheme}://${host.toLowerCase()}`];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface PreviewEgress {
|
|
150
|
+
/** Remember that `realPath` (an HTML document) was served under this token. */
|
|
151
|
+
noteServed(grant: LiveCapabilityGrant, realPath: string): void;
|
|
152
|
+
/** The origins view for the token (scans lazily). `own`: extra dashboard origins (the request's). */
|
|
153
|
+
view(grant: LiveCapabilityGrant, opts?: { own?: readonly string[]; promptable?: boolean }): Promise<EgressView>;
|
|
154
|
+
/** Markup-detected origins of the token (scans lazily). */
|
|
155
|
+
markupOrigins(grant: LiveCapabilityGrant): Promise<string[]>;
|
|
156
|
+
/** CSP `<G>`: project grants ∪ the viewer's allow-once set. */
|
|
157
|
+
grantedFor(grant: LiveCapabilityGrant): string[];
|
|
158
|
+
/** The project a token belongs to (a static target's cwd project), or `null` for a port target. */
|
|
159
|
+
projectKeyFor(grant: LiveCapabilityGrant): string | null;
|
|
160
|
+
/** Record a CSP report body for the token (rate limited). */
|
|
161
|
+
report(grant: LiveCapabilityGrant, body: unknown, own?: readonly string[]): ReportOutcome;
|
|
162
|
+
/** Is the origin grantable at all (https, public, not the dashboard's own)? */
|
|
163
|
+
isGrantable(origin: string, own?: readonly string[]): boolean;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Per-token report limits: 20 in any second, 600 in an hour. Counts the report when admitted. */
|
|
167
|
+
function admitReport(state: TokenState, t: number): boolean {
|
|
168
|
+
state.recent = state.recent.filter((x) => t - x < 1000);
|
|
169
|
+
if (state.recent.length >= CSP_REPORTS_PER_SECOND) return false;
|
|
170
|
+
if (t - state.hourStart >= 3_600_000) {
|
|
171
|
+
state.hourStart = t;
|
|
172
|
+
state.hourCount = 0;
|
|
173
|
+
}
|
|
174
|
+
if (state.hourCount >= CSP_REPORTS_PER_HOUR) return false;
|
|
175
|
+
state.recent.push(t);
|
|
176
|
+
state.hourCount++;
|
|
177
|
+
state.reports++;
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Add the report's blocked origins as `runtime` entries, bounded: evict the oldest not-allowed one first. */
|
|
182
|
+
function recordRuntimeOrigins(state: TokenState, body: unknown, ownList: readonly string[], allowed: ReadonlySet<string>): void {
|
|
183
|
+
for (const uri of blockedUrisOf(body)) {
|
|
184
|
+
const origin = listedOriginOf(uri);
|
|
185
|
+
// The dashboard's own origin is not an egress target.
|
|
186
|
+
if (!origin || ownList.includes(origin)) continue;
|
|
187
|
+
state.runtime.delete(origin); // re-insert: most recently seen last
|
|
188
|
+
state.runtime.add(origin);
|
|
189
|
+
}
|
|
190
|
+
while (state.runtime.size > MAX_BLOCKED_ORIGINS) {
|
|
191
|
+
const victim = [...state.runtime].find((o) => !allowed.has(o));
|
|
192
|
+
if (victim === undefined) break;
|
|
193
|
+
state.runtime.delete(victim);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function createPreviewEgress(deps: PreviewEgressDeps): PreviewEgress {
|
|
198
|
+
const now = deps.now ?? Date.now;
|
|
199
|
+
const states = new Map<string, TokenState>();
|
|
200
|
+
const scans = new Map<string, ScanOutcome>();
|
|
201
|
+
const projectKeys = new Map<string, string>();
|
|
202
|
+
|
|
203
|
+
const stateOf = (tokenId: string): TokenState => {
|
|
204
|
+
let s = states.get(tokenId);
|
|
205
|
+
if (s) {
|
|
206
|
+
states.delete(tokenId);
|
|
207
|
+
} else {
|
|
208
|
+
s = { served: [], runtime: new Set(), reports: 0, recent: [], hourStart: now(), hourCount: 0 };
|
|
209
|
+
}
|
|
210
|
+
states.set(tokenId, s);
|
|
211
|
+
while (states.size > MAX_TOKEN_STATES) {
|
|
212
|
+
const oldest = states.keys().next().value;
|
|
213
|
+
if (oldest === undefined) break;
|
|
214
|
+
states.delete(oldest);
|
|
215
|
+
}
|
|
216
|
+
return s;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const own = (extra?: readonly string[]): readonly string[] => [...(deps.ownOrigins?.() ?? []), ...(extra ?? [])];
|
|
220
|
+
|
|
221
|
+
const projectKeyFor = (grant: LiveCapabilityGrant): string | null => {
|
|
222
|
+
// A static target carries its cwd; a preset port target borrows its opening session's.
|
|
223
|
+
const cwd = deps.getStatic(grant.targetId)?.cwd ?? deps.presetCwdOf?.(grant.targetId);
|
|
224
|
+
if (!cwd) return null;
|
|
225
|
+
let key = projectKeys.get(cwd);
|
|
226
|
+
if (key === undefined) {
|
|
227
|
+
try {
|
|
228
|
+
key = deps.projectKeyOf(cwd);
|
|
229
|
+
} catch {
|
|
230
|
+
key = cwd;
|
|
231
|
+
}
|
|
232
|
+
projectKeys.set(cwd, key);
|
|
233
|
+
while (projectKeys.size > 256) {
|
|
234
|
+
const oldest = projectKeys.keys().next().value;
|
|
235
|
+
if (oldest === undefined) break;
|
|
236
|
+
projectKeys.delete(oldest);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return key;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const grantedFor = (grant: LiveCapabilityGrant): string[] => {
|
|
243
|
+
const project = projectKeyFor(grant);
|
|
244
|
+
const projectGrants = project ? deps.projectGrants(project) : [];
|
|
245
|
+
return [...new Set([...projectGrants, ...deps.allowOnce(grant.viewerId)])];
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const scanFile = async (root: string, file: string): Promise<ScanOutcome> => {
|
|
249
|
+
const cached = scans.get(file);
|
|
250
|
+
if (cached && (await scanStillValid(cached))) {
|
|
251
|
+
scans.delete(file);
|
|
252
|
+
scans.set(file, cached);
|
|
253
|
+
return cached;
|
|
254
|
+
}
|
|
255
|
+
const fresh = await scanHtmlFile(root, file);
|
|
256
|
+
scans.delete(file);
|
|
257
|
+
scans.set(file, fresh);
|
|
258
|
+
while (scans.size > MAX_SCAN_CACHE) {
|
|
259
|
+
const oldest = scans.keys().next().value;
|
|
260
|
+
if (oldest === undefined) break;
|
|
261
|
+
scans.delete(oldest);
|
|
262
|
+
}
|
|
263
|
+
return fresh;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/** Union of the scans of every document served under the token. */
|
|
267
|
+
const scanToken = async (
|
|
268
|
+
grant: LiveCapabilityGrant,
|
|
269
|
+
): Promise<{ origins: string[]; incomplete: boolean; rootAbsolute: boolean }> => {
|
|
270
|
+
const target = deps.getStatic(grant.targetId);
|
|
271
|
+
const state = states.get(grant.tokenId);
|
|
272
|
+
const result = { origins: [] as string[], incomplete: false, rootAbsolute: false };
|
|
273
|
+
if (!target || !state) return result;
|
|
274
|
+
const seen = new Set<string>();
|
|
275
|
+
for (const file of [...state.served]) {
|
|
276
|
+
const out = await scanFile(target.root, file);
|
|
277
|
+
for (const o of out.origins) seen.add(o);
|
|
278
|
+
if (out.incomplete) result.incomplete = true;
|
|
279
|
+
if (out.rootAbsolute) result.rootAbsolute = true;
|
|
280
|
+
}
|
|
281
|
+
result.origins = [...seen];
|
|
282
|
+
return result;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
noteServed(grant, realPath) {
|
|
287
|
+
const s = stateOf(grant.tokenId);
|
|
288
|
+
const i = s.served.indexOf(realPath);
|
|
289
|
+
if (i >= 0) s.served.splice(i, 1);
|
|
290
|
+
s.served.push(realPath);
|
|
291
|
+
while (s.served.length > MAX_SERVED_DOCS) s.served.shift();
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
async view(grant, opts = {}) {
|
|
295
|
+
const state = stateOf(grant.tokenId);
|
|
296
|
+
const scan = await scanToken(grant);
|
|
297
|
+
const ownList = own(opts.own);
|
|
298
|
+
const project = projectKeyFor(grant);
|
|
299
|
+
const projectSet = new Set(project ? deps.projectGrants(project) : []);
|
|
300
|
+
const onceSet = new Set(deps.allowOnce(grant.viewerId));
|
|
301
|
+
const stateOfOrigin = (o: string): EgressOriginView["state"] =>
|
|
302
|
+
projectSet.has(o) ? "allowed-project" : onceSet.has(o) ? "allowed-once" : "blocked";
|
|
303
|
+
const isAllowed = (o: string): boolean => projectSet.has(o) || onceSet.has(o);
|
|
304
|
+
|
|
305
|
+
const markup = scan.origins.slice(0, MAX_BLOCKED_ORIGINS);
|
|
306
|
+
const incomplete = scan.incomplete || scan.origins.length > MAX_BLOCKED_ORIGINS;
|
|
307
|
+
const markupSet = new Set(markup);
|
|
308
|
+
// Runtime entries: not already in markup; oldest evicted first, never an allowed one.
|
|
309
|
+
const runtime = [...state.runtime].filter((o) => !markupSet.has(o));
|
|
310
|
+
let over = markup.length + runtime.length - MAX_BLOCKED_ORIGINS;
|
|
311
|
+
for (let i = 0; i < runtime.length && over > 0; ) {
|
|
312
|
+
if (isAllowed(runtime[i])) {
|
|
313
|
+
i++;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
state.runtime.delete(runtime[i]);
|
|
317
|
+
runtime.splice(i, 1);
|
|
318
|
+
over--;
|
|
319
|
+
}
|
|
320
|
+
// Allowed-once origins the page no longer asks for (reload after allowing) stay visible.
|
|
321
|
+
const listed = new Set([...markup, ...runtime]);
|
|
322
|
+
const extras = [...onceSet].filter((o) => !listed.has(o));
|
|
323
|
+
const rows: EgressOriginView[] = [
|
|
324
|
+
...markup.map((o) => ({ origin: o, source: "markup" as const })),
|
|
325
|
+
...runtime.map((o) => ({ origin: o, source: "runtime" as const })),
|
|
326
|
+
...extras.map((o) => ({ origin: o, source: "runtime" as const })),
|
|
327
|
+
].map(({ origin, source }) => {
|
|
328
|
+
const ungrantable = isUngrantableEgressOrigin(origin, ownList);
|
|
329
|
+
return {
|
|
330
|
+
origin,
|
|
331
|
+
source,
|
|
332
|
+
state: stateOfOrigin(origin),
|
|
333
|
+
...(isSharedHostingOrigin(origin) ? { shared: true } : {}),
|
|
334
|
+
...(ungrantable ? { ungrantable: true } : {}),
|
|
335
|
+
};
|
|
336
|
+
});
|
|
337
|
+
return {
|
|
338
|
+
origins: rows,
|
|
339
|
+
scanIncomplete: incomplete,
|
|
340
|
+
rootAbsolute: scan.rootAbsolute,
|
|
341
|
+
runtimeChannel: state.reports > 0,
|
|
342
|
+
};
|
|
343
|
+
},
|
|
344
|
+
|
|
345
|
+
async markupOrigins(grant) {
|
|
346
|
+
return (await scanToken(grant)).origins;
|
|
347
|
+
},
|
|
348
|
+
|
|
349
|
+
grantedFor,
|
|
350
|
+
projectKeyFor,
|
|
351
|
+
|
|
352
|
+
report(grant, body, ownExtra) {
|
|
353
|
+
const state = stateOf(grant.tokenId);
|
|
354
|
+
if (!admitReport(state, now())) return "rate-limited";
|
|
355
|
+
recordRuntimeOrigins(state, body, own(ownExtra), new Set(grantedFor(grant)));
|
|
356
|
+
return "ok";
|
|
357
|
+
},
|
|
358
|
+
|
|
359
|
+
isGrantable(origin, ownExtra) {
|
|
360
|
+
return !isUngrantableEgressOrigin(origin, own(ownExtra));
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** How a `POST /:id/egress-grant-request` ended (the registry's decision, in the route's terms). */
|
|
366
|
+
export type EgressGrantRequestOutcome =
|
|
367
|
+
| "raised"
|
|
368
|
+
| "prompting-disabled"
|
|
369
|
+
| "rate-limited"
|
|
370
|
+
| "not-promptable"
|
|
371
|
+
| "unavailable";
|
|
372
|
+
|
|
373
|
+
/** Map the coordinator's denial reason for a deferred request onto the route's outcome. */
|
|
374
|
+
function outcomeOfDenialReason(reason: string): EgressGrantRequestOutcome {
|
|
375
|
+
switch (reason) {
|
|
376
|
+
// The entry was recorded and the dialog raised (a deferred request is never held).
|
|
377
|
+
case "not-held":
|
|
378
|
+
return "raised";
|
|
379
|
+
case "disabled":
|
|
380
|
+
case "report-mode":
|
|
381
|
+
case "no-audience":
|
|
382
|
+
case "ineligible":
|
|
383
|
+
return "prompting-disabled";
|
|
384
|
+
case "not-promptable":
|
|
385
|
+
return "not-promptable";
|
|
386
|
+
case "capacity":
|
|
387
|
+
case "channel-share":
|
|
388
|
+
case "deferred-share":
|
|
389
|
+
case "backoff":
|
|
390
|
+
case "plane-rate":
|
|
391
|
+
case "channel-rate":
|
|
392
|
+
case "concurrent-cap":
|
|
393
|
+
case "channel-concurrent":
|
|
394
|
+
return "rate-limited";
|
|
395
|
+
default:
|
|
396
|
+
return "unavailable";
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** The slice of the grant coordinator the requester needs. */
|
|
401
|
+
interface DenialSink {
|
|
402
|
+
onDenial(
|
|
403
|
+
ctx: {
|
|
404
|
+
plane: "preview-egress";
|
|
405
|
+
rawSubject: string;
|
|
406
|
+
origin: string;
|
|
407
|
+
channel: string;
|
|
408
|
+
requestHoldsCapability: boolean;
|
|
409
|
+
},
|
|
410
|
+
holdable: boolean,
|
|
411
|
+
): { result: Promise<{ kind: string; reason?: string }> };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Build the `requestEgressGrant` dependency of the route: records a deferred
|
|
416
|
+
* `(preview-egress, origin)` registry entry charged to `channel` (the requesting
|
|
417
|
+
* browser's source) and raises the standard dialog, with the project key as the
|
|
418
|
+
* audit origin the plane's `grant` writes under.
|
|
419
|
+
*/
|
|
420
|
+
export function createEgressGrantRequester(coordinator: DenialSink) {
|
|
421
|
+
return async (input: { origin: string; projectKey: string; channel: string }): Promise<EgressGrantRequestOutcome> => {
|
|
422
|
+
const hold = coordinator.onDenial(
|
|
423
|
+
{
|
|
424
|
+
plane: "preview-egress",
|
|
425
|
+
rawSubject: input.origin,
|
|
426
|
+
origin: input.projectKey,
|
|
427
|
+
channel: input.channel,
|
|
428
|
+
requestHoldsCapability: false,
|
|
429
|
+
},
|
|
430
|
+
false,
|
|
431
|
+
);
|
|
432
|
+
const res = await hold.result;
|
|
433
|
+
return res.kind === "deny" ? outcomeOfDenialReason(res.reason ?? "") : "unavailable";
|
|
434
|
+
};
|
|
435
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Preview exposure: is the dashboard reachable only from this machine?
|
|
3
|
+
*
|
|
4
|
+
* `exposed` when ANY of: the resolved bind host is not loopback-only (an
|
|
5
|
+
* unparseable value counts as exposed), a non-local endpoint exists (tunnel,
|
|
6
|
+
* `publicBaseUrls`, TLS listener), a request carrying a forwarding header has
|
|
7
|
+
* been seen since start (sticky — fails closed), or the operator set
|
|
8
|
+
* `liveServer.previewExposure: "exposed"` (covers a same-host relay that adds
|
|
9
|
+
* no header, e.g. `ssh -L`). Otherwise `local-only`.
|
|
10
|
+
*
|
|
11
|
+
* Every change of the computed value bumps a monotonic epoch and notifies the
|
|
12
|
+
* listeners (the server revokes restricted scripted local-only tokens and pushes
|
|
13
|
+
* `preview_exposure_changed` to the browsers). Inputs are injected so the
|
|
14
|
+
* predicate is a plain matrix test.
|
|
15
|
+
*
|
|
16
|
+
* See change: improve-canvas-html-and-url-ux (design D5).
|
|
17
|
+
*/
|
|
18
|
+
import { isIP } from "node:net";
|
|
19
|
+
import { hasProxyForwardingHeaders } from "../auth/localhost-guard.js";
|
|
20
|
+
import type { LiveCapabilityStore, LiveExposure } from "./live-capability.js";
|
|
21
|
+
import { type LiveLogSink, liveLog } from "./live-log.js";
|
|
22
|
+
|
|
23
|
+
export interface ExposureInputs {
|
|
24
|
+
/** The bind host this process actually bound (`127.0.0.1`, `0.0.0.0`, a LAN IP, a name, ...). */
|
|
25
|
+
getBindHost: () => string;
|
|
26
|
+
/** Non-local endpoints (tunnel / publicBaseUrls / TLS listener) — empty when none. */
|
|
27
|
+
getEndpoints: () => readonly string[];
|
|
28
|
+
/** `liveServer.previewExposure` from config (`"exposed"` forces exposed). */
|
|
29
|
+
getConfigOverride?: () => string | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ExposureState {
|
|
33
|
+
exposure: LiveExposure;
|
|
34
|
+
epoch: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Is `host` unambiguously loopback-only? Names other than `localhost` are NOT (fail closed). */
|
|
38
|
+
function isLoopbackBindHost(host: string): boolean {
|
|
39
|
+
const h = host.trim().toLowerCase().replace(/^\[|\]$/g, "");
|
|
40
|
+
if (h === "localhost") return true;
|
|
41
|
+
if (h === "::1") return true;
|
|
42
|
+
if (isIP(h) === 4) return h.split(".")[0] === "127";
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Pure predicate: the matrix of design D5. */
|
|
47
|
+
export function computeExposure(args: {
|
|
48
|
+
bindHost: string;
|
|
49
|
+
endpoints: readonly string[];
|
|
50
|
+
forwardedSeen: boolean;
|
|
51
|
+
configOverride?: string;
|
|
52
|
+
}): LiveExposure {
|
|
53
|
+
if (args.configOverride === "exposed") return "exposed";
|
|
54
|
+
if (args.forwardedSeen) return "exposed";
|
|
55
|
+
if (args.endpoints.length > 0) return "exposed";
|
|
56
|
+
return isLoopbackBindHost(args.bindHost) ? "local-only" : "exposed";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
type ExposureListener = (state: ExposureState, previous: ExposureState) => void;
|
|
60
|
+
|
|
61
|
+
export interface PreviewExposure {
|
|
62
|
+
/** Recompute (cheap) and return the current state; fires listeners on a change. */
|
|
63
|
+
current(): ExposureState;
|
|
64
|
+
/** Note a request's headers: a forwarding header pins `exposed` until restart. */
|
|
65
|
+
noteRequest(headers: Record<string, unknown>): void;
|
|
66
|
+
onChange(listener: ExposureListener): void;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function createPreviewExposure(inputs: ExposureInputs): PreviewExposure {
|
|
70
|
+
let forwardedSeen = false;
|
|
71
|
+
let state: ExposureState | null = null;
|
|
72
|
+
const listeners: ExposureListener[] = [];
|
|
73
|
+
|
|
74
|
+
const compute = (): LiveExposure => {
|
|
75
|
+
let endpoints: readonly string[] = [];
|
|
76
|
+
let bindHost = "";
|
|
77
|
+
try {
|
|
78
|
+
endpoints = inputs.getEndpoints();
|
|
79
|
+
bindHost = inputs.getBindHost();
|
|
80
|
+
} catch {
|
|
81
|
+
return "exposed"; // an input we cannot read is an input we cannot trust
|
|
82
|
+
}
|
|
83
|
+
return computeExposure({
|
|
84
|
+
bindHost,
|
|
85
|
+
endpoints,
|
|
86
|
+
forwardedSeen,
|
|
87
|
+
configOverride: inputs.getConfigOverride?.(),
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const current = (): ExposureState => {
|
|
92
|
+
const next = compute();
|
|
93
|
+
if (state === null) {
|
|
94
|
+
state = { exposure: next, epoch: 1 };
|
|
95
|
+
return state;
|
|
96
|
+
}
|
|
97
|
+
if (next !== state.exposure) {
|
|
98
|
+
const previous = state;
|
|
99
|
+
state = { exposure: next, epoch: previous.epoch + 1 };
|
|
100
|
+
for (const l of listeners) {
|
|
101
|
+
try {
|
|
102
|
+
l(state, previous);
|
|
103
|
+
} catch (err) {
|
|
104
|
+
console.error("[live-preview] exposure listener failed:", err);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return state;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
current(); // fix the baseline now, so a later flip is a real epoch change
|
|
112
|
+
return {
|
|
113
|
+
current,
|
|
114
|
+
noteRequest(headers) {
|
|
115
|
+
if (forwardedSeen) return;
|
|
116
|
+
if (!hasProxyForwardingHeaders(headers)) return;
|
|
117
|
+
forwardedSeen = true;
|
|
118
|
+
current();
|
|
119
|
+
},
|
|
120
|
+
onChange(listener) {
|
|
121
|
+
listeners.push(listener);
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Wire the consequences of an exposure flip: on a change TO `exposed`, revoke every
|
|
128
|
+
* restricted scripts-on token minted while `local-only` (open tokens are the operator's
|
|
129
|
+
* own choice and stay), log the count (no token text), and push `preview_exposure_changed`
|
|
130
|
+
* so open viewers re-mint without scripts. A flip back to `local-only` only notifies.
|
|
131
|
+
*/
|
|
132
|
+
export function wirePreviewExposure(deps: {
|
|
133
|
+
exposure: PreviewExposure;
|
|
134
|
+
capabilities: Pick<LiveCapabilityStore, "revokeWhere">;
|
|
135
|
+
/** Push `preview_exposure_changed {epoch}` to every browser. */
|
|
136
|
+
broadcast: (epoch: number) => void;
|
|
137
|
+
log?: LiveLogSink;
|
|
138
|
+
}): void {
|
|
139
|
+
deps.exposure.onChange((state) => {
|
|
140
|
+
const revoked =
|
|
141
|
+
state.exposure === "exposed"
|
|
142
|
+
? deps.capabilities.revokeWhere(
|
|
143
|
+
(e) => e.policy.egress === "restricted" && e.policy.scripts && e.mintExposure === "local-only",
|
|
144
|
+
)
|
|
145
|
+
: 0;
|
|
146
|
+
liveLog(deps.log, "exposure changed", { exposure: state.exposure, epoch: state.epoch, revoked });
|
|
147
|
+
deps.broadcast(state.epoch);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-line structured log for live-preview events (`[live-preview] <event> k=v …`).
|
|
3
|
+
* Values are JSON-encoded (no field can forge a second line) and the whole line
|
|
4
|
+
* passes `redactCapability`, so a capability token never reaches a log.
|
|
5
|
+
*
|
|
6
|
+
* See change: improve-canvas-html-and-url-ux (task 2.11).
|
|
7
|
+
*/
|
|
8
|
+
import { redactCapability } from "./capability-redact.js";
|
|
9
|
+
|
|
10
|
+
export type LiveLogSink = (line: string) => void;
|
|
11
|
+
|
|
12
|
+
function formatLiveLog(event: string, fields: Record<string, unknown> = {}): string {
|
|
13
|
+
const kv = Object.entries(fields)
|
|
14
|
+
.map(([k, v]) => `${k}=${JSON.stringify(v) ?? "null"}`)
|
|
15
|
+
.join(" ");
|
|
16
|
+
return redactCapability(`[live-preview] ${event}${kv ? ` ${kv}` : ""}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function liveLog(sink: LiveLogSink | undefined, event: string, fields?: Record<string, unknown>): void {
|
|
20
|
+
(sink ?? ((line) => console.log(line)))(formatLiveLog(event, fields));
|
|
21
|
+
}
|