@blackbelt-technology/pi-agent-dashboard 0.9.0 → 0.10.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/README.md +38 -5
- package/docs/architecture.md +1673 -75
- package/package.json +23 -15
- package/packages/extension/package.json +8 -8
- package/packages/extension/src/ask-user-tool.ts +65 -3
- package/packages/extension/src/auto-session-namer.ts +190 -50
- package/packages/extension/src/bridge-handlers/dispatch.ts +450 -0
- package/packages/extension/src/bridge-handlers/forwarders.ts +616 -0
- package/packages/extension/src/bridge-handlers/instance.ts +220 -0
- package/packages/extension/src/bridge-handlers/lifecycle.ts +1030 -0
- package/packages/extension/src/bridge-handlers/ui-prompt-patch.ts +198 -0
- package/packages/extension/src/bridge-identity-log.ts +45 -0
- package/packages/extension/src/bridge.ts +480 -2510
- package/packages/extension/src/command-handler.ts +180 -11
- package/packages/extension/src/connection.ts +265 -18
- package/packages/extension/src/dashboard-context-injector.ts +26 -47
- package/packages/extension/src/empty-actionable-guard.ts +8 -3
- package/packages/extension/src/flow-event-wiring.ts +1 -1
- package/packages/extension/src/followup-buffer.ts +1 -1
- package/packages/extension/src/followup-drain.ts +79 -0
- package/packages/extension/src/local-token-header.ts +161 -17
- package/packages/extension/src/message-update-coalescer.ts +3 -1
- package/packages/extension/src/model-refresh.ts +19 -0
- package/packages/extension/src/multiselect-list.ts +6 -2
- package/packages/extension/src/multiselect-polyfill.ts +2 -2
- package/packages/extension/src/openspec-cli-shim.ts +1 -1
- package/packages/extension/src/path-gate/decide.ts +15 -41
- package/packages/extension/src/path-gate/grant-cache.ts +3 -1
- package/packages/extension/src/path-gate/handler.ts +51 -2
- package/packages/extension/src/path-gate/index.ts +12 -1
- package/packages/extension/src/path-gate/resolve.ts +6 -124
- package/packages/extension/src/path-gate/roots.ts +4 -1
- package/packages/extension/src/path-gate/suppression.ts +4 -1
- package/packages/extension/src/path-gate/yolo-link.ts +104 -0
- package/packages/extension/src/plugin-event-forward-registry.ts +200 -0
- package/packages/extension/src/project-trust.ts +119 -25
- package/packages/extension/src/prompt-expander.ts +5 -1
- package/packages/extension/src/prompt-meta.ts +64 -0
- package/packages/extension/src/provider-register.ts +87 -20
- package/packages/extension/src/role-manager.ts +5 -4
- package/packages/extension/src/server-auto-start.ts +12 -5
- package/packages/extension/src/server-launcher.ts +11 -13
- package/packages/extension/src/session-move.ts +19 -0
- package/packages/extension/src/session-sync.ts +31 -38
- package/packages/extension/src/spawn-token.ts +46 -0
- package/packages/extension/src/stream-forward-buffer.ts +161 -0
- package/packages/extension/src/subagent-frame-buffer.ts +6 -2
- package/packages/extension/src/subagent-tick-throttle.ts +3 -1
- package/packages/extension/src/vault-delivery.ts +394 -0
- package/packages/extension/src/vault-forwarder.ts +128 -0
- package/packages/extension/src/vault-masker.ts +165 -0
- package/packages/extension/src/vault-redaction.ts +181 -0
- package/packages/extension/src/vcs-info.ts +5 -2
- package/packages/server/bin/pi-dashboard.mjs +26 -89
- package/packages/server/package.json +29 -15
- package/packages/server/src/access/access-grants.ts +18 -0
- package/packages/server/src/access/agent-confirm-registry.ts +31 -13
- package/packages/server/src/access/agent-grant.ts +1 -1
- package/packages/server/src/access/agent-yolo.ts +91 -0
- package/packages/server/src/access/containment-gate.ts +28 -1
- package/packages/server/src/access/cwd-admission-log.ts +46 -0
- package/packages/server/src/access/cwd-verdict.ts +144 -0
- package/packages/server/src/access/grant-coordinator.ts +3 -1
- package/packages/server/src/access/pi-trust-mirror-service.ts +374 -0
- package/packages/server/src/access/planes.ts +10 -1
- package/packages/server/src/access/refusal-ledger.ts +6 -6
- package/packages/server/src/access/yolo-session.ts +73 -12
- package/packages/server/src/acp-bridge/acp-connection.ts +225 -0
- package/packages/server/src/acp-bridge/acp-restore.ts +41 -0
- package/packages/server/src/acp-bridge/acp-session-adapter.ts +544 -0
- package/packages/server/src/acp-bridge/child-env.ts +73 -0
- package/packages/server/src/acp-bridge/event-log.ts +194 -0
- package/packages/server/src/acp-bridge/gateway-client.ts +136 -0
- package/packages/server/src/acp-bridge/local-bridge-socket.ts +67 -0
- package/packages/server/src/acp-bridge/local-host.ts +132 -0
- package/packages/server/src/acp-bridge/main.ts +118 -0
- package/packages/server/src/acp-bridge/models.ts +33 -0
- package/packages/server/src/acp-bridge/permission-bridge.ts +106 -0
- package/packages/server/src/acp-bridge/server-requests.ts +136 -0
- package/packages/server/src/acp-bridge/session-state.ts +127 -0
- package/packages/server/src/addons/addon-toggle-restore.ts +110 -0
- package/packages/server/src/addons/addons-catalog.ts +408 -0
- package/packages/server/src/addons/addons-folder-service.ts +347 -0
- package/packages/server/src/addons/addons-folder.ts +310 -0
- package/packages/server/src/addons/addons-service.ts +470 -0
- package/packages/server/src/addons/package-contents.ts +99 -0
- package/packages/server/src/addons/settings-entries.ts +120 -0
- package/packages/server/src/attachments/fit-worker-pool.ts +12 -17
- package/packages/server/src/auth/approval-refusal.ts +87 -0
- package/packages/server/src/auth/auth-plugin.ts +292 -79
- package/packages/server/src/auth/auth.ts +78 -7
- package/packages/server/src/auth/bearer-auth.ts +23 -1
- package/packages/server/src/auth/cors-origin.ts +10 -0
- package/packages/server/src/auth/csp.ts +6 -0
- package/packages/server/src/auth/host-admission.ts +26 -0
- package/packages/server/src/auth/host-gate.ts +6 -1
- package/packages/server/src/auth/local-credential.ts +258 -0
- package/packages/server/src/auth/local-proof.ts +7 -4
- package/packages/server/src/auth/local-token.ts +16 -0
- package/packages/server/src/auth/localhost-guard.ts +251 -8
- package/packages/server/src/auth/network-trust-telemetry.ts +126 -0
- package/packages/server/src/auth/node-guard.ts +43 -0
- package/packages/server/src/auth/operate-required.ts +126 -0
- package/packages/server/src/auth/passkey/challenge-store.ts +90 -0
- package/packages/server/src/auth/passkey/passkey-log.ts +34 -0
- package/packages/server/src/auth/passkey/passkey-pages.ts +173 -0
- package/packages/server/src/auth/passkey/passkey-routes.ts +224 -0
- package/packages/server/src/auth/passkey/passkey-service.ts +247 -0
- package/packages/server/src/auth/passkey/phone-signin.ts +211 -0
- package/packages/server/src/auth/passkey/requester.ts +83 -0
- package/packages/server/src/auth/passkey/rp-context.ts +98 -0
- package/packages/server/src/auth/passkey/user-directory.ts +348 -0
- package/packages/server/src/auth/pi-legacy-credentials.ts +321 -0
- package/packages/server/src/auth/provider-auth-registry.ts +35 -1
- package/packages/server/src/auth/provider-auth-storage.ts +55 -8
- package/packages/server/src/auth/radius-override.ts +13 -0
- package/packages/server/src/auth/route-tier-gate.ts +27 -11
- package/packages/server/src/auth/runtime-provider-catalogue.ts +107 -0
- package/packages/server/src/auth/scan-legacy-provider-ids.ts +80 -0
- package/packages/server/src/auth/serve-app-host.ts +97 -0
- package/packages/server/src/auth/trust-mode-boot.ts +93 -0
- package/packages/server/src/auth/trust-mode.ts +83 -0
- package/packages/server/src/auto-name-outcome-store.ts +4 -1
- package/packages/server/src/browse.ts +114 -2
- package/packages/server/src/browser-handlers/acp-guard.ts +79 -0
- package/packages/server/src/browser-handlers/directory-handler.ts +128 -6
- package/packages/server/src/browser-handlers/session-action-handler.ts +129 -62
- package/packages/server/src/browser-handlers/session-meta-handler.ts +16 -6
- package/packages/server/src/browser-handlers/spawn-choice.ts +99 -0
- package/packages/server/src/cli.ts +883 -97
- package/packages/server/src/config-api.ts +143 -1
- package/packages/server/src/config-snapshot.ts +5 -0
- package/packages/server/src/directory-service.ts +435 -32
- package/packages/server/src/embed-lifecycle/caps.ts +35 -4
- package/packages/server/src/embed-lifecycle/embed-lifecycle-controller.ts +45 -4
- package/packages/server/src/embed-lifecycle/idle-reaper.ts +195 -20
- package/packages/server/src/embed-lifecycle/lifecycle-metrics.ts +38 -0
- package/packages/server/src/embed-lifecycle/plugin-session-close.ts +123 -0
- package/packages/server/src/embed-lifecycle/quiescence.ts +29 -3
- package/packages/server/src/embed-lifecycle/reclaim-give-up.ts +54 -0
- package/packages/server/src/event-wiring.ts +208 -35
- package/packages/server/src/file-watch-manager.ts +187 -39
- package/packages/server/src/git-worktree/git-operations.ts +16 -5
- package/packages/server/src/git-worktree/worktree-init-trust.ts +48 -5
- package/packages/server/src/git-worktree/worktree-init.ts +7 -56
- package/packages/server/src/identity/host-resources.ts +3 -0
- package/packages/server/src/identity/http-road-classification.ts +18 -0
- package/packages/server/src/identity/identity-registration-tracker.ts +4 -1
- package/packages/server/src/identity/plugin-identity.ts +19 -0
- package/packages/server/src/identity/policy-registry.ts +4 -1
- package/packages/server/src/identity/resolver-registry.ts +12 -3
- package/packages/server/src/identity/ws-message-scope.ts +9 -0
- package/packages/server/src/identity/ws-road-classification.ts +9 -0
- package/packages/server/src/known-server-moves.ts +175 -0
- package/packages/server/src/lib/browse-roots.ts +36 -0
- package/packages/server/src/lib/digest-gate.ts +82 -0
- package/packages/server/src/lib/file-write-lock.ts +24 -0
- package/packages/server/src/lib/folder-project.ts +439 -0
- package/packages/server/src/lib/purify.ts +13 -5
- package/packages/server/src/lib/writable-handler-target.ts +66 -0
- package/packages/server/src/lib/writable-md-target.ts +12 -1
- package/packages/server/src/lifecycle/classify-dashboard-port.ts +134 -0
- package/packages/server/src/lifecycle/home-lock.ts +344 -130
- package/packages/server/src/lifecycle/home-rendezvous.ts +41 -11
- package/packages/server/src/lifecycle/recovery-server.ts +77 -1
- package/packages/server/src/lifecycle/stop-ownership.ts +97 -11
- package/packages/server/src/live-server/live-capability.ts +254 -0
- package/packages/server/src/live-server/live-server-proxy.ts +173 -26
- package/packages/server/src/metrics/eventloop-spike-metrics.ts +2 -2
- package/packages/server/src/model-proxy/internal-auth-storage.ts +27 -26
- package/packages/server/src/model-proxy/internal-registry.ts +6 -0
- package/packages/server/src/model-proxy/server-model-runtime.ts +6 -0
- package/packages/server/src/openspec/openspec-change-watcher.ts +109 -16
- package/packages/server/src/openspec/openspec-poll-fs-helpers.ts +132 -0
- package/packages/server/src/openspec/openspec-poll-worker-pool.ts +90 -35
- package/packages/server/src/openspec/openspec-poll-worker.ts +200 -44
- package/packages/server/src/openspec/proposal-attach-naming.ts +5 -3
- package/packages/server/src/openspec/readiness.ts +9 -0
- package/packages/server/src/openspec/workflow-schema.ts +1128 -0
- package/packages/server/src/package/installed-package-enricher.ts +38 -3
- package/packages/server/src/package/package-health-scanner.ts +133 -0
- package/packages/server/src/package/package-manager-wrapper.ts +18 -2
- package/packages/server/src/package/provider-catalogue-cache.ts +17 -3
- package/packages/server/src/package/provider-probe.ts +5 -3
- package/packages/server/src/pairing/browser-gateway.ts +108 -8
- package/packages/server/src/pairing/knock-admission.ts +277 -0
- package/packages/server/src/pairing/pairing.ts +154 -27
- package/packages/server/src/pairing/subagent-resync-routing.ts +4 -1
- package/packages/server/src/pairing/ws-tier-gate.ts +34 -0
- package/packages/server/src/peer-servers.ts +77 -0
- package/packages/server/src/pending/pending-plugin-ref-registry.ts +57 -2
- package/packages/server/src/persistence/boot-state.ts +48 -1
- package/packages/server/src/persistence/memory-event-store.ts +216 -237
- package/packages/server/src/persistence/meta-persistence.ts +12 -1
- package/packages/server/src/persistence/preferences-store.ts +116 -7
- package/packages/server/src/pi/bridge-upgrade-auth.ts +32 -21
- package/packages/server/src/pi/folder-commands.ts +141 -0
- package/packages/server/src/pi/gateway-socket-bind.ts +9 -3
- package/packages/server/src/pi/gateway-transport-policy.ts +13 -0
- package/packages/server/src/pi/host-project-trust.ts +158 -33
- package/packages/server/src/pi/pi-gateway.ts +93 -9
- package/packages/server/src/pi/pi-resource-scanner.ts +15 -0
- package/packages/server/src/pi/resource-activation-toggle.ts +6 -0
- package/packages/server/src/pi/resource-toggle-trust.ts +34 -2
- package/packages/server/src/pi/session-skill-registry.ts +13 -3
- package/packages/server/src/pi/settings-write-lock.ts +32 -0
- package/packages/server/src/pi/trust-challenges.ts +38 -0
- package/packages/server/src/pi-agent-settings.ts +701 -100
- package/packages/server/src/pi-settings-themes.ts +104 -0
- package/packages/server/src/questions/brief-generator.ts +138 -0
- package/packages/server/src/questions/brief-model.ts +55 -0
- package/packages/server/src/questions/brief-policy.ts +36 -0
- package/packages/server/src/questions/brief-prompt.ts +92 -0
- package/packages/server/src/questions/brief-runtime.ts +50 -0
- package/packages/server/src/questions/question-answer.ts +55 -0
- package/packages/server/src/questions/question-broadcast.ts +106 -0
- package/packages/server/src/questions/question-files.ts +128 -0
- package/packages/server/src/questions/question-routes.ts +126 -0
- package/packages/server/src/questions/question-snapshot.ts +150 -0
- package/packages/server/src/questions/question-stack.ts +57 -0
- package/packages/server/src/questions/question-store.ts +477 -0
- package/packages/server/src/routes/access-prompt-routes.ts +5 -3
- package/packages/server/src/routes/access-routes.ts +123 -5
- package/packages/server/src/routes/acme-routes.ts +190 -0
- package/packages/server/src/routes/acp-routes.ts +16 -0
- package/packages/server/src/routes/addons-routes.ts +221 -0
- package/packages/server/src/routes/file-routes.ts +366 -75
- package/packages/server/src/routes/file-write-binary-routes.ts +473 -0
- package/packages/server/src/routes/folder-project-routes.ts +99 -0
- package/packages/server/src/routes/git-routes.ts +17 -2
- package/packages/server/src/routes/known-servers-routes.ts +333 -53
- package/packages/server/src/routes/live-server-routes.ts +72 -4
- package/packages/server/src/routes/local-proof-routes.ts +12 -7
- package/packages/server/src/routes/mermaid-repair-routes.ts +254 -0
- package/packages/server/src/routes/model-proxy-routes.ts +4 -2
- package/packages/server/src/routes/openspec-routes.ts +270 -40
- package/packages/server/src/routes/package-health-routes.ts +140 -0
- package/packages/server/src/routes/package-routes.ts +28 -1
- package/packages/server/src/routes/pairing-routes.ts +158 -24
- package/packages/server/src/routes/pi-retry-routes.ts +18 -2
- package/packages/server/src/routes/pi-settings-routes.ts +264 -0
- package/packages/server/src/routes/plugin-activation-routes.ts +87 -27
- package/packages/server/src/routes/provider-auth-routes.ts +22 -5
- package/packages/server/src/routes/provider-routes.ts +29 -0
- package/packages/server/src/routes/resource-activation-routes.ts +16 -32
- package/packages/server/src/routes/session-routes.ts +49 -5
- package/packages/server/src/routes/system-routes.ts +207 -23
- package/packages/server/src/routes/tls-routes.ts +55 -0
- package/packages/server/src/routes/user-routes.ts +192 -0
- package/packages/server/src/runtime-overlay/runtime-stager.ts +15 -2
- package/packages/server/src/runtime-overlay/runtime-update-checker.ts +11 -2
- package/packages/server/src/server.ts +919 -139
- package/packages/server/src/services/attached-driver.ts +175 -0
- package/packages/server/src/services/change-notifier.ts +55 -0
- package/packages/server/src/services/cli-service.ts +450 -0
- package/packages/server/src/services/command-runner.ts +92 -0
- package/packages/server/src/services/definitions-store.ts +121 -0
- package/packages/server/src/services/driver.ts +112 -0
- package/packages/server/src/services/health.ts +94 -0
- package/packages/server/src/services/leases.ts +117 -0
- package/packages/server/src/services/log-text.ts +97 -0
- package/packages/server/src/services/native-driver.ts +283 -0
- package/packages/server/src/services/oci-driver.ts +590 -0
- package/packages/server/src/services/output-capture.ts +183 -0
- package/packages/server/src/services/paths.ts +82 -0
- package/packages/server/src/services/ports.ts +26 -0
- package/packages/server/src/services/routes.ts +140 -0
- package/packages/server/src/services/runtime-detect.ts +108 -0
- package/packages/server/src/services/secret-delivery.ts +61 -0
- package/packages/server/src/services/secrets-resolver.ts +145 -0
- package/packages/server/src/services/secrets-store.ts +137 -0
- package/packages/server/src/services/service-manager.ts +1565 -0
- package/packages/server/src/services/start-record.ts +66 -0
- package/packages/server/src/services/state-machine.ts +241 -0
- package/packages/server/src/session/archive-sweeper.ts +92 -3
- package/packages/server/src/session/custom-event-group-matcher.ts +4 -3
- package/packages/server/src/session/in-flight-tool-calls.ts +74 -0
- package/packages/server/src/session/memory-session-manager.ts +53 -0
- package/packages/server/src/session/name-provenance.ts +102 -0
- package/packages/server/src/session/open-tool-calls.ts +15 -6
- package/packages/server/src/session/session-api.ts +47 -12
- package/packages/server/src/session/session-archive.ts +56 -4
- package/packages/server/src/session/session-diff-cache.ts +11 -4
- package/packages/server/src/session/session-load-worker-pool.ts +4 -3
- package/packages/server/src/session/session-scanner.ts +73 -3
- package/packages/server/src/session/session-to-meta.ts +15 -0
- package/packages/server/src/spawn-process/abort-spawned-run.ts +55 -0
- package/packages/server/src/spawn-process/acp-spawn.ts +131 -0
- package/packages/server/src/spawn-process/process-manager.ts +164 -30
- package/packages/server/src/spawn-process/restart-helper.ts +9 -8
- package/packages/server/src/spawn-process/server-pid.ts +16 -7
- package/packages/server/src/spawn-process/session-terminator.ts +218 -0
- package/packages/server/src/spawn-process/spawn-env-contributors.ts +159 -0
- package/packages/server/src/spawn-process/spawn-register-watchdog.ts +10 -1
- package/packages/server/src/terminal/terminal-manager.ts +67 -1
- package/packages/server/src/tls/cert-files.ts +228 -0
- package/packages/server/src/tls/cert-manager.ts +703 -0
- package/packages/server/src/tls/cert-store.ts +69 -0
- package/packages/server/src/tls/source-registry.ts +71 -0
- package/packages/server/src/tls/sources/acme/accounts.ts +76 -0
- package/packages/server/src/tls/sources/acme/acme-source.ts +508 -0
- package/packages/server/src/tls/sources/acme/config-projection.ts +34 -0
- package/packages/server/src/tls/sources/acme/credentials.ts +185 -0
- package/packages/server/src/tls/sources/acme/dns.ts +303 -0
- package/packages/server/src/tls/sources/acme/e2e-overrides.ts +49 -0
- package/packages/server/src/tls/sources/acme/entry.ts +180 -0
- package/packages/server/src/tls/sources/acme/gc.ts +78 -0
- package/packages/server/src/tls/sources/acme/runtime.ts +51 -0
- package/packages/server/src/tls/sources/acme/solvers/acme-dns.ts +154 -0
- package/packages/server/src/tls/sources/acme/solvers/cloudflare.ts +147 -0
- package/packages/server/src/tls/sources/acme/types.ts +72 -0
- package/packages/server/src/tls/sources/tailscale-cert.ts +172 -0
- package/packages/server/src/tls/tls-config-validation.ts +92 -0
- package/packages/server/src/tls/tls-listener.ts +351 -0
- package/packages/server/src/tls/types.ts +80 -0
- package/packages/server/src/tunnel/tunnel-block-events.ts +4 -1
- package/packages/server/src/tunnel/tunnel-core.ts +4 -1
- package/packages/server/src/tunnel/tunnel-endpoints.ts +4 -0
- package/packages/server/src/tunnel-providers/tailscale.ts +27 -6
- package/packages/server/src/vault/vault-bindings.ts +101 -0
- package/packages/server/src/vault/vault-delivery-hub.ts +294 -0
- package/packages/server/src/vault/vault-grants.ts +260 -0
- package/packages/server/src/vault/vault-import.ts +161 -0
- package/packages/server/src/vault/vault-permissions.ts +39 -0
- package/packages/server/src/vault/vault-plugin-read.ts +64 -0
- package/packages/server/src/vault/vault-projects.ts +156 -0
- package/packages/server/src/vault/vault-routes.ts +277 -0
- package/packages/server/src/vault/vault-store.ts +371 -0
- package/packages/shared/package.json +5 -2
- package/packages/shared/src/acp/acp-config.ts +123 -0
- package/packages/shared/src/acp/normalize-v1.ts +92 -0
- package/packages/shared/src/acp/paths.ts +36 -0
- package/packages/shared/src/acp/to-dashboard-events.ts +533 -0
- package/packages/shared/src/acp/update-model.ts +574 -0
- package/packages/shared/src/anthropic-messages-source.ts +8 -0
- package/packages/shared/src/bind-reachability.ts +21 -2
- package/packages/shared/src/boot-state.ts +6 -0
- package/packages/shared/src/browser-host-identity.ts +49 -0
- package/packages/shared/src/browser-host-ipc.ts +107 -0
- package/packages/shared/src/browser-protocol.ts +385 -71
- package/packages/shared/src/canonical-subject.ts +27 -2
- package/packages/shared/src/card-sections.ts +185 -1
- package/packages/shared/src/chrome-profiles.ts +188 -0
- package/packages/shared/src/config.ts +473 -26
- package/packages/shared/src/custom-event-groups.ts +5 -2
- package/packages/shared/src/cwd-guard.ts +381 -1
- package/packages/shared/src/dashboard-paths.ts +12 -0
- package/packages/shared/src/dashboard-plugin/editor-pane-tab.ts +26 -0
- package/packages/shared/src/dashboard-plugin/file-handler.ts +71 -0
- package/packages/shared/src/dashboard-plugin/index.ts +7 -3
- package/packages/shared/src/dashboard-plugin/manifest-types.ts +106 -2
- package/packages/shared/src/dashboard-plugin/project-identity.ts +42 -0
- package/packages/shared/src/dashboard-plugin/project-markers.ts +60 -0
- package/packages/shared/src/dashboard-plugin/slot-props.ts +95 -0
- package/packages/shared/src/dashboard-plugin/slot-types.ts +64 -4
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +22 -3
- package/packages/shared/src/dashboard-starter.ts +30 -0
- package/packages/shared/src/display-name.ts +42 -0
- package/packages/shared/src/doctor-core.ts +25 -107
- package/packages/shared/src/enabled-models-match.ts +47 -0
- package/packages/shared/src/event-forward-declaration.ts +187 -0
- package/packages/shared/src/file-kind.ts +54 -2
- package/packages/shared/src/git-worktree-helpers.ts +8 -4
- package/packages/shared/src/grammar-types.ts +3 -1
- package/packages/shared/src/host-admission.ts +2 -1
- package/packages/shared/src/host-port.ts +34 -0
- package/packages/shared/src/launcher-origin.ts +26 -0
- package/packages/shared/src/local-credential-proof.ts +219 -0
- package/packages/shared/src/markup-repair/asciidoc.ts +130 -0
- package/packages/shared/src/markup-repair/types.ts +13 -0
- package/packages/shared/src/md-table-cell.ts +11 -0
- package/packages/shared/src/mdns-discovery.ts +392 -42
- package/packages/shared/src/models-json-reader.ts +6 -0
- package/packages/shared/src/openspec-design-evidence.ts +25 -1
- package/packages/shared/src/openspec-poller.ts +210 -9
- package/packages/shared/src/openspec-status-all.ts +393 -0
- package/packages/shared/src/operate-required.ts +29 -0
- package/packages/shared/src/pairing-knock-client.ts +261 -0
- package/packages/shared/src/pairing-knock.ts +71 -0
- package/packages/shared/src/pairing-refusal.ts +32 -0
- package/packages/shared/src/path-containment-guard-name.ts +8 -0
- package/packages/shared/src/pi-settings-keys.ts +9 -0
- package/packages/shared/src/pi-settings-schema.ts +285 -0
- package/packages/shared/src/pi-trust-mirror-snapshot.ts +200 -0
- package/packages/shared/src/pi-trust-mirror-types.ts +57 -0
- package/packages/shared/src/pi-trust-mirror.ts +328 -0
- package/packages/shared/src/platform/binary-lookup.ts +2 -194
- package/packages/shared/src/platform/git.ts +204 -4
- package/packages/shared/src/platform/native-ts-hooks.mjs +93 -40
- package/packages/shared/src/platform/native-ts-register.mjs +22 -10
- package/packages/shared/src/platform/native-ts-tier.d.mts +1 -0
- package/packages/shared/src/platform/native-ts-tier.mjs +17 -0
- package/packages/shared/src/platform/node-spawn.ts +1 -2
- package/packages/shared/src/platform/openspec.ts +129 -31
- package/packages/shared/src/platform/process-scan.ts +199 -1
- package/packages/shared/src/platform/process.ts +51 -0
- package/packages/shared/src/platform/runner.ts +46 -2
- package/packages/shared/src/platform/spawn-mechanism.ts +14 -2
- package/packages/shared/src/platform/ts-loader-select.d.mts +1 -7
- package/packages/shared/src/platform/ts-loader-select.mjs +14 -43
- package/packages/shared/src/plugin-bridge-register.ts +24 -1
- package/packages/shared/src/project-name.ts +42 -0
- package/packages/shared/src/prompt-answer.ts +183 -0
- package/packages/shared/src/protocol.ts +133 -8
- package/packages/shared/src/question-types.ts +73 -0
- package/packages/shared/src/recommended-extensions.ts +80 -23
- package/packages/shared/src/recovery-timing.ts +12 -0
- package/packages/shared/src/redaction.ts +50 -0
- package/packages/shared/src/rest-api.ts +484 -1
- package/packages/shared/src/role-config-disk.ts +23 -0
- package/packages/shared/src/role-schema.ts +79 -0
- package/packages/shared/src/route-tiers.ts +127 -0
- package/packages/shared/src/runtime-overlay/materialize-plugins.d.mts +17 -0
- package/packages/shared/src/runtime-overlay/materialize-plugins.mjs +119 -8
- package/packages/shared/src/server-identity.ts +51 -15
- package/packages/shared/src/server-launcher.ts +12 -33
- package/packages/shared/src/server-pinning.ts +81 -0
- package/packages/shared/src/services/offers.ts +114 -0
- package/packages/shared/src/services/schema.ts +628 -0
- package/packages/shared/src/services/status.ts +169 -0
- package/packages/shared/src/session-meta.ts +59 -2
- package/packages/shared/src/session-name.ts +8 -0
- package/packages/shared/src/skill-block-parser.ts +14 -0
- package/packages/shared/src/source-matching.ts +5 -1
- package/packages/shared/src/state-replay.ts +19 -0
- package/packages/shared/src/terminal-safe.ts +24 -0
- package/packages/shared/src/tls-status.ts +69 -0
- package/packages/shared/src/tool-path-resolve.ts +243 -0
- package/packages/shared/src/tool-registry/definitions.ts +26 -4
- package/packages/shared/src/tool-registry/index.ts +2 -2
- package/packages/shared/src/tool-registry/pi-tools.ts +32 -3
- package/packages/shared/src/tunnel-provider.ts +3 -2
- package/packages/shared/src/types.ts +104 -9
- package/packages/shared/src/vault-delivery.ts +73 -0
- package/packages/shared/src/vault-origin.ts +51 -0
- package/packages/shared/src/vault-project-key.ts +53 -0
- package/packages/shared/src/worktree-init-hook.ts +69 -0
- package/packages/shared/src/ws-message-tiers.ts +135 -0
- package/tsconfig.base.json +1 -0
|
@@ -6,6 +6,7 @@ import crypto from "node:crypto";
|
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import type { AuthConfig, AuthProviderConfig } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
8
8
|
import { CONFIG_FILE, writeConfigFileSecure } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
9
|
+
import { isTier, rank, type Tier } from "@blackbelt-technology/pi-dashboard-shared/tiers.js";
|
|
9
10
|
import jwt from "jsonwebtoken";
|
|
10
11
|
import { getTunnelUrl } from "../tunnel/tunnel.js";
|
|
11
12
|
|
|
@@ -23,10 +24,16 @@ export interface ResolvedProvider {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
export interface AuthUser {
|
|
26
|
-
sub: string; // email
|
|
27
|
+
sub: string; // email (OAuth) or directory user id (passkey)
|
|
27
28
|
name: string;
|
|
28
29
|
username: string;
|
|
29
30
|
provider: string;
|
|
31
|
+
/**
|
|
32
|
+
* Session tier. Optional: a JWT issued before this claim existed has none
|
|
33
|
+
* and is treated as `operate` on validation.
|
|
34
|
+
* See change: add-passkey-user-auth (D4).
|
|
35
|
+
*/
|
|
36
|
+
tier?: Tier;
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
export interface TokenPayload extends AuthUser {
|
|
@@ -157,7 +164,13 @@ export const COOKIE_NAME = "pi_dash_token";
|
|
|
157
164
|
|
|
158
165
|
export function signToken(user: AuthUser, secret: string): string {
|
|
159
166
|
return jwt.sign(
|
|
160
|
-
{
|
|
167
|
+
{
|
|
168
|
+
sub: user.sub,
|
|
169
|
+
name: user.name,
|
|
170
|
+
username: user.username,
|
|
171
|
+
provider: user.provider,
|
|
172
|
+
...(user.tier ? { tier: user.tier } : {}),
|
|
173
|
+
},
|
|
161
174
|
secret,
|
|
162
175
|
{ expiresIn: TOKEN_EXPIRY },
|
|
163
176
|
);
|
|
@@ -166,12 +179,60 @@ export function signToken(user: AuthUser, secret: string): string {
|
|
|
166
179
|
export function verifyToken(token: string, secret: string): TokenPayload | null {
|
|
167
180
|
try {
|
|
168
181
|
const payload = jwt.verify(token, secret) as TokenPayload;
|
|
182
|
+
// A present-but-unknown tier is never widened to `operate`: reject the
|
|
183
|
+
// token outright (fail closed). See change: add-passkey-user-auth (D4).
|
|
184
|
+
if (payload.tier !== undefined && !isTier(payload.tier)) return null;
|
|
169
185
|
return payload;
|
|
170
186
|
} catch {
|
|
171
187
|
return null;
|
|
172
188
|
}
|
|
173
189
|
}
|
|
174
190
|
|
|
191
|
+
/**
|
|
192
|
+
* `groups` claim of an OIDC `id_token`, or undefined.
|
|
193
|
+
*
|
|
194
|
+
* The token comes straight from the provider's token endpoint over the
|
|
195
|
+
* back-channel (TLS), so per OIDC Core §3.1.3.7 its signature need not be
|
|
196
|
+
* re-validated here; the `aud` claim must still name this client, so a token
|
|
197
|
+
* minted for another relying party is never trusted. Only `groups` is read.
|
|
198
|
+
* Non-string entries are dropped. See change: add-passkey-user-auth (D4).
|
|
199
|
+
*/
|
|
200
|
+
export function idTokenGroups(idToken: string | undefined, clientId: string): string[] | undefined {
|
|
201
|
+
if (!idToken) return undefined;
|
|
202
|
+
const part = idToken.split(".")[1];
|
|
203
|
+
if (!part) return undefined;
|
|
204
|
+
let claims: { aud?: unknown; groups?: unknown };
|
|
205
|
+
try {
|
|
206
|
+
claims = JSON.parse(Buffer.from(part, "base64url").toString("utf8"));
|
|
207
|
+
} catch {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
const aud = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
211
|
+
if (!aud.includes(clientId)) return undefined;
|
|
212
|
+
if (!Array.isArray(claims.groups)) return undefined;
|
|
213
|
+
return (claims.groups as unknown[]).filter((g): g is string => typeof g === "string");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Map an OIDC `groups` claim to a session tier (D4).
|
|
218
|
+
*
|
|
219
|
+
* - `groupTiers` unconfigured/empty → `operate` (legacy behaviour).
|
|
220
|
+
* - configured → the HIGHEST tier among matching groups; no match → `null`
|
|
221
|
+
* (login refused — fail closed).
|
|
222
|
+
*/
|
|
223
|
+
export function resolveGroupTier(
|
|
224
|
+
groups: readonly string[] | undefined,
|
|
225
|
+
groupTiers: Record<string, Tier> | undefined,
|
|
226
|
+
): Tier | null {
|
|
227
|
+
if (!groupTiers || Object.keys(groupTiers).length === 0) return "operate";
|
|
228
|
+
let best: Tier | null = null;
|
|
229
|
+
for (const g of groups ?? []) {
|
|
230
|
+
const t = Object.hasOwn(groupTiers, g) ? groupTiers[g] : undefined;
|
|
231
|
+
if (t && (best === null || rank(t) > rank(best))) best = t;
|
|
232
|
+
}
|
|
233
|
+
return best;
|
|
234
|
+
}
|
|
235
|
+
|
|
175
236
|
// ─── Cookie Parsing ─────────────────────────────────────────────────────────
|
|
176
237
|
|
|
177
238
|
/**
|
|
@@ -362,13 +423,15 @@ export function buildAuthorizeUrl(
|
|
|
362
423
|
}
|
|
363
424
|
|
|
364
425
|
/**
|
|
365
|
-
* Exchange an authorization code for
|
|
426
|
+
* Exchange an authorization code for tokens: the access token plus, for OIDC
|
|
427
|
+
* providers, the `id_token` (read for its `groups` claim — see
|
|
428
|
+
* `idTokenGroups`). See change: add-passkey-user-auth (D4).
|
|
366
429
|
*/
|
|
367
430
|
export async function exchangeCode(
|
|
368
431
|
provider: ResolvedProvider,
|
|
369
432
|
code: string,
|
|
370
433
|
redirectUri: string,
|
|
371
|
-
): Promise<string | null> {
|
|
434
|
+
): Promise<{ accessToken: string; idToken?: string } | null> {
|
|
372
435
|
try {
|
|
373
436
|
const body = new URLSearchParams({
|
|
374
437
|
client_id: provider.clientId,
|
|
@@ -395,7 +458,11 @@ export async function exchangeCode(
|
|
|
395
458
|
if (!res.ok) return null;
|
|
396
459
|
|
|
397
460
|
const data = await res.json();
|
|
398
|
-
|
|
461
|
+
if (typeof data.access_token !== "string" || !data.access_token) return null;
|
|
462
|
+
return {
|
|
463
|
+
accessToken: data.access_token,
|
|
464
|
+
...(typeof data.id_token === "string" ? { idToken: data.id_token } : {}),
|
|
465
|
+
};
|
|
399
466
|
} catch {
|
|
400
467
|
return null;
|
|
401
468
|
}
|
|
@@ -408,7 +475,7 @@ export async function exchangeCode(
|
|
|
408
475
|
export async function fetchUserInfo(
|
|
409
476
|
provider: ResolvedProvider,
|
|
410
477
|
accessToken: string,
|
|
411
|
-
): Promise<{ email: string; name: string; username: string } | null> {
|
|
478
|
+
): Promise<{ email: string; name: string; username: string; groups?: string[] } | null> {
|
|
412
479
|
try {
|
|
413
480
|
const res = await fetch(provider.userInfoUrl, {
|
|
414
481
|
headers: { Authorization: `Bearer ${accessToken}` },
|
|
@@ -440,7 +507,11 @@ export async function fetchUserInfo(
|
|
|
440
507
|
const email = data.email;
|
|
441
508
|
const name = data.name || data.preferred_username || data.sub || "Unknown";
|
|
442
509
|
const username = data.preferred_username || data.sub || "";
|
|
443
|
-
|
|
510
|
+
// `groups` (userinfo) feeds `auth.groupTiers`. Non-string entries dropped.
|
|
511
|
+
const groups = Array.isArray(data.groups)
|
|
512
|
+
? (data.groups as unknown[]).filter((g): g is string => typeof g === "string")
|
|
513
|
+
: undefined;
|
|
514
|
+
return email ? { email, name, username, ...(groups ? { groups } : {}) } : null;
|
|
444
515
|
} catch {
|
|
445
516
|
return null;
|
|
446
517
|
}
|
|
@@ -19,8 +19,11 @@
|
|
|
19
19
|
* (`/api/ws-ticket`) and presents only that ticket on the socket
|
|
20
20
|
* (see `ws-ticket.ts`).
|
|
21
21
|
*/
|
|
22
|
+
import type { TLSSocket } from "node:tls";
|
|
22
23
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
|
23
24
|
import type { PairedDeviceRegistry } from "../pairing/paired-devices.js";
|
|
25
|
+
import { hasProxyForwardingHeaders } from "./localhost-guard.js";
|
|
26
|
+
import { isLoopbackRange } from "./loopback.js";
|
|
24
27
|
|
|
25
28
|
/** Extract a `Bearer` token from an Authorization header, or null. */
|
|
26
29
|
export function parseBearerHeader(authorization: string | undefined): string | null {
|
|
@@ -39,6 +42,25 @@ export const DEVICE_COOKIE = "pi_dash_device";
|
|
|
39
42
|
/** 400 days — `PairedDeviceRegistry` rows carry no expiry; revocation is the kill switch. */
|
|
40
43
|
const DEVICE_COOKIE_MAX_AGE_S = 400 * 24 * 60 * 60;
|
|
41
44
|
|
|
45
|
+
/**
|
|
46
|
+
* `Secure` attribute for the device cookie, from the exchanging request's own
|
|
47
|
+
* transport: always `Secure` over a TLS socket; NOT `Secure` for a direct
|
|
48
|
+
* plain-http connection (no forwarding header) from a peer outside the loopback range (a browser paired
|
|
49
|
+
* over `http://<lan-ip>` would otherwise silently drop the cookie and lose its
|
|
50
|
+
* single-use bearer); otherwise the global resolved-base rule — so a tunnel / TLS
|
|
51
|
+
* reverse proxy that forwards plain http on the loopback hop still gets `Secure`.
|
|
52
|
+
* Uses the SOCKET peer, never `request.ip`. See change: add-lan-knock-pairing (D15).
|
|
53
|
+
*/
|
|
54
|
+
function deviceCookieSecure(request: FastifyRequest, globalSecure: () => boolean): boolean {
|
|
55
|
+
const socket = request.raw.socket as TLSSocket;
|
|
56
|
+
if (socket.encrypted) return true;
|
|
57
|
+
const peer = socket.remoteAddress;
|
|
58
|
+
// DIRECT only: a request carrying proxy-forwarding headers came through a TLS-terminating
|
|
59
|
+
// proxy that may sit on another host/container (peer 172.x), and keeps the global rule.
|
|
60
|
+
if (peer && !isLoopbackRange(peer) && !hasProxyForwardingHeaders(request.headers)) return false;
|
|
61
|
+
return globalSecure();
|
|
62
|
+
}
|
|
63
|
+
|
|
42
64
|
/** Pathname of a request URL (no query/fragment). */
|
|
43
65
|
function pathnameOf(url: string): string {
|
|
44
66
|
const q = url.search(/[?#]/);
|
|
@@ -103,7 +125,7 @@ export function registerDeviceSessionRoutes(
|
|
|
103
125
|
path: "/api/",
|
|
104
126
|
httpOnly: true,
|
|
105
127
|
sameSite: "strict",
|
|
106
|
-
secure: deps.isSecure
|
|
128
|
+
secure: deviceCookieSecure(request, deps.isSecure),
|
|
107
129
|
maxAge: DEVICE_COOKIE_MAX_AGE_S,
|
|
108
130
|
});
|
|
109
131
|
return { success: true as const };
|
|
@@ -14,6 +14,14 @@ import { isHostAdmitted } from "./host-admission.js";
|
|
|
14
14
|
import { isBypassedHost } from "./localhost-guard.js";
|
|
15
15
|
import type { WsRouteScope } from "./ws-ticket.js";
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Methods advertised on CORS preflight. `@fastify/cors` v11 defaults to the
|
|
19
|
+
* safelisted GET,HEAD,POST, which blocks every cross-origin PUT/PATCH/DELETE
|
|
20
|
+
* (e.g. the Electron renderer). Widens no origin: this list only matters for
|
|
21
|
+
* origins the allow-decision already admits; auth still gates each mutation.
|
|
22
|
+
*/
|
|
23
|
+
export const CORS_ALLOWED_METHODS = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"];
|
|
24
|
+
|
|
17
25
|
export interface CorsOriginOptions {
|
|
18
26
|
/** Explicitly configured allowed origins (`cors.allowedOrigins`). */
|
|
19
27
|
configuredOrigins: string[];
|
|
@@ -51,6 +59,8 @@ export interface CorsOriginOptions {
|
|
|
51
59
|
publicBaseUrls?: string[];
|
|
52
60
|
/** The boot-time bind address — host-admission only. */
|
|
53
61
|
bindHost?: string;
|
|
62
|
+
/** TLS-listener certificate names (live) — host-admission only. See change: add-tailscale-cert-tls. */
|
|
63
|
+
tlsListenerNames?: () => string[];
|
|
54
64
|
/**
|
|
55
65
|
* Host-gate rollout mode (live). In `enforce` the same-origin-by-Host rule
|
|
56
66
|
* additionally requires the `Host` to be admissible (D1); in `report` it is
|
|
@@ -13,6 +13,12 @@
|
|
|
13
13
|
* prefixes (`/live/*` sandboxed dev servers) are skipped so their own policies
|
|
14
14
|
* are not overwritten.
|
|
15
15
|
*
|
|
16
|
+
* A `ctx.serveApp` mount response sets its own ENFORCED `Content-Security-Policy`
|
|
17
|
+
* before `onSend`; the hook only adds a header that is absent, so that policy is
|
|
18
|
+
* never overwritten in any mode (report mode additionally gets the baseline
|
|
19
|
+
* `-Report-Only`; off mode registers no hook and the mount sets its own).
|
|
20
|
+
* See change: add-plugin-capability-routes (D7).
|
|
21
|
+
*
|
|
16
22
|
* See change: improve-content-editor (baseline CSP §7).
|
|
17
23
|
*/
|
|
18
24
|
import type { FastifyInstance } from "fastify";
|
|
@@ -27,6 +27,14 @@ export interface HostAdmissionOptions {
|
|
|
27
27
|
getLiveTunnelOrigins?: () => string[];
|
|
28
28
|
/** The address the listener was bound to (boot-time `ServerConfig.host`). */
|
|
29
29
|
bindHost?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Exact (non-wildcard) names the optional TLS listener holds a certificate
|
|
32
|
+
* for — empty unless the listener is enabled AND bound. Read live; a throwing
|
|
33
|
+
* accessor yields `[]` (fail-empty). Admits the NAME only; wildcard
|
|
34
|
+
* certificates are deliberately never admitted (DNS-rebinding).
|
|
35
|
+
* See change: add-tailscale-cert-tls (D6).
|
|
36
|
+
*/
|
|
37
|
+
tlsListenerNames?: () => string[];
|
|
30
38
|
}
|
|
31
39
|
|
|
32
40
|
/** The source labels the operator UI renders, in first-match order (D1). */
|
|
@@ -67,6 +75,23 @@ export function safeLiveTunnelOrigins(opts: HostAdmissionOptions): string[] {
|
|
|
67
75
|
}
|
|
68
76
|
}
|
|
69
77
|
|
|
78
|
+
/**
|
|
79
|
+
* TLS-listener certificate names, fail-empty: a cert-store fault must never 500
|
|
80
|
+
* the main listener's request path (mirrors `safeLiveTunnelOrigins`). Warns once.
|
|
81
|
+
*/
|
|
82
|
+
let tlsNamesWarned = false;
|
|
83
|
+
export function safeTlsListenerNames(opts: HostAdmissionOptions): string[] {
|
|
84
|
+
try {
|
|
85
|
+
return opts.tlsListenerNames?.() ?? [];
|
|
86
|
+
} catch (err) {
|
|
87
|
+
if (!tlsNamesWarned) {
|
|
88
|
+
tlsNamesWarned = true;
|
|
89
|
+
console.warn(`[tls] certificate-name lookup failed; no TLS names admitted: ${(err as Error).message}`);
|
|
90
|
+
}
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
70
95
|
/**
|
|
71
96
|
* First matching admissible source for an already-normalized hostname, or
|
|
72
97
|
* `null` when the hostname is not admissible. Order is D1's: IP literals before
|
|
@@ -86,6 +111,7 @@ export function classifyAdmittedHostname(
|
|
|
86
111
|
if (hostnameInUrlList(opts.publicBaseUrls, hostname)) return "public-base-url";
|
|
87
112
|
if (hostnameInUrlList(opts.configuredOrigins, hostname)) return "cors-origin";
|
|
88
113
|
if (hostnameInUrlList(safeLiveTunnelOrigins(opts), hostname)) return "live-tunnel";
|
|
114
|
+
if (safeTlsListenerNames(opts).some((n) => normalizeName(n) === hostname)) return "tls-listener";
|
|
89
115
|
if ((opts.allowedHosts ?? []).some((entry) => normalizeName(entry) === hostname)) {
|
|
90
116
|
return "allowed-host";
|
|
91
117
|
}
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
type HostAdmissionOptions,
|
|
29
29
|
isHostAdmitted,
|
|
30
30
|
safeLiveTunnelOrigins,
|
|
31
|
+
safeTlsListenerNames,
|
|
31
32
|
} from "./host-admission.js";
|
|
32
33
|
|
|
33
34
|
export type { HostGateMode };
|
|
@@ -106,13 +107,16 @@ export interface HostGateStateOptions {
|
|
|
106
107
|
* close). Header values are already sanitized before they reach `recordRefusal`.
|
|
107
108
|
*/
|
|
108
109
|
export class HostGateState {
|
|
110
|
+
private readonly opts: HostGateStateOptions;
|
|
109
111
|
private readonly ring = new Map<string, HostGateRefusal>();
|
|
110
112
|
private readonly logHosts = new Map<string, number>();
|
|
111
113
|
private windowStart = 0;
|
|
112
114
|
private windowCount = 0;
|
|
113
115
|
private suppressed = 0;
|
|
114
116
|
|
|
115
|
-
constructor(
|
|
117
|
+
constructor(opts: HostGateStateOptions = {}) {
|
|
118
|
+
this.opts = opts;
|
|
119
|
+
}
|
|
116
120
|
|
|
117
121
|
private now(): number {
|
|
118
122
|
return (this.opts.now ?? Date.now)();
|
|
@@ -345,6 +349,7 @@ export function admittedHostRows(
|
|
|
345
349
|
for (const raw of opts.publicBaseUrls ?? []) add(hostnameFromUrl(raw), "public-base-url");
|
|
346
350
|
for (const raw of opts.configuredOrigins ?? []) add(hostnameFromUrl(raw), "cors-origin");
|
|
347
351
|
for (const raw of safeLiveTunnelOrigins(opts)) add(hostnameFromUrl(raw), "live-tunnel");
|
|
352
|
+
for (const name of safeTlsListenerNames(opts)) add(normalizeName(name), "tls-listener");
|
|
348
353
|
for (const raw of opts.allowedHosts ?? []) add(normalizeName(raw), "allowed-host");
|
|
349
354
|
return rows;
|
|
350
355
|
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local credential verifier + edge stamp.
|
|
3
|
+
*
|
|
4
|
+
* Verifies the `X-Pi-Local-Proof` header (endpoint-bound HMAC proof of possession,
|
|
5
|
+
* see `shared/local-credential-proof.ts`) — or, while `acceptLegacyToken` is on,
|
|
6
|
+
* the deprecated raw `X-Pi-Local-Token` — exactly ONCE per request, at the
|
|
7
|
+
* listener edge, and stamps the verdict on the request's own `headers` object
|
|
8
|
+
* under a module-private Symbol key. Every exemption site then reads the verdict
|
|
9
|
+
* through `hasLocalCredential(headers)`; an unstamped request counts as `absent`
|
|
10
|
+
* (fail closed). Fastify's `request.headers` returns that same object or an
|
|
11
|
+
* `Object.assign` copy (which keeps enumerable Symbol keys); JSON,
|
|
12
|
+
* `Object.entries` and header forwarding ignore Symbol keys, and a client cannot
|
|
13
|
+
* set one over the wire (in-process code can read it — no new boundary).
|
|
14
|
+
*
|
|
15
|
+
* Order per proof: bounds → window ±30 s → listener bind-time floor → binding vs
|
|
16
|
+
* the connection's OWN local address/port → constant-time MAC → replay cache
|
|
17
|
+
* (insert only after the MAC verifies). The clock, the cache and the log are
|
|
18
|
+
* injected so the decision logic stays pure.
|
|
19
|
+
*
|
|
20
|
+
* See change: loopback-credential-proof (D3, D4, D9).
|
|
21
|
+
*/
|
|
22
|
+
import {
|
|
23
|
+
BRIDGE_PURPOSE,
|
|
24
|
+
canonicalBinding,
|
|
25
|
+
deriveKey,
|
|
26
|
+
httpPurpose,
|
|
27
|
+
LOCAL_PROOF_HEADER,
|
|
28
|
+
PROOF_WINDOW_MS,
|
|
29
|
+
parseProof,
|
|
30
|
+
serverCounterProof,
|
|
31
|
+
verifyProofMac,
|
|
32
|
+
} from "@blackbelt-technology/pi-dashboard-shared/local-credential-proof.js";
|
|
33
|
+
import { verifyLocalToken } from "./local-token.js";
|
|
34
|
+
|
|
35
|
+
type InvalidCause =
|
|
36
|
+
| "malformed"
|
|
37
|
+
| "stale"
|
|
38
|
+
| "before-start"
|
|
39
|
+
| "binding-mismatch"
|
|
40
|
+
| "bad-mac"
|
|
41
|
+
| "replayed"
|
|
42
|
+
| "replay-cache-full";
|
|
43
|
+
|
|
44
|
+
export type LocalCredentialVerdict =
|
|
45
|
+
| { kind: "proof"; binding: string; nonce: string; mac: string }
|
|
46
|
+
| { kind: "legacy" }
|
|
47
|
+
| { kind: "absent" }
|
|
48
|
+
| { kind: "invalid"; cause: InvalidCause };
|
|
49
|
+
|
|
50
|
+
/** Which edge received the request. Fixes the proof purpose. */
|
|
51
|
+
export interface CredentialListener {
|
|
52
|
+
kind: "bridge" | "http";
|
|
53
|
+
/** ms epoch taken immediately BEFORE the bind call that succeeded. */
|
|
54
|
+
boundAt: number;
|
|
55
|
+
/** The listener's configured port (diagnostics only). */
|
|
56
|
+
port?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface StampableRequest {
|
|
60
|
+
headers: Record<string, unknown>;
|
|
61
|
+
method?: string;
|
|
62
|
+
url?: string;
|
|
63
|
+
socket: { localAddress?: string; localPort?: number };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface LocalCredentialVerifierOptions {
|
|
67
|
+
token: string;
|
|
68
|
+
now?: () => number;
|
|
69
|
+
acceptLegacyToken: boolean;
|
|
70
|
+
log?: (line: string) => void;
|
|
71
|
+
maxNonces?: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface LocalCredentialVerifier {
|
|
75
|
+
stamp(req: StampableRequest, listener: CredentialListener): LocalCredentialVerdict;
|
|
76
|
+
/** `X-Pi-Local-Proof-Server` value answering a `proof` verdict (D5). */
|
|
77
|
+
counterProof(verdict: { binding: string; nonce: string; mac: string }): string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const STAMP = Symbol("local-credential");
|
|
81
|
+
const REPLAY_TTL_MS = 61_000;
|
|
82
|
+
const DEFAULT_MAX_NONCES = 10_000;
|
|
83
|
+
const LOG_INTERVAL_MS = 60_000;
|
|
84
|
+
|
|
85
|
+
/** The verdict stamped on these headers, or `absent` when never stamped. */
|
|
86
|
+
export function credentialVerdict(headers: unknown): LocalCredentialVerdict {
|
|
87
|
+
if (headers && typeof headers === "object") {
|
|
88
|
+
const v = (headers as Record<symbol, unknown>)[STAMP];
|
|
89
|
+
if (v && typeof v === "object") return v as LocalCredentialVerdict;
|
|
90
|
+
}
|
|
91
|
+
return { kind: "absent" };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** True for a verified proof or an accepted legacy raw token. */
|
|
95
|
+
export function hasLocalCredential(headers: unknown): boolean {
|
|
96
|
+
const k = credentialVerdict(headers).kind;
|
|
97
|
+
return k === "proof" || k === "legacy";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function firstString(raw: unknown): string | undefined {
|
|
101
|
+
const v = Array.isArray(raw) ? raw[0] : raw;
|
|
102
|
+
return typeof v === "string" && v.length > 0 ? v : undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Diagnostic-only candidates for what the caller may have dialled. */
|
|
106
|
+
function diagnosticBindings(req: StampableRequest, listener: CredentialListener, expected: string): string[] {
|
|
107
|
+
const ports = new Set<number>();
|
|
108
|
+
if (typeof req.socket.localPort === "number") ports.add(req.socket.localPort);
|
|
109
|
+
if (typeof listener.port === "number") ports.add(listener.port);
|
|
110
|
+
const host = firstString(req.headers.host);
|
|
111
|
+
const m = host ? /:(\d{1,5})$/.exec(host) : null;
|
|
112
|
+
if (m) ports.add(Number(m[1]));
|
|
113
|
+
const out = new Set<string>();
|
|
114
|
+
for (const port of ports) {
|
|
115
|
+
for (const addr of ["127.0.0.1", "::1"]) {
|
|
116
|
+
try {
|
|
117
|
+
out.add(canonicalBinding(addr, port));
|
|
118
|
+
} catch {
|
|
119
|
+
/* unreachable for literals */
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
out.delete(expected);
|
|
124
|
+
return [...out];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function createLocalCredentialVerifier(opts: LocalCredentialVerifierOptions): LocalCredentialVerifier {
|
|
128
|
+
const now = opts.now ?? Date.now;
|
|
129
|
+
const log = opts.log ?? ((l: string) => console.warn(l));
|
|
130
|
+
const maxNonces = opts.maxNonces ?? DEFAULT_MAX_NONCES;
|
|
131
|
+
const key = deriveKey(opts.token);
|
|
132
|
+
const replay = new Map<string, number>(); // nonce → expiresAt, insertion order ≈ expiry order
|
|
133
|
+
const limiter = new Map<string, { last: number; suppressed: number }>();
|
|
134
|
+
|
|
135
|
+
/** At most one line per key per 60 s; the next line carries the suppressed count. */
|
|
136
|
+
const limited = (limitKey: string, line: string): void => {
|
|
137
|
+
const t = now();
|
|
138
|
+
const s = limiter.get(limitKey);
|
|
139
|
+
if (!s) {
|
|
140
|
+
limiter.set(limitKey, { last: t, suppressed: 0 });
|
|
141
|
+
log(line);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (t - s.last < LOG_INTERVAL_MS) {
|
|
145
|
+
s.suppressed += 1;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const suppressed = s.suppressed;
|
|
149
|
+
s.last = t;
|
|
150
|
+
s.suppressed = 0;
|
|
151
|
+
log(suppressed > 0 ? `${line} suppressed=${suppressed}` : line);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const sweep = (t: number): void => {
|
|
155
|
+
for (const [nonce, exp] of replay) {
|
|
156
|
+
if (exp > t) break;
|
|
157
|
+
replay.delete(nonce);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const invalid = (cause: InvalidCause, listener: CredentialListener): LocalCredentialVerdict => {
|
|
162
|
+
limited(`invalid:${listener.kind}:${cause}`, `[local-credential] ${listener.kind} proof rejected: ${cause}`);
|
|
163
|
+
return { kind: "invalid", cause };
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const verifyProof = (
|
|
167
|
+
headerValue: string,
|
|
168
|
+
req: StampableRequest,
|
|
169
|
+
listener: CredentialListener,
|
|
170
|
+
): LocalCredentialVerdict => {
|
|
171
|
+
const parsed = parseProof(headerValue);
|
|
172
|
+
if (!parsed) return invalid("malformed", listener);
|
|
173
|
+
const t = now();
|
|
174
|
+
const ts = Number(parsed.ts);
|
|
175
|
+
if (Math.abs(t - ts) > PROOF_WINDOW_MS) return invalid("stale", listener);
|
|
176
|
+
if (ts < listener.boundAt) return invalid("before-start", listener);
|
|
177
|
+
|
|
178
|
+
const { localAddress, localPort } = req.socket;
|
|
179
|
+
let binding: string;
|
|
180
|
+
try {
|
|
181
|
+
if (localAddress === undefined || localPort === undefined) throw new Error("no local address");
|
|
182
|
+
binding = canonicalBinding(localAddress, localPort);
|
|
183
|
+
} catch {
|
|
184
|
+
return invalid("binding-mismatch", listener);
|
|
185
|
+
}
|
|
186
|
+
const purpose =
|
|
187
|
+
listener.kind === "bridge" ? BRIDGE_PURPOSE : httpPurpose(req.method ?? "GET", req.url ?? "/");
|
|
188
|
+
if (!verifyProofMac(key, parsed, binding, purpose)) {
|
|
189
|
+
// Distinguish a relayed/mis-bound proof from a forged one — for the log only.
|
|
190
|
+
const relayed = diagnosticBindings(req, listener, binding).some((b) =>
|
|
191
|
+
verifyProofMac(key, parsed, b, purpose),
|
|
192
|
+
);
|
|
193
|
+
return invalid(relayed ? "binding-mismatch" : "bad-mac", listener);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
sweep(t);
|
|
197
|
+
if (replay.has(parsed.nonce)) return invalid("replayed", listener);
|
|
198
|
+
if (replay.size >= maxNonces) {
|
|
199
|
+
limited("replay-cache-full", "[local-credential] replay cache full; refusing proofs");
|
|
200
|
+
return { kind: "invalid", cause: "replay-cache-full" };
|
|
201
|
+
}
|
|
202
|
+
replay.set(parsed.nonce, t + REPLAY_TTL_MS);
|
|
203
|
+
return { kind: "proof", binding, nonce: parsed.nonce, mac: parsed.mac };
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
counterProof: (v) => serverCounterProof({ token: opts.token, binding: v.binding, nonce: v.nonce, mac: v.mac }),
|
|
208
|
+
stamp(req, listener) {
|
|
209
|
+
const existing = (req.headers as Record<symbol, unknown>)[STAMP];
|
|
210
|
+
if (existing && typeof existing === "object") return existing as LocalCredentialVerdict;
|
|
211
|
+
|
|
212
|
+
let verdict: LocalCredentialVerdict;
|
|
213
|
+
const proofValue = firstString(req.headers[LOCAL_PROOF_HEADER.toLowerCase()]);
|
|
214
|
+
if (proofValue !== undefined) {
|
|
215
|
+
// A request carrying both headers is judged on the proof alone.
|
|
216
|
+
verdict = verifyProof(proofValue, req, listener);
|
|
217
|
+
} else {
|
|
218
|
+
if (opts.acceptLegacyToken && verifyLocalToken(req.headers, opts.token)) {
|
|
219
|
+
verdict = { kind: "legacy" };
|
|
220
|
+
limited(
|
|
221
|
+
`legacy:${listener.kind}`,
|
|
222
|
+
`[local-credential] DEPRECATED raw X-Pi-Local-Token accepted from a ${listener.kind} caller (send X-Pi-Local-Proof)`,
|
|
223
|
+
);
|
|
224
|
+
} else {
|
|
225
|
+
verdict = { kind: "absent" };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
Object.defineProperty(req.headers, STAMP, {
|
|
229
|
+
// Frozen too: the property is immutable, so the verdict it holds must be.
|
|
230
|
+
value: Object.freeze(verdict),
|
|
231
|
+
enumerable: true,
|
|
232
|
+
// Immutable: no later code may redefine or overwrite a recorded verdict.
|
|
233
|
+
// See change: harden-credential-proof-binding.
|
|
234
|
+
configurable: false,
|
|
235
|
+
writable: false,
|
|
236
|
+
});
|
|
237
|
+
return verdict;
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Take the listener's proof floor AFTER the application is ready and immediately
|
|
244
|
+
* before the bind. `listen()` itself awaits `ready()` (plugin boot, possibly
|
|
245
|
+
* seconds); a floor taken before it would let a proof captured by the previous
|
|
246
|
+
* port holder during that window pass `before-start`. A failing `ready()` sets no
|
|
247
|
+
* floor and never listens. See change: harden-credential-proof-binding.
|
|
248
|
+
*/
|
|
249
|
+
export async function listenWithProofFloor(
|
|
250
|
+
app: { ready(): PromiseLike<unknown>; listen(opts: { port: number; host: string }): PromiseLike<unknown> },
|
|
251
|
+
setFloor: (boundAt: number) => void,
|
|
252
|
+
listenOpts: { port: number; host: string },
|
|
253
|
+
now: () => number = Date.now,
|
|
254
|
+
): Promise<void> {
|
|
255
|
+
await app.ready();
|
|
256
|
+
setFloor(now());
|
|
257
|
+
await app.listen(listenOpts);
|
|
258
|
+
}
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* See change: harden-trust-and-credential-boundaries (D2).
|
|
13
13
|
*/
|
|
14
14
|
import crypto from "node:crypto";
|
|
15
|
-
import {
|
|
15
|
+
import { hasLocalCredential } from "./local-credential.js";
|
|
16
16
|
|
|
17
17
|
export const LOCAL_PROOF_COOKIE = "pi_dash_local";
|
|
18
18
|
export const LOCAL_PROOF_MAX_AGE_S = 30 * 24 * 60 * 60;
|
|
@@ -69,17 +69,20 @@ function readCookie(headers: Record<string, unknown> | undefined, name: string):
|
|
|
69
69
|
return undefined;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
/** Local proof = valid proof cookie OR
|
|
72
|
+
/** Local proof = valid proof cookie OR a verified local credential (proof / legacy token). */
|
|
73
73
|
export function hasLocalProof(headers: Record<string, unknown> | undefined, ctx: LocalTrustContext): boolean {
|
|
74
|
-
if (
|
|
74
|
+
if (hasLocalCredential(headers)) return true;
|
|
75
75
|
return verifyLocalProofCookie(readCookie(headers, LOCAL_PROOF_COOKIE), ctx);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/** One-time bootstrap codes (32 random bytes, 60 s TTL, single use). */
|
|
79
79
|
export class LocalProofCodeStore {
|
|
80
|
+
private now: () => number;
|
|
80
81
|
private codes = new Map<string, number>();
|
|
81
82
|
|
|
82
|
-
constructor(
|
|
83
|
+
constructor(now: () => number = Date.now) {
|
|
84
|
+
this.now = now;
|
|
85
|
+
}
|
|
83
86
|
|
|
84
87
|
mint(): string {
|
|
85
88
|
this.sweep();
|
|
@@ -59,6 +59,22 @@ export function ensureLocalToken(dir = defaultLocalTokenDir()): string {
|
|
|
59
59
|
return token;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Read the local IPC token WITHOUT creating anything: no dir, no file, no chmod.
|
|
64
|
+
* `null` when the file is absent, unreadable (mode 000, a directory, ...) or
|
|
65
|
+
* empty. Used by `pi-dashboard pair` so an approval attempt on a host where the
|
|
66
|
+
* server never ran leaves no token behind, and never replaces a credential the
|
|
67
|
+
* real server holds. See change: fix-pairing-approval-credential (D9).
|
|
68
|
+
*/
|
|
69
|
+
export function readLocalTokenIfPresent(dir = defaultLocalTokenDir()): string | null {
|
|
70
|
+
try {
|
|
71
|
+
const token = fs.readFileSync(path.join(dir, "token"), "utf-8").trim();
|
|
72
|
+
return token || null;
|
|
73
|
+
} catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
62
78
|
/** Constant-time compare of a presented local-token header against expected. */
|
|
63
79
|
export function verifyLocalToken(
|
|
64
80
|
headers: Record<string, unknown> | undefined,
|