@blackbelt-technology/pi-agent-dashboard 0.11.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 +2 -0
- package/README.md +17 -12
- package/docs/architecture.md +973 -224
- package/package.json +29 -18
- package/packages/extension/package.json +16 -9
- package/packages/extension/src/bridge-context.ts +6 -0
- package/packages/extension/src/bridge-handlers/dispatch.ts +54 -69
- package/packages/extension/src/bridge-handlers/forwarders.ts +19 -42
- package/packages/extension/src/bridge-handlers/instance.ts +21 -12
- package/packages/extension/src/bridge-handlers/lifecycle.ts +84 -40
- package/packages/extension/src/bridge-shutdown.ts +44 -0
- package/packages/extension/src/bridge.ts +272 -260
- package/packages/extension/src/command-handler.ts +26 -31
- package/packages/extension/src/connection.ts +123 -31
- package/packages/extension/src/credential-guard.ts +300 -0
- package/packages/extension/src/custom-entry-forward.ts +27 -18
- package/packages/extension/src/default-model-apply.ts +83 -0
- package/packages/extension/src/flow-event-wiring.ts +19 -95
- package/packages/extension/src/followup-drain.ts +19 -5
- 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/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.ts +24 -15
- package/packages/extension/src/provider-model-cache.ts +118 -0
- package/packages/extension/src/provider-register.ts +428 -28
- 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 +21 -4
- package/packages/extension/src/server-auto-start.ts +68 -2
- package/packages/extension/src/server-launcher.ts +26 -8
- package/packages/extension/src/session-sync.ts +11 -12
- 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-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 +244 -58
- package/packages/server/src/access/access-plane.ts +7 -0
- package/packages/server/src/access/containment-gate.ts +66 -21
- 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-mirror-service.ts +7 -1
- package/packages/server/src/access/planes.ts +1 -0
- package/packages/server/src/access/verified-read.ts +24 -7
- package/packages/server/src/acp-bridge/acp-session-adapter.ts +10 -2
- package/packages/server/src/acp-bridge/server-requests.ts +10 -4
- package/packages/server/src/agent-events/agent-event-hub.ts +1 -1
- package/packages/server/src/auth/audit-log.ts +28 -0
- package/packages/server/src/auth/auth-plugin.ts +188 -15
- package/packages/server/src/auth/auth.ts +80 -0
- package/packages/server/src/auth/bridge-ticket-eligibility.ts +53 -10
- package/packages/server/src/auth/cors-origin.ts +14 -1
- 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 +0 -5
- package/packages/server/src/auth/csp.ts +74 -36
- 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/localhost-guard.ts +50 -23
- 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/node-guard.ts +36 -49
- package/packages/server/src/auth/plugin-credential-store.ts +81 -10
- package/packages/server/src/auth/provider-auth-storage.ts +208 -11
- 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/route-tier-gate.ts +3 -2
- package/packages/server/src/auth/vertex-key-check.ts +140 -0
- package/packages/server/src/auth/ws-ticket.ts +1 -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 +11 -24
- package/packages/server/src/browser-handlers/session-action-handler.ts +158 -62
- package/packages/server/src/browser-handlers/spawn-choice.ts +138 -1
- package/packages/server/src/browser-handlers/subscription-handler.ts +46 -1
- package/packages/server/src/build-plugin-send-prompt.ts +18 -0
- package/packages/server/src/cli/credentials-command.ts +555 -0
- package/packages/server/src/cli.ts +21 -16
- package/packages/server/src/config-api.ts +34 -38
- package/packages/server/src/config-snapshot.ts +149 -0
- package/packages/server/src/directory-service.ts +33 -14
- package/packages/server/src/embed-lifecycle/idle-reaper.ts +13 -3
- package/packages/server/src/event-wiring.ts +181 -101
- package/packages/server/src/git-worktree/ensure-worktree.ts +185 -0
- package/packages/server/src/git-worktree/git-operations.ts +1 -1
- 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.ts +5 -1
- package/packages/server/src/identity/http-road-classification.ts +5 -1
- package/packages/server/src/identity/identity-floor.ts +5 -2
- package/packages/server/src/identity/ws-message-scope.ts +10 -3
- package/packages/server/src/identity/ws-road-classification.ts +2 -1
- package/packages/server/src/lib/browse-roots.ts +13 -2
- package/packages/server/src/lib/content-disposition.ts +20 -0
- package/packages/server/src/lib/dashboard-state-guard.ts +304 -21
- package/packages/server/src/lib/eml.ts +2 -2
- 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.ts +4 -1
- package/packages/server/src/lib/index-html.ts +2 -2
- package/packages/server/src/lib/mention-fuzzy-index.ts +263 -0
- package/packages/server/src/lib/office-preview.ts +47 -10
- package/packages/server/src/lib/path-containment.ts +45 -12
- package/packages/server/src/lib/resolve-file-mention.ts +230 -38
- package/packages/server/src/lib/resolve-mentions-batch.ts +143 -0
- package/packages/server/src/lib/writable-handler-target.ts +4 -0
- package/packages/server/src/lib/writable-md-target.ts +22 -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/live-server/capability-redact.ts +21 -5
- package/packages/server/src/live-server/live-capability.ts +416 -52
- package/packages/server/src/live-server/live-csp-report.ts +1 -1
- package/packages/server/src/live-server/live-csp.ts +64 -16
- package/packages/server/src/live-server/live-log.ts +5 -1
- package/packages/server/src/live-server/live-server-manager.ts +11 -0
- package/packages/server/src/live-server/live-server-proxy.ts +268 -52
- package/packages/server/src/live-server/live-static.ts +4 -1
- 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/readiness.ts +1 -1
- package/packages/server/src/package/provider-probe.ts +194 -5
- package/packages/server/src/pairing/browser-gateway.ts +140 -42
- package/packages/server/src/pairing/device-key-intake.ts +120 -0
- package/packages/server/src/pairing/paired-devices.ts +370 -8
- package/packages/server/src/pairing/pairing.ts +69 -7
- package/packages/server/src/pairing/{subagent-resync-routing.ts → resync-requester-registry.ts} +6 -10
- 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 +52 -2
- package/packages/server/src/pi/bridge-contention.ts +84 -2
- package/packages/server/src/pi/host-project-trust.ts +15 -9
- package/packages/server/src/pi/pi-gateway.ts +208 -21
- package/packages/server/src/pi/provisional-registration.ts +4 -1
- package/packages/server/src/pi/resource-toggle-trust.ts +17 -6
- 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-entry-stats.ts +61 -0
- 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/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 +23 -6
- package/packages/server/src/routes/acme-routes.ts +14 -4
- package/packages/server/src/routes/agent-event-stream-routes.ts +1 -1
- package/packages/server/src/routes/file-routes.ts +327 -86
- package/packages/server/src/routes/file-write-binary-routes.ts +20 -4
- package/packages/server/src/routes/frame-policy-routes.ts +215 -0
- package/packages/server/src/routes/git-routes.ts +29 -78
- package/packages/server/src/routes/grep-routes.ts +26 -0
- package/packages/server/src/routes/live-server-routes.ts +198 -4
- package/packages/server/src/routes/models-introspection-routes.ts +1 -1
- package/packages/server/src/routes/openspec-routes.ts +17 -3
- package/packages/server/src/routes/pairing-routes.ts +200 -11
- package/packages/server/src/routes/pi-settings-routes.ts +2 -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 +167 -9
- 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/session-routes.ts +25 -22
- package/packages/server/src/routes/system-routes.ts +87 -39
- 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-stager.ts +4 -2
- package/packages/server/src/server.ts +512 -175
- 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/memory-session-manager.ts +24 -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/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 +22 -3
- 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 +383 -72
- 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/tls/sources/acme/credentials.ts +3 -2
- package/packages/server/src/tls/sources/acme/runtime.ts +2 -1
- package/packages/server/src/vault/vault-delivery-hub.ts +17 -1
- package/packages/server/src/vault/vault-routes.ts +9 -2
- package/packages/server/src/vault/vault-store.ts +87 -24
- 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 +137 -57
- package/packages/shared/src/canvas-detect.ts +1 -1
- package/packages/shared/src/canvas-types.ts +2 -2
- package/packages/shared/src/config.ts +73 -136
- package/packages/shared/src/containment-guard.ts +14 -4
- 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/host-api.ts +1 -1
- package/packages/shared/src/dashboard-plugin/manifest-types.ts +46 -0
- package/packages/shared/src/dashboard-plugin/plugin-status.ts +4 -3
- package/packages/shared/src/dashboard-plugin/replay-entry-types.ts +46 -0
- package/packages/shared/src/dashboard-plugin/slot-props.ts +2 -0
- package/packages/shared/src/dashboard-plugin/slot-types.ts +1 -1
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +11 -3
- package/packages/shared/src/device-key-proof.ts +309 -0
- 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 +20 -0
- package/packages/shared/src/mcp/verb-tiers.generated.ts +1 -3
- package/packages/shared/src/models-json-reader.ts +41 -0
- package/packages/shared/src/network-denial-signal.ts +15 -0
- package/packages/shared/src/node-version.ts +63 -60
- package/packages/shared/src/pairing-knock-client.ts +92 -8
- 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-mirror-snapshot.ts +16 -4
- package/packages/shared/src/pi-trust-mirror-types.ts +2 -0
- package/packages/shared/src/pi-trust-mirror.ts +18 -8
- package/packages/shared/src/pi-trust-resource-digest.ts +18 -12
- package/packages/shared/src/pid-namespace.ts +86 -0
- package/packages/shared/src/platform/native-ts-register.mjs +1 -1
- package/packages/shared/src/platform/process.ts +404 -1
- package/packages/shared/src/platform/resolve-pi-shell.ts +38 -9
- package/packages/shared/src/platform/spawn-runtime.ts +193 -7
- package/packages/shared/src/preview-egress.ts +56 -2
- package/packages/shared/src/preview-origins.ts +398 -0
- package/packages/shared/src/prompt-answer.ts +9 -5
- package/packages/shared/src/protocol.ts +147 -85
- 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 +79 -26
- package/packages/shared/src/renderer-by-ext.ts +5 -2
- package/packages/shared/src/rest-api.ts +93 -3
- package/packages/shared/src/role-schema.ts +24 -0
- package/packages/shared/src/route-tiers.ts +94 -3
- package/packages/shared/src/runtime-overlay/compat.ts +70 -6
- package/packages/shared/src/secret-patterns.ts +49 -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/tool-path-resolve.ts +16 -1
- package/packages/shared/src/tool-registry/strategies.ts +1 -1
- 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 +68 -109
- package/packages/shared/src/vertex-regions.ts +81 -0
- package/packages/shared/src/ws-message-tiers.ts +10 -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
- package/packages/shared/src/dashboard-plugin/anthropic-peer-names.ts +0 -9
- package/packages/shared/src/dashboard-plugin/subagent-types.ts +0 -219
|
@@ -23,28 +23,67 @@ export interface LiveCspInput {
|
|
|
23
23
|
prefix: string;
|
|
24
24
|
/** Granted egress origins `<G>`: project grants ∪ the viewer's allow-once set. */
|
|
25
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[];
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
export interface LiveCspResult {
|
|
29
36
|
/** `Content-Security-Policy` header value. */
|
|
30
37
|
csp: string;
|
|
31
|
-
/** `Reporting-Endpoints` header value, restricted mode only. */
|
|
32
|
-
reportingEndpoints?: string;
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
/** A canonical https origin: no path/space/`;`/`,` can ride into the header. */
|
|
36
41
|
const SAFE_ORIGIN = /^https:\/\/[a-z0-9.-]+(?::\d{1,5})?$/;
|
|
37
42
|
/** A `Host` header value we are willing to embed in a header. */
|
|
38
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;
|
|
39
46
|
|
|
40
47
|
/** The report endpoint, relative to the capability prefix. */
|
|
41
48
|
export const CSP_REPORT_SUBPATH = "/__pi/csp-report";
|
|
42
49
|
|
|
43
|
-
function sandboxDirective(policy: LivePolicy): string {
|
|
50
|
+
function sandboxDirective(policy: LivePolicy, sameOrigin: boolean): string {
|
|
44
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" : "";
|
|
45
54
|
return policy.egress === "open"
|
|
46
|
-
?
|
|
47
|
-
:
|
|
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
|
+
};
|
|
48
87
|
}
|
|
49
88
|
|
|
50
89
|
/**
|
|
@@ -53,23 +92,33 @@ function sandboxDirective(policy: LivePolicy): string {
|
|
|
53
92
|
* header it cannot trust).
|
|
54
93
|
*/
|
|
55
94
|
export function buildLiveCsp(input: LiveCspInput): LiveCspResult | null {
|
|
56
|
-
const { policy, host
|
|
95
|
+
const { policy, host } = input;
|
|
57
96
|
if (!SAFE_HOST.test(host)) return null;
|
|
58
|
-
const
|
|
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
|
+
}
|
|
59
111
|
|
|
60
112
|
if (policy.egress === "open") {
|
|
61
|
-
return { csp:
|
|
113
|
+
return { csp: [sandbox, "object-src 'none'", frameAncestors].filter(Boolean).join("; ") };
|
|
62
114
|
}
|
|
63
115
|
|
|
64
|
-
const
|
|
65
|
-
const p = `${scheme}://${host}${prefix}/`;
|
|
66
|
-
const pws = `${wsScheme}://${host}${prefix}/`;
|
|
116
|
+
const { p, pws, frameSrc, reportPath } = sourceBases(input);
|
|
67
117
|
const g = [...new Set(input.granted ?? [])].filter((o) => SAFE_ORIGIN.test(o));
|
|
68
118
|
const gStr = g.length > 0 ? ` ${g.join(" ")}` : "";
|
|
69
119
|
const scriptKw = policy.scripts ? "'unsafe-inline' 'unsafe-eval'" : "'unsafe-inline'";
|
|
70
120
|
const s = `${p} ${scriptKw}${gStr}`;
|
|
71
121
|
const styleS = `${p} 'unsafe-inline'${gStr}`;
|
|
72
|
-
const reportPath = `${prefix}${CSP_REPORT_SUBPATH}`;
|
|
73
122
|
const directives = [
|
|
74
123
|
sandbox,
|
|
75
124
|
"default-src 'none'",
|
|
@@ -81,13 +130,12 @@ export function buildLiveCsp(input: LiveCspInput): LiveCspResult | null {
|
|
|
81
130
|
`connect-src ${p} ${pws}${gStr}`,
|
|
82
131
|
"form-action 'none'",
|
|
83
132
|
"base-uri 'none'",
|
|
84
|
-
|
|
133
|
+
frameSrc,
|
|
85
134
|
"object-src 'none'",
|
|
86
|
-
|
|
135
|
+
frameAncestors,
|
|
87
136
|
`worker-src ${p} blob:`,
|
|
88
137
|
"manifest-src 'none'",
|
|
89
138
|
`report-uri ${reportPath}`,
|
|
90
|
-
"report-to csp",
|
|
91
139
|
];
|
|
92
|
-
return { csp: directives.join("; ")
|
|
140
|
+
return { csp: directives.filter(Boolean).join("; ") };
|
|
93
141
|
}
|
|
@@ -9,9 +9,13 @@ import { redactCapability } from "./capability-redact.js";
|
|
|
9
9
|
|
|
10
10
|
export type LiveLogSink = (line: string) => void;
|
|
11
11
|
|
|
12
|
+
/** A plain token (ids, hashes, tiers, ports, reasons) prints bare; anything else is JSON-encoded so no field can forge a second line. */
|
|
13
|
+
const BARE = /^[A-Za-z0-9._:-]{1,64}$/;
|
|
14
|
+
const render = (v: unknown): string => (typeof v === "string" && BARE.test(v) ? v : (JSON.stringify(v) ?? "null"));
|
|
15
|
+
|
|
12
16
|
function formatLiveLog(event: string, fields: Record<string, unknown> = {}): string {
|
|
13
17
|
const kv = Object.entries(fields)
|
|
14
|
-
.map(([k, v]) => `${k}=${
|
|
18
|
+
.map(([k, v]) => `${k}=${render(v)}`)
|
|
15
19
|
.join(" ");
|
|
16
20
|
return redactCapability(`[live-preview] ${event}${kv ? ` ${kv}` : ""}`);
|
|
17
21
|
}
|
|
@@ -57,6 +57,10 @@ export interface LiveServerManager {
|
|
|
57
57
|
presetSessionOf(id: string): string | undefined;
|
|
58
58
|
/** Is `id` a live target of either kind? */
|
|
59
59
|
has(id: string): boolean;
|
|
60
|
+
/** The registered loopback PORT target for `port`, if any (reservation lifetime / live-pid lookup, D9). */
|
|
61
|
+
getByPort(port: number): LiveServerTarget | undefined;
|
|
62
|
+
/** Why `127.0.0.1:port` may not be a preview target (the SSRF gate + never an `ownPorts` port), or `null`. */
|
|
63
|
+
portRefusal(port: number): string | null;
|
|
60
64
|
/** Every port target (persisted and preset), each with `addedBy`. Static targets are not listed. */
|
|
61
65
|
list(): LiveServerTarget[];
|
|
62
66
|
remove(id: string): void;
|
|
@@ -235,6 +239,13 @@ export function createLiveServerManager(
|
|
|
235
239
|
getStatic(id) {
|
|
236
240
|
return statics.get(id)?.target;
|
|
237
241
|
},
|
|
242
|
+
getByPort(port) {
|
|
243
|
+
return [...persisted.values(), ...[...presets.values()].map((p) => p.target)].find((t) => t.port === port);
|
|
244
|
+
},
|
|
245
|
+
portRefusal(port) {
|
|
246
|
+
const v = validateLiveTarget({ host: "127.0.0.1", port });
|
|
247
|
+
return v.ok ? refusesOwnPort(v.port) : v.error;
|
|
248
|
+
},
|
|
238
249
|
has(id) {
|
|
239
250
|
return persisted.has(id) || presets.has(id) || statics.has(id);
|
|
240
251
|
},
|
|
@@ -20,6 +20,7 @@ import { buildLiveCsp, type LiveCspResult } from "./live-csp.js";
|
|
|
20
20
|
import { registerCspReportParsers } from "./live-csp-report.js";
|
|
21
21
|
import type { LiveServerManager } from "./live-server-manager.js";
|
|
22
22
|
import { serveStatic } from "./live-static.js";
|
|
23
|
+
import { parsePreviewPath } from "./preview-path.js";
|
|
23
24
|
|
|
24
25
|
/** Dashboard cookie namespace (`pi_dash_token`, `pi_dash_device`, `pi_dash_local`, …). */
|
|
25
26
|
const DASHBOARD_COOKIE_PREFIX = "pi_dash_";
|
|
@@ -81,6 +82,62 @@ function stripDashboardCredentials(
|
|
|
81
82
|
return headers;
|
|
82
83
|
}
|
|
83
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Tier 5: the upstream already serves under `prefix`, so a redirect inside it is left as is (never prefixed twice);
|
|
87
|
+
* any other root-absolute redirect, or an absolute one back to the same upstream, is rewritten into the prefix.
|
|
88
|
+
*/
|
|
89
|
+
function rewriteFullPathLocation(location: string, prefix: string, host: string, port: number): string {
|
|
90
|
+
const intoPrefix = (local: string): string => {
|
|
91
|
+
if (staysInside(local, prefix)) return local;
|
|
92
|
+
const prefixed = `${prefix}${local}`;
|
|
93
|
+
return staysInside(prefixed, prefix) ? prefixed : `${prefix}/`;
|
|
94
|
+
};
|
|
95
|
+
if (location.startsWith("/")) return intoPrefix(location);
|
|
96
|
+
try {
|
|
97
|
+
const u = new URL(location);
|
|
98
|
+
if (u.hostname === host && u.port === String(port)) return intoPrefix(`${u.pathname}${u.search}`);
|
|
99
|
+
} catch {
|
|
100
|
+
/* not an absolute URL — leave as-is */
|
|
101
|
+
}
|
|
102
|
+
return location;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Merge `Origin` into `Vary` (once): the answer of a tier-5 response depends on the request `Origin`. */
|
|
106
|
+
function mergeVaryOrigin(headers: Record<string, unknown>): void {
|
|
107
|
+
const vary = headers.vary;
|
|
108
|
+
const list = (Array.isArray(vary) ? vary.join(",") : typeof vary === "string" ? vary : "")
|
|
109
|
+
.split(",")
|
|
110
|
+
.map((v) => v.trim())
|
|
111
|
+
.filter(Boolean);
|
|
112
|
+
if (!list.some((v) => v.toLowerCase() === "origin" || v === "*")) list.push("Origin");
|
|
113
|
+
headers.vary = list.join(", ");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Tier 5 CORS (P6): only on a VALID `/preview/<pid>/` response to `Origin: null`. Overwrites whatever the upstream said
|
|
118
|
+
* (a single `ACAO: null`, never credentials), and merges `Origin` into `Vary` without duplicating it.
|
|
119
|
+
*/
|
|
120
|
+
function applyNullCors(headers: Record<string, unknown>): void {
|
|
121
|
+
for (const k of Object.keys(headers)) if (k.toLowerCase().startsWith("access-control-")) delete headers[k];
|
|
122
|
+
headers["access-control-allow-origin"] = "null";
|
|
123
|
+
mergeVaryOrigin(headers);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Tiers 1-5 carry a secret in the URL (label / pid), so `Referrer-Policy: no-referrer` REPLACES any upstream value
|
|
128
|
+
* there (design D3); the legacy path keeps "only when the app set none".
|
|
129
|
+
*/
|
|
130
|
+
function applyReferrerPolicy(headers: Record<string, unknown>, grant: LiveCapabilityGrant | undefined, capabilityPrefix: string | undefined): void {
|
|
131
|
+
if (grant && grant.tier !== "legacy") headers["referrer-policy"] = "no-referrer";
|
|
132
|
+
else if (capabilityPrefix && headers["referrer-policy"] === undefined) headers["referrer-policy"] = "no-referrer";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Tier-5 CORS: `ACAO: null` for an opaque-origin request; `Vary: Origin` is present even where no ACAO is granted. */
|
|
136
|
+
function applyTier5Cors(headers: Record<string, unknown>, requestOrigin: string | undefined): void {
|
|
137
|
+
if (requestOrigin === "null") applyNullCors(headers);
|
|
138
|
+
else mergeVaryOrigin(headers);
|
|
139
|
+
}
|
|
140
|
+
|
|
84
141
|
/**
|
|
85
142
|
* Keep an upstream redirect inside the proxied prefix (`/live/<id>` or the
|
|
86
143
|
* capability prefix). A relative or root-relative `Location` is prefixed; an
|
|
@@ -88,7 +145,8 @@ function stripDashboardCredentials(
|
|
|
88
145
|
* rewritten into the prefix so the browser stays on the dashboard origin and the
|
|
89
146
|
* single-port tunnel keeps working. Foreign absolute redirects are untouched.
|
|
90
147
|
*/
|
|
91
|
-
function rewriteLocation(location: string, prefix: string, host: string, port: number): string {
|
|
148
|
+
function rewriteLocation(location: string, prefix: string, host: string, port: number, fullPath = false): string {
|
|
149
|
+
if (fullPath) return rewriteFullPathLocation(location, prefix, host, port);
|
|
92
150
|
const relative = location.startsWith("./") ? `${prefix}/${location.slice(2)}` : location.startsWith("/") ? `${prefix}${location}` : null;
|
|
93
151
|
if (relative !== null) return staysInside(relative, prefix) ? relative : `${prefix}/`;
|
|
94
152
|
try {
|
|
@@ -118,6 +176,12 @@ function staysInside(path: string, prefix: string): boolean {
|
|
|
118
176
|
}
|
|
119
177
|
|
|
120
178
|
export interface LiveProxyDeps {
|
|
179
|
+
/**
|
|
180
|
+
* Is this absolute URL an origin the dashboard app itself is served on (host-admitted, dashboard port, not preview-shaped)?
|
|
181
|
+
* Tiers 1-4 never deliver an upstream `Location` / `Refresh` aimed at it (D3(c)). Absent ⇒ no containment.
|
|
182
|
+
* See change: serve-live-previews-on-capability-origin (D3(c), T.F40).
|
|
183
|
+
*/
|
|
184
|
+
isDashboardUrl?: (url: URL) => boolean;
|
|
121
185
|
/**
|
|
122
186
|
* Does this token verify as a dashboard device bearer on THIS request's channel? Absent ⇒ never
|
|
123
187
|
* strip `Authorization`. Receives the request so the registry can record a non-proven channel.
|
|
@@ -147,6 +211,11 @@ export interface LiveProxyDeps {
|
|
|
147
211
|
reply: FastifyReply,
|
|
148
212
|
ctx: { grant: LiveCapabilityGrant; subPath: string },
|
|
149
213
|
) => Promise<FastifyReply> | FastifyReply;
|
|
214
|
+
/**
|
|
215
|
+
* Tier 5: a `/preview/<pid>/` response is now in flight. The pid's drop destroys it, so a revoked preview cannot keep an
|
|
216
|
+
* HTTP stream (SSE, long download) open to its dev server (test-plan #P11).
|
|
217
|
+
*/
|
|
218
|
+
trackPreviewResponse?: (pid: string, res: import("node:http").ServerResponse) => void;
|
|
150
219
|
}
|
|
151
220
|
|
|
152
221
|
/** `/__pi` as a whole path segment (the reserved namespace under a capability prefix). */
|
|
@@ -160,7 +229,7 @@ const isReservedPath = (subPath: string): boolean => {
|
|
|
160
229
|
const schemeOf = (request: FastifyRequest): "http" | "https" => (request.protocol === "https" ? "https" : "http");
|
|
161
230
|
|
|
162
231
|
/**
|
|
163
|
-
* APPEND our CSP
|
|
232
|
+
* APPEND our CSP to the upstream's response headers —
|
|
164
233
|
* never replacing or stripping the upstream's: browsers enforce every header, so the
|
|
165
234
|
* result is the stricter intersection.
|
|
166
235
|
*/
|
|
@@ -168,10 +237,6 @@ function appendCsp(headers: Record<string, unknown>, csp: LiveCspResult): void {
|
|
|
168
237
|
const upstreamCsp = headers["content-security-policy"];
|
|
169
238
|
const existing = upstreamCsp === undefined ? [] : Array.isArray(upstreamCsp) ? upstreamCsp : [upstreamCsp];
|
|
170
239
|
headers["content-security-policy"] = [...existing, csp.csp];
|
|
171
|
-
if (!csp.reportingEndpoints) return;
|
|
172
|
-
const re = headers["reporting-endpoints"];
|
|
173
|
-
headers["reporting-endpoints"] =
|
|
174
|
-
typeof re === "string" && re.length > 0 ? `${re}, ${csp.reportingEndpoints}` : csp.reportingEndpoints;
|
|
175
240
|
}
|
|
176
241
|
|
|
177
242
|
/** localhost/::1 resolve to 127.0.0.1 for the upstream TCP connect. */
|
|
@@ -179,36 +244,81 @@ function upstreamHost(host: string): string {
|
|
|
179
244
|
return host === "::1" ? "::1" : "127.0.0.1";
|
|
180
245
|
}
|
|
181
246
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
247
|
+
/**
|
|
248
|
+
* A preview-origin request reaches the upstream as if the browser had been talking to it directly:
|
|
249
|
+
* `Origin` (except the opaque `null`) and a same-origin `Referer` are rewritten to the upstream origin.
|
|
250
|
+
*/
|
|
251
|
+
function rewritePreviewOrigin(headers: HeaderBag, upstreamOrigin: string, previewHost: string | undefined): void {
|
|
252
|
+
if (typeof headers.origin === "string" && headers.origin !== "null") headers.origin = upstreamOrigin;
|
|
253
|
+
if (typeof headers.referer === "string") {
|
|
254
|
+
try {
|
|
255
|
+
const r = new URL(headers.referer);
|
|
256
|
+
if (r.host === previewHost) headers.referer = `${upstreamOrigin}${r.pathname}${r.search}`;
|
|
257
|
+
} catch {
|
|
258
|
+
delete headers.referer;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** `url=<target>` of a `Refresh` header value (`0; url=http://x/`, `5;URL='x'`); `null` when it names none. */
|
|
264
|
+
function refreshTarget(value: string): string | null {
|
|
265
|
+
const m = /;\s*url\s*=\s*(?:"([^"]*)"|'([^']*)'|([^;]*))/i.exec(value);
|
|
266
|
+
const raw = (m?.[1] ?? m?.[2] ?? m?.[3] ?? "").trim();
|
|
267
|
+
return raw === "" ? null : raw;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** The absolute http(s) URL `value` names, or `null` for a relative / non-http / malformed one (those resolve on the preview origin). */
|
|
271
|
+
function absoluteHttpUrl(value: string): URL | null {
|
|
272
|
+
try {
|
|
273
|
+
const u = new URL(value);
|
|
274
|
+
return u.protocol === "http:" || u.protocol === "https:" ? u : null;
|
|
275
|
+
} catch {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
198
279
|
|
|
280
|
+
/**
|
|
281
|
+
* D3(c), tiers 1-4: an upstream must not steer the frame onto the dashboard origin by header. A `Refresh` whose
|
|
282
|
+
* target is a dashboard origin is dropped; such a `Location` is rewritten onto the preview origin (same path + query).
|
|
283
|
+
* JS and `<meta refresh>` navigation are not header-contained: the client guard (D6/D7) handles them.
|
|
284
|
+
*/
|
|
285
|
+
export function containDashboardRedirects(headers: Record<string, unknown>, previewOrigin: string, isDashboardUrl: (u: URL) => boolean): void {
|
|
286
|
+
const dashboard = (v: string): URL | null => {
|
|
287
|
+
const u = absoluteHttpUrl(v);
|
|
288
|
+
return u && u.origin !== previewOrigin && isDashboardUrl(u) ? u : null;
|
|
289
|
+
};
|
|
290
|
+
if (typeof headers.refresh === "string") {
|
|
291
|
+
const target = refreshTarget(headers.refresh);
|
|
292
|
+
if (target !== null && dashboard(target)) delete headers.refresh;
|
|
293
|
+
}
|
|
294
|
+
if (typeof headers.location === "string") {
|
|
295
|
+
const u = dashboard(headers.location);
|
|
296
|
+
if (u) headers.location = `${previewOrigin}${u.pathname}${u.search}${u.hash}`;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Options of a forwarded port request that differ between `/live/<id>` and a preview origin. */
|
|
301
|
+
export interface ForwardPortOptions {
|
|
302
|
+
/** Preview origin: the browser's `Origin`/`Referer` carry the preview origin, which the upstream must see as itself. */
|
|
303
|
+
rewriteOrigin?: boolean;
|
|
199
304
|
/**
|
|
200
|
-
* `
|
|
201
|
-
*
|
|
202
|
-
*
|
|
305
|
+
* Tier 5: `subPath` is relative to `capabilityPrefix` (reserved-path / CSP decisions) but the upstream receives the
|
|
306
|
+
* FULL `<prefix><subPath>` — the site was started under that base (D8) — and a `Location` already inside the
|
|
307
|
+
* prefix is left alone instead of being prefixed again.
|
|
203
308
|
*/
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
309
|
+
fullPath?: boolean;
|
|
310
|
+
/** Tier 5: a valid response to an opaque `Origin: null` gets `Access-Control-Allow-Origin: null` (no credentials). */
|
|
311
|
+
nullCors?: boolean;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* The target-forwarding core shared by the main-origin `/live/:id/*` proxy and the preview-origin
|
|
316
|
+
* `previewApp` (D3): one CSP/cookie/credential/redirect policy, two entry points. The caller's
|
|
317
|
+
* Fastify instance must have `@fastify/reply-from` registered.
|
|
318
|
+
*/
|
|
319
|
+
export function createLiveForwarder(manager: LiveServerManager, deps: LiveProxyDeps = {}) {
|
|
320
|
+
const isDeviceBearer = deps.isDeviceBearer ?? (() => false);
|
|
321
|
+
const isKnownBearer = deps.isKnownBearer ?? (() => false);
|
|
212
322
|
|
|
213
323
|
/** CSP for a capability response, or `null` (fail closed) when no safe header can be built. */
|
|
214
324
|
function cspFor(request: FastifyRequest, grant: LiveCapabilityGrant, prefix: string): LiveCspResult | null {
|
|
@@ -220,7 +330,17 @@ export function registerLiveServerProxy(
|
|
|
220
330
|
} catch {
|
|
221
331
|
granted = []; // a failing grant lookup must not widen the policy
|
|
222
332
|
}
|
|
223
|
-
|
|
333
|
+
// Tiers 1-4 are origin-bound: the policy comes from the entry, not from the request Host / scheme (D6).
|
|
334
|
+
const bound = grant.tier !== "legacy" && grant.tier !== 5;
|
|
335
|
+
if (bound && grant.origin === undefined) return null; // an origin-bound entry without its origin: fail closed
|
|
336
|
+
return buildLiveCsp({
|
|
337
|
+
policy: grant.policy,
|
|
338
|
+
host,
|
|
339
|
+
scheme: schemeOf(request),
|
|
340
|
+
prefix,
|
|
341
|
+
granted,
|
|
342
|
+
...(bound ? { origin: grant.origin, ancestors: grant.ancestors } : {}),
|
|
343
|
+
});
|
|
224
344
|
}
|
|
225
345
|
|
|
226
346
|
const notFoundReply = (reply: FastifyReply) => reply.code(404).send({ error: "live-server target not found" });
|
|
@@ -234,13 +354,12 @@ export function registerLiveServerProxy(
|
|
|
234
354
|
grant: LiveCapabilityGrant | undefined,
|
|
235
355
|
capabilityPrefix: string | undefined,
|
|
236
356
|
) => {
|
|
237
|
-
if (!grant ||
|
|
357
|
+
if (!grant || capabilityPrefix === undefined) return notFoundReply(reply);
|
|
238
358
|
if (isReservedPath(subPath)) return reservedOr404(request, reply, grant, subPath);
|
|
239
359
|
const csp = cspFor(request, grant, capabilityPrefix);
|
|
240
360
|
if (!csp) return notFoundReply(reply);
|
|
241
361
|
manager.use(target.id);
|
|
242
362
|
const extra: Record<string, string> = { "content-security-policy": csp.csp };
|
|
243
|
-
if (csp.reportingEndpoints) extra["reporting-endpoints"] = csp.reportingEndpoints;
|
|
244
363
|
return serveStatic(request, reply, target, subPath, extra, {
|
|
245
364
|
gate: deps.staticGate ?? (async () => false),
|
|
246
365
|
onHtmlServed: (real) => deps.onStaticHtmlServed?.(grant, real),
|
|
@@ -254,16 +373,18 @@ export function registerLiveServerProxy(
|
|
|
254
373
|
subPath: string,
|
|
255
374
|
grant: LiveCapabilityGrant | undefined,
|
|
256
375
|
capabilityPrefix: string | undefined,
|
|
376
|
+
opts: ForwardPortOptions = {},
|
|
257
377
|
) => {
|
|
258
378
|
if (grant && isReservedPath(subPath)) return reservedOr404(request, reply, grant, subPath);
|
|
259
|
-
const csp = grant && capabilityPrefix ? cspFor(request, grant, capabilityPrefix) : null;
|
|
379
|
+
const csp = grant && capabilityPrefix !== undefined ? cspFor(request, grant, capabilityPrefix) : null;
|
|
260
380
|
// A capability response without a buildable CSP would be less isolated than promised.
|
|
261
381
|
if (grant && !csp) return notFoundReply(reply);
|
|
262
382
|
manager.use(target.id);
|
|
263
383
|
// Redirects stay inside the capability prefix when the request used one.
|
|
264
384
|
const prefix = capabilityPrefix ?? `/live/${target.id}`;
|
|
265
385
|
const host = upstreamHost(target.host);
|
|
266
|
-
|
|
386
|
+
const upstreamPath = opts.fullPath && capabilityPrefix !== undefined ? `${capabilityPrefix}${subPath}` : subPath;
|
|
387
|
+
reply.from(`http://${host}:${target.port}${upstreamPath}`, {
|
|
267
388
|
rewriteRequestHeaders: (_req, headers) => {
|
|
268
389
|
headers.host = `${host}:${target.port}`;
|
|
269
390
|
// Dashboard credentials never reach the previewed app; its own cookies
|
|
@@ -274,6 +395,7 @@ export function registerLiveServerProxy(
|
|
|
274
395
|
headers["x-forwarded-for"] = [headers["x-forwarded-for"], request.ip].filter(Boolean).join(", ");
|
|
275
396
|
// A forwarded Referer must not carry the capability token upstream.
|
|
276
397
|
if (typeof headers.referer === "string") headers.referer = stripCapabilitySegment(headers.referer);
|
|
398
|
+
if (opts.rewriteOrigin) rewritePreviewOrigin(headers as HeaderBag, `http://${host}:${target.port}`, request.headers.host);
|
|
277
399
|
return headers;
|
|
278
400
|
},
|
|
279
401
|
rewriteHeaders: (headers) => {
|
|
@@ -281,21 +403,60 @@ export function registerLiveServerProxy(
|
|
|
281
403
|
const setCookie = filterSetCookie(headers["set-cookie"] as string | string[] | undefined);
|
|
282
404
|
if (setCookie === undefined) delete headers["set-cookie"];
|
|
283
405
|
else headers["set-cookie"] = setCookie;
|
|
284
|
-
// The token is in this page's URL: keep it out of Referer headers the
|
|
285
|
-
|
|
286
|
-
if (capabilityPrefix && headers["referrer-policy"] === undefined) {
|
|
287
|
-
headers["referrer-policy"] = "no-referrer";
|
|
288
|
-
}
|
|
406
|
+
// The token is in this page's URL: keep it out of Referer headers the page sends to third parties.
|
|
407
|
+
applyReferrerPolicy(headers, grant, capabilityPrefix);
|
|
289
408
|
if (csp) appendCsp(headers, csp);
|
|
409
|
+
if (opts.nullCors) applyTier5Cors(headers, request.headers.origin);
|
|
410
|
+
if (grant && grant.tier !== "legacy" && grant.tier !== 5 && grant.origin !== undefined && deps.isDashboardUrl) {
|
|
411
|
+
containDashboardRedirects(headers, grant.origin, deps.isDashboardUrl);
|
|
412
|
+
}
|
|
290
413
|
const location = headers.location;
|
|
291
414
|
if (typeof location === "string") {
|
|
292
|
-
headers.location = rewriteLocation(location, prefix, host, target.port);
|
|
415
|
+
headers.location = rewriteLocation(location, prefix, host, target.port, opts.fullPath === true);
|
|
293
416
|
}
|
|
294
417
|
return headers;
|
|
295
418
|
},
|
|
296
419
|
});
|
|
297
420
|
};
|
|
298
421
|
|
|
422
|
+
const reservedOr404 = (request: FastifyRequest, reply: FastifyReply, grant: LiveCapabilityGrant, subPath: string) =>
|
|
423
|
+
deps.reservedHandler
|
|
424
|
+
? deps.reservedHandler(request, reply, { grant, subPath })
|
|
425
|
+
: notFoundReply(reply);
|
|
426
|
+
|
|
427
|
+
return { forwardStatic, forwardPort, notFoundReply, cspFor, reservedOr404 };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function registerLiveServerProxy(
|
|
431
|
+
fastify: FastifyInstance,
|
|
432
|
+
manager: LiveServerManager,
|
|
433
|
+
deps: LiveProxyDeps = {},
|
|
434
|
+
) {
|
|
435
|
+
// Register the `@fastify/reply-from` proxy plugin. live-server-proxy is now
|
|
436
|
+
// the SOLE consumer of the `from` decorator — the editor proxy that used to
|
|
437
|
+
// provide it was deleted in change: remove-external-editor-integration, which
|
|
438
|
+
// orphaned this call and produced `reply.from is not a function` on
|
|
439
|
+
// `/live/:id/*`. Owning the dependency here keeps the proxy self-contained.
|
|
440
|
+
// See change: improve-content-editor, fix-live-server-proxy-reply-from.
|
|
441
|
+
fastify.register(import("@fastify/reply-from"));
|
|
442
|
+
// CSP reports arrive as `application/csp-report` / `application/reports+json`.
|
|
443
|
+
if (deps.reservedHandler) registerCspReportParsers(fastify);
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* `capabilityPrefix` is set (by the capability hook, only after the token
|
|
447
|
+
* validated) when the request arrived under `/live/<id>/~c/<token>/`: that
|
|
448
|
+
* exact segment is removed from the upstream path and kept in redirects.
|
|
449
|
+
*/
|
|
450
|
+
function resolve(url: string, capabilityPrefix?: string) {
|
|
451
|
+
const match = url.match(/^\/live\/([^/?]+)(.*)$/);
|
|
452
|
+
if (!match) return null;
|
|
453
|
+
let rest = match[2] || "";
|
|
454
|
+
if (capabilityPrefix && url.startsWith(capabilityPrefix)) rest = url.slice(capabilityPrefix.length);
|
|
455
|
+
const subPath = rest.startsWith("?") ? `/${rest}` : rest.startsWith("/") ? rest : "/";
|
|
456
|
+
return { id: match[1], subPath };
|
|
457
|
+
}
|
|
458
|
+
const { forwardStatic, forwardPort, notFoundReply } = createLiveForwarder(manager, deps);
|
|
459
|
+
|
|
299
460
|
const forward = (request: FastifyRequest, reply: FastifyReply) => {
|
|
300
461
|
const capabilityPrefix = (request as { liveCapabilityPrefix?: string }).liveCapabilityPrefix;
|
|
301
462
|
const grant = (request as { liveCapability?: LiveCapabilityGrant }).liveCapability;
|
|
@@ -311,13 +472,29 @@ export function registerLiveServerProxy(
|
|
|
311
472
|
return forwardPort(request, reply, target, subPath, grant, capabilityPrefix);
|
|
312
473
|
};
|
|
313
474
|
|
|
314
|
-
const reservedOr404 = (request: FastifyRequest, reply: FastifyReply, grant: LiveCapabilityGrant, subPath: string) =>
|
|
315
|
-
deps.reservedHandler
|
|
316
|
-
? deps.reservedHandler(request, reply, { grant, subPath })
|
|
317
|
-
: notFoundReply(reply);
|
|
318
|
-
|
|
319
475
|
fastify.all("/live/:id/*", forward);
|
|
320
476
|
fastify.all("/live/:id", forward);
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Tier 5 (D8): `/preview/<pid>[/…]`. The capability hook validated the pid and set `liveCapability`; any request
|
|
480
|
+
* without one (browser-originated or not) is 404, as is a path the raw-path validator refuses.
|
|
481
|
+
*/
|
|
482
|
+
const forwardPreview = (request: FastifyRequest, reply: FastifyReply) => {
|
|
483
|
+
const grant = (request as { liveCapability?: LiveCapabilityGrant }).liveCapability;
|
|
484
|
+
const path = parsePreviewPath(request.raw.url);
|
|
485
|
+
if (!grant || grant.tier !== 5 || !path) return notFoundReply(reply);
|
|
486
|
+
const target = manager.get(grant.targetId);
|
|
487
|
+
if (!target) return notFoundReply(reply);
|
|
488
|
+
deps.trackPreviewResponse?.(path.pid, reply.raw);
|
|
489
|
+
return forwardPort(request, reply, target, path.rest, grant, path.prefix, { fullPath: true, nullCors: true });
|
|
490
|
+
};
|
|
491
|
+
// A dead or resetting upstream is a bad gateway (never a dashboard 500), like the preview-origin and pool forwarders.
|
|
492
|
+
const badGateway = (error: { statusCode?: number }, _request: FastifyRequest, reply: FastifyReply) => {
|
|
493
|
+
const status = error.statusCode !== undefined && error.statusCode < 500 ? error.statusCode : error.statusCode === 504 ? 504 : 502;
|
|
494
|
+
return reply.code(status).header("cache-control", "no-store").type("application/json").send({ error: status === 502 ? "bad_gateway" : status === 504 ? "gateway_timeout" : "bad_request" });
|
|
495
|
+
};
|
|
496
|
+
fastify.all("/preview/:pid/*", { errorHandler: badGateway }, forwardPreview);
|
|
497
|
+
fastify.all("/preview/:pid", { errorHandler: badGateway }, forwardPreview);
|
|
321
498
|
}
|
|
322
499
|
|
|
323
500
|
/** Longest upstream 101 head we will buffer before giving up. */
|
|
@@ -339,6 +516,7 @@ function forwardHandshakeFiltered(upstream: net.Socket, socket: import("node:str
|
|
|
339
516
|
return;
|
|
340
517
|
}
|
|
341
518
|
upstream.off("data", onData);
|
|
519
|
+
upstream.setTimeout(0); // the handshake finished: the pipe owns the socket lifetime from here
|
|
342
520
|
const headText = buf.subarray(0, end).toString("latin1");
|
|
343
521
|
const filtered = headText
|
|
344
522
|
.split("\r\n")
|
|
@@ -359,8 +537,11 @@ export function handleLiveServerUpgrade(
|
|
|
359
537
|
deps: LiveProxyDeps & {
|
|
360
538
|
/** `/live/<id>/~c/<token>` when the upgrade was admitted by a validated capability. */
|
|
361
539
|
capabilityPrefix?: string;
|
|
540
|
+
/** Tier 5: the upgrade was admitted by a valid `/preview/<pid>/` capability for this target; the FULL path is forwarded. */
|
|
541
|
+
previewTargetId?: string;
|
|
362
542
|
} = {},
|
|
363
543
|
) {
|
|
544
|
+
if (deps.previewTargetId !== undefined) return upgradePreview(manager, request, socket, head, deps, deps.previewTargetId);
|
|
364
545
|
const match = request.url?.match(/^\/live\/([^/?]+)(.*)$/);
|
|
365
546
|
if (!match) {
|
|
366
547
|
socket.destroy();
|
|
@@ -381,14 +562,30 @@ export function handleLiveServerUpgrade(
|
|
|
381
562
|
let rest = match[2] || "";
|
|
382
563
|
if (capabilityPrefix && request.url?.startsWith(capabilityPrefix)) rest = request.url.slice(capabilityPrefix.length);
|
|
383
564
|
const subPath = rest.startsWith("?") ? `/${rest}` : rest.startsWith("/") ? rest : "/";
|
|
565
|
+
pipeUpgradeToTarget(target, subPath, request, socket, head, deps);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Open the upstream socket for an admitted upgrade and pipe both directions. Shared by the
|
|
570
|
+
* main-origin `/live/<id>` upgrade and the preview-origin upgrade (D3). The browser `Origin` is
|
|
571
|
+
* never forwarded; `Host` is rewritten to the upstream.
|
|
572
|
+
*/
|
|
573
|
+
export function pipeUpgradeToTarget(
|
|
574
|
+
target: LiveServerTarget,
|
|
575
|
+
subPath: string,
|
|
576
|
+
request: import("node:http").IncomingMessage,
|
|
577
|
+
socket: import("node:stream").Duplex,
|
|
578
|
+
head: Buffer,
|
|
579
|
+
deps: LiveProxyDeps,
|
|
580
|
+
) {
|
|
384
581
|
const host = upstreamHost(target.host);
|
|
385
582
|
const isDeviceBearer = deps.isDeviceBearer ?? (() => false);
|
|
386
583
|
const isKnownBearer = deps.isKnownBearer ?? (() => false);
|
|
387
584
|
|
|
388
585
|
const upstream = net.connect(target.port, host, () => {
|
|
389
|
-
//
|
|
390
|
-
//
|
|
391
|
-
|
|
586
|
+
// The handshake timeout below stays armed until the upstream's 101 head is complete
|
|
587
|
+
// (`forwardHandshakeFiltered` clears it): an upstream that accepts TCP and never answers the upgrade
|
|
588
|
+
// must not hold the socket pair open forever.
|
|
392
589
|
// Dashboard credentials never reach the previewed app (same rule as the
|
|
393
590
|
// HTTP path); a forwarded Referer loses any capability segment.
|
|
394
591
|
const forwarded = stripDashboardCredentials({ ...request.headers } as HeaderBag, isDeviceBearer, request, isKnownBearer);
|
|
@@ -418,3 +615,22 @@ export function handleLiveServerUpgrade(
|
|
|
418
615
|
socket.on("close", () => upstream.destroy());
|
|
419
616
|
upstream.on("close", () => socket.destroy());
|
|
420
617
|
}
|
|
618
|
+
|
|
619
|
+
/** Tier 5 upgrade: re-parse the raw path (never trust the caller) and pipe the FULL path to the target. */
|
|
620
|
+
function upgradePreview(
|
|
621
|
+
manager: LiveServerManager,
|
|
622
|
+
request: import("node:http").IncomingMessage,
|
|
623
|
+
socket: import("node:stream").Duplex,
|
|
624
|
+
head: Buffer,
|
|
625
|
+
deps: LiveProxyDeps,
|
|
626
|
+
targetId: string,
|
|
627
|
+
) {
|
|
628
|
+
const path = parsePreviewPath(request.url);
|
|
629
|
+
const target = path ? manager.get(targetId) : undefined;
|
|
630
|
+
if (!path || !target) {
|
|
631
|
+
socket.destroy();
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
manager.use(target.id);
|
|
635
|
+
pipeUpgradeToTarget(target, request.url as string, request, socket, head, deps);
|
|
636
|
+
}
|