@blackbelt-technology/pi-agent-dashboard 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +2 -2
- package/README.md +85 -11
- package/docs/architecture.md +3020 -116
- package/package.json +31 -18
- package/packages/extension/package.json +10 -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 +504 -0
- package/packages/extension/src/bridge-handlers/forwarders.ts +616 -0
- package/packages/extension/src/bridge-handlers/instance.ts +229 -0
- package/packages/extension/src/bridge-handlers/lifecycle.ts +1069 -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-ticket-client.ts +61 -7
- package/packages/extension/src/bridge-tls-checks.ts +79 -0
- package/packages/extension/src/bridge.ts +517 -2534
- package/packages/extension/src/canvas-tool.ts +2 -0
- package/packages/extension/src/command-handler.ts +259 -16
- package/packages/extension/src/connect-target.ts +23 -0
- package/packages/extension/src/connection.ts +294 -24
- package/packages/extension/src/dashboard-context-injector.ts +26 -47
- package/packages/extension/src/dashboard-http-origin.ts +88 -0
- package/packages/extension/src/dev-build.ts +11 -1
- 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/git-link-builder.ts +11 -2
- package/packages/extension/src/inbound-limits.ts +138 -0
- package/packages/extension/src/local-token-header.ts +232 -17
- package/packages/extension/src/message-update-coalescer.ts +3 -1
- package/packages/extension/src/model-refresh.ts +88 -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/pinned-endpoint.ts +44 -0
- package/packages/extension/src/plugin-event-forward-registry.ts +200 -0
- package/packages/extension/src/project-trust-live.ts +143 -0
- package/packages/extension/src/project-trust.ts +258 -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 +146 -38
- package/packages/extension/src/remote-registration-gate.ts +21 -21
- package/packages/extension/src/remote-upgrade-preparer.ts +104 -0
- package/packages/extension/src/role-manager.ts +5 -4
- package/packages/extension/src/role-model-tools.ts +47 -6
- package/packages/extension/src/server-auto-start.ts +13 -6
- package/packages/extension/src/server-launcher.ts +11 -13
- package/packages/extension/src/server-proof-probe.ts +117 -0
- package/packages/extension/src/session-move.ts +19 -0
- package/packages/extension/src/session-sync.ts +73 -50
- 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 +404 -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 +79 -8
- package/packages/server/src/access/access-health.ts +10 -0
- package/packages/server/src/access/access-plane.ts +5 -1
- package/packages/server/src/access/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 +82 -17
- 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-live.ts +244 -0
- package/packages/server/src/access/pi-trust-mirror-service.ts +407 -0
- package/packages/server/src/access/planes.ts +116 -2
- 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/refusal-ledger.ts +6 -6
- package/packages/server/src/access/yolo-session.ts +127 -14
- 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 +549 -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/agent-events/agent-event-hub.ts +158 -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/audit-log.ts +80 -0
- package/packages/server/src/auth/auth-plugin.ts +329 -97
- package/packages/server/src/auth/auth.ts +102 -11
- package/packages/server/src/auth/bearer-auth.ts +74 -12
- package/packages/server/src/auth/cors-origin.ts +86 -29
- package/packages/server/src/auth/csp-report-paths.ts +31 -0
- package/packages/server/src/auth/csp-report-sink.ts +302 -0
- package/packages/server/src/auth/csp-report-store.ts +406 -0
- package/packages/server/src/auth/csp.ts +111 -9
- package/packages/server/src/auth/dashboard-credential-store.ts +4 -0
- package/packages/server/src/auth/dashboard-state-caller.ts +29 -0
- package/packages/server/src/auth/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-refusal-log.ts +89 -0
- package/packages/server/src/auth/local-proof.ts +76 -22
- package/packages/server/src/auth/local-token.ts +16 -0
- package/packages/server/src/auth/localhost-guard.ts +298 -17
- package/packages/server/src/auth/nav-intent.ts +72 -0
- 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-assets.ts +99 -0
- package/packages/server/src/auth/passkey/passkey-log.ts +34 -0
- package/packages/server/src/auth/passkey/passkey-pages.ts +109 -0
- package/packages/server/src/auth/passkey/passkey-routes.ts +232 -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 +324 -0
- package/packages/server/src/auth/plugin-server-trust.ts +29 -0
- package/packages/server/src/auth/plugin-trust-store.ts +103 -0
- package/packages/server/src/auth/proven-tls.ts +45 -0
- package/packages/server/src/auth/provider-auth-registry.ts +35 -1
- package/packages/server/src/auth/provider-auth-storage.ts +61 -9
- package/packages/server/src/auth/radius-mcp.ts +1 -1
- package/packages/server/src/auth/radius-override.ts +13 -0
- package/packages/server/src/auth/request-principal.ts +32 -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/secret-rotation.ts +246 -0
- package/packages/server/src/auth/secure-origin.ts +56 -0
- package/packages/server/src/auth/serve-app-host.ts +197 -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/auth/ws-ticket.ts +39 -1
- package/packages/server/src/auth/ws-upgrade-reject-log.ts +4 -1
- 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 +151 -6
- 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 +138 -63
- 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/browser-handlers/subscription-handler.ts +100 -2
- package/packages/server/src/browser-handlers/terminal-handler.ts +108 -10
- package/packages/server/src/canvas/canvas-accumulator.ts +35 -7
- package/packages/server/src/cli.ts +1063 -106
- package/packages/server/src/config-api.ts +166 -2
- package/packages/server/src/config-snapshot.ts +53 -1
- package/packages/server/src/directory-service.ts +545 -36
- 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 +349 -141
- package/packages/server/src/file-watch-manager.ts +187 -39
- package/packages/server/src/git-worktree/git-operations.ts +60 -15
- package/packages/server/src/git-worktree/redact-git-text.ts +124 -0
- package/packages/server/src/git-worktree/ref-arg.ts +39 -0
- package/packages/server/src/git-worktree/worktree-init-trust.ts +91 -6
- 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 +31 -1
- 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 +10 -0
- package/packages/server/src/identity/ws-road-classification.ts +10 -0
- package/packages/server/src/known-server-moves.ts +196 -0
- package/packages/server/src/lib/browse-roots.ts +137 -0
- package/packages/server/src/lib/client-dist.ts +36 -21
- package/packages/server/src/lib/dashboard-state-guard.ts +230 -0
- package/packages/server/src/lib/diagram-render.ts +4 -6
- package/packages/server/src/lib/digest-gate.ts +82 -0
- package/packages/server/src/lib/eml.ts +3 -8
- package/packages/server/src/lib/file-cwd-admission.ts +145 -0
- package/packages/server/src/lib/file-write-lock.ts +24 -0
- package/packages/server/src/lib/folder-project.ts +439 -0
- package/packages/server/src/lib/grep-worker.ts +23 -0
- package/packages/server/src/lib/grep.ts +93 -7
- package/packages/server/src/lib/index-html.ts +81 -0
- package/packages/server/src/lib/office-preview.ts +2 -3
- package/packages/server/src/lib/path-containment.ts +7 -0
- package/packages/server/src/lib/purify.ts +13 -5
- package/packages/server/src/lib/resolve-file-mention.ts +11 -9
- package/packages/server/src/lib/route-admission.ts +80 -0
- package/packages/server/src/lib/writable-handler-target.ts +66 -0
- package/packages/server/src/lib/writable-md-target.ts +16 -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 +129 -6
- package/packages/server/src/lifecycle/stop-ownership.ts +97 -11
- package/packages/server/src/live-server/capability-redact.ts +15 -0
- package/packages/server/src/live-server/live-browser-gate.ts +22 -0
- package/packages/server/src/live-server/live-capability.ts +461 -0
- package/packages/server/src/live-server/live-csp-report.ts +63 -0
- package/packages/server/src/live-server/live-csp.ts +93 -0
- package/packages/server/src/live-server/live-egress.ts +435 -0
- package/packages/server/src/live-server/live-exposure.ts +149 -0
- package/packages/server/src/live-server/live-log.ts +21 -0
- package/packages/server/src/live-server/live-origin-scan.ts +298 -0
- package/packages/server/src/live-server/live-server-manager.ts +209 -33
- package/packages/server/src/live-server/live-server-proxy.ts +326 -34
- package/packages/server/src/live-server/live-static.ts +228 -0
- 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/dependencies-file.ts +122 -0
- package/packages/server/src/openspec/openspec-change-watcher.ts +111 -16
- package/packages/server/src/openspec/openspec-dependency-inputs.ts +281 -0
- package/packages/server/src/openspec/openspec-poll-fs-helpers.ts +189 -11
- package/packages/server/src/openspec/openspec-poll-worker-pool.ts +126 -35
- package/packages/server/src/openspec/openspec-poll-worker.ts +286 -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-status.ts +379 -0
- package/packages/server/src/openspec/workflow-schema.ts +1002 -0
- package/packages/server/src/package/installed-package-enricher.ts +38 -3
- package/packages/server/src/package/npm-search-proxy.ts +5 -2
- 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 +13 -5
- package/packages/server/src/pairing/browser-gateway.ts +292 -16
- package/packages/server/src/pairing/device-token-services.ts +26 -0
- package/packages/server/src/pairing/knock-admission.ts +277 -0
- package/packages/server/src/pairing/paired-devices.ts +335 -22
- package/packages/server/src/pairing/pairing.ts +188 -31
- package/packages/server/src/pairing/socket-locality.ts +26 -0
- 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 +125 -7
- package/packages/server/src/pi/bridge-channel.ts +33 -0
- package/packages/server/src/pi/bridge-upgrade-auth.ts +48 -22
- package/packages/server/src/pi/folder-commands.ts +141 -0
- package/packages/server/src/pi/gateway-listeners.ts +9 -3
- 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 +166 -33
- package/packages/server/src/pi/pi-gateway.ts +283 -29
- 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/plugin-discovery-wiring.ts +142 -0
- package/packages/server/src/plugin-event-seam.ts +116 -4
- package/packages/server/src/plugin-runtime-clients.ts +89 -0
- package/packages/server/src/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 +274 -6
- 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/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 +620 -209
- package/packages/server/src/routes/file-write-binary-routes.ts +530 -0
- package/packages/server/src/routes/folder-project-routes.ts +105 -0
- package/packages/server/src/routes/git-routes.ts +334 -203
- package/packages/server/src/routes/grep-routes.ts +38 -14
- package/packages/server/src/routes/known-servers-routes.ts +433 -55
- package/packages/server/src/routes/live-server-routes.ts +438 -7
- package/packages/server/src/routes/local-proof-routes.ts +26 -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/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 +636 -55
- package/packages/server/src/routes/package-health-routes.ts +140 -0
- package/packages/server/src/routes/package-routes.ts +86 -11
- package/packages/server/src/routes/pairing-routes.ts +303 -33
- 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/pi-trust-query-routes.ts +330 -0
- package/packages/server/src/routes/plugin-activation-routes.ts +146 -27
- package/packages/server/src/routes/plugin-runtime-client-routes.ts +149 -0
- package/packages/server/src/routes/provider-auth-routes.ts +37 -7
- package/packages/server/src/routes/provider-routes.ts +29 -0
- package/packages/server/src/routes/resource-activation-routes.ts +33 -35
- package/packages/server/src/routes/session-routes.ts +100 -9
- package/packages/server/src/routes/system-routes.ts +286 -33
- package/packages/server/src/routes/tls-routes.ts +55 -0
- package/packages/server/src/routes/ui-link-base.ts +88 -0
- package/packages/server/src/routes/user-routes.ts +192 -0
- package/packages/server/src/runtime-overlay/runtime-io.ts +2 -9
- 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 +1537 -246
- 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/hydration-state.ts +148 -0
- package/packages/server/src/session/in-flight-tool-calls.ts +74 -0
- package/packages/server/src/session/memory-session-manager.ts +60 -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/replay-exit-controller.ts +221 -0
- package/packages/server/src/session/retained-transcript.ts +9 -2
- 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-load-worker.ts +11 -2
- 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/headless-pid-registry.ts +14 -9
- 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 +75 -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-bridge-route.ts +61 -0
- package/packages/server/src/tls/tls-config-validation.ts +94 -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/device-expiry-sweep.ts +36 -0
- package/packages/server/src/vault/vault-bindings.ts +101 -0
- package/packages/server/src/vault/vault-delivery-hub.ts +424 -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/server/src/watch-files-handler.ts +66 -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 +533 -73
- package/packages/shared/src/canonical-subject.ts +27 -2
- package/packages/shared/src/canvas-declare.ts +27 -0
- package/packages/shared/src/card-sections.ts +185 -1
- package/packages/shared/src/chrome-profiles.ts +188 -0
- package/packages/shared/src/config.ts +817 -51
- package/packages/shared/src/containment-guard.ts +35 -0
- 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/anthropic-peer-names.ts +9 -0
- package/packages/shared/src/dashboard-plugin/editor-pane-tab.ts +26 -0
- package/packages/shared/src/dashboard-plugin/event-type-owners.ts +50 -0
- package/packages/shared/src/dashboard-plugin/file-handler.ts +73 -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 +9 -3
- package/packages/shared/src/dashboard-plugin/manifest-types.ts +160 -3
- package/packages/shared/src/dashboard-plugin/plugin-status.ts +54 -0
- 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 +139 -2
- package/packages/shared/src/dashboard-plugin/slot-types.ts +72 -4
- package/packages/shared/src/dashboard-plugin/subagent-types.ts +219 -0
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +50 -3
- package/packages/shared/src/dashboard-starter.ts +30 -0
- package/packages/shared/src/display-name.ts +42 -0
- package/packages/shared/src/display-prefs.ts +13 -0
- package/packages/shared/src/doctor-core.ts +119 -134
- 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/live-server.ts +141 -0
- package/packages/shared/src/local-credential-proof.ts +219 -0
- package/packages/shared/src/local-proof-signal.ts +16 -0
- package/packages/shared/src/loopback-host.ts +26 -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/mcp/verb-tiers.generated.ts +178 -0
- package/packages/shared/src/md-table-cell.ts +11 -0
- package/packages/shared/src/mdns-discovery.ts +414 -45
- package/packages/shared/src/models-json-reader.ts +6 -0
- package/packages/shared/src/nav-intent.ts +29 -0
- package/packages/shared/src/openspec-dependencies.ts +760 -0
- package/packages/shared/src/openspec-design-evidence.ts +25 -1
- package/packages/shared/src/openspec-poller.ts +213 -11
- 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 +301 -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-live-query.ts +93 -0
- package/packages/shared/src/pi-trust-mirror-snapshot.ts +198 -0
- package/packages/shared/src/pi-trust-mirror-types.ts +90 -0
- package/packages/shared/src/pi-trust-mirror.ts +354 -0
- package/packages/shared/src/pi-trust-resource-digest.ts +255 -0
- package/packages/shared/src/platform/binary-lookup.ts +2 -194
- 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 +210 -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 +99 -4
- package/packages/shared/src/platform/resolve-pi-shell.ts +76 -0
- package/packages/shared/src/platform/runner.ts +46 -2
- package/packages/shared/src/platform/select-git-source.ts +7 -0
- 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/plugin-route-cwd.ts +95 -0
- package/packages/shared/src/preview-egress.ts +138 -0
- package/packages/shared/src/project-name.ts +42 -0
- package/packages/shared/src/project-root.ts +16 -0
- package/packages/shared/src/prompt-answer.ts +183 -0
- package/packages/shared/src/protocol.ts +172 -8
- package/packages/shared/src/question-types.ts +73 -0
- package/packages/shared/src/recommended-extensions.ts +98 -23
- package/packages/shared/src/recovery-timing.ts +39 -0
- package/packages/shared/src/redaction.ts +50 -0
- package/packages/shared/src/remote-credentials.ts +63 -0
- package/packages/shared/src/rest-api.ts +533 -3
- 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 +181 -0
- package/packages/shared/src/runtime-overlay/archive-safety.d.mts +2 -0
- package/packages/shared/src/runtime-overlay/archive-safety.mjs +33 -0
- package/packages/shared/src/runtime-overlay/manifest.d.mts +7 -0
- package/packages/shared/src/runtime-overlay/manifest.mjs +22 -2
- package/packages/shared/src/runtime-overlay/materialize-plugins.d.mts +17 -0
- package/packages/shared/src/runtime-overlay/materialize-plugins.mjs +119 -8
- package/packages/shared/src/safe-local-path.ts +24 -0
- package/packages/shared/src/sanitize-profiles.ts +98 -0
- package/packages/shared/src/server-identity.ts +51 -15
- package/packages/shared/src/server-launcher.ts +21 -34
- package/packages/shared/src/server-pinning.ts +136 -2
- package/packages/shared/src/server-routes.ts +462 -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/session-ui-path.ts +11 -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-only-token.ts +16 -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 +31 -9
- 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/tool-registry/strategies.ts +12 -11
- package/packages/shared/src/tunnel-provider.ts +3 -2
- package/packages/shared/src/types.ts +170 -9
- package/packages/shared/src/url-policy.ts +96 -0
- package/packages/shared/src/vault-delivery.ts +88 -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 +136 -0
- package/tsconfig.base.json +1 -0
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
* Network access guard for Fastify routes.
|
|
3
3
|
* Supports loopback, trusted networks (CIDR/wildcard/exact), and authenticated users.
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
import { isIPv4, isIPv6 } from "node:net";
|
|
7
|
+
import os from "node:os";
|
|
6
8
|
import { routeTier } from "@blackbelt-technology/pi-dashboard-shared/route-tiers.js";
|
|
9
|
+
import type { FastifyReply, FastifyRequest } from "fastify";
|
|
10
|
+
import { redactCapability } from "../live-server/capability-redact.js";
|
|
7
11
|
import { blockEvents } from "../tunnel/tunnel-block-events.js";
|
|
8
12
|
import { isBypassed } from "./bypass-urls.js";
|
|
9
|
-
import {
|
|
13
|
+
import { hasLocalCredential } from "./local-credential.js";
|
|
10
14
|
import { hasLocalProof, type LocalTrustContext } from "./local-proof.js";
|
|
11
15
|
import { isLoopback, isLoopbackRange } from "./loopback.js";
|
|
16
|
+
import { type NetworkOnlySource, noteNetworkOnlyAdmission } from "./network-trust-telemetry.js";
|
|
12
17
|
|
|
13
18
|
/**
|
|
14
19
|
* Request headers a reverse proxy / tunnel injects. Their presence on a
|
|
@@ -33,7 +38,7 @@ const PROXY_FORWARDING_HEADERS = [
|
|
|
33
38
|
* Core callers keep the 5-header list above: widening the shared constant
|
|
34
39
|
* would change core admission; plugin scopes get the stricter set.
|
|
35
40
|
*/
|
|
36
|
-
const PLUGIN_PROXY_FORWARDING_HEADERS = [
|
|
41
|
+
export const PLUGIN_PROXY_FORWARDING_HEADERS = [
|
|
37
42
|
...PROXY_FORWARDING_HEADERS,
|
|
38
43
|
"via",
|
|
39
44
|
"x-forwarded-server",
|
|
@@ -94,6 +99,21 @@ export function isLocallyTrusted(
|
|
|
94
99
|
return hasLocalProof(input.headers, ctx);
|
|
95
100
|
}
|
|
96
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Is this a genuinely-local caller that the strict default refuses ONLY for want
|
|
104
|
+
* of proof — i.e. the remedy is `pi-dashboard open`? Built from the one shared
|
|
105
|
+
* predicate: the call without ctx means "genuinely local", the call with ctx
|
|
106
|
+
* means "trusted now". A signed-in or paired caller is not asked for proof.
|
|
107
|
+
* See change: close-container-loopback-impersonation (D3).
|
|
108
|
+
*/
|
|
109
|
+
export function needsLocalProof(
|
|
110
|
+
request: { ip: string; headers: unknown; isAuthenticated?: boolean },
|
|
111
|
+
ctx: LocalTrustContext | undefined,
|
|
112
|
+
): boolean {
|
|
113
|
+
const input = { ip: request.ip, headers: request.headers as HeaderBag };
|
|
114
|
+
return isLocallyTrusted(input) && !isLocallyTrusted(input, ctx) && request.isAuthenticated !== true;
|
|
115
|
+
}
|
|
116
|
+
|
|
97
117
|
/**
|
|
98
118
|
* May this request see the access-prompting posture (host-gate mode, whether
|
|
99
119
|
* prompting is on, why a denial was not asked about)? Authenticated OR
|
|
@@ -141,13 +161,20 @@ export function isPluginScopePeerLocal(
|
|
|
141
161
|
hostHeader: string | undefined,
|
|
142
162
|
headers: HeaderBag,
|
|
143
163
|
ctx?: LocalTrustContext,
|
|
164
|
+
/**
|
|
165
|
+
* `peerCredential: "plugin"` scope: the plugin authenticates the peer itself (a ticket in the URL), so
|
|
166
|
+
* ONLY the strict local-proof requirement is skipped here; loopback peer, loopback Host and
|
|
167
|
+
* no-forwarding-header still hold. See change: close-container-loopback-impersonation (D10).
|
|
168
|
+
*/
|
|
169
|
+
opts: { pluginAuthenticatesPeer?: boolean } = {},
|
|
144
170
|
): boolean {
|
|
145
171
|
return (
|
|
146
172
|
isLoopback(ip) &&
|
|
147
173
|
isLoopbackHostHeader(hostHeader) &&
|
|
148
174
|
!hasProxyForwardingHeaders(headers, { extended: true }) &&
|
|
149
|
-
// Strict: a marker-less relay is indistinguishable here; require proof
|
|
150
|
-
|
|
175
|
+
// Strict: a marker-less relay is indistinguishable here; require proof (unless the plugin
|
|
176
|
+
// authenticates the peer with its own credential).
|
|
177
|
+
(opts.pluginAuthenticatesPeer === true || !ctx || !ctx.strict() || hasLocalProof(headers, ctx))
|
|
151
178
|
);
|
|
152
179
|
}
|
|
153
180
|
|
|
@@ -320,6 +347,19 @@ function readRequestOrigin(headers: FastifyRequest["headers"]): string | undefin
|
|
|
320
347
|
*/
|
|
321
348
|
let networkDenialObserver: ((request: FastifyRequest) => void) | null = null;
|
|
322
349
|
|
|
350
|
+
/**
|
|
351
|
+
* Observer told about every REST refusal whose only fault is missing local proof
|
|
352
|
+
* (`local_proof_required`). The server installs its rate-limited logger here and
|
|
353
|
+
* clears it on stop. Best-effort: a throwing observer never alters the refusal.
|
|
354
|
+
* See change: close-container-loopback-impersonation (D6).
|
|
355
|
+
*/
|
|
356
|
+
let localProofRefusalObserver: ((request: FastifyRequest) => void) | null = null;
|
|
357
|
+
|
|
358
|
+
/** Install (or clear, with `null`) the local-proof refusal observer. */
|
|
359
|
+
export function setLocalProofRefusalObserver(observer: ((request: FastifyRequest) => void) | null): void {
|
|
360
|
+
localProofRefusalObserver = observer;
|
|
361
|
+
}
|
|
362
|
+
|
|
323
363
|
/** Install (or clear, with `null`) the network-denial observer. */
|
|
324
364
|
export function setNetworkDenialObserver(observer: ((request: FastifyRequest) => void) | null): void {
|
|
325
365
|
networkDenialObserver = observer;
|
|
@@ -339,17 +379,20 @@ function sendNetworkDenied(request: FastifyRequest, reply: FastifyReply, localTr
|
|
|
339
379
|
// No grant prompt for a relayed-loopback peer: "trust 127.0.0.1?" would name
|
|
340
380
|
// the tunnel agent, and the resulting entry is inert under isTrustedSource
|
|
341
381
|
// (grant->deny loop). See change: fix-trusted-network-tunnel-bypass (D4).
|
|
342
|
-
|
|
382
|
+
// Nor for a bare-loopback caller that merely lacks proof: the remedy is `pi-dashboard open`,
|
|
383
|
+
// and an unproven local caller must not be able to raise an operator prompt.
|
|
384
|
+
// See change: close-container-loopback-impersonation (D3, D8).
|
|
385
|
+
const proofNeeded = needsLocalProof(request as unknown as { ip: string; headers: unknown; isAuthenticated?: boolean }, localTrust);
|
|
386
|
+
if (!proofNeeded && !isRelayedLoopback(request.ip, request.headers as Record<string, unknown>)) {
|
|
343
387
|
try {
|
|
344
388
|
networkDenialObserver?.(request);
|
|
345
389
|
} catch { /* observing is best-effort, never blocks the denial */ }
|
|
346
390
|
}
|
|
347
391
|
// Strict mode: a bare-loopback caller lacking proof gets an actionable denial.
|
|
348
|
-
if (
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
) {
|
|
392
|
+
if (proofNeeded) {
|
|
393
|
+
try {
|
|
394
|
+
localProofRefusalObserver?.(request);
|
|
395
|
+
} catch { /* logging is best-effort, never blocks the refusal */ }
|
|
353
396
|
reply.code(403).send({
|
|
354
397
|
success: false,
|
|
355
398
|
error: "network_not_allowed",
|
|
@@ -365,8 +408,10 @@ function sendNetworkDenied(request: FastifyRequest, reply: FastifyReply, localTr
|
|
|
365
408
|
reply.code(403).send({
|
|
366
409
|
success: false,
|
|
367
410
|
error: "network_not_allowed",
|
|
368
|
-
|
|
369
|
-
|
|
411
|
+
// Identical in every trust mode: this body goes to credential-less callers
|
|
412
|
+
// and must not disclose the mode or point at a trusted list.
|
|
413
|
+
reason: "Source IP is not loopback and the request is not authenticated.",
|
|
414
|
+
hint: "Pair this device (Request access) or sign in.",
|
|
370
415
|
});
|
|
371
416
|
}
|
|
372
417
|
|
|
@@ -404,11 +449,20 @@ function hasNetworkPassCondition(
|
|
|
404
449
|
return true;
|
|
405
450
|
}
|
|
406
451
|
// Affirmative local-IPC token.
|
|
407
|
-
if (opts.localToken &&
|
|
452
|
+
if (opts.localToken && hasLocalCredential(headers)) return true;
|
|
453
|
+
// Credentials BEFORE the network match, so a request that holds one is never
|
|
454
|
+
// classified (or counted) as network-only. Admission outcomes are unchanged:
|
|
455
|
+
// both are "allow". See change: retire-trusted-networks-bypass (D3).
|
|
456
|
+
if ((request as any).isAuthenticated) return true;
|
|
408
457
|
const trusted = opts.readTrusted();
|
|
409
458
|
noteTrustedList(trusted);
|
|
410
|
-
if (isTrustedSource(request.ip, headers, trusted, opts.localTrust))
|
|
411
|
-
|
|
459
|
+
if (isTrustedSource(request.ip, headers, trusted, opts.localTrust)) {
|
|
460
|
+
// Admitted by the network match alone — counted once per request (the
|
|
461
|
+
// per-route guard and the universal hook both evaluate it).
|
|
462
|
+
noteNetworkOnlyAdmission(request as unknown as NetworkOnlySource);
|
|
463
|
+
return true;
|
|
464
|
+
}
|
|
465
|
+
return false;
|
|
412
466
|
}
|
|
413
467
|
|
|
414
468
|
/**
|
|
@@ -715,7 +769,8 @@ export function createNetworkGuardHook(opts: NetworkGuardHookOptions) {
|
|
|
715
769
|
const emitDenial = (path: string, ip: string, reason: string): void =>
|
|
716
770
|
logDenial({
|
|
717
771
|
reason: sanitizeLogField(reason, 60),
|
|
718
|
-
path
|
|
772
|
+
// A live-preview capability token in a refused path must not reach the log.
|
|
773
|
+
path: sanitizeLogField(redactCapability(path)),
|
|
719
774
|
ip: sanitizeLogField(ip, 60),
|
|
720
775
|
});
|
|
721
776
|
|
|
@@ -806,6 +861,232 @@ export function networkAddress(ip: string, netmask: string): string {
|
|
|
806
861
|
].join(".");
|
|
807
862
|
}
|
|
808
863
|
|
|
864
|
+
// ── LAN knock peer-address predicates ────────────────────────────────
|
|
865
|
+
// See change: add-lan-knock-pairing (design D3, D4).
|
|
866
|
+
|
|
867
|
+
/** A parsed peer address: 4 or 16 bytes, plus its canonical text form. */
|
|
868
|
+
export interface ParsedIp {
|
|
869
|
+
family: 4 | 6;
|
|
870
|
+
bytes: Uint8Array;
|
|
871
|
+
/** Canonical text: dotted quad, or zone-less lower-case compressed IPv6. */
|
|
872
|
+
text: string;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
function compressIpv6(groups: number[]): string {
|
|
876
|
+
let bestStart = -1;
|
|
877
|
+
let bestLen = 0;
|
|
878
|
+
for (let i = 0; i < 8; ) {
|
|
879
|
+
if (groups[i] !== 0) {
|
|
880
|
+
i++;
|
|
881
|
+
continue;
|
|
882
|
+
}
|
|
883
|
+
let j = i;
|
|
884
|
+
while (j < 8 && groups[j] === 0) j++;
|
|
885
|
+
if (j - i > bestLen) {
|
|
886
|
+
bestStart = i;
|
|
887
|
+
bestLen = j - i;
|
|
888
|
+
}
|
|
889
|
+
i = j;
|
|
890
|
+
}
|
|
891
|
+
const hex = groups.map((g) => g.toString(16));
|
|
892
|
+
if (bestLen < 2) return hex.join(":");
|
|
893
|
+
return `${hex.slice(0, bestStart).join(":")}::${hex.slice(bestStart + bestLen).join(":")}`;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
/** Expand an IPv6 literal (no zone) to its 8 groups, or null. */
|
|
897
|
+
function ipv6Groups(raw: string): number[] | null {
|
|
898
|
+
// Embedded IPv4 tail (`::ffff:1.2.3.4`) → two hex groups.
|
|
899
|
+
let body = raw;
|
|
900
|
+
const lastColon = body.lastIndexOf(":");
|
|
901
|
+
const tail = body.slice(lastColon + 1);
|
|
902
|
+
if (tail.includes(".")) {
|
|
903
|
+
const o = tail.split(".").map(Number);
|
|
904
|
+
body = `${body.slice(0, lastColon + 1)}${((o[0] << 8) | o[1]).toString(16)}:${((o[2] << 8) | o[3]).toString(16)}`;
|
|
905
|
+
}
|
|
906
|
+
const halves = body.split("::");
|
|
907
|
+
if (halves.length > 2) return null;
|
|
908
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
909
|
+
const rest = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
|
|
910
|
+
const fill = halves.length === 2 ? 8 - head.length - rest.length : 0;
|
|
911
|
+
if (fill < 0 || (halves.length === 1 && head.length !== 8)) return null;
|
|
912
|
+
const groups = [...head, ...Array<string>(fill).fill("0"), ...rest].map((g) => Number.parseInt(g, 16));
|
|
913
|
+
return groups.length === 8 && groups.every((g) => g >= 0 && g <= 0xffff) ? groups : null;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* Parse an IPv4 / IPv6 literal (zone id stripped, IPv4-mapped IPv6 unwrapped to
|
|
918
|
+
* IPv4). Anything that is not an IP literal → `null`. Never throws.
|
|
919
|
+
*/
|
|
920
|
+
export function parseIpAddress(input: unknown): ParsedIp | null {
|
|
921
|
+
if (typeof input !== "string" || input.length === 0 || input.length > 80) return null;
|
|
922
|
+
const pct = input.indexOf("%");
|
|
923
|
+
const raw = (pct === -1 ? input : input.slice(0, pct)).toLowerCase();
|
|
924
|
+
if (isIPv4(raw)) {
|
|
925
|
+
const octets = raw.split(".").map(Number);
|
|
926
|
+
return { family: 4, bytes: Uint8Array.from(octets), text: octets.join(".") };
|
|
927
|
+
}
|
|
928
|
+
const groups = isIPv6(raw) ? ipv6Groups(raw) : null;
|
|
929
|
+
if (!groups) return null;
|
|
930
|
+
// IPv4-mapped (::ffff:a.b.c.d) → the IPv4 form.
|
|
931
|
+
if (groups.slice(0, 5).every((g) => g === 0) && groups[5] === 0xffff) {
|
|
932
|
+
const bytes = Uint8Array.from([groups[6] >> 8, groups[6] & 0xff, groups[7] >> 8, groups[7] & 0xff]);
|
|
933
|
+
return { family: 4, bytes, text: Array.from(bytes).join(".") };
|
|
934
|
+
}
|
|
935
|
+
const bytes = new Uint8Array(16);
|
|
936
|
+
groups.forEach((g, i) => {
|
|
937
|
+
bytes[i * 2] = g >> 8;
|
|
938
|
+
bytes[i * 2 + 1] = g & 0xff;
|
|
939
|
+
});
|
|
940
|
+
return { family: 6, bytes, text: compressIpv6(groups) };
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/** Private ranges a knock may come from (design D4); loopback is NOT one. */
|
|
944
|
+
function isPrivateIp(p: ParsedIp): boolean {
|
|
945
|
+
const b = p.bytes;
|
|
946
|
+
if (p.family === 4) {
|
|
947
|
+
return (
|
|
948
|
+
b[0] === 10 ||
|
|
949
|
+
(b[0] === 172 && b[1] >= 16 && b[1] <= 31) ||
|
|
950
|
+
(b[0] === 192 && b[1] === 168) ||
|
|
951
|
+
(b[0] === 100 && b[1] >= 64 && b[1] <= 127) ||
|
|
952
|
+
(b[0] === 169 && b[1] === 254)
|
|
953
|
+
);
|
|
954
|
+
}
|
|
955
|
+
return (b[0] & 0xfe) === 0xfc || (b[0] === 0xfe && (b[1] & 0xc0) === 0x80);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
function isLoopbackIp(p: ParsedIp): boolean {
|
|
959
|
+
if (p.family === 4) return p.bytes[0] === 127;
|
|
960
|
+
return p.bytes.slice(0, 15).every((x) => x === 0) && p.bytes[15] === 1;
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/** A non-internal interface address the server serves on. */
|
|
964
|
+
export interface LanInterface {
|
|
965
|
+
address: string;
|
|
966
|
+
netmask: string;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/** Leading-ones length of a netmask, or null when it is unparsable or /0 (matches nothing safely). */
|
|
970
|
+
function prefixBits(netmask: string): number | null {
|
|
971
|
+
const m = parseIpAddress(netmask);
|
|
972
|
+
if (!m) return null;
|
|
973
|
+
let bits = 0;
|
|
974
|
+
for (const byte of m.bytes) {
|
|
975
|
+
if (byte === 0xff) {
|
|
976
|
+
bits += 8;
|
|
977
|
+
continue;
|
|
978
|
+
}
|
|
979
|
+
let b = byte;
|
|
980
|
+
while (b & 0x80) {
|
|
981
|
+
bits++;
|
|
982
|
+
b = (b << 1) & 0xff;
|
|
983
|
+
}
|
|
984
|
+
break;
|
|
985
|
+
}
|
|
986
|
+
return bits === 0 ? null : bits;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
function sameNetwork(a: Uint8Array, b: Uint8Array, bits: number): boolean {
|
|
990
|
+
const full = Math.floor(bits / 8);
|
|
991
|
+
for (let i = 0; i < full; i++) if (a[i] !== b[i]) return false;
|
|
992
|
+
const rem = bits % 8;
|
|
993
|
+
if (rem === 0) return true;
|
|
994
|
+
const mask = (0xff << (8 - rem)) & 0xff;
|
|
995
|
+
return (a[full] & mask) === (b[full] & mask);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Is `ip` a private, non-loopback address inside the subnet of one of
|
|
1000
|
+
* `interfaces`? A missing / unparsable address fails closed.
|
|
1001
|
+
*/
|
|
1002
|
+
export function isOnLinkPrivate(ip: string | undefined, interfaces: readonly LanInterface[]): boolean {
|
|
1003
|
+
const peer = parseIpAddress(ip);
|
|
1004
|
+
if (!peer || isLoopbackIp(peer) || !isPrivateIp(peer)) return false;
|
|
1005
|
+
for (const iface of interfaces) {
|
|
1006
|
+
const own = parseIpAddress(iface.address);
|
|
1007
|
+
if (!own || own.family !== peer.family) continue;
|
|
1008
|
+
const bits = prefixBits(iface.netmask);
|
|
1009
|
+
// An unparsable / zero mask must never admit the whole address family; a /32 (or
|
|
1010
|
+
// /128) point-to-point interface has no neighbours.
|
|
1011
|
+
if (bits === null || bits >= peer.bytes.length * 8) continue;
|
|
1012
|
+
if (sameNetwork(peer.bytes, own.bytes, bits)) return true;
|
|
1013
|
+
}
|
|
1014
|
+
return false;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* Does this entry match every address? `x/0` for any base or spelling (`0.0.0.0/00`,
|
|
1019
|
+
* `::0/0`, `1.2.3.4/0`), or a wildcard of only `*` — never a knock-eligibility grant.
|
|
1020
|
+
*/
|
|
1021
|
+
function isMatchAllEntry(entry: string): boolean {
|
|
1022
|
+
const slash = entry.indexOf("/");
|
|
1023
|
+
if (slash >= 0) return Number.parseInt(entry.slice(slash + 1), 10) === 0;
|
|
1024
|
+
return /^[*.]+$/.test(entry) && entry.includes("*");
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* Knock eligibility from the configured list: may `peer` KNOCK because an entry
|
|
1029
|
+
* of the raw trusted list covers it? Applies only to a request with NO
|
|
1030
|
+
* proxy-forwarding header at all (as strict as the private-range branch), ignores
|
|
1031
|
+
* match-all entries, and does not require the peer to be on-link. A relayed
|
|
1032
|
+
* loopback peer is refused (it carries a forwarding header). It authenticates
|
|
1033
|
+
* nothing — it only widens where a knock may come from.
|
|
1034
|
+
* See change: retire-trusted-networks-bypass (D7).
|
|
1035
|
+
*/
|
|
1036
|
+
export function isKnockListedPeer(
|
|
1037
|
+
peer: string | undefined,
|
|
1038
|
+
headers: HeaderBag,
|
|
1039
|
+
list: readonly string[],
|
|
1040
|
+
): boolean {
|
|
1041
|
+
if (!peer || hasProxyForwardingHeaders(headers)) return false;
|
|
1042
|
+
const entries = list.map((e) => e.trim()).filter((e) => e !== "" && !isMatchAllEntry(e));
|
|
1043
|
+
return entries.length > 0 && isBypassedHost(peer, entries);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* The per-source identity of a knock: the full address (IPv6 without zone,
|
|
1048
|
+
* IPv4-mapped unwrapped). Deliberately NOT a /64 — see design D4.
|
|
1049
|
+
*/
|
|
1050
|
+
export function knockSourceKey(ip: string): string {
|
|
1051
|
+
return parseIpAddress(ip)?.text ?? ip;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
const LAN_INTERFACE_TTL_MS = 30_000;
|
|
1055
|
+
|
|
1056
|
+
/** Does the server's bind host cover an interface address? Wildcard / non-IP host → all. */
|
|
1057
|
+
function bindCovers(bindHost: string | undefined, address: string): boolean {
|
|
1058
|
+
const bind = parseIpAddress(bindHost);
|
|
1059
|
+
if (!bindHost || bindHost === "0.0.0.0" || bindHost === "::" || bindHost === "::0" || bind === null) return true;
|
|
1060
|
+
const own = parseIpAddress(address);
|
|
1061
|
+
return own !== null && own.family === bind.family && own.text === bind.text;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
/**
|
|
1065
|
+
* Cached provider of the non-internal interface addresses the server's bind
|
|
1066
|
+
* host covers. A wildcard (or non-IP host name) covers all of them; a specific
|
|
1067
|
+
* address covers only itself (so a loopback bind covers none). Cached 30 s so a
|
|
1068
|
+
* refusal flood costs no per-request syscall.
|
|
1069
|
+
*/
|
|
1070
|
+
export function createLanInterfaceProvider(opts: {
|
|
1071
|
+
bindHost?: string;
|
|
1072
|
+
read?: () => ReturnType<typeof os.networkInterfaces>;
|
|
1073
|
+
now?: () => number;
|
|
1074
|
+
}): () => LanInterface[] {
|
|
1075
|
+
const read = opts.read ?? os.networkInterfaces;
|
|
1076
|
+
const now = opts.now ?? Date.now;
|
|
1077
|
+
let cached: LanInterface[] = [];
|
|
1078
|
+
let at = Number.NEGATIVE_INFINITY;
|
|
1079
|
+
return () => {
|
|
1080
|
+
if (now() - at < LAN_INTERFACE_TTL_MS) return cached;
|
|
1081
|
+
cached = Object.values(read())
|
|
1082
|
+
.flatMap((addrs) => addrs ?? [])
|
|
1083
|
+
.filter((info) => !info.internal && bindCovers(opts.bindHost, info.address))
|
|
1084
|
+
.map((info) => ({ address: info.address, netmask: info.netmask }));
|
|
1085
|
+
at = now();
|
|
1086
|
+
return cached;
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
|
|
809
1090
|
/** Legacy localhost-only guard. Prefer createNetworkGuard() for new code. */
|
|
810
1091
|
export async function localhostGuard(
|
|
811
1092
|
request: FastifyRequest,
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-signed navigation intents (change: harden-client-sinks, design D10).
|
|
3
|
+
*
|
|
4
|
+
* `ni = base64url(e) "." base64url(HMAC-SHA256(key, p \0 k \0 canon(target) \0 e))`
|
|
5
|
+
*
|
|
6
|
+
* Only the expiry `e` (epoch seconds, exactly {@link NAV_INTENT_TTL_S} after
|
|
7
|
+
* minting) and the MAC travel in the URL. The principal `p`, the kind `k`
|
|
8
|
+
* (`url-view` | `file-view`) and the canonical target are NOT in the value —
|
|
9
|
+
* the verifier takes them from its own request context and the route, then
|
|
10
|
+
* recomputes the MAC (constant-time compare). The key is a per-process random
|
|
11
|
+
* 32 bytes (the `SignedChallenges` pattern): a restart invalidates every
|
|
12
|
+
* intent, by design. An intent only upgrades a route-backed overlay's
|
|
13
|
+
* provenance from `agent` to `operator`; without one, the overlay keeps
|
|
14
|
+
* today's fail-closed agent provenance.
|
|
15
|
+
*/
|
|
16
|
+
import crypto from "node:crypto";
|
|
17
|
+
import type { NavIntentKind, NavIntentTarget } from "@blackbelt-technology/pi-dashboard-shared/nav-intent.js";
|
|
18
|
+
|
|
19
|
+
export const NAV_INTENT_TTL_S = 600;
|
|
20
|
+
const MAX_NI_LENGTH = 256;
|
|
21
|
+
|
|
22
|
+
export type { NavIntentKind, NavIntentTarget };
|
|
23
|
+
|
|
24
|
+
export function isNavIntentKind(v: unknown): v is NavIntentKind {
|
|
25
|
+
return v === "url-view" || v === "file-view";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Canonical target bytes for the MAC; `null` when `target` does not fit `kind`. */
|
|
29
|
+
export function canonicalNavTarget(kind: NavIntentKind, target: unknown): string | null {
|
|
30
|
+
if (!target || typeof target !== "object") return null;
|
|
31
|
+
const t = target as Record<string, unknown>;
|
|
32
|
+
if (kind === "url-view") return typeof t.url === "string" && t.url.length > 0 && t.url.length <= 4096 ? t.url : null;
|
|
33
|
+
if (typeof t.cwd !== "string" || typeof t.path !== "string" || !t.cwd || !t.path) return null;
|
|
34
|
+
if (t.cwd.length > 4096 || t.path.length > 4096) return null;
|
|
35
|
+
return `${t.cwd}\u0000${t.path}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class NavIntents {
|
|
39
|
+
private readonly key: Buffer;
|
|
40
|
+
private readonly now: () => number;
|
|
41
|
+
|
|
42
|
+
constructor(now: () => number = Date.now, key: Buffer = crypto.randomBytes(32)) {
|
|
43
|
+
this.now = now;
|
|
44
|
+
this.key = key;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private mac(principal: string, kind: NavIntentKind, canon: string, e: number): Buffer {
|
|
48
|
+
return crypto.createHmac("sha256", this.key).update(`${principal}\u0000${kind}\u0000${canon}\u0000${e}`).digest();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Mint an intent for `principal` opening `target`. Throws on a target that does not fit `kind`. */
|
|
52
|
+
mint(principal: string, kind: NavIntentKind, target: NavIntentTarget): string {
|
|
53
|
+
const canon = canonicalNavTarget(kind, target);
|
|
54
|
+
if (canon === null) throw new TypeError("nav-intent: target does not match kind");
|
|
55
|
+
const e = Math.floor(this.now() / 1000) + NAV_INTENT_TTL_S;
|
|
56
|
+
return `${Buffer.from(String(e)).toString("base64url")}.${this.mac(principal, kind, canon, e).toString("base64url")}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** True only for an unexpired intent minted by this process for exactly this principal, kind and target. */
|
|
60
|
+
verify(ni: unknown, principal: string, kind: NavIntentKind, target: unknown): boolean {
|
|
61
|
+
if (typeof ni !== "string" || ni.length > MAX_NI_LENGTH) return false;
|
|
62
|
+
const parts = ni.split(".");
|
|
63
|
+
if (parts.length !== 2) return false;
|
|
64
|
+
const e = Number(Buffer.from(parts[0], "base64url").toString("utf8"));
|
|
65
|
+
if (!Number.isSafeInteger(e) || e * 1000 <= this.now()) return false;
|
|
66
|
+
const canon = canonicalNavTarget(kind, target);
|
|
67
|
+
if (canon === null) return false;
|
|
68
|
+
const given = Buffer.from(parts[1], "base64url");
|
|
69
|
+
const want = this.mac(principal, kind, canon, e);
|
|
70
|
+
return given.length === want.length && crypto.timingSafeEqual(given, want);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory telemetry of NETWORK-ONLY admissions: requests / WebSocket upgrades
|
|
3
|
+
* admitted solely because the socket peer matched the trusted list (legacy
|
|
4
|
+
* trust mode), with no other pass condition holding. It tells the operator who
|
|
5
|
+
* still depends on network trust before they move to `pair`.
|
|
6
|
+
*
|
|
7
|
+
* Bounded by construction: peers live in a recency-ordered `Map` capped at
|
|
8
|
+
* {@link MAX_PEERS} (least recently seen evicted, a repeat refreshes), the
|
|
9
|
+
* admission count is bucketed per hour over the {@link WINDOW_MS} window, and
|
|
10
|
+
* the per-peer log line is throttled to once per {@link LOG_THROTTLE_MS}.
|
|
11
|
+
* Nothing is persisted; the window is "7 days or since the last restart",
|
|
12
|
+
* whichever is shorter. Peer addresses are never exposed by `snapshot` — only
|
|
13
|
+
* counts — so `/api/health` cannot leak them.
|
|
14
|
+
*
|
|
15
|
+
* A request is counted at most once: the per-route guard and the universal hook
|
|
16
|
+
* both evaluate it, so a per-request marker (`networkOnlyCounted`) dedupes.
|
|
17
|
+
*
|
|
18
|
+
* See change: retire-trusted-networks-bypass (design D4).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export const MAX_PEERS = 256;
|
|
22
|
+
export const WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
23
|
+
export const LOG_THROTTLE_MS = 60 * 60 * 1000;
|
|
24
|
+
const BUCKET_MS = 60 * 60 * 1000;
|
|
25
|
+
|
|
26
|
+
export interface NetworkOnlySnapshot {
|
|
27
|
+
/** Network-only admissions inside the window. */
|
|
28
|
+
admissions: number;
|
|
29
|
+
/** Distinct peer addresses inside the window (never listed). */
|
|
30
|
+
distinctPeers: number;
|
|
31
|
+
/** Epoch ms of the most recent admission, or `null` when none. */
|
|
32
|
+
lastAt: number | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Anything with a socket peer; the marker dedupes a request seen by two guards. */
|
|
36
|
+
export interface NetworkOnlySource {
|
|
37
|
+
ip: string;
|
|
38
|
+
networkOnlyCounted?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface NetworkTrustTelemetry {
|
|
42
|
+
note(source: NetworkOnlySource, now?: number): void;
|
|
43
|
+
snapshot(now?: number): NetworkOnlySnapshot;
|
|
44
|
+
reset(): void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function createNetworkTrustTelemetry(
|
|
48
|
+
log: (line: string) => void = (line) => console.warn(line),
|
|
49
|
+
): NetworkTrustTelemetry {
|
|
50
|
+
/** ip → lastSeen; delete-then-set keeps insertion order == recency order. */
|
|
51
|
+
const peers = new Map<string, number>();
|
|
52
|
+
const lastLogged = new Map<string, number>();
|
|
53
|
+
/** hour bucket index → admissions in it. */
|
|
54
|
+
const buckets = new Map<number, number>();
|
|
55
|
+
let lastAt: number | null = null;
|
|
56
|
+
|
|
57
|
+
const prune = (now: number): void => {
|
|
58
|
+
for (const [ip, seen] of peers) {
|
|
59
|
+
if (now - seen > WINDOW_MS) {
|
|
60
|
+
peers.delete(ip);
|
|
61
|
+
lastLogged.delete(ip);
|
|
62
|
+
} else {
|
|
63
|
+
break; // recency order: everything after is newer
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const oldestBucket = Math.floor((now - WINDOW_MS) / BUCKET_MS);
|
|
67
|
+
for (const idx of buckets.keys()) if (idx < oldestBucket) buckets.delete(idx);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
note(source, now = Date.now()) {
|
|
72
|
+
if (source.networkOnlyCounted) return;
|
|
73
|
+
source.networkOnlyCounted = true;
|
|
74
|
+
prune(now);
|
|
75
|
+
const ip = source.ip;
|
|
76
|
+
peers.delete(ip);
|
|
77
|
+
peers.set(ip, now);
|
|
78
|
+
while (peers.size > MAX_PEERS) {
|
|
79
|
+
const oldest = peers.keys().next().value as string | undefined;
|
|
80
|
+
if (oldest === undefined) break;
|
|
81
|
+
peers.delete(oldest);
|
|
82
|
+
lastLogged.delete(oldest);
|
|
83
|
+
}
|
|
84
|
+
const bucket = Math.floor(now / BUCKET_MS);
|
|
85
|
+
buckets.set(bucket, (buckets.get(bucket) ?? 0) + 1);
|
|
86
|
+
lastAt = now;
|
|
87
|
+
const logged = lastLogged.get(ip);
|
|
88
|
+
if (logged === undefined || now - logged >= LOG_THROTTLE_MS) {
|
|
89
|
+
lastLogged.set(ip, now);
|
|
90
|
+
log(
|
|
91
|
+
`[trusted-networks] ${ip} reaches this dashboard by network trust only (no sign-in, no paired device) — pair it, or move to "pair" mode`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
snapshot(now = Date.now()) {
|
|
96
|
+
prune(now);
|
|
97
|
+
let admissions = 0;
|
|
98
|
+
for (const n of buckets.values()) admissions += n;
|
|
99
|
+
return { admissions, distinctPeers: peers.size, lastAt: admissions > 0 ? lastAt : null };
|
|
100
|
+
},
|
|
101
|
+
reset() {
|
|
102
|
+
peers.clear();
|
|
103
|
+
lastLogged.clear();
|
|
104
|
+
buckets.clear();
|
|
105
|
+
lastAt = null;
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The process-wide instance every admission site reports to. */
|
|
111
|
+
const telemetry = createNetworkTrustTelemetry();
|
|
112
|
+
|
|
113
|
+
/** Record a network-only admission (idempotent per request). */
|
|
114
|
+
export function noteNetworkOnlyAdmission(source: NetworkOnlySource, now?: number): void {
|
|
115
|
+
telemetry.note(source, now);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Counters for `/api/health` `trustPosture.networkOnly`. */
|
|
119
|
+
export function networkOnlySnapshot(now?: number): NetworkOnlySnapshot {
|
|
120
|
+
return telemetry.snapshot(now);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Test seam. */
|
|
124
|
+
export function resetNetworkTrustTelemetryForTests(): void {
|
|
125
|
+
telemetry.reset();
|
|
126
|
+
}
|
|
@@ -22,6 +22,8 @@ import {
|
|
|
22
22
|
isAffectedNode,
|
|
23
23
|
isOutOfEnginesRange,
|
|
24
24
|
} from "@blackbelt-technology/pi-dashboard-shared/node-version.js";
|
|
25
|
+
import { isJitiLoader, isTsxLoader } from "@blackbelt-technology/pi-dashboard-shared/platform/node-spawn.js";
|
|
26
|
+
import { legacyTsLoaderEnvWarning } from "@blackbelt-technology/pi-dashboard-shared/platform/ts-loader-select.mjs";
|
|
25
27
|
|
|
26
28
|
export { isAffectedNode, isOutOfEnginesRange };
|
|
27
29
|
|
|
@@ -99,3 +101,44 @@ export function assertNodeVersionSupported(): void {
|
|
|
99
101
|
process.exit(1);
|
|
100
102
|
}
|
|
101
103
|
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Warning for a server that booted under a non-native TypeScript loader, or
|
|
107
|
+
* `null`. A jiti or tsx `--import` is recognised and named; any other
|
|
108
|
+
* `--import` (e.g. a tracing hook) is left alone because it is not provably a
|
|
109
|
+
* TypeScript loader. This happens when a pre-upgrade server restarts itself
|
|
110
|
+
* and hands the new process its own jiti `--import`.
|
|
111
|
+
* See change: strip-only-native-ts-loader (design D5).
|
|
112
|
+
*/
|
|
113
|
+
export function foreignTsLoaderWarning(execArgv: readonly string[] = process.execArgv): string | null {
|
|
114
|
+
for (let i = 0; i < execArgv.length; i++) {
|
|
115
|
+
const arg = execArgv[i] ?? "";
|
|
116
|
+
// Both `--import <url>` and the combined `--import=<url>` spellings.
|
|
117
|
+
const loader = arg === "--import" ? (execArgv[i + 1] ?? "") : arg.startsWith("--import=") ? arg.slice("--import=".length) : null;
|
|
118
|
+
if (loader === null) continue;
|
|
119
|
+
const name = isJitiLoader(loader) ? "jiti" : isTsxLoader(loader) ? "tsx" : null;
|
|
120
|
+
if (name) {
|
|
121
|
+
return (
|
|
122
|
+
`[pi-dashboard] server booted under the ${name} TypeScript loader (${loader}), which is no longer supported; ` +
|
|
123
|
+
"run `pi-dashboard stop && pi-dashboard start` to switch to the native loader."
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Boot-time TypeScript-loader hygiene, run once by the server process: warn on
|
|
132
|
+
* a legacy `PI_DASHBOARD_TS_LOADER` value and on a foreign loader. Only the
|
|
133
|
+
* server logs these; wrappers and launchers neither warn nor rewrite the env.
|
|
134
|
+
*/
|
|
135
|
+
export function warnTsLoaderState(
|
|
136
|
+
env: Record<string, string | undefined> = process.env,
|
|
137
|
+
execArgv: readonly string[] = process.execArgv,
|
|
138
|
+
warn: (msg: string) => void = console.warn,
|
|
139
|
+
): void {
|
|
140
|
+
const legacy = legacyTsLoaderEnvWarning(env);
|
|
141
|
+
if (legacy) warn(`[pi-dashboard] ${legacy}`);
|
|
142
|
+
const foreign = foreignTsLoaderWarning(execArgv);
|
|
143
|
+
if (foreign) warn(foreign);
|
|
144
|
+
}
|