@blackbelt-technology/pi-agent-dashboard 0.10.0 → 0.12.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 +4 -2
- package/README.md +64 -18
- package/docs/architecture.md +2325 -270
- package/package.json +36 -20
- package/packages/extension/package.json +18 -9
- package/packages/extension/src/bridge-context.ts +6 -0
- package/packages/extension/src/bridge-handlers/dispatch.ts +109 -70
- package/packages/extension/src/bridge-handlers/forwarders.ts +19 -42
- package/packages/extension/src/bridge-handlers/instance.ts +31 -13
- package/packages/extension/src/bridge-handlers/lifecycle.ts +149 -66
- package/packages/extension/src/bridge-shutdown.ts +44 -0
- 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 +309 -284
- package/packages/extension/src/canvas-tool.ts +2 -0
- package/packages/extension/src/command-handler.ts +106 -37
- package/packages/extension/src/connect-target.ts +23 -0
- package/packages/extension/src/connection.ts +153 -38
- package/packages/extension/src/credential-guard.ts +300 -0
- package/packages/extension/src/custom-entry-forward.ts +27 -18
- package/packages/extension/src/dashboard-http-origin.ts +88 -0
- package/packages/extension/src/default-model-apply.ts +83 -0
- package/packages/extension/src/dev-build.ts +11 -1
- package/packages/extension/src/flow-event-wiring.ts +19 -95
- package/packages/extension/src/followup-drain.ts +19 -5
- 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/path-gate/decide.ts +68 -5
- package/packages/extension/src/path-gate/handler.ts +16 -8
- package/packages/extension/src/path-gate/index.ts +2 -2
- package/packages/extension/src/path-gate/roots.ts +15 -3
- package/packages/extension/src/path-gate/suppression.ts +3 -0
- package/packages/extension/src/pi-env.d.ts +8 -1
- package/packages/extension/src/pi-retry-settings.ts +11 -2
- package/packages/extension/src/pinned-endpoint.ts +44 -0
- package/packages/extension/src/plugin-event-forward-registry.ts +90 -14
- package/packages/extension/src/preview-path-link.ts +151 -0
- package/packages/extension/src/project-init/scaffold.ts +98 -35
- package/packages/extension/src/project-trust-live.ts +143 -0
- package/packages/extension/src/project-trust.ts +178 -30
- package/packages/extension/src/provider-model-cache.ts +118 -0
- package/packages/extension/src/provider-register.ts +487 -46
- package/packages/extension/src/remote-registration-gate.ts +21 -21
- package/packages/extension/src/remote-upgrade-preparer.ts +104 -0
- package/packages/extension/src/retry-tracker.ts +90 -7
- package/packages/extension/src/role-manager.ts +4 -2
- package/packages/extension/src/role-model-tools.ts +68 -10
- package/packages/extension/src/server-auto-start.ts +69 -3
- package/packages/extension/src/server-launcher.ts +26 -8
- package/packages/extension/src/server-proof-probe.ts +117 -0
- package/packages/extension/src/session-sync.ts +53 -24
- package/packages/extension/src/spawn-new-session.ts +22 -0
- package/packages/extension/src/steer-buffer.ts +164 -0
- package/packages/extension/src/stream-forward-buffer.ts +64 -15
- package/packages/extension/src/sudo-askpass/config-reader.ts +32 -0
- package/packages/extension/src/sudo-askpass/controller.ts +405 -0
- package/packages/extension/src/sudo-askpass/helper-script.ts +69 -0
- package/packages/extension/src/sudo-askpass/in-flight.ts +79 -0
- package/packages/extension/src/sudo-askpass/index.ts +11 -0
- package/packages/extension/src/sudo-askpass/install.ts +143 -0
- package/packages/extension/src/sudo-askpass/password-cache.ts +118 -0
- package/packages/extension/src/sudo-askpass/prompt-text.ts +55 -0
- package/packages/extension/src/sudo-askpass/protocol.ts +18 -0
- package/packages/extension/src/sudo-askpass/sudo-mask-registry.ts +52 -0
- package/packages/extension/src/terminal-reload.ts +19 -5
- package/packages/extension/src/tool-update-filter-registry.ts +210 -0
- package/packages/extension/src/vault-delivery.ts +11 -1
- package/packages/extension/src/vault-redaction.ts +4 -3
- package/packages/server/package.json +17 -14
- package/packages/server/src/access/access-denials.ts +8 -0
- package/packages/server/src/access/access-grants.ts +303 -64
- package/packages/server/src/access/access-health.ts +10 -0
- package/packages/server/src/access/access-plane.ts +12 -1
- package/packages/server/src/access/containment-gate.ts +113 -30
- package/packages/server/src/access/cwd-verdict.ts +3 -3
- package/packages/server/src/access/denial-hold.ts +3 -0
- package/packages/server/src/access/grant-coordinator.ts +17 -1
- package/packages/server/src/access/named-grant.ts +3 -0
- package/packages/server/src/access/pending-grant-registry.ts +32 -5
- package/packages/server/src/access/pi-trust-live.ts +244 -0
- package/packages/server/src/access/pi-trust-mirror-service.ts +50 -11
- package/packages/server/src/access/planes.ts +107 -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/verified-read.ts +24 -7
- package/packages/server/src/access/yolo-session.ts +54 -2
- package/packages/server/src/acp-bridge/acp-session-adapter.ts +18 -5
- package/packages/server/src/acp-bridge/server-requests.ts +10 -4
- package/packages/server/src/agent-events/agent-event-hub.ts +158 -0
- package/packages/server/src/auth/audit-log.ts +108 -0
- package/packages/server/src/auth/auth-plugin.ts +230 -38
- package/packages/server/src/auth/auth.ts +104 -4
- package/packages/server/src/auth/bearer-auth.ts +52 -12
- package/packages/server/src/auth/bridge-ticket-eligibility.ts +53 -10
- package/packages/server/src/auth/cors-origin.ts +90 -30
- package/packages/server/src/auth/credential-envelope.ts +233 -0
- package/packages/server/src/auth/credential-key-worker.d.mts +16 -0
- package/packages/server/src/auth/credential-key-worker.mjs +108 -0
- package/packages/server/src/auth/credential-key.ts +479 -0
- package/packages/server/src/auth/credential-migration.ts +162 -0
- package/packages/server/src/auth/csp-report-paths.ts +26 -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 +145 -11
- 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/device-proof.ts +202 -0
- package/packages/server/src/auth/duplicate-cookie-gate.ts +271 -0
- package/packages/server/src/auth/host-admission.ts +7 -0
- package/packages/server/src/auth/host-gate.ts +13 -2
- 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 +98 -33
- package/packages/server/src/auth/locked-json-file.ts +68 -6
- package/packages/server/src/auth/models-json-store.ts +269 -0
- package/packages/server/src/auth/mutation-origin-gate.ts +5 -2
- package/packages/server/src/auth/nav-intent.ts +72 -0
- package/packages/server/src/auth/node-guard.ts +36 -49
- 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-credential-store.ts +81 -10
- 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 +214 -12
- package/packages/server/src/auth/provider-block-checks.ts +321 -0
- package/packages/server/src/auth/provider-host-detect.ts +151 -0
- package/packages/server/src/auth/provider-key-file.ts +71 -0
- package/packages/server/src/auth/provider-readiness.ts +91 -0
- 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/route-tier-gate.ts +3 -2
- 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/vertex-key-check.ts +140 -0
- package/packages/server/src/auth/ws-ticket.ts +40 -2
- package/packages/server/src/auth/ws-upgrade-reject-log.ts +4 -1
- package/packages/server/src/browse.ts +73 -20
- package/packages/server/src/browser-handlers/acp-guard.ts +1 -4
- package/packages/server/src/browser-handlers/directory-handler.ts +34 -24
- 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 +167 -63
- package/packages/server/src/browser-handlers/spawn-choice.ts +138 -1
- package/packages/server/src/browser-handlers/subscription-handler.ts +146 -3
- package/packages/server/src/browser-handlers/terminal-handler.ts +108 -10
- package/packages/server/src/build-plugin-send-prompt.ts +18 -0
- package/packages/server/src/canvas/canvas-accumulator.ts +35 -7
- package/packages/server/src/cli/credentials-command.ts +555 -0
- package/packages/server/src/cli.ts +229 -53
- package/packages/server/src/config-api.ts +53 -35
- package/packages/server/src/config-snapshot.ts +197 -1
- package/packages/server/src/directory-service.ts +193 -68
- package/packages/server/src/embed-lifecycle/idle-reaper.ts +13 -3
- package/packages/server/src/event-wiring.ts +322 -207
- package/packages/server/src/git-worktree/ensure-worktree.ts +185 -0
- package/packages/server/src/git-worktree/git-operations.ts +45 -11
- 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/repo-lock.ts +42 -0
- package/packages/server/src/git-worktree/start-worktree-init.ts +151 -0
- package/packages/server/src/git-worktree/worktree-init-trust.ts +43 -1
- package/packages/server/src/git-worktree/worktree-init.ts +5 -1
- package/packages/server/src/identity/http-road-classification.ts +18 -2
- package/packages/server/src/identity/identity-floor.ts +5 -2
- package/packages/server/src/identity/ws-message-scope.ts +11 -3
- package/packages/server/src/identity/ws-road-classification.ts +3 -1
- package/packages/server/src/known-server-moves.ts +36 -15
- package/packages/server/src/lib/browse-roots.ts +129 -17
- package/packages/server/src/lib/client-dist.ts +36 -21
- package/packages/server/src/lib/content-disposition.ts +20 -0
- package/packages/server/src/lib/dashboard-state-guard.ts +513 -0
- package/packages/server/src/lib/diagram-render.ts +4 -6
- package/packages/server/src/lib/eml.ts +5 -10
- package/packages/server/src/lib/file-cwd-admission.ts +145 -0
- package/packages/server/src/lib/frame-policy-probe.ts +308 -0
- package/packages/server/src/lib/frame-policy-test-seam.ts +99 -0
- package/packages/server/src/lib/frame-policy-verdict.ts +125 -0
- package/packages/server/src/lib/grep-worker.ts +23 -0
- package/packages/server/src/lib/grep.ts +96 -7
- package/packages/server/src/lib/index-html.ts +81 -0
- package/packages/server/src/lib/mention-fuzzy-index.ts +263 -0
- package/packages/server/src/lib/office-preview.ts +49 -13
- package/packages/server/src/lib/path-containment.ts +50 -10
- package/packages/server/src/lib/resolve-file-mention.ts +234 -40
- package/packages/server/src/lib/resolve-mentions-batch.ts +143 -0
- package/packages/server/src/lib/route-admission.ts +80 -0
- package/packages/server/src/lib/writable-handler-target.ts +4 -0
- package/packages/server/src/lib/writable-md-target.ts +26 -0
- package/packages/server/src/lifecycle/boot-parent-liveness.ts +10 -25
- package/packages/server/src/lifecycle/credential-exit-barrier.ts +207 -0
- package/packages/server/src/lifecycle/exit-signal.ts +43 -0
- package/packages/server/src/lifecycle/recovery-server.ts +65 -18
- package/packages/server/src/live-server/capability-redact.ts +31 -0
- package/packages/server/src/live-server/live-browser-gate.ts +22 -0
- package/packages/server/src/live-server/live-capability.ts +615 -44
- package/packages/server/src/live-server/live-csp-report.ts +63 -0
- package/packages/server/src/live-server/live-csp.ts +141 -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 +25 -0
- package/packages/server/src/live-server/live-origin-scan.ts +298 -0
- package/packages/server/src/live-server/live-server-manager.ts +220 -33
- package/packages/server/src/live-server/live-server-proxy.ts +411 -50
- package/packages/server/src/live-server/live-static.ts +231 -0
- package/packages/server/src/live-server/preview-dispatch.ts +286 -0
- package/packages/server/src/live-server/preview-observe.ts +53 -0
- package/packages/server/src/live-server/preview-origin.ts +182 -0
- package/packages/server/src/live-server/preview-path-bridge.ts +70 -0
- package/packages/server/src/live-server/preview-path-store.ts +420 -0
- package/packages/server/src/live-server/preview-path.ts +40 -0
- package/packages/server/src/live-server/preview-pool.ts +385 -0
- package/packages/server/src/live-server/preview-preflight.ts +41 -0
- package/packages/server/src/live-server/preview-reserve.ts +44 -0
- package/packages/server/src/live-server/preview-tier.ts +217 -0
- package/packages/server/src/live-server/preview-viewers.ts +54 -0
- package/packages/server/src/live-server/preview-ws-registry.ts +43 -0
- package/packages/server/src/model-proxy/custom-provider-discovery.ts +4 -2
- package/packages/server/src/model-proxy/internal-auth-storage.ts +5 -1
- package/packages/server/src/model-proxy/internal-registry.ts +283 -21
- package/packages/server/src/model-proxy/models-block-apply.ts +291 -0
- package/packages/server/src/model-proxy/pi-models-validator.ts +164 -0
- package/packages/server/src/model-proxy/registry-singleton.ts +44 -9
- package/packages/server/src/model-proxy/server-model-runtime.ts +17 -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/readiness.ts +1 -1
- 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 +202 -7
- package/packages/server/src/pairing/browser-gateway.ts +325 -51
- package/packages/server/src/pairing/device-key-intake.ts +120 -0
- package/packages/server/src/pairing/device-token-services.ts +26 -0
- package/packages/server/src/pairing/paired-devices.ts +702 -27
- package/packages/server/src/pairing/pairing.ts +103 -11
- package/packages/server/src/pairing/{subagent-resync-routing.ts → resync-requester-registry.ts} +6 -10
- package/packages/server/src/pairing/socket-locality.ts +26 -0
- package/packages/server/src/pairing/ws-tier-gate.ts +74 -0
- package/packages/server/src/pending/pending-attach-registry.ts +15 -0
- package/packages/server/src/pending/pending-initial-prompt-registry.ts +15 -0
- package/packages/server/src/persistence/memory-event-store.ts +251 -296
- package/packages/server/src/persistence/preferences-store.ts +61 -2
- package/packages/server/src/pi/bridge-channel.ts +33 -0
- package/packages/server/src/pi/bridge-contention.ts +84 -2
- 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 +29 -15
- package/packages/server/src/pi/pi-gateway.ts +398 -41
- package/packages/server/src/pi/provisional-registration.ts +4 -1
- package/packages/server/src/pi/resource-toggle-trust.ts +18 -7
- package/packages/server/src/pi-agent-settings.ts +95 -301
- package/packages/server/src/pi-default-remirror.ts +89 -0
- package/packages/server/src/plugin-append-event.ts +35 -0
- package/packages/server/src/plugin-discovery-wiring.ts +142 -0
- package/packages/server/src/plugin-entry-stats.ts +61 -0
- package/packages/server/src/plugin-event-seam.ts +116 -4
- package/packages/server/src/plugin-local-request.ts +207 -0
- package/packages/server/src/plugin-ownership.ts +111 -0
- package/packages/server/src/plugin-policy-registry.ts +297 -0
- package/packages/server/src/plugin-request-lane.ts +92 -16
- package/packages/server/src/plugin-runtime-clients.ts +89 -0
- package/packages/server/src/preferences-relay.ts +20 -0
- package/packages/server/src/project-seed.ts +347 -0
- package/packages/server/src/push/push-transports/webhook-url.ts +8 -5
- package/packages/server/src/questions/question-store.ts +3 -2
- package/packages/server/src/routes/access-routes.ts +182 -15
- package/packages/server/src/routes/acme-routes.ts +14 -4
- 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 +640 -279
- package/packages/server/src/routes/file-write-binary-routes.ts +97 -24
- package/packages/server/src/routes/folder-project-routes.ts +18 -12
- package/packages/server/src/routes/frame-policy-routes.ts +215 -0
- package/packages/server/src/routes/git-routes.ts +340 -273
- package/packages/server/src/routes/grep-routes.ts +64 -14
- package/packages/server/src/routes/known-servers-routes.ts +189 -91
- package/packages/server/src/routes/live-server-routes.ts +572 -15
- package/packages/server/src/routes/local-proof-routes.ts +14 -0
- package/packages/server/src/routes/models-introspection-routes.ts +1 -1
- 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 +396 -31
- package/packages/server/src/routes/package-routes.ts +58 -10
- package/packages/server/src/routes/pairing-routes.ts +342 -17
- package/packages/server/src/routes/pi-settings-routes.ts +2 -0
- 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/preferences-auto-name-routes.ts +5 -2
- package/packages/server/src/routes/preferences-claude-compat-routes.ts +49 -0
- package/packages/server/src/routes/provider-auth-routes.ts +181 -10
- package/packages/server/src/routes/provider-override-routes.ts +158 -0
- package/packages/server/src/routes/provider-routes.ts +348 -86
- package/packages/server/src/routes/resource-activation-routes.ts +18 -4
- package/packages/server/src/routes/session-routes.ts +75 -25
- package/packages/server/src/routes/system-routes.ts +169 -52
- package/packages/server/src/routes/ui-link-base.ts +88 -0
- package/packages/server/src/rpc-keeper/keeper-manager.ts +196 -25
- package/packages/server/src/rpc-keeper/keeper.cjs +37 -23
- package/packages/server/src/runtime-overlay/extension-reload.ts +16 -10
- package/packages/server/src/runtime-overlay/runtime-io.ts +2 -9
- package/packages/server/src/runtime-overlay/runtime-stager.ts +4 -2
- package/packages/server/src/server.ts +1139 -291
- package/packages/server/src/services/cli-service.ts +59 -10
- package/packages/server/src/services/secrets-resolver.ts +46 -30
- package/packages/server/src/session/custom-event-group-annotation.ts +3 -3
- package/packages/server/src/session/custom-event-group-resolver.ts +10 -5
- package/packages/server/src/session/death-reason.ts +11 -7
- package/packages/server/src/session/event-status-extraction.ts +5 -1
- package/packages/server/src/session/hydration-state.ts +148 -0
- package/packages/server/src/session/memory-session-manager.ts +31 -2
- package/packages/server/src/session/open-tool-calls.ts +81 -47
- package/packages/server/src/session/pid-target.ts +127 -0
- package/packages/server/src/session/replay-compaction.ts +18 -5
- 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/seed-discovered-history.ts +85 -0
- package/packages/server/src/session/server-pid-context.ts +14 -0
- package/packages/server/src/session/session-api.ts +78 -29
- package/packages/server/src/session/session-archive.ts +7 -0
- package/packages/server/src/session/session-bootstrap.ts +9 -54
- package/packages/server/src/session/session-load-worker-pool.ts +11 -1
- package/packages/server/src/session/session-load-worker.ts +32 -4
- package/packages/server/src/spawn-process/abort-spawned-run.ts +28 -9
- package/packages/server/src/spawn-process/acp-spawn.ts +8 -2
- package/packages/server/src/spawn-process/headless-pid-registry.ts +393 -77
- package/packages/server/src/spawn-process/link-register.ts +42 -0
- package/packages/server/src/spawn-process/plugin-spawn-choice.ts +48 -0
- package/packages/server/src/spawn-process/plugin-spawn-worktree.ts +290 -0
- package/packages/server/src/spawn-process/process-classifier.ts +8 -3
- package/packages/server/src/spawn-process/process-manager.ts +61 -6
- package/packages/server/src/spawn-process/restored-identity.ts +112 -0
- package/packages/server/src/spawn-process/session-terminator.ts +179 -18
- package/packages/server/src/spawn-process/spawn-env-contributors.ts +2 -1
- package/packages/server/src/spawn-process/spawn-failure-log.ts +9 -0
- package/packages/server/src/spawn-process/spawn-register-watchdog.ts +44 -19
- package/packages/server/src/sudo-credential/relay.ts +337 -0
- package/packages/server/src/terminal/terminal-gateway.ts +10 -0
- package/packages/server/src/terminal/terminal-manager.ts +54 -46
- package/packages/server/src/tls/sources/acme/credentials.ts +3 -2
- package/packages/server/src/tls/sources/acme/runtime.ts +2 -1
- 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 +172 -26
- package/packages/server/src/vault/vault-routes.ts +9 -2
- package/packages/server/src/vault/vault-store.ts +87 -24
- package/packages/server/src/watch-files-handler.ts +66 -0
- package/packages/server/vendor/xlsx/LICENSE +201 -0
- package/packages/server/vendor/xlsx/PROVENANCE.json +13 -0
- package/packages/server/vendor/xlsx/xlsx.d.mts +1065 -0
- package/packages/server/vendor/xlsx/xlsx.mjs +28228 -0
- package/packages/shared/package.json +4 -2
- package/packages/shared/src/agent-credential-files.ts +32 -0
- package/packages/shared/src/browser-protocol.ts +285 -59
- package/packages/shared/src/canvas-declare.ts +27 -0
- package/packages/shared/src/canvas-detect.ts +1 -1
- package/packages/shared/src/canvas-types.ts +2 -2
- package/packages/shared/src/config.ts +443 -187
- package/packages/shared/src/containment-guard.ts +45 -0
- package/packages/shared/src/credential-encryption.ts +87 -0
- package/packages/shared/src/custom-event-groups.ts +2 -0
- package/packages/shared/src/dashboard-plugin/claude-compat.ts +159 -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 +100 -1
- package/packages/shared/src/dashboard-plugin/plugin-status.ts +58 -3
- package/packages/shared/src/dashboard-plugin/replay-entry-types.ts +46 -0
- package/packages/shared/src/dashboard-plugin/slot-props.ts +47 -3
- package/packages/shared/src/dashboard-plugin/slot-types.ts +9 -1
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +39 -3
- package/packages/shared/src/device-key-proof.ts +309 -0
- package/packages/shared/src/display-prefs.ts +13 -0
- package/packages/shared/src/doctor-core.ts +94 -27
- package/packages/shared/src/event-forward-declaration.ts +85 -12
- package/packages/shared/src/forbidden-subjects.ts +126 -14
- package/packages/shared/src/heap-limits.ts +0 -30
- package/packages/shared/src/host-admission.ts +2 -1
- package/packages/shared/src/index.ts +7 -6
- package/packages/shared/src/live-server.ts +161 -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 +176 -0
- package/packages/shared/src/mdns-discovery.ts +25 -6
- package/packages/shared/src/models-json-reader.ts +41 -0
- package/packages/shared/src/nav-intent.ts +29 -0
- package/packages/shared/src/network-denial-signal.ts +15 -0
- package/packages/shared/src/node-version.ts +63 -60
- 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 +135 -11
- package/packages/shared/src/pi-agent-settings-lock.ts +311 -0
- package/packages/shared/src/pi-path-compare.ts +31 -0
- package/packages/shared/src/pi-settings-schema.ts +52 -9
- package/packages/shared/src/pi-trust-live-query.ts +93 -0
- package/packages/shared/src/pi-trust-mirror-snapshot.ts +21 -11
- package/packages/shared/src/pi-trust-mirror-types.ts +38 -3
- package/packages/shared/src/pi-trust-mirror.ts +47 -11
- package/packages/shared/src/pi-trust-resource-digest.ts +261 -0
- package/packages/shared/src/pid-namespace.ts +86 -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/native-ts-register.mjs +1 -1
- package/packages/shared/src/platform/process.ts +452 -5
- package/packages/shared/src/platform/resolve-pi-shell.ts +105 -0
- package/packages/shared/src/platform/select-git-source.ts +7 -0
- package/packages/shared/src/platform/spawn-runtime.ts +193 -7
- package/packages/shared/src/plugin-route-cwd.ts +95 -0
- package/packages/shared/src/preview-egress.ts +192 -0
- package/packages/shared/src/preview-origins.ts +398 -0
- package/packages/shared/src/project-root.ts +16 -0
- package/packages/shared/src/prompt-answer.ts +9 -5
- package/packages/shared/src/protocol.ts +187 -86
- package/packages/shared/src/provider-env-catalog.ts +413 -0
- package/packages/shared/src/provider-overrides.ts +112 -0
- package/packages/shared/src/question-types.ts +6 -0
- package/packages/shared/src/recommended-extensions.ts +97 -26
- package/packages/shared/src/recovery-timing.ts +27 -0
- package/packages/shared/src/remote-credentials.ts +63 -0
- package/packages/shared/src/renderer-by-ext.ts +5 -2
- package/packages/shared/src/rest-api.ts +148 -11
- package/packages/shared/src/role-schema.ts +24 -0
- package/packages/shared/src/route-tiers.ts +149 -4
- 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/compat.ts +70 -6
- 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/secret-patterns.ts +49 -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/source-matching.ts +2 -2
- package/packages/shared/src/state-replay.ts +40 -24
- package/packages/shared/src/sudo-credential.ts +71 -0
- package/packages/shared/src/tls-only-token.ts +16 -0
- package/packages/shared/src/tool-path-resolve.ts +16 -1
- package/packages/shared/src/tool-registry/definitions.ts +5 -5
- package/packages/shared/src/tool-registry/strategies.ts +13 -12
- package/packages/shared/src/tool-workdir.ts +56 -0
- package/packages/shared/src/trusted-types-mode.ts +72 -0
- package/packages/shared/src/types.ts +134 -109
- package/packages/shared/src/url-policy.ts +96 -0
- package/packages/shared/src/vault-delivery.ts +15 -0
- package/packages/shared/src/vertex-regions.ts +81 -0
- package/packages/shared/src/ws-message-tiers.ts +11 -3
- package/packages/extension/src/subagent-fanout-admission.ts +0 -234
- package/packages/extension/src/subagent-forward-sites.ts +0 -69
- package/packages/extension/src/subagent-frame-buffer.ts +0 -245
- package/packages/extension/src/subagent-frame-strip.ts +0 -109
- package/packages/extension/src/subagent-saturation.ts +0 -196
- package/packages/extension/src/subagent-tick-throttle.ts +0 -252
- package/packages/shared/src/anthropic-messages-source.ts +0 -8
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reserved `/__pi/csp-report` endpoint under a capability prefix: the sink the
|
|
3
|
+
* restricted-preview CSP's `report-uri` points at (never `report-to`: undeliverable on plain http).
|
|
4
|
+
*
|
|
5
|
+
* Capability-required (the proxy only dispatches a VALID capability here), `POST`
|
|
6
|
+
* only, both report media types, 64 KiB (413 above), rate limited per token (429,
|
|
7
|
+
* see `live-egress.ts`). Only `blocked-uri` is read, and the result is a `runtime`
|
|
8
|
+
* entry the page itself could have forged — never project-grantable. Nothing from
|
|
9
|
+
* the body (it carries the capability in `document-uri`) is logged.
|
|
10
|
+
*
|
|
11
|
+
* See change: improve-canvas-html-and-url-ux (design D6, task 2.8).
|
|
12
|
+
*/
|
|
13
|
+
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
14
|
+
import type { LiveCapabilityGrant } from "./live-capability.js";
|
|
15
|
+
import { CSP_REPORT_SUBPATH } from "./live-csp.js";
|
|
16
|
+
import { CSP_REPORT_MAX_BYTES, type PreviewEgress } from "./live-egress.js";
|
|
17
|
+
import { type LiveLogSink, liveLog } from "./live-log.js";
|
|
18
|
+
|
|
19
|
+
const REPORT_TYPES = ["application/csp-report", "application/reports+json"] as const;
|
|
20
|
+
|
|
21
|
+
/** Parse the two report media types as JSON (an unparseable body becomes `null`, never a throw). Idempotent. */
|
|
22
|
+
export function registerCspReportParsers(fastify: FastifyInstance): void {
|
|
23
|
+
for (const type of REPORT_TYPES) {
|
|
24
|
+
if (fastify.hasContentTypeParser(type)) continue;
|
|
25
|
+
fastify.addContentTypeParser(type, { parseAs: "buffer", bodyLimit: CSP_REPORT_MAX_BYTES }, (_req, body, done) => {
|
|
26
|
+
try {
|
|
27
|
+
done(null, JSON.parse((body as Buffer).toString("utf8")));
|
|
28
|
+
} catch {
|
|
29
|
+
done(null, null);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const notFound = (reply: FastifyReply) => reply.code(404).send({ error: "live-server target not found" });
|
|
36
|
+
|
|
37
|
+
export interface CspReportHandlerDeps {
|
|
38
|
+
egress: PreviewEgress;
|
|
39
|
+
/** The dashboard's own origin as this request reached it (reports of it are not egress). */
|
|
40
|
+
ownOriginsOf?: (request: FastifyRequest) => readonly string[];
|
|
41
|
+
log?: LiveLogSink;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The proxy's `reservedHandler`: serves `/__pi/csp-report`, answers 404 for the rest of `/__pi/*`. */
|
|
45
|
+
export function createReservedHandler(deps: CspReportHandlerDeps) {
|
|
46
|
+
return (request: FastifyRequest, reply: FastifyReply, ctx: { grant: LiveCapabilityGrant; subPath: string }) => {
|
|
47
|
+
const q = ctx.subPath.search(/[?#]/);
|
|
48
|
+
const pathname = q >= 0 ? ctx.subPath.slice(0, q) : ctx.subPath;
|
|
49
|
+
if (pathname !== CSP_REPORT_SUBPATH) return notFound(reply);
|
|
50
|
+
if (request.method !== "POST") return reply.code(405).header("allow", "POST").send({ error: "method not allowed" });
|
|
51
|
+
const declared = Number(request.headers["content-length"]);
|
|
52
|
+
if (Number.isFinite(declared) && declared > CSP_REPORT_MAX_BYTES) {
|
|
53
|
+
liveLog(deps.log, "csp-report refused", { reason: "too-large", tokenId: ctx.grant.tokenId });
|
|
54
|
+
return reply.code(413).send({ error: "report too large" });
|
|
55
|
+
}
|
|
56
|
+
const outcome = deps.egress.report(ctx.grant, request.body, deps.ownOriginsOf?.(request));
|
|
57
|
+
if (outcome === "rate-limited") {
|
|
58
|
+
liveLog(deps.log, "csp-report refused", { reason: "rate-limited", tokenId: ctx.grant.tokenId });
|
|
59
|
+
return reply.code(429).send({ error: "too many reports" });
|
|
60
|
+
}
|
|
61
|
+
return reply.code(204).send();
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content-Security-Policy builder for every capability response under
|
|
3
|
+
* `/live/<id>/~c/<token>/` (port AND static targets).
|
|
4
|
+
*
|
|
5
|
+
* Pure: the policy comes from the token (never from a request path, query or
|
|
6
|
+
* header), the prefix origin from the already host-admitted `Host`. The frame
|
|
7
|
+
* is sandboxed (opaque origin); this header makes the same isolation hold when
|
|
8
|
+
* the capability URL is opened directly. Restricted mode confines every fetch
|
|
9
|
+
* directive to the capability prefix plus the granted egress origins `<G>`;
|
|
10
|
+
* it never contains `'self'` (that would be the dashboard origin).
|
|
11
|
+
*
|
|
12
|
+
* See change: improve-canvas-html-and-url-ux (design D3, D4).
|
|
13
|
+
*/
|
|
14
|
+
import type { LivePolicy } from "./live-capability.js";
|
|
15
|
+
|
|
16
|
+
export interface LiveCspInput {
|
|
17
|
+
policy: LivePolicy;
|
|
18
|
+
/** Validated `Host` header value (host[:port]). */
|
|
19
|
+
host: string;
|
|
20
|
+
/** `http` or `https` — the scheme the dashboard is reached on. */
|
|
21
|
+
scheme: "http" | "https";
|
|
22
|
+
/** `/live/<id>/~c/<token>` (no trailing slash). */
|
|
23
|
+
prefix: string;
|
|
24
|
+
/** Granted egress origins `<G>`: project grants ∪ the viewer's allow-once set. */
|
|
25
|
+
granted?: readonly string[];
|
|
26
|
+
/**
|
|
27
|
+
* Origin-bound capability (tiers 1-4, D6): the exact capability origin (`http://<label>.localhost:8000`) and
|
|
28
|
+
* the exact dashboard origin(s) it was minted from. Sources come from the ENTRY, never from the request.
|
|
29
|
+
* Absent ⇒ legacy / tier 5 (path-scoped lists, `frame-ancestors 'self'`, opaque sandbox).
|
|
30
|
+
*/
|
|
31
|
+
origin?: string;
|
|
32
|
+
ancestors?: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface LiveCspResult {
|
|
36
|
+
/** `Content-Security-Policy` header value. */
|
|
37
|
+
csp: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A canonical https origin: no path/space/`;`/`,` can ride into the header. */
|
|
41
|
+
const SAFE_ORIGIN = /^https:\/\/[a-z0-9.-]+(?::\d{1,5})?$/;
|
|
42
|
+
/** A `Host` header value we are willing to embed in a header. */
|
|
43
|
+
const SAFE_HOST = /^(?:[a-z0-9.-]+|\[[0-9a-f:.]+\])(?::\d{1,5})?$/i;
|
|
44
|
+
/** A dashboard / capability origin we are willing to embed in `frame-ancestors` (http or https, never IPv6, no path). */
|
|
45
|
+
const SAFE_ANCESTOR = /^https?:\/\/[a-z0-9.-]+(?::\d{1,5})?$/i;
|
|
46
|
+
|
|
47
|
+
/** The report endpoint, relative to the capability prefix. */
|
|
48
|
+
export const CSP_REPORT_SUBPATH = "/__pi/csp-report";
|
|
49
|
+
|
|
50
|
+
function sandboxDirective(policy: LivePolicy, sameOrigin: boolean): string {
|
|
51
|
+
if (!policy.scripts) return "sandbox";
|
|
52
|
+
// Without `allow-same-origin` the header would force an opaque origin whatever the frame attribute says (P3).
|
|
53
|
+
const so = sameOrigin ? " allow-same-origin" : "";
|
|
54
|
+
return policy.egress === "open"
|
|
55
|
+
? `sandbox allow-scripts${so} allow-forms allow-popups allow-downloads`
|
|
56
|
+
: `sandbox allow-scripts${so} allow-forms`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** `frame-ancestors` of an origin-bound capability: the minting dashboard origin(s) plus the capability origin itself. */
|
|
60
|
+
function boundAncestors(origin: string, ancestors: readonly string[] | undefined): string | null {
|
|
61
|
+
if (!ancestors || ancestors.length === 0) return null;
|
|
62
|
+
const list = [...new Set([...ancestors, origin])];
|
|
63
|
+
if (!list.every((a) => SAFE_ANCESTOR.test(a))) return null;
|
|
64
|
+
return `frame-ancestors ${list.join(" ")}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Source bases of the restricted lists. Origin-bound: origin-root from the entry (never the request Host / scheme). Legacy / tier 5: path-scoped. */
|
|
68
|
+
function sourceBases(input: LiveCspInput): { p: string; pws: string; frameSrc: string; reportPath: string } {
|
|
69
|
+
const { host, scheme, prefix } = input;
|
|
70
|
+
if (input.origin !== undefined) {
|
|
71
|
+
const originUrl = new URL(input.origin); // validated by `boundAncestors` (SAFE_ANCESTOR) before this runs
|
|
72
|
+
return {
|
|
73
|
+
p: `${input.origin}/`,
|
|
74
|
+
pws: `${originUrl.protocol === "https:" ? "wss" : "ws"}://${originUrl.host}/`,
|
|
75
|
+
frameSrc: "frame-src 'self'",
|
|
76
|
+
reportPath: CSP_REPORT_SUBPATH,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const p = `${scheme}://${host}${prefix}/`;
|
|
80
|
+
return {
|
|
81
|
+
p,
|
|
82
|
+
pws: `${scheme === "https" ? "wss" : "ws"}://${host}${prefix}/`,
|
|
83
|
+
// Tier 5 (a `/preview/<pid>` prefix) frames its own base; legacy keeps `'none'`.
|
|
84
|
+
frameSrc: prefix.startsWith("/preview/") ? `frame-src ${p}` : "frame-src 'none'",
|
|
85
|
+
reportPath: `${prefix}${CSP_REPORT_SUBPATH}`,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Build the CSP for one capability response. Returns `null` when `host` is not a
|
|
91
|
+
* plain host[:port] (fail closed — the caller answers 404 rather than emit a
|
|
92
|
+
* header it cannot trust).
|
|
93
|
+
*/
|
|
94
|
+
export function buildLiveCsp(input: LiveCspInput): LiveCspResult | null {
|
|
95
|
+
const { policy, host } = input;
|
|
96
|
+
if (!SAFE_HOST.test(host)) return null;
|
|
97
|
+
const bound = input.origin !== undefined;
|
|
98
|
+
const sandbox = sandboxDirective(policy, bound);
|
|
99
|
+
// Tier 5 (`/preview/<pid>`, not origin-bound): a same-pid nested pane has an OPAQUE parent (the sandboxed frame), and
|
|
100
|
+
// `frame-ancestors 'self'` never matches an opaque ancestor (Chromium blocks it: T11 / F26), so tier 5 omits the directive —
|
|
101
|
+
// the sandbox CSP still forces the opaque origin and the pid is the capability. Legacy keeps `'self'`.
|
|
102
|
+
const tier5 = !bound && input.prefix.startsWith("/preview/");
|
|
103
|
+
let frameAncestors = tier5 ? "" : "frame-ancestors 'self'";
|
|
104
|
+
if (input.origin !== undefined) {
|
|
105
|
+
// Fail closed on a malformed origin / missing ancestors: a bad entry never yields a header (D6, X38).
|
|
106
|
+
if (!SAFE_ANCESTOR.test(input.origin)) return null;
|
|
107
|
+
const fa = boundAncestors(input.origin, input.ancestors);
|
|
108
|
+
if (!fa) return null;
|
|
109
|
+
frameAncestors = fa;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (policy.egress === "open") {
|
|
113
|
+
return { csp: [sandbox, "object-src 'none'", frameAncestors].filter(Boolean).join("; ") };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const { p, pws, frameSrc, reportPath } = sourceBases(input);
|
|
117
|
+
const g = [...new Set(input.granted ?? [])].filter((o) => SAFE_ORIGIN.test(o));
|
|
118
|
+
const gStr = g.length > 0 ? ` ${g.join(" ")}` : "";
|
|
119
|
+
const scriptKw = policy.scripts ? "'unsafe-inline' 'unsafe-eval'" : "'unsafe-inline'";
|
|
120
|
+
const s = `${p} ${scriptKw}${gStr}`;
|
|
121
|
+
const styleS = `${p} 'unsafe-inline'${gStr}`;
|
|
122
|
+
const directives = [
|
|
123
|
+
sandbox,
|
|
124
|
+
"default-src 'none'",
|
|
125
|
+
`script-src ${s}`,
|
|
126
|
+
`style-src ${styleS}`,
|
|
127
|
+
`img-src ${p} data: blob:${gStr}`,
|
|
128
|
+
`font-src ${p} data:${gStr}`,
|
|
129
|
+
`media-src ${p} blob:${gStr}`,
|
|
130
|
+
`connect-src ${p} ${pws}${gStr}`,
|
|
131
|
+
"form-action 'none'",
|
|
132
|
+
"base-uri 'none'",
|
|
133
|
+
frameSrc,
|
|
134
|
+
"object-src 'none'",
|
|
135
|
+
frameAncestors,
|
|
136
|
+
`worker-src ${p} blob:`,
|
|
137
|
+
"manifest-src 'none'",
|
|
138
|
+
`report-uri ${reportPath}`,
|
|
139
|
+
];
|
|
140
|
+
return { csp: directives.filter(Boolean).join("; ") };
|
|
141
|
+
}
|
|
@@ -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
|
+
}
|