@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,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host detection for the provider settings pane: what the dashboard host
|
|
3
|
+
* already has for a provider (gcloud ADC login, AWS profiles, AWS credentials
|
|
4
|
+
* in the environment / a role), and the catalogue method to preselect.
|
|
5
|
+
*
|
|
6
|
+
* Reads the same locations pi reads, as the account the server runs as. Every
|
|
7
|
+
* file is size-capped (64 KiB); unreadable or malformed → not found. Returns
|
|
8
|
+
* names, regions, project ids and account emails only — never a key, token,
|
|
9
|
+
* secret or file contents (the AWS credentials file contributes section names
|
|
10
|
+
* only). Pure parsers + one I/O wrapper. See change: add-provider-env-config (D6).
|
|
11
|
+
*/
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
import os from "node:os";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import type { ProviderHostDetect } from "@blackbelt-technology/pi-dashboard-shared/rest-api.js";
|
|
16
|
+
|
|
17
|
+
const HOST_FILE_MAX_BYTES = 64 * 1024;
|
|
18
|
+
|
|
19
|
+
// ── Pure parsers ─────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
/** INI sections → key/value maps. `null` when any line is not blank, comment, section or `key = value`. */
|
|
22
|
+
export function parseIni(text: string): Map<string, Record<string, string>> | null {
|
|
23
|
+
const out = new Map<string, Record<string, string>>();
|
|
24
|
+
let current: Record<string, string> | null = null;
|
|
25
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
26
|
+
const line = raw.trim();
|
|
27
|
+
if (!line || line.startsWith("#") || line.startsWith(";")) continue;
|
|
28
|
+
const section = /^\[([^\]]+)\]$/.exec(line);
|
|
29
|
+
if (section) {
|
|
30
|
+
const name = section[1].trim();
|
|
31
|
+
current = out.get(name) ?? Object.create(null);
|
|
32
|
+
out.set(name, current as Record<string, string>);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const eq = line.indexOf("=");
|
|
36
|
+
if (eq > 0) {
|
|
37
|
+
// Indented continuation lines (AWS nested settings) land in the same section.
|
|
38
|
+
if (current) current[line.slice(0, eq).trim()] = line.slice(eq + 1).trim();
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (/^\s/.test(raw)) continue;
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface AdcInfo {
|
|
48
|
+
type: string;
|
|
49
|
+
quotaProject?: string;
|
|
50
|
+
account?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Application Default Credentials JSON → type, quota project, service-account email. */
|
|
54
|
+
function parseAdc(text: string): AdcInfo | null {
|
|
55
|
+
try {
|
|
56
|
+
const j = JSON.parse(text) as Record<string, unknown>;
|
|
57
|
+
if (!j || typeof j !== "object" || Array.isArray(j) || typeof j.type !== "string") return null;
|
|
58
|
+
const info: AdcInfo = { type: j.type };
|
|
59
|
+
if (typeof j.quota_project_id === "string" && j.quota_project_id) info.quotaProject = j.quota_project_id;
|
|
60
|
+
if (typeof j.client_email === "string" && j.client_email) info.account = j.client_email;
|
|
61
|
+
return info;
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** AWS profiles: config sections (`[profile x]` → x, `[default]`) with region, then credentials-only names. */
|
|
68
|
+
function parseAwsProfiles(configText: string | null, credentialsText: string | null): { name: string; region?: string }[] {
|
|
69
|
+
const profiles: { name: string; region?: string }[] = [];
|
|
70
|
+
const seen = new Set<string>();
|
|
71
|
+
const config = configText === null ? null : parseIni(configText);
|
|
72
|
+
for (const [section, values] of config ?? []) {
|
|
73
|
+
let name: string | null = null;
|
|
74
|
+
if (section === "default") name = "default";
|
|
75
|
+
else if (section.startsWith("profile ")) name = section.slice("profile ".length).trim();
|
|
76
|
+
if (!name || seen.has(name)) continue;
|
|
77
|
+
seen.add(name);
|
|
78
|
+
profiles.push(values.region ? { name, region: values.region } : { name });
|
|
79
|
+
}
|
|
80
|
+
const credentials = credentialsText === null ? null : parseIni(credentialsText);
|
|
81
|
+
for (const section of credentials?.keys() ?? []) {
|
|
82
|
+
const name = section.trim();
|
|
83
|
+
if (!name || seen.has(name)) continue;
|
|
84
|
+
seen.add(name);
|
|
85
|
+
profiles.push({ name });
|
|
86
|
+
}
|
|
87
|
+
return profiles;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ── I/O wrapper ──────────────────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
export interface HostDetectDeps {
|
|
93
|
+
env?: Readonly<Record<string, string | undefined>>;
|
|
94
|
+
home?: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Read a small regular file, else `null` (missing, too large, not a file, unreadable). */
|
|
98
|
+
function readSmallFile(file: string): string | null {
|
|
99
|
+
try {
|
|
100
|
+
const st = fs.statSync(file);
|
|
101
|
+
if (!st.isFile() || st.size > HOST_FILE_MAX_BYTES) return null;
|
|
102
|
+
return fs.readFileSync(file, "utf-8");
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function detectVertex(env: HostDetectDeps["env"] & object, home: string): ProviderHostDetect {
|
|
109
|
+
const adcPath = env.GOOGLE_APPLICATION_CREDENTIALS || path.join(home, ".config", "gcloud", "application_default_credentials.json");
|
|
110
|
+
const adcText = readSmallFile(adcPath);
|
|
111
|
+
const adc = adcText === null ? null : parseAdc(adcText);
|
|
112
|
+
if (!adc) return { suggested: "service-account" };
|
|
113
|
+
let project = adc.quotaProject;
|
|
114
|
+
if (!project) {
|
|
115
|
+
const gcloudDir = path.join(home, ".config", "gcloud");
|
|
116
|
+
const active = env.CLOUDSDK_ACTIVE_CONFIG_NAME || readSmallFile(path.join(gcloudDir, "active_config"))?.trim() || "default";
|
|
117
|
+
if (/^[A-Za-z0-9_.-]+$/.test(active)) {
|
|
118
|
+
const cfgText = readSmallFile(path.join(gcloudDir, "configurations", `config_${active}`));
|
|
119
|
+
const cfg = cfgText === null ? null : parseIni(cfgText);
|
|
120
|
+
const p = cfg?.get("core")?.project;
|
|
121
|
+
if (p) project = p;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const found: { project?: string; account?: string } = {};
|
|
125
|
+
if (project) found.project = project;
|
|
126
|
+
if (adc.account) found.account = adc.account;
|
|
127
|
+
return { adc: found, suggested: "adc" };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function detectBedrock(env: HostDetectDeps["env"] & object, home: string): ProviderHostDetect {
|
|
131
|
+
const configText = readSmallFile(env.AWS_CONFIG_FILE || path.join(home, ".aws", "config"));
|
|
132
|
+
const credentialsText = readSmallFile(env.AWS_SHARED_CREDENTIALS_FILE || path.join(home, ".aws", "credentials"));
|
|
133
|
+
const profiles = parseAwsProfiles(configText, credentialsText);
|
|
134
|
+
let chain: ProviderHostDetect["chain"];
|
|
135
|
+
if (env.AWS_ACCESS_KEY_ID && env.AWS_SECRET_ACCESS_KEY) chain = "environment";
|
|
136
|
+
else if (env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI || env.AWS_CONTAINER_CREDENTIALS_FULL_URI) chain = "container role";
|
|
137
|
+
else if (env.AWS_WEB_IDENTITY_TOKEN_FILE) chain = "web identity";
|
|
138
|
+
const out: ProviderHostDetect = { profiles };
|
|
139
|
+
if (chain) out.chain = chain;
|
|
140
|
+
out.suggested = profiles.length > 0 ? "aws-profile" : chain ? "credential-chain" : "bearer-token";
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** What the host has for `provider` (bare catalogue id). Unknown providers → `{}`. */
|
|
145
|
+
export function detectHost(provider: string, deps: HostDetectDeps = {}): ProviderHostDetect {
|
|
146
|
+
const env = deps.env ?? process.env;
|
|
147
|
+
const home = deps.home ?? os.homedir();
|
|
148
|
+
if (provider === "google-vertex") return detectVertex(env, home);
|
|
149
|
+
if (provider === "amazon-bedrock") return detectBedrock(env, home);
|
|
150
|
+
return {};
|
|
151
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service-account key-file inspection for the settings pane: reads ONLY
|
|
3
|
+
* `project_id` and `client_email` from a user-named file on the dashboard host.
|
|
4
|
+
*
|
|
5
|
+
* The path must pass the browse confinement for the request (non-local →
|
|
6
|
+
* shared roots, sensitive subtrees refused; symlinks resolved) BEFORE anything
|
|
7
|
+
* is opened. Regular file, ≤ 64 KiB, JSON, `type: "service_account"`.
|
|
8
|
+
* `private_key` and every other field never leave this function.
|
|
9
|
+
* See change: add-provider-env-config (D7).
|
|
10
|
+
*/
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import os from "node:os";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import type { KeyFileInfo } from "@blackbelt-technology/pi-dashboard-shared/rest-api.js";
|
|
15
|
+
import { type BrowseRoots, isWithinBrowseRoots } from "../lib/browse-roots.js";
|
|
16
|
+
|
|
17
|
+
const KEY_FILE_MAX_BYTES = 64 * 1024;
|
|
18
|
+
|
|
19
|
+
type KeyFileError = Extract<KeyFileInfo, { ok: false }>["error"];
|
|
20
|
+
const fail = (error: KeyFileError): KeyFileInfo => ({ ok: false, error });
|
|
21
|
+
|
|
22
|
+
function expandHome(p: string): string {
|
|
23
|
+
if (p === "~") return os.homedir();
|
|
24
|
+
if (p.startsWith("~/")) return path.join(os.homedir(), p.slice(2));
|
|
25
|
+
return p;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `roots === null` = locally trusted request (unconfined). */
|
|
29
|
+
export async function inspectKeyFile(rawPath: string, roots: BrowseRoots | null): Promise<KeyFileInfo> {
|
|
30
|
+
const requested = path.resolve(os.homedir(), expandHome(rawPath.trim()));
|
|
31
|
+
if (roots && !(await isWithinBrowseRoots(requested, roots))) return fail("provider_auth.key_file_outside_roots");
|
|
32
|
+
let real: string;
|
|
33
|
+
try {
|
|
34
|
+
real = await fs.promises.realpath(requested);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
return (err as NodeJS.ErrnoException)?.code === "ENOENT" ? fail("provider_auth.key_file_not_found") : fail("provider_auth.key_file_unreadable");
|
|
37
|
+
}
|
|
38
|
+
// Re-check the resolved target: a symlink inside a root may point outside it.
|
|
39
|
+
if (roots && !(await isWithinBrowseRoots(real, roots))) return fail("provider_auth.key_file_outside_roots");
|
|
40
|
+
let text: string;
|
|
41
|
+
// Open the resolved path without following a (swapped-in) symlink, then
|
|
42
|
+
// fstat + a bounded read on the SAME descriptor: no check-then-read gap.
|
|
43
|
+
let handle: fs.promises.FileHandle | undefined;
|
|
44
|
+
try {
|
|
45
|
+
handle = await fs.promises.open(real, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0));
|
|
46
|
+
const st = await handle.stat();
|
|
47
|
+
if (!st.isFile()) return fail("provider_auth.key_file_unreadable");
|
|
48
|
+
if (st.size > KEY_FILE_MAX_BYTES) return fail("provider_auth.key_file_too_large");
|
|
49
|
+
const buf = Buffer.alloc(KEY_FILE_MAX_BYTES + 1);
|
|
50
|
+
const { bytesRead } = await handle.read(buf, 0, buf.length, 0);
|
|
51
|
+
if (bytesRead > KEY_FILE_MAX_BYTES) return fail("provider_auth.key_file_too_large");
|
|
52
|
+
text = buf.subarray(0, bytesRead).toString("utf-8");
|
|
53
|
+
} catch (err) {
|
|
54
|
+
const code = (err as NodeJS.ErrnoException)?.code;
|
|
55
|
+
if (code === "ENOENT") return fail("provider_auth.key_file_not_found");
|
|
56
|
+
if (code === "EISDIR") return fail("provider_auth.key_file_unreadable");
|
|
57
|
+
return fail("provider_auth.key_file_unreadable");
|
|
58
|
+
} finally {
|
|
59
|
+
await handle?.close().catch(() => {});
|
|
60
|
+
}
|
|
61
|
+
let json: unknown;
|
|
62
|
+
try {
|
|
63
|
+
json = JSON.parse(text);
|
|
64
|
+
} catch {
|
|
65
|
+
return fail("provider_auth.key_file_not_service_account");
|
|
66
|
+
}
|
|
67
|
+
const j = json as Record<string, unknown> | null;
|
|
68
|
+
if (!j || typeof j !== "object" || j.type !== "service_account") return fail("provider_auth.key_file_not_service_account");
|
|
69
|
+
if (typeof j.project_id !== "string" || typeof j.client_email !== "string") return fail("provider_auth.key_file_not_service_account");
|
|
70
|
+
return { ok: true, project: j.project_id, account: j.client_email };
|
|
71
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi readiness of stored api-key credentials: the server's own pi runtime
|
|
3
|
+
* decides (`checkAuth` → the provider's `resolve`), never re-implemented here.
|
|
4
|
+
*
|
|
5
|
+
* Bounded: 2 s per check, at most 4 checks at once, results cached per
|
|
6
|
+
* auth.json stamp (mtime + size) and dropped by every dashboard write. A check
|
|
7
|
+
* that throws or times out, and a provider the runtime does not know, yield no
|
|
8
|
+
* entry (→ no `piReady`). See change: add-provider-env-config (D5).
|
|
9
|
+
*/
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import type { AuthData } from "./provider-auth-storage.js";
|
|
12
|
+
import type { RuntimeCatalogueSource } from "./runtime-provider-catalogue.js";
|
|
13
|
+
|
|
14
|
+
const READINESS_TIMEOUT_MS = 2_000;
|
|
15
|
+
const READINESS_CONCURRENCY = 4;
|
|
16
|
+
|
|
17
|
+
interface ReadinessDeps {
|
|
18
|
+
runtime: RuntimeCatalogueSource | undefined;
|
|
19
|
+
/** auth.json path whose stamp keys the cache; absent → no cache. */
|
|
20
|
+
authPath?: string;
|
|
21
|
+
timeoutMs?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let cache: { stamp: string; map: Map<string, boolean> } | null = null;
|
|
25
|
+
|
|
26
|
+
/** Drop cached results (called after every credential write). */
|
|
27
|
+
export function invalidateReadiness(): void {
|
|
28
|
+
cache = null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function stampOf(file: string | undefined): string | null {
|
|
32
|
+
if (!file) return null;
|
|
33
|
+
try {
|
|
34
|
+
const st = fs.statSync(file);
|
|
35
|
+
return `${st.mtimeMs}:${st.size}`;
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const TIMED_OUT = Symbol("timeout");
|
|
42
|
+
|
|
43
|
+
async function checkOne(runtime: RuntimeCatalogueSource, id: string, timeoutMs: number): Promise<boolean | undefined> {
|
|
44
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
45
|
+
try {
|
|
46
|
+
const result = await Promise.race([
|
|
47
|
+
runtime.checkAuth(id),
|
|
48
|
+
new Promise<typeof TIMED_OUT>((resolve) => {
|
|
49
|
+
timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
|
|
50
|
+
timer.unref?.();
|
|
51
|
+
}),
|
|
52
|
+
]);
|
|
53
|
+
if (result === TIMED_OUT) return undefined;
|
|
54
|
+
return !!result && result.type === "api_key";
|
|
55
|
+
} catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
} finally {
|
|
58
|
+
if (timer) clearTimeout(timer);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Readiness per auth.json key, for every stored api-key credential the runtime knows. */
|
|
63
|
+
export async function computeReadiness(authData: AuthData, deps: ReadinessDeps): Promise<Map<string, boolean>> {
|
|
64
|
+
const { runtime } = deps;
|
|
65
|
+
const out = new Map<string, boolean>();
|
|
66
|
+
if (!runtime || typeof runtime.checkAuth !== "function" || typeof runtime.getProviders !== "function") return out;
|
|
67
|
+
const stamp = stampOf(deps.authPath);
|
|
68
|
+
if (stamp && cache?.stamp === stamp) return new Map(cache.map);
|
|
69
|
+
|
|
70
|
+
let known: Set<string>;
|
|
71
|
+
try {
|
|
72
|
+
known = new Set(runtime.getProviders().map((p) => p.id));
|
|
73
|
+
} catch {
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
const ids = Object.entries(authData)
|
|
77
|
+
.filter(([id, cred]) => cred?.type === "api_key" && known.has(id))
|
|
78
|
+
.map(([id]) => id);
|
|
79
|
+
const timeoutMs = deps.timeoutMs ?? READINESS_TIMEOUT_MS;
|
|
80
|
+
let next = 0;
|
|
81
|
+
const worker = async () => {
|
|
82
|
+
while (next < ids.length) {
|
|
83
|
+
const id = ids[next++];
|
|
84
|
+
const ready = await checkOne(runtime, id, timeoutMs);
|
|
85
|
+
if (ready !== undefined) out.set(id, ready);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
await Promise.all(Array.from({ length: Math.min(READINESS_CONCURRENCY, ids.length) }, worker));
|
|
89
|
+
if (stamp) cache = { stamp, map: new Map(out) };
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
@@ -19,7 +19,7 @@ import type {
|
|
|
19
19
|
EffectiveView,
|
|
20
20
|
McpClientConfigService,
|
|
21
21
|
ServerEntry,
|
|
22
|
-
} from "@blackbelt-technology/pi-dashboard-mcp-
|
|
22
|
+
} from "@blackbelt-technology/pi-dashboard-mcp-core";
|
|
23
23
|
|
|
24
24
|
/** pi `RADIUS_MCP_URL` (`core/radius.js`); pinned by a drift test. */
|
|
25
25
|
export const RADIUS_MCP_URL = "https://radius.pi.dev/mcp";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable per-principal key for a request the admission hooks already settled
|
|
3
|
+
* (change: harden-client-sinks, D8/D10). Used to rate-limit violation reports
|
|
4
|
+
* and to bind navigation intents to their minting principal.
|
|
5
|
+
*
|
|
6
|
+
* identity plane principal → `identity:<iss>\0<sub>`
|
|
7
|
+
* paired device → `device:<deviceId>`
|
|
8
|
+
* login session cookie → `session:<sha256(cookie)>`
|
|
9
|
+
* otherwise → `local-operator` (inert plane: the operator the
|
|
10
|
+
* network guard admitted — loopback / trusted)
|
|
11
|
+
*
|
|
12
|
+
* Callers register their routes behind the network guard, so an unadmitted
|
|
13
|
+
* request never reaches this function.
|
|
14
|
+
*/
|
|
15
|
+
import crypto from "node:crypto";
|
|
16
|
+
import type { Principal } from "@blackbelt-technology/pi-dashboard-shared/identity.js";
|
|
17
|
+
import type { FastifyRequest } from "fastify";
|
|
18
|
+
import { COOKIE_NAME } from "./auth.js";
|
|
19
|
+
|
|
20
|
+
export function requestPrincipalKey(request: FastifyRequest): string {
|
|
21
|
+
const r = request as {
|
|
22
|
+
authVia?: string;
|
|
23
|
+
principalDeviceId?: string;
|
|
24
|
+
principal?: Principal | null;
|
|
25
|
+
cookies?: Record<string, string | undefined>;
|
|
26
|
+
};
|
|
27
|
+
if (r.principal) return `identity:${r.principal.iss}\u0000${r.principal.sub}`;
|
|
28
|
+
if (r.authVia === "device" && r.principalDeviceId) return `device:${r.principalDeviceId}`;
|
|
29
|
+
const cookie = r.authVia === "session" ? r.cookies?.[COOKIE_NAME] : undefined;
|
|
30
|
+
if (cookie) return `session:${crypto.createHash("sha256").update(cookie).digest("hex")}`;
|
|
31
|
+
return "local-operator";
|
|
32
|
+
}
|
|
@@ -27,8 +27,9 @@
|
|
|
27
27
|
*
|
|
28
28
|
* See change: expand-mcp-tiered-surface (task 2.2).
|
|
29
29
|
*/
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
import { routeTier } from "@blackbelt-technology/pi-dashboard-shared/route-tiers.js";
|
|
32
|
+
import { rank, type Tier } from "@blackbelt-technology/pi-dashboard-shared/tiers.js";
|
|
32
33
|
import { LIVE_PREVIEW_TIER } from "@blackbelt-technology/pi-dashboard-shared/ws-message-tiers.js";
|
|
33
34
|
import type { FastifyReply, FastifyRequest } from "fastify";
|
|
34
35
|
import type { LocalTrustContext } from "./local-proof.js";
|
|
@@ -123,7 +124,7 @@ export function createRouteTierGate(deps: RouteTierGateDeps) {
|
|
|
123
124
|
// tier its WS upgrade needs. See change: add-passkey-user-auth.
|
|
124
125
|
let requiredTier: Tier;
|
|
125
126
|
if (route.startsWith("/api/")) requiredTier = routeTier(request.method, route);
|
|
126
|
-
else if (route.startsWith("/live/")) requiredTier = LIVE_PREVIEW_TIER;
|
|
127
|
+
else if (route.startsWith("/live/") || route.startsWith("/preview/")) requiredTier = LIVE_PREVIEW_TIER;
|
|
127
128
|
else return;
|
|
128
129
|
|
|
129
130
|
const refusal = tierRefusalFor(request, requiredTier, deps.getTrustedNetworks, deps.localTrust);
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-shot rotation of secrets that may already have been read through the
|
|
3
|
+
* `~/.pi` file anchor by a bare-loopback caller (the local IPC token and the
|
|
4
|
+
* auth HMAC secret). Runs once per HOME at server start, before anything reads
|
|
5
|
+
* either value.
|
|
6
|
+
*
|
|
7
|
+
* - Exclusive `local/rotation.lock`, claimed with a hard link (atomic, fails
|
|
8
|
+
* with EEXIST when held). Stale after 30 s or when the holder pid is dead;
|
|
9
|
+
* the wait is capped at 35 s.
|
|
10
|
+
* - `local/rotation-epoch` below `REQUIRED_ROTATION_EPOCH` → write a new token
|
|
11
|
+
* (0600, atomic), replace `auth.secret` via `writeConfigFileSecure`, and only
|
|
12
|
+
* then write the epoch. The epoch is last, so any failure retries the whole
|
|
13
|
+
* rotation on the next start (a second token rotation is harmless).
|
|
14
|
+
* - Failure is closed: callers exit 1 before listening (`RotationError`).
|
|
15
|
+
*
|
|
16
|
+
* `close-loopback-legacy-window` reuses this lock and raises the epoch to 2.
|
|
17
|
+
* See change: close-container-loopback-impersonation (D11).
|
|
18
|
+
*/
|
|
19
|
+
import crypto from "node:crypto";
|
|
20
|
+
import fs from "node:fs";
|
|
21
|
+
import os from "node:os";
|
|
22
|
+
import path from "node:path";
|
|
23
|
+
import { writeConfigFileSecure } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
24
|
+
import { getDashboardConfigDir } from "@blackbelt-technology/pi-dashboard-shared/dashboard-paths.js";
|
|
25
|
+
import { isProcessAlive } from "@blackbelt-technology/pi-dashboard-shared/platform/process.js";
|
|
26
|
+
|
|
27
|
+
/** Bump (never reuse) when another one-time rotation becomes necessary. */
|
|
28
|
+
export const REQUIRED_ROTATION_EPOCH = 1;
|
|
29
|
+
const ROTATION_LOCK_STALE_MS = 30_000;
|
|
30
|
+
export const ROTATION_LOCK_WAIT_CAP_MS = 35_000;
|
|
31
|
+
const POLL_MS = 100;
|
|
32
|
+
const TOKEN_BYTES = 32;
|
|
33
|
+
|
|
34
|
+
/** The failed step is named so the single log line tells the operator what to look at. */
|
|
35
|
+
export class RotationError extends Error {
|
|
36
|
+
readonly step: "lock" | "token" | "secret" | "epoch";
|
|
37
|
+
readonly paths: { lock: string; epoch: string };
|
|
38
|
+
|
|
39
|
+
constructor(step: "lock" | "token" | "secret" | "epoch", message: string, paths: { lock: string; epoch: string }) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = "RotationError";
|
|
42
|
+
this.step = step;
|
|
43
|
+
this.paths = paths;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** One line: step, cause, lock and epoch paths. */
|
|
47
|
+
describe(): string {
|
|
48
|
+
return `[rotation] secret rotation failed at step=${this.step}: ${this.message} (lock=${this.paths.lock} epoch=${this.paths.epoch}); exiting before listen`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface RotationDeps {
|
|
53
|
+
/** `~/.pi/dashboard/local` */
|
|
54
|
+
localDir?: string;
|
|
55
|
+
/** `~/.pi/dashboard/config.json` */
|
|
56
|
+
configFile?: string;
|
|
57
|
+
now?: () => number;
|
|
58
|
+
sleep?: (ms: number) => Promise<void>;
|
|
59
|
+
isPidAlive?: (pid: number) => boolean;
|
|
60
|
+
/** Replaces `writeConfigFileSecure` (fault injection). */
|
|
61
|
+
writeConfig?: (file: string, text: string) => void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface RotationResult {
|
|
65
|
+
rotated: boolean;
|
|
66
|
+
/** The replacement `auth.secret`, when one existed to replace. */
|
|
67
|
+
newSecret?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function atomicWrite(file: string, text: string): void {
|
|
71
|
+
const tmp = `${file}.tmp.${process.pid}.${crypto.randomUUID()}`;
|
|
72
|
+
try {
|
|
73
|
+
fs.writeFileSync(tmp, text, { mode: 0o600 });
|
|
74
|
+
try {
|
|
75
|
+
fs.chmodSync(tmp, 0o600);
|
|
76
|
+
} catch {
|
|
77
|
+
/* best-effort (Windows) */
|
|
78
|
+
}
|
|
79
|
+
fs.renameSync(tmp, file);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
try {
|
|
82
|
+
fs.unlinkSync(tmp);
|
|
83
|
+
} catch {
|
|
84
|
+
/* already gone */
|
|
85
|
+
}
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function readEpoch(file: string): number {
|
|
91
|
+
try {
|
|
92
|
+
const n = Number.parseInt(fs.readFileSync(file, "utf-8").trim(), 10);
|
|
93
|
+
return Number.isFinite(n) ? n : 0;
|
|
94
|
+
} catch {
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface LockBody {
|
|
100
|
+
pid: number;
|
|
101
|
+
at: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function readLock(lock: string): LockBody | null {
|
|
105
|
+
try {
|
|
106
|
+
const body = JSON.parse(fs.readFileSync(lock, "utf-8")) as Partial<LockBody>;
|
|
107
|
+
if (typeof body.pid === "number" && typeof body.at === "number") return { pid: body.pid, at: body.at };
|
|
108
|
+
} catch {
|
|
109
|
+
/* unreadable / half-written: judged by mtime below */
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Try once. `true` when this process now holds the lock. */
|
|
115
|
+
function tryClaim(lock: string, now: () => number, isPidAlive: (pid: number) => boolean, stalePath: () => string): boolean {
|
|
116
|
+
const mine = `${lock}.${process.pid}.${crypto.randomUUID()}`;
|
|
117
|
+
fs.writeFileSync(mine, JSON.stringify({ pid: process.pid, at: now() } satisfies LockBody), { mode: 0o600 });
|
|
118
|
+
try {
|
|
119
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
120
|
+
try {
|
|
121
|
+
fs.linkSync(mine, lock); // atomic; EEXIST when held
|
|
122
|
+
return true;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err;
|
|
125
|
+
}
|
|
126
|
+
const held = readLock(lock);
|
|
127
|
+
let stale: boolean;
|
|
128
|
+
if (held) {
|
|
129
|
+
stale = now() - held.at > ROTATION_LOCK_STALE_MS || !isPidAlive(held.pid);
|
|
130
|
+
} else {
|
|
131
|
+
// Half-written or foreign content: fall back to the file's age.
|
|
132
|
+
try {
|
|
133
|
+
stale = now() - fs.statSync(lock).mtimeMs > ROTATION_LOCK_STALE_MS;
|
|
134
|
+
} catch {
|
|
135
|
+
stale = true; // vanished between link and stat → retry the link
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (!stale) return false;
|
|
139
|
+
// Move (not unlink) the stale lock aside: rename is atomic, so exactly one
|
|
140
|
+
// contender removes it and a fresh lock claimed since is not unlinked by name.
|
|
141
|
+
try {
|
|
142
|
+
fs.renameSync(lock, stalePath());
|
|
143
|
+
} catch {
|
|
144
|
+
/* someone else cleared it first */
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
} finally {
|
|
149
|
+
try {
|
|
150
|
+
fs.unlinkSync(mine);
|
|
151
|
+
} catch {
|
|
152
|
+
/* already gone */
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Rotate the local token and `auth.secret` once per HOME. Idempotent: a second
|
|
159
|
+
* call (or a second process) finds the epoch and returns `{ rotated: false }`.
|
|
160
|
+
* Throws `RotationError` on any failure; the epoch is then still unwritten.
|
|
161
|
+
*/
|
|
162
|
+
export async function rotateSecretsOnce(deps: RotationDeps = {}): Promise<RotationResult> {
|
|
163
|
+
const dashDir = getDashboardConfigDir();
|
|
164
|
+
const localDir = deps.localDir ?? path.join(dashDir, "local");
|
|
165
|
+
const configFile = deps.configFile ?? path.join(os.homedir(), ".pi", "dashboard", "config.json");
|
|
166
|
+
const now = deps.now ?? Date.now;
|
|
167
|
+
const sleep = deps.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));
|
|
168
|
+
const isPidAlive = deps.isPidAlive ?? ((pid: number) => isProcessAlive(pid, { epermAlive: true }));
|
|
169
|
+
const writeConfig = deps.writeConfig ?? writeConfigFileSecure;
|
|
170
|
+
|
|
171
|
+
const lockPath = path.join(localDir, "rotation.lock");
|
|
172
|
+
const epochPath = path.join(localDir, "rotation-epoch");
|
|
173
|
+
const tokenPath = path.join(localDir, "token");
|
|
174
|
+
const paths = { lock: lockPath, epoch: epochPath };
|
|
175
|
+
const fail = (step: RotationError["step"], err: unknown): never => {
|
|
176
|
+
throw new RotationError(step, err instanceof Error ? err.message : String(err), paths);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// Cheap path: already rotated, no lock needed.
|
|
180
|
+
if (readEpoch(epochPath) >= REQUIRED_ROTATION_EPOCH) return { rotated: false };
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
fs.mkdirSync(localDir, { recursive: true, mode: 0o700 });
|
|
184
|
+
try {
|
|
185
|
+
fs.chmodSync(localDir, 0o700);
|
|
186
|
+
} catch {
|
|
187
|
+
/* best-effort (Windows) */
|
|
188
|
+
}
|
|
189
|
+
} catch (err) {
|
|
190
|
+
return fail("lock", err);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const started = now();
|
|
194
|
+
let claimed = false;
|
|
195
|
+
const stalePath = () => `${lockPath}.stale.${process.pid}.${crypto.randomUUID()}`;
|
|
196
|
+
while (!claimed) {
|
|
197
|
+
try {
|
|
198
|
+
claimed = tryClaim(lockPath, now, isPidAlive, stalePath);
|
|
199
|
+
} catch (err) {
|
|
200
|
+
return fail("lock", err);
|
|
201
|
+
}
|
|
202
|
+
if (claimed) break;
|
|
203
|
+
if (now() - started >= ROTATION_LOCK_WAIT_CAP_MS) {
|
|
204
|
+
return fail("lock", new Error(`rotation.lock still held after ${ROTATION_LOCK_WAIT_CAP_MS / 1000}s`));
|
|
205
|
+
}
|
|
206
|
+
await sleep(POLL_MS);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
// Another process may have finished while we waited.
|
|
211
|
+
if (readEpoch(epochPath) >= REQUIRED_ROTATION_EPOCH) return { rotated: false };
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
atomicWrite(tokenPath, crypto.randomBytes(TOKEN_BYTES).toString("base64url"));
|
|
215
|
+
} catch (err) {
|
|
216
|
+
return fail("token", err);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
let newSecret: string | undefined;
|
|
220
|
+
try {
|
|
221
|
+
if (fs.existsSync(configFile)) {
|
|
222
|
+
const parsed = JSON.parse(fs.readFileSync(configFile, "utf-8")) as { auth?: { secret?: unknown } } | null;
|
|
223
|
+
if (parsed && typeof parsed === "object" && parsed.auth && typeof parsed.auth.secret === "string" && parsed.auth.secret) {
|
|
224
|
+
newSecret = crypto.randomBytes(32).toString("hex"); // 32 bytes, as `ensureAuthSecret` (harden-auth-credentials B17)
|
|
225
|
+
parsed.auth.secret = newSecret;
|
|
226
|
+
writeConfig(configFile, `${JSON.stringify(parsed, null, 2)}\n`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
} catch (err) {
|
|
230
|
+
return fail("secret", err);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
atomicWrite(epochPath, `${REQUIRED_ROTATION_EPOCH}\n`);
|
|
235
|
+
} catch (err) {
|
|
236
|
+
return fail("epoch", err);
|
|
237
|
+
}
|
|
238
|
+
return { rotated: true, ...(newSecret ? { newSecret } : {}) };
|
|
239
|
+
} finally {
|
|
240
|
+
try {
|
|
241
|
+
fs.unlinkSync(lockPath);
|
|
242
|
+
} catch {
|
|
243
|
+
/* already gone */
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One per-request "is this origin secure?" predicate for every cookie the
|
|
3
|
+
* dashboard sets with a `Secure` attribute: the OAuth state cookie, the
|
|
4
|
+
* `pi_dash_token` session cookie and the `pi_dash_device` cookie.
|
|
5
|
+
*
|
|
6
|
+
* `isSecureOrigin(request, baseIsHttps)` is true when ANY of:
|
|
7
|
+
* 1. the socket itself is TLS;
|
|
8
|
+
* 2. the resolved redirect base is https (`resolveRedirectBase`) — passed in as
|
|
9
|
+
* `baseIsHttps` so this module stays a leaf with no `auth.ts` import;
|
|
10
|
+
* 3. the SOCKET peer is loopback AND the request `Host` equals (host AND port,
|
|
11
|
+
* compared with `canonicalOrigin`) an **https** entry of `liveTunnelOrigins()`.
|
|
12
|
+
* An `http:` tunnel origin never qualifies.
|
|
13
|
+
*
|
|
14
|
+
* It keys on the socket peer, never a forwarding header: Fastify runs with
|
|
15
|
+
* `trustProxy` OFF on purpose, and header-injecting tunnels (zrok, cloudflared)
|
|
16
|
+
* still arrive from a loopback socket. A forged `Host` can only make a cookie
|
|
17
|
+
* STRICTER (`Secure`), never weaker. Any malformed `Host` or tunnel list yields
|
|
18
|
+
* `false` — the predicate never throws.
|
|
19
|
+
*
|
|
20
|
+
* See change: harden-auth-credentials (D3, B16r).
|
|
21
|
+
*/
|
|
22
|
+
import type { TLSSocket } from "node:tls";
|
|
23
|
+
import type { FastifyRequest } from "fastify";
|
|
24
|
+
import { liveTunnelOrigins } from "../tunnel/tunnel.js";
|
|
25
|
+
import { canonicalOrigin } from "./cors-origin.js";
|
|
26
|
+
import { isLoopbackRange } from "./loopback.js";
|
|
27
|
+
|
|
28
|
+
/** A `Host` is only host[:port]: letters, digits, `.`, `-`, IPv6 `[]:`. Positive allowlist so `a@b`, `a/x`, `a#`… never parse into a bare host. */
|
|
29
|
+
const HOST_CHARS = /^[A-Za-z0-9.\-[\]:]+$/;
|
|
30
|
+
|
|
31
|
+
function hostIsLiveHttpsTunnel(request: FastifyRequest): boolean {
|
|
32
|
+
try {
|
|
33
|
+
const peer = request.raw.socket?.remoteAddress;
|
|
34
|
+
if (!peer || !isLoopbackRange(peer)) return false;
|
|
35
|
+
const host = request.headers.host;
|
|
36
|
+
if (typeof host !== "string" || !HOST_CHARS.test(host)) return false;
|
|
37
|
+
const requestOrigin = canonicalOrigin(`https://${host}`);
|
|
38
|
+
if (!requestOrigin) return false;
|
|
39
|
+
const origins = liveTunnelOrigins();
|
|
40
|
+
if (!Array.isArray(origins)) return false;
|
|
41
|
+
return origins.some((raw) => {
|
|
42
|
+
if (typeof raw !== "string") return false;
|
|
43
|
+
const origin = canonicalOrigin(raw);
|
|
44
|
+
return origin?.startsWith("https://") === true && origin === requestOrigin;
|
|
45
|
+
});
|
|
46
|
+
} catch {
|
|
47
|
+
// A throwing tunnel module / odd socket means "no live tunnel", never a 500.
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function isSecureOrigin(request: FastifyRequest, baseIsHttps: boolean): boolean {
|
|
53
|
+
if ((request.raw.socket as TLSSocket | undefined)?.encrypted) return true;
|
|
54
|
+
if (baseIsHttps) return true;
|
|
55
|
+
return hostIsLiveHttpsTunnel(request);
|
|
56
|
+
}
|