machine-bridge-mcp 2.0.0 → 3.0.0-beta.102
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/CHANGELOG.md +1142 -0
- package/CONTRIBUTING.md +23 -25
- package/GOVERNANCE.md +2 -2
- package/README.md +58 -20
- package/SECURITY.md +161 -120
- package/SUPPORT.md +1 -1
- package/browser-extension/broker-auth.js +83 -0
- package/browser-extension/broker-liveness.js +35 -0
- package/browser-extension/browser-error-boundary.js +96 -0
- package/browser-extension/browser-operations.js +1456 -101
- package/browser-extension/devtools-input.js +195 -34
- package/browser-extension/devtools-observation.js +429 -0
- package/browser-extension/devtools-session.js +48 -0
- package/browser-extension/manifest.json +4 -3
- package/browser-extension/page-automation.js +578 -118
- package/browser-extension/pairing-bootstrap.js +42 -0
- package/browser-extension/pairing.js +28 -12
- package/browser-extension/service-worker.js +126 -106
- package/docs/AGENT_CONTEXT.md +13 -10
- package/docs/ARCHITECTURE.md +159 -67
- package/docs/AUDIT.md +1302 -4
- package/docs/CLIENTS.md +19 -6
- package/docs/COMPUTER_USE.md +297 -0
- package/docs/ENGINEERING.md +72 -13
- package/docs/GETTING_STARTED.md +18 -20
- package/docs/LOCAL_AUTHORIZATION.md +115 -72
- package/docs/LOCAL_AUTOMATION.md +27 -21
- package/docs/LOGGING.md +40 -19
- package/docs/MANAGED_JOBS.md +37 -34
- package/docs/MULTI_ACCOUNT.md +98 -37
- package/docs/OPERATIONS.md +145 -51
- package/docs/OVERVIEW.md +14 -11
- package/docs/POLICY_REFERENCE.md +71 -3
- package/docs/PRIVACY.md +41 -2
- package/docs/PROJECT_STANDARDS.md +29 -6
- package/docs/RELEASING.md +238 -64
- package/docs/TESTING.md +150 -39
- package/docs/THREAT_MODEL.md +164 -67
- package/docs/TOOL_REFERENCE.md +653 -60
- package/docs/UPGRADING.md +78 -18
- package/native/macos/MachineBridgeBackgroundInput.swift +1051 -0
- package/native/macos/MachineBridgeBackgroundInputSmokeFixture.swift +295 -0
- package/native/macos/MachineBridgeTrustBroker.swift +233 -0
- package/package.json +94 -16
- package/scripts/accepted-candidate-tarball.mjs +77 -0
- package/scripts/candidate-runtime-store.mjs +93 -0
- package/scripts/check-plan.mjs +73 -3
- package/scripts/check-runner.mjs +129 -17
- package/scripts/commit-message-check.mjs +5 -2
- package/scripts/consumer-package-security.mjs +293 -0
- package/scripts/coverage-check.mjs +251 -15
- package/scripts/coverage-generation.mjs +41 -0
- package/scripts/coverage-range-merge.mjs +41 -0
- package/scripts/foreground-daemon-recovery.mjs +88 -0
- package/scripts/generate-policy-reference.mjs +28 -5
- package/scripts/generate-tool-reference.mjs +1 -1
- package/scripts/generate-worker-types.mjs +21 -11
- package/scripts/github-backlog.mjs +11 -10
- package/scripts/github-push.mjs +62 -16
- package/scripts/github-release-asset.mjs +58 -0
- package/scripts/github-release.mjs +241 -132
- package/scripts/global-package-installation.mjs +68 -0
- package/scripts/hardened-npm-session.mjs +42 -0
- package/scripts/install-published-prerelease.mjs +198 -0
- package/scripts/local-release-acceptance.mjs +103 -45
- package/scripts/macos-background-input-smoke.mjs +515 -0
- package/scripts/network-retry.mjs +8 -1
- package/scripts/npm-global-prefix.mjs +27 -0
- package/scripts/npm-publication-policy.mjs +17 -0
- package/scripts/official-mcp-conformance.mjs +267 -0
- package/scripts/persistent-activation-process.mjs +101 -0
- package/scripts/prepare-pinned-npm.mjs +12 -69
- package/scripts/prerelease-activation.mjs +160 -0
- package/scripts/privacy-check.mjs +14 -6
- package/scripts/promotion-digest.mjs +128 -0
- package/scripts/publish-npm.mjs +260 -0
- package/scripts/published-release.mjs +132 -0
- package/scripts/release-acceptance.mjs +62 -54
- package/scripts/release-browser-extension-store.mjs +172 -0
- package/scripts/release-candidate-manifest.mjs +52 -0
- package/scripts/release-channel.mjs +106 -0
- package/scripts/release-diagnostic.mjs +41 -0
- package/scripts/release-impact-check.mjs +12 -22
- package/scripts/release-oauth-canary-core.mjs +325 -0
- package/scripts/release-oauth-canary-evidence.mjs +100 -0
- package/scripts/release-oauth-canary.mjs +127 -0
- package/scripts/release-publication-guard.mjs +67 -0
- package/scripts/release-soak.mjs +284 -0
- package/scripts/release-state.mjs +1 -1
- package/scripts/run-checks.mjs +44 -7
- package/scripts/run-worker-dry-run.mjs +21 -0
- package/scripts/sarif-security-gate.mjs +2 -1
- package/scripts/sbom-check.mjs +106 -0
- package/scripts/start-release-candidate.mjs +267 -44
- package/scripts/syntax-check.mjs +36 -14
- package/scripts/verification-generation-guard.mjs +15 -0
- package/scripts/verification-idle-sleep-guard.mjs +37 -0
- package/scripts/verification-state.mjs +108 -0
- package/scripts/wrangler-command-lifecycle.mjs +115 -0
- package/src/local/account-access.mjs +13 -1
- package/src/local/account-admin-response.mjs +92 -0
- package/src/local/account-admin.mjs +52 -19
- package/src/local/agent-context-projection.mjs +26 -7
- package/src/local/agent-context.mjs +88 -49
- package/src/local/agent-skill-discovery.mjs +16 -5
- package/src/local/app-automation-macos-jxa.mjs +350 -0
- package/src/local/app-automation.mjs +873 -163
- package/src/local/application-capability-projection.mjs +24 -0
- package/src/local/authority-context.mjs +106 -0
- package/src/local/autostart-log-maintenance.mjs +36 -0
- package/src/local/browser-bridge-http.mjs +5 -3
- package/src/local/browser-bridge.mjs +109 -31
- package/src/local/browser-broker-auth-http.mjs +67 -0
- package/src/local/browser-broker-auth.mjs +98 -0
- package/src/local/browser-broker-routes.mjs +63 -20
- package/src/local/browser-broker-server.mjs +17 -7
- package/src/local/browser-command.mjs +79 -30
- package/src/local/browser-computer-observation-service.mjs +266 -0
- package/src/local/browser-extension-identity.mjs +50 -0
- package/src/local/browser-extension-path.mjs +22 -0
- package/src/local/browser-extension-protocol.mjs +50 -11
- package/src/local/browser-operation-service.mjs +147 -117
- package/src/local/browser-pairing-grant.mjs +109 -0
- package/src/local/browser-pairing-http.mjs +36 -0
- package/src/local/browser-pairing-launch.mjs +58 -0
- package/src/local/browser-pairing-store.mjs +85 -48
- package/src/local/browser-request-registry.mjs +40 -14
- package/src/local/browser-request-settlement.mjs +78 -0
- package/src/local/browser-resource-input.mjs +74 -0
- package/src/local/browser-trusted-input-health.mjs +63 -0
- package/src/local/call-authority.mjs +21 -0
- package/src/local/call-capacity.mjs +45 -0
- package/src/local/call-registry-drain.mjs +54 -0
- package/src/local/call-registry.mjs +30 -15
- package/src/local/capability-observer.mjs +5 -0
- package/src/local/capability-ranking.mjs +3 -2
- package/src/local/child-process-settlement.mjs +132 -0
- package/src/local/cli-account-admin.mjs +40 -6
- package/src/local/cli-activate.mjs +162 -0
- package/src/local/cli-local-admin.mjs +20 -45
- package/src/local/cli-options.mjs +12 -13
- package/src/local/cli-ready-output.mjs +52 -0
- package/src/local/cli-service.mjs +91 -11
- package/src/local/cli.mjs +232 -150
- package/src/local/computer-use-application-observation.mjs +224 -0
- package/src/local/computer-use-arguments.mjs +270 -0
- package/src/local/computer-use-deadline.mjs +50 -0
- package/src/local/computer-use-dispatch-settlement.mjs +40 -0
- package/src/local/computer-use-expectation.mjs +171 -0
- package/src/local/computer-use-observation.mjs +769 -0
- package/src/local/computer-use-recovery.mjs +82 -0
- package/src/local/computer-use-result-budget.mjs +145 -0
- package/src/local/computer-use-snapshot-store.mjs +67 -0
- package/src/local/computer-use.mjs +2255 -0
- package/src/local/daemon-http-relay-auth.mjs +26 -0
- package/src/local/daemon-http-relay-connection.mjs +298 -0
- package/src/local/daemon-http-relay-request.mjs +64 -0
- package/src/local/daemon-http-relay-sequence.mjs +49 -0
- package/src/local/daemon-process.mjs +40 -7
- package/src/local/delegated-process-sandbox.mjs +148 -0
- package/src/local/device-identity.mjs +156 -49
- package/src/local/device-root-provider.mjs +81 -0
- package/src/local/directory-metadata.mjs +47 -0
- package/src/local/doctor-reporting.mjs +23 -0
- package/src/local/durable-process-spec.mjs +80 -0
- package/src/local/errors.mjs +25 -15
- package/src/local/exclusive-file.mjs +74 -26
- package/src/local/execution-limits.mjs +9 -0
- package/src/local/execution-routing.mjs +276 -0
- package/src/local/execution-surface.mjs +23 -0
- package/src/local/file-mutation-coordinator.mjs +47 -0
- package/src/local/file-snapshot-preservation.mjs +24 -0
- package/src/local/filesystem-identity.mjs +54 -0
- package/src/local/fixed-process-environment.mjs +21 -0
- package/src/local/full-access-test.mjs +31 -13
- package/src/local/git-commit.mjs +69 -0
- package/src/local/git-config-safety.mjs +55 -0
- package/src/local/git-log-parser.mjs +41 -0
- package/src/local/git-metadata-boundary.mjs +77 -0
- package/src/local/git-metadata-tree-safety.mjs +59 -0
- package/src/local/git-operation-state.mjs +19 -0
- package/src/local/git-service.mjs +165 -30
- package/src/local/hardened-npm-download-timeout.mjs +46 -0
- package/src/local/hardened-npm-download.mjs +109 -0
- package/src/local/hardened-npm-extract.mjs +20 -0
- package/src/local/hardened-npm-verification.mjs +104 -0
- package/src/local/hardened-npm.mjs +199 -0
- package/src/local/job-runner.mjs +244 -89
- package/src/local/lifecycle.mjs +6 -0
- package/src/local/log.mjs +19 -45
- package/src/local/macos-background-input.mjs +398 -0
- package/src/local/macos-idle-sleep-assertion.mjs +65 -0
- package/src/local/macos-trust-broker.mjs +404 -0
- package/src/local/managed-job-cancellation.mjs +42 -0
- package/src/local/managed-job-capacity.mjs +16 -0
- package/src/local/managed-job-directory-generation.mjs +60 -0
- package/src/local/managed-job-directory.mjs +75 -0
- package/src/local/managed-job-durable-process.mjs +49 -0
- package/src/local/managed-job-lock.mjs +46 -14
- package/src/local/managed-job-plan.mjs +38 -14
- package/src/local/managed-job-projection.mjs +23 -15
- package/src/local/managed-job-retention.mjs +128 -0
- package/src/local/managed-job-runner-claim.mjs +70 -0
- package/src/local/managed-job-runner.mjs +35 -10
- package/src/local/managed-job-storage.mjs +22 -6
- package/src/local/managed-job-terminal-maintenance.mjs +40 -0
- package/src/local/managed-job-terminal.mjs +150 -0
- package/src/local/managed-jobs.mjs +424 -287
- package/src/local/npm-cli.mjs +42 -0
- package/src/local/npm-environment.mjs +37 -0
- package/src/local/operation-authorization.mjs +68 -324
- package/src/local/operation-risk.mjs +51 -4
- package/src/local/owner-state-lock.mjs +118 -0
- package/src/local/package-identity.mjs +9 -0
- package/src/local/patch.mjs +20 -18
- package/src/local/path-inspection.mjs +23 -0
- package/src/local/policy.mjs +19 -0
- package/src/local/private-toolchain-integrity.mjs +54 -0
- package/src/local/process-execution.mjs +87 -75
- package/src/local/process-foreground-timeout.mjs +50 -0
- package/src/local/process-identity.mjs +41 -7
- package/src/local/process-nonreplayable-settlement.mjs +50 -0
- package/src/local/process-result-projection.mjs +22 -0
- package/src/local/process-session-events.mjs +47 -0
- package/src/local/process-session-remote-activity.mjs +10 -0
- package/src/local/process-session-termination.mjs +73 -0
- package/src/local/process-sessions.mjs +144 -112
- package/src/local/process-tracker.mjs +140 -13
- package/src/local/process-tree-ownership-types.d.ts +37 -0
- package/src/local/process-tree-ownership.mjs +57 -0
- package/src/local/process-tree-signal.mjs +34 -0
- package/src/local/process-tree-snapshot.mjs +77 -0
- package/src/local/process-tree-supervisor.mjs +69 -0
- package/src/local/process-tree.mjs +11 -53
- package/src/local/project-metadata.mjs +19 -6
- package/src/local/relay-call-recovery.mjs +58 -38
- package/src/local/relay-connection-classification.mjs +131 -0
- package/src/local/relay-connection-support.mjs +91 -0
- package/src/local/relay-connection.mjs +179 -272
- package/src/local/relay-diagnostics.mjs +91 -0
- package/src/local/relay-heartbeat.mjs +119 -0
- package/src/local/relay-liveness.mjs +109 -0
- package/src/local/relay-peer-diagnostics.mjs +29 -0
- package/src/local/release-runtime-lock.mjs +18 -0
- package/src/local/remote-activity-idle-sleep-guard.mjs +77 -0
- package/src/local/remote-configuration.mjs +48 -0
- package/src/local/resilient-relay-connection.mjs +178 -0
- package/src/local/resource-admission-diagnostics.mjs +52 -0
- package/src/local/resource-admission-policy.mjs +115 -0
- package/src/local/resource-admission.mjs +339 -0
- package/src/local/resource-build-root.mjs +83 -0
- package/src/local/resource-cargo-concurrency.mjs +38 -0
- package/src/local/resource-cmake-concurrency.mjs +42 -0
- package/src/local/resource-command-concurrency.mjs +37 -0
- package/src/local/resource-command-profile.mjs +176 -0
- package/src/local/resource-coordinator-accounting.mjs +21 -0
- package/src/local/resource-cpu-window.mjs +20 -0
- package/src/local/resource-disk-headroom.mjs +13 -0
- package/src/local/resource-elastic-memory.mjs +19 -0
- package/src/local/resource-elastic-request.mjs +36 -0
- package/src/local/resource-foreground-wait.mjs +29 -0
- package/src/local/resource-go-concurrency.mjs +43 -0
- package/src/local/resource-gradle-concurrency.mjs +57 -0
- package/src/local/resource-host-cache.mjs +39 -0
- package/src/local/resource-host-darwin.mjs +54 -0
- package/src/local/resource-host-linux.mjs +35 -0
- package/src/local/resource-host-sample-file.mjs +23 -0
- package/src/local/resource-host-snapshot.mjs +99 -0
- package/src/local/resource-lease-accounting.mjs +119 -0
- package/src/local/resource-light-command.mjs +6 -0
- package/src/local/resource-make-concurrency.mjs +42 -0
- package/src/local/resource-maven-concurrency.mjs +38 -0
- package/src/local/resource-ninja-command-concurrency.mjs +20 -0
- package/src/local/resource-ninja-concurrency.mjs +28 -0
- package/src/local/resource-operations.mjs +26 -10
- package/src/local/resource-pressure.mjs +67 -0
- package/src/local/resource-probe-command.mjs +35 -0
- package/src/local/resource-process-admission.mjs +59 -0
- package/src/local/resource-process-ancestry-cache.mjs +43 -0
- package/src/local/resource-process-ancestry.mjs +32 -0
- package/src/local/resource-process-priority.mjs +15 -0
- package/src/local/resource-project-key.mjs +53 -0
- package/src/local/resource-pytest-concurrency.mjs +33 -0
- package/src/local/resource-release-control-classification.mjs +14 -0
- package/src/local/resource-release-control-executable.mjs +70 -0
- package/src/local/resource-release-control-workspace.mjs +41 -0
- package/src/local/resource-request-contract.mjs +30 -0
- package/src/local/resource-script-classification.mjs +44 -0
- package/src/local/resource-shell-analysis.mjs +58 -0
- package/src/local/resource-staging-recovery.mjs +97 -0
- package/src/local/resource-swift-concurrency.mjs +31 -0
- package/src/local/resource-transaction-lock.mjs +189 -0
- package/src/local/resource-wait.mjs +48 -0
- package/src/local/resource-waiters.mjs +139 -0
- package/src/local/resource-xcode-command.mjs +7 -0
- package/src/local/resource-xcode-concurrency.mjs +34 -0
- package/src/local/resource-xcode-non-build.mjs +19 -0
- package/src/local/runtime-activation.mjs +700 -0
- package/src/local/runtime-activity-projection.mjs +48 -0
- package/src/local/runtime-capabilities.mjs +48 -15
- package/src/local/runtime-diagnostic-state.mjs +14 -0
- package/src/local/runtime-diagnostics.mjs +40 -13
- package/src/local/runtime-info-projection.mjs +60 -0
- package/src/local/runtime-info-relay-projection.mjs +21 -0
- package/src/local/runtime-paths.mjs +2 -1
- package/src/local/runtime-process-routing.mjs +33 -0
- package/src/local/runtime-relay-connection-options.mjs +44 -0
- package/src/local/runtime-relay-control.mjs +98 -0
- package/src/local/runtime-relay.mjs +26 -50
- package/src/local/runtime-reporting.mjs +37 -21
- package/src/local/runtime-resource-service.mjs +77 -0
- package/src/local/runtime-tool-handlers.mjs +13 -10
- package/src/local/runtime.mjs +244 -208
- package/src/local/secure-file.mjs +73 -20
- package/src/local/security-audit-dispatch.mjs +35 -0
- package/src/local/security-audit-log.mjs +215 -0
- package/src/local/security-audit-state.mjs +139 -0
- package/src/local/security-audit-storage.mjs +135 -0
- package/src/local/security-audit-warning.mjs +31 -0
- package/src/local/security-audit-worker.mjs +87 -0
- package/src/local/service-convergence.mjs +42 -1
- package/src/local/service-definition.mjs +24 -0
- package/src/local/service-environment.mjs +18 -10
- package/src/local/service-owner.mjs +146 -0
- package/src/local/service-ownership.mjs +17 -0
- package/src/local/service-restart-handoff.mjs +64 -0
- package/src/local/service-restart-scheduler.mjs +49 -0
- package/src/local/service-runtime-convergence.mjs +52 -0
- package/src/local/service-runtime.mjs +105 -0
- package/src/local/service-status.mjs +60 -0
- package/src/local/service.mjs +385 -92
- package/src/local/shell.mjs +39 -19
- package/src/local/ssh-key.mjs +158 -42
- package/src/local/state-inventory.mjs +50 -24
- package/src/local/state-owner-lock-inventory.mjs +23 -0
- package/src/local/state-root-owned-namespaces.mjs +83 -0
- package/src/local/state-root-retirement.mjs +89 -0
- package/src/local/state.mjs +386 -96
- package/src/local/stdio.mjs +96 -66
- package/src/local/support-state-projection.mjs +30 -0
- package/src/local/system-network-route.mjs +76 -0
- package/src/local/systemd-removal.mjs +20 -0
- package/src/local/tool-executor.mjs +85 -20
- package/src/local/tool-result-boundary.mjs +51 -0
- package/src/local/toolchain-operation-lock.mjs +41 -0
- package/src/local/tools.mjs +7 -5
- package/src/local/trusted-executable.mjs +53 -0
- package/src/local/trusted-git-executable.mjs +34 -0
- package/src/local/trusted-github-cli.mjs +24 -0
- package/src/local/windows-service-convergence.mjs +49 -0
- package/src/local/windows-service.mjs +52 -45
- package/src/local/worker-deployment-fingerprint.mjs +102 -0
- package/src/local/worker-deployment.mjs +48 -60
- package/src/local/worker-health.mjs +12 -4
- package/src/local/worker-secret-file.mjs +37 -17
- package/src/local/workspace-file-service.mjs +156 -239
- package/src/local/workspace-file-transaction.mjs +189 -0
- package/src/local/workspace-search.mjs +55 -0
- package/src/local/wrangler-toolchain/package-lock.json +1505 -0
- package/src/local/wrangler-toolchain/package.json +23 -0
- package/src/local/wrangler-toolchain-verification.mjs +135 -0
- package/src/local/wrangler-toolchain.mjs +188 -0
- package/src/shared/access-contract.json +7 -1
- package/src/shared/activation-recovery.mjs +49 -0
- package/src/shared/admin-auth.d.mts +2 -1
- package/src/shared/admin-auth.mjs +3 -3
- package/src/shared/authority-revocation.d.mts +9 -0
- package/src/shared/authority-revocation.mjs +28 -0
- package/src/shared/daemon-auth.d.mts +11 -0
- package/src/shared/daemon-auth.mjs +17 -0
- package/src/shared/device-session-auth.d.mts +20 -0
- package/src/shared/device-session-auth.mjs +51 -0
- package/src/shared/foreground-timeout.d.mts +15 -0
- package/src/shared/foreground-timeout.mjs +70 -0
- package/src/shared/log-redaction.d.mts +9 -0
- package/src/shared/log-redaction.mjs +51 -0
- package/src/shared/mcp-protocol.d.mts +27 -0
- package/src/shared/mcp-protocol.mjs +275 -0
- package/src/shared/project-overview-projection.d.mts +4 -0
- package/src/shared/project-overview-projection.mjs +96 -0
- package/src/shared/relay-contract.json +28 -0
- package/src/shared/result-projection.d.mts +2 -1
- package/src/shared/result-projection.mjs +13 -2
- package/src/shared/server-metadata.json +15 -9
- package/src/shared/tool-argument-validation.d.mts +17 -0
- package/src/shared/tool-argument-validation.mjs +325 -0
- package/src/shared/tool-call-capacity.d.mts +41 -0
- package/src/shared/tool-call-capacity.mjs +59 -0
- package/src/shared/tool-catalog.json +626 -57
- package/src/worker/access.ts +12 -0
- package/src/worker/account-admin.ts +55 -33
- package/src/worker/authority-revocations.ts +174 -0
- package/src/worker/authority.ts +52 -6
- package/src/worker/daemon-auth.ts +49 -68
- package/src/worker/daemon-channel.ts +36 -0
- package/src/worker/daemon-http-auth.ts +63 -0
- package/src/worker/daemon-http-channel.ts +120 -0
- package/src/worker/daemon-http-controller.ts +163 -0
- package/src/worker/daemon-http-protocol.ts +62 -0
- package/src/worker/daemon-http-registry.ts +82 -0
- package/src/worker/daemon-last-observation.ts +30 -0
- package/src/worker/daemon-liveness.ts +8 -6
- package/src/worker/daemon-ready-dispatch.ts +17 -0
- package/src/worker/daemon-ready-messages.ts +73 -0
- package/src/worker/daemon-ready-waiters.ts +79 -0
- package/src/worker/daemon-recovery-budget.ts +13 -0
- package/src/worker/daemon-registry.ts +58 -0
- package/src/worker/daemon-relay-diagnostics.ts +127 -0
- package/src/worker/daemon-socket-attachment.ts +55 -0
- package/src/worker/daemon-sockets.ts +72 -62
- package/src/worker/daemon-status.ts +38 -0
- package/src/worker/device-session-verifier.ts +129 -0
- package/src/worker/dpop.ts +157 -0
- package/src/worker/durable-process-timeout.ts +29 -0
- package/src/worker/errors.ts +57 -0
- package/src/worker/http.ts +112 -26
- package/src/worker/index.ts +446 -440
- package/src/worker/mcp-access.ts +57 -0
- package/src/worker/mcp-controller.ts +188 -0
- package/src/worker/mcp-http-accept.ts +11 -0
- package/src/worker/mcp-http-contract.ts +322 -0
- package/src/worker/mcp-initialization-compat.ts +141 -0
- package/src/worker/mcp-jsonrpc.ts +11 -29
- package/src/worker/mcp-removed-protocol.ts +17 -0
- package/src/worker/mcp-response-proxy.ts +135 -0
- package/src/worker/mcp-response-stream.ts +65 -0
- package/src/worker/mcp-stale-schema-compat.ts +57 -0
- package/src/worker/mcp-stream-proxy-contract.ts +48 -0
- package/src/worker/mcp-tool-call-input.ts +23 -0
- package/src/worker/nonce-store.ts +33 -35
- package/src/worker/oauth-authorization-page.ts +21 -1
- package/src/worker/oauth-client-admin.ts +52 -0
- package/src/worker/oauth-client-contract.ts +38 -0
- package/src/worker/oauth-controller.ts +107 -47
- package/src/worker/oauth-field-contract.ts +31 -0
- package/src/worker/oauth-record-contract.ts +7 -0
- package/src/worker/oauth-refresh-authority.ts +27 -0
- package/src/worker/oauth-refresh-exchange.ts +162 -0
- package/src/worker/oauth-refresh-families.ts +130 -27
- package/src/worker/oauth-refresh-persistence.ts +116 -0
- package/src/worker/oauth-state.ts +33 -18
- package/src/worker/oauth-store-validation.ts +131 -0
- package/src/worker/oauth-token-derivation.ts +33 -0
- package/src/worker/oauth-token-issuance.ts +119 -0
- package/src/worker/oauth-tokens.ts +56 -190
- package/src/worker/observability.ts +38 -5
- package/src/worker/pending-admission.ts +15 -0
- package/src/worker/pending-call-capacity.ts +108 -0
- package/src/worker/pending-call-contract.ts +42 -5
- package/src/worker/pending-call-deadlines.ts +75 -0
- package/src/worker/pending-calls.ts +131 -125
- package/src/worker/runtime-alarm-storage.ts +37 -0
- package/src/worker/runtime-alarm.ts +136 -0
- package/src/worker/server-info-activity.ts +36 -0
- package/src/worker/server-info-tool-delivery.ts +15 -0
- package/src/worker/server-info.ts +168 -0
- package/src/worker/tool-catalog.ts +47 -1
- package/src/worker/tool-timeout.ts +75 -14
- package/src/worker/websocket-protocol.ts +26 -2
- package/src/worker/worker-edge-guard.ts +90 -0
- package/src/worker/worker-edge-log.ts +65 -0
- package/src/worker/worker-entry.ts +52 -0
- package/src/worker/worker-mcp-config.ts +23 -0
- package/src/worker/worker-metadata.ts +48 -0
- package/src/worker/worker-rate-limit-key.ts +30 -0
- package/src/worker/worker-runtime-config.ts +20 -0
- package/src/worker/worker-static-routes.ts +54 -0
- package/src/worker/worker-task-lifetime.ts +18 -0
- package/tsconfig.local.json +17 -1
- package/wrangler.jsonc +13 -0
- package/src/local/cli-approval.mjs +0 -117
- package/src/local/operation-state-lock.mjs +0 -92
- package/src/worker/mcp-session.ts +0 -72
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import AppKit
|
|
2
|
+
import CoreGraphics
|
|
3
|
+
import Foundation
|
|
4
|
+
|
|
5
|
+
private final class SmokeWindow: NSWindow {
|
|
6
|
+
let hitURL: URL
|
|
7
|
+
private var keyboardDownCount = 0
|
|
8
|
+
private var keyboardChunks: [String] = []
|
|
9
|
+
|
|
10
|
+
init(contentRect: NSRect, hitURL: URL) {
|
|
11
|
+
self.hitURL = hitURL
|
|
12
|
+
super.init(contentRect: contentRect, styleMask: [.titled, .closable], backing: .buffered, defer: false)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
override func sendEvent(_ event: NSEvent) {
|
|
16
|
+
if event.type == .keyDown {
|
|
17
|
+
keyboardDownCount += 1
|
|
18
|
+
keyboardChunks.append(event.characters ?? "")
|
|
19
|
+
let payload: [String: Any] = [
|
|
20
|
+
"type": "keyDown",
|
|
21
|
+
"handledBy": "SmokeWindow",
|
|
22
|
+
"windowNumber": event.windowNumber,
|
|
23
|
+
"keyboardDownCount": keyboardDownCount,
|
|
24
|
+
"keyboardChunks": keyboardChunks,
|
|
25
|
+
"keyboardText": keyboardChunks.joined(),
|
|
26
|
+
"keyboardKeyCode": Int(event.keyCode),
|
|
27
|
+
"keyboardModifierFlags": event.modifierFlags.rawValue,
|
|
28
|
+
]
|
|
29
|
+
if let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) {
|
|
30
|
+
try? data.write(to: hitURL, options: [.atomic])
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if event.type == .leftMouseDown {
|
|
34
|
+
let point = event.locationInWindow
|
|
35
|
+
let hitView = contentView?.hitTest(point)
|
|
36
|
+
let payload: [String: Any] = [
|
|
37
|
+
"type": "mouseDown",
|
|
38
|
+
"handledBy": "SmokeWindow",
|
|
39
|
+
"windowNumber": event.windowNumber,
|
|
40
|
+
"x": point.x,
|
|
41
|
+
"y": point.y,
|
|
42
|
+
"hitView": hitView.map { String(describing: type(of: $0)) } ?? "",
|
|
43
|
+
"timestamp": event.timestamp,
|
|
44
|
+
]
|
|
45
|
+
if let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) {
|
|
46
|
+
try? data.write(to: hitURL, options: [.atomic])
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
super.sendEvent(event)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private var targetDownCount = 0
|
|
54
|
+
private var targetClickCounts: [Int] = []
|
|
55
|
+
|
|
56
|
+
private final class TargetView: NSView {
|
|
57
|
+
let hitURL: URL
|
|
58
|
+
override var acceptsFirstResponder: Bool { true }
|
|
59
|
+
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
60
|
+
|
|
61
|
+
init(frame: NSRect, hitURL: URL) {
|
|
62
|
+
self.hitURL = hitURL
|
|
63
|
+
super.init(frame: frame)
|
|
64
|
+
wantsLayer = true
|
|
65
|
+
layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
required init?(coder: NSCoder) { nil }
|
|
69
|
+
|
|
70
|
+
override func mouseDown(with event: NSEvent) {
|
|
71
|
+
targetDownCount += 1
|
|
72
|
+
targetClickCounts.append(event.clickCount)
|
|
73
|
+
let point = event.locationInWindow
|
|
74
|
+
let payload: [String: Any] = [
|
|
75
|
+
"type": "mouseDown",
|
|
76
|
+
"handledBy": "TargetView",
|
|
77
|
+
"windowNumber": event.windowNumber,
|
|
78
|
+
"x": point.x,
|
|
79
|
+
"y": point.y,
|
|
80
|
+
"timestamp": event.timestamp,
|
|
81
|
+
"downCount": targetDownCount,
|
|
82
|
+
"clickCounts": targetClickCounts,
|
|
83
|
+
]
|
|
84
|
+
if let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) {
|
|
85
|
+
try? data.write(to: hitURL, options: [.atomic])
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private final class DragProbeView: NSView {
|
|
91
|
+
let evidenceURL: URL
|
|
92
|
+
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
93
|
+
private var downCount = 0
|
|
94
|
+
private var draggedCount = 0
|
|
95
|
+
private var upCount = 0
|
|
96
|
+
private var lastPoint = NSPoint.zero
|
|
97
|
+
|
|
98
|
+
init(frame: NSRect, evidenceURL: URL) {
|
|
99
|
+
self.evidenceURL = evidenceURL
|
|
100
|
+
super.init(frame: frame)
|
|
101
|
+
wantsLayer = true
|
|
102
|
+
layer?.backgroundColor = NSColor.controlBackgroundColor.cgColor
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
required init?(coder: NSCoder) { nil }
|
|
106
|
+
|
|
107
|
+
override func mouseDown(with event: NSEvent) {
|
|
108
|
+
downCount += 1
|
|
109
|
+
lastPoint = event.locationInWindow
|
|
110
|
+
writeEvidence(lastType: "mouseDown", windowNumber: event.windowNumber)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
override func mouseDragged(with event: NSEvent) {
|
|
114
|
+
draggedCount += 1
|
|
115
|
+
lastPoint = event.locationInWindow
|
|
116
|
+
writeEvidence(lastType: "mouseDragged", windowNumber: event.windowNumber)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
override func mouseUp(with event: NSEvent) {
|
|
120
|
+
upCount += 1
|
|
121
|
+
lastPoint = event.locationInWindow
|
|
122
|
+
writeEvidence(lastType: "mouseUp", windowNumber: event.windowNumber)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private func writeEvidence(lastType: String, windowNumber: Int) {
|
|
126
|
+
let payload: [String: Any] = [
|
|
127
|
+
"lastType": lastType,
|
|
128
|
+
"windowNumber": windowNumber,
|
|
129
|
+
"downCount": downCount,
|
|
130
|
+
"draggedCount": draggedCount,
|
|
131
|
+
"upCount": upCount,
|
|
132
|
+
"x": lastPoint.x,
|
|
133
|
+
"y": lastPoint.y,
|
|
134
|
+
]
|
|
135
|
+
if let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) {
|
|
136
|
+
try? data.write(to: evidenceURL, options: [.atomic])
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private final class FlippedDocumentView: NSView {
|
|
142
|
+
override var isFlipped: Bool { true }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private final class ScrollProbeView: NSScrollView {
|
|
146
|
+
let evidenceURL: URL
|
|
147
|
+
private var eventCount = 0
|
|
148
|
+
|
|
149
|
+
init(frame: NSRect, evidenceURL: URL) {
|
|
150
|
+
self.evidenceURL = evidenceURL
|
|
151
|
+
super.init(frame: frame)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
required init?(coder: NSCoder) { nil }
|
|
155
|
+
|
|
156
|
+
override func scrollWheel(with event: NSEvent) {
|
|
157
|
+
let before = contentView.bounds.origin.y
|
|
158
|
+
let point = event.locationInWindow
|
|
159
|
+
let deltaX = event.scrollingDeltaX
|
|
160
|
+
let deltaY = event.scrollingDeltaY
|
|
161
|
+
let precise = event.hasPreciseScrollingDeltas
|
|
162
|
+
let windowNumber = event.windowNumber
|
|
163
|
+
eventCount += 1
|
|
164
|
+
super.scrollWheel(with: event)
|
|
165
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.08) { [weak self] in
|
|
166
|
+
guard let self else { return }
|
|
167
|
+
let payload: [String: Any] = [
|
|
168
|
+
"eventCount": self.eventCount,
|
|
169
|
+
"windowNumber": windowNumber,
|
|
170
|
+
"x": point.x,
|
|
171
|
+
"y": point.y,
|
|
172
|
+
"deltaX": deltaX,
|
|
173
|
+
"deltaY": deltaY,
|
|
174
|
+
"precise": precise,
|
|
175
|
+
"beforeOffsetY": before,
|
|
176
|
+
"afterOffsetY": self.contentView.bounds.origin.y,
|
|
177
|
+
]
|
|
178
|
+
if let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) {
|
|
179
|
+
try? data.write(to: self.evidenceURL, options: [.atomic])
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private final class FixtureDelegate: NSObject, NSApplicationDelegate {
|
|
186
|
+
let statusURL: URL
|
|
187
|
+
let hitURL: URL
|
|
188
|
+
let dragURL: URL
|
|
189
|
+
let scrollURL: URL
|
|
190
|
+
let activationURL: URL?
|
|
191
|
+
var activationCount = 0
|
|
192
|
+
var window: NSWindow?
|
|
193
|
+
|
|
194
|
+
init(statusURL: URL, hitURL: URL, dragURL: URL, scrollURL: URL, activationURL: URL?) {
|
|
195
|
+
self.statusURL = statusURL
|
|
196
|
+
self.hitURL = hitURL
|
|
197
|
+
self.dragURL = dragURL
|
|
198
|
+
self.scrollURL = scrollURL
|
|
199
|
+
self.activationURL = activationURL
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
func applicationDidBecomeActive(_ notification: Notification) {
|
|
203
|
+
activationCount += 1
|
|
204
|
+
guard let activationURL else { return }
|
|
205
|
+
let payload: [String: Any] = ["activationCount": activationCount, "pid": ProcessInfo.processInfo.processIdentifier]
|
|
206
|
+
if let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) {
|
|
207
|
+
try? data.write(to: activationURL, options: [.atomic])
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
212
|
+
let frame = NSRect(x: 80, y: 80, width: 360, height: 230)
|
|
213
|
+
let window = SmokeWindow(contentRect: frame, hitURL: hitURL)
|
|
214
|
+
window.title = "MBM Computer Use Fixture"
|
|
215
|
+
window.isReleasedWhenClosed = false
|
|
216
|
+
let content = TargetView(frame: NSRect(origin: .zero, size: frame.size), hitURL: hitURL)
|
|
217
|
+
let button = NSButton(checkboxWithTitle: "AX Guard", target: nil, action: nil)
|
|
218
|
+
button.frame = NSRect(x: 20, y: 20, width: 100, height: 32)
|
|
219
|
+
button.identifier = NSUserInterfaceItemIdentifier("mbm-smoke-button")
|
|
220
|
+
button.state = .off
|
|
221
|
+
content.addSubview(button)
|
|
222
|
+
let textField = NSTextField(string: "before")
|
|
223
|
+
textField.frame = NSRect(x: 150, y: 20, width: 170, height: 24)
|
|
224
|
+
textField.identifier = NSUserInterfaceItemIdentifier("mbm-smoke-text")
|
|
225
|
+
content.addSubview(textField)
|
|
226
|
+
let dragProbe = DragProbeView(frame: NSRect(x: 20, y: 82, width: 145, height: 96), evidenceURL: dragURL)
|
|
227
|
+
content.addSubview(dragProbe)
|
|
228
|
+
let scrollProbe = ScrollProbeView(frame: NSRect(x: 215, y: 68, width: 125, height: 122), evidenceURL: scrollURL)
|
|
229
|
+
scrollProbe.hasVerticalScroller = true
|
|
230
|
+
scrollProbe.scrollerStyle = .legacy
|
|
231
|
+
scrollProbe.autohidesScrollers = false
|
|
232
|
+
scrollProbe.verticalScrollElasticity = .none
|
|
233
|
+
scrollProbe.horizontalScrollElasticity = .none
|
|
234
|
+
let document = FlippedDocumentView(frame: NSRect(x: 0, y: 0, width: 105, height: 1200))
|
|
235
|
+
for index in 0..<18 {
|
|
236
|
+
let label = NSTextField(labelWithString: "row-\(index)")
|
|
237
|
+
label.frame = NSRect(x: 8, y: CGFloat(index * 64 + 8), width: 82, height: 20)
|
|
238
|
+
document.addSubview(label)
|
|
239
|
+
}
|
|
240
|
+
scrollProbe.documentView = document
|
|
241
|
+
scrollProbe.contentView.scroll(to: .zero)
|
|
242
|
+
content.addSubview(scrollProbe)
|
|
243
|
+
window.contentView = content
|
|
244
|
+
window.orderFrontRegardless()
|
|
245
|
+
self.window = window
|
|
246
|
+
|
|
247
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
|
|
248
|
+
let windowID = CGWindowID(window.windowNumber)
|
|
249
|
+
guard let raw = CGWindowListCopyWindowInfo([.optionIncludingWindow], windowID),
|
|
250
|
+
let windows = raw as? [[String: Any]],
|
|
251
|
+
let info = windows.first,
|
|
252
|
+
let boundsObject = info[kCGWindowBounds as String] as? NSDictionary,
|
|
253
|
+
let windowBounds = CGRect(dictionaryRepresentation: boundsObject) else { return }
|
|
254
|
+
func normalizedPoint(_ point: NSPoint) -> [String: Double] {
|
|
255
|
+
[
|
|
256
|
+
"x": Double(point.x / windowBounds.width),
|
|
257
|
+
"y": Double(1 - point.y / windowBounds.height),
|
|
258
|
+
]
|
|
259
|
+
}
|
|
260
|
+
let dragSource = dragProbe.convert(NSPoint(x: 22, y: dragProbe.bounds.midY), to: nil)
|
|
261
|
+
let dragDestination = dragProbe.convert(NSPoint(x: dragProbe.bounds.width - 22, y: dragProbe.bounds.midY), to: nil)
|
|
262
|
+
let scrollAnchor = scrollProbe.convert(NSPoint(x: scrollProbe.bounds.midX, y: scrollProbe.bounds.midY), to: nil)
|
|
263
|
+
let clickProbe = content.convert(NSPoint(x: 180, y: 208), to: nil)
|
|
264
|
+
let payload: [String: Any] = [
|
|
265
|
+
"pid": ProcessInfo.processInfo.processIdentifier,
|
|
266
|
+
"windowNumber": window.windowNumber,
|
|
267
|
+
"clickProbe": normalizedPoint(clickProbe),
|
|
268
|
+
"dragSource": normalizedPoint(dragSource),
|
|
269
|
+
"dragDestination": normalizedPoint(dragDestination),
|
|
270
|
+
"scrollAnchor": normalizedPoint(scrollAnchor),
|
|
271
|
+
"scrollInitialOffsetY": scrollProbe.contentView.bounds.origin.y,
|
|
272
|
+
]
|
|
273
|
+
if let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) {
|
|
274
|
+
try? data.write(to: self.statusURL, options: [.atomic])
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
guard CommandLine.arguments.count == 5 || CommandLine.arguments.count == 6 else {
|
|
281
|
+
fputs("usage: fixture <status-json> <hit-json> <drag-json> <scroll-json> [activation-json]\n", stderr)
|
|
282
|
+
exit(64)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
let application = NSApplication.shared
|
|
286
|
+
application.setActivationPolicy(.accessory)
|
|
287
|
+
private let delegate = FixtureDelegate(
|
|
288
|
+
statusURL: URL(fileURLWithPath: CommandLine.arguments[1]),
|
|
289
|
+
hitURL: URL(fileURLWithPath: CommandLine.arguments[2]),
|
|
290
|
+
dragURL: URL(fileURLWithPath: CommandLine.arguments[3]),
|
|
291
|
+
scrollURL: URL(fileURLWithPath: CommandLine.arguments[4]),
|
|
292
|
+
activationURL: CommandLine.arguments.count == 6 ? URL(fileURLWithPath: CommandLine.arguments[5]) : nil
|
|
293
|
+
)
|
|
294
|
+
application.delegate = delegate
|
|
295
|
+
application.run()
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Security
|
|
3
|
+
import LocalAuthentication
|
|
4
|
+
|
|
5
|
+
private enum BrokerError: Error, CustomStringConvertible {
|
|
6
|
+
case usage(String)
|
|
7
|
+
case security(OSStatus, String)
|
|
8
|
+
case invalidKey(String)
|
|
9
|
+
case invalidInput(String)
|
|
10
|
+
|
|
11
|
+
var description: String {
|
|
12
|
+
switch self {
|
|
13
|
+
case .usage(let value), .invalidKey(let value), .invalidInput(let value): return value
|
|
14
|
+
case .security(let status, let operation):
|
|
15
|
+
let text = SecCopyErrorMessageString(status, nil) as String? ?? "OSStatus \(status)"
|
|
16
|
+
return "\(operation) failed: \(text)"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private struct Output: Encodable {
|
|
22
|
+
let ok: Bool
|
|
23
|
+
let provider: String
|
|
24
|
+
let keyTag: String
|
|
25
|
+
let publicJwk: PublicJwk?
|
|
26
|
+
let signature: String?
|
|
27
|
+
let secureEnclave: Bool
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private struct PublicJwk: Codable {
|
|
31
|
+
let kty: String
|
|
32
|
+
let crv: String
|
|
33
|
+
let x: String
|
|
34
|
+
let y: String
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private let providerName = "macos-secure-enclave-v1"
|
|
38
|
+
|
|
39
|
+
@main
|
|
40
|
+
private struct MachineBridgeTrustBroker {
|
|
41
|
+
static func main() {
|
|
42
|
+
do {
|
|
43
|
+
let arguments = CommandLine.arguments
|
|
44
|
+
guard arguments.count >= 4, arguments[2] == "--tag" else {
|
|
45
|
+
throw BrokerError.usage("usage: machine-bridge-trust-broker <ensure|public|sign|delete|status> --tag <tag> [--reason <text>]")
|
|
46
|
+
}
|
|
47
|
+
let action = arguments[1]
|
|
48
|
+
let tag = arguments[3]
|
|
49
|
+
guard tag.range(of: #"^[A-Za-z0-9._-]{8,180}$"#, options: .regularExpression) != nil else {
|
|
50
|
+
throw BrokerError.invalidInput("key tag is invalid")
|
|
51
|
+
}
|
|
52
|
+
let reason = option("--reason", in: arguments) ?? "Authorize Machine Bridge startup"
|
|
53
|
+
let result: Output
|
|
54
|
+
switch action {
|
|
55
|
+
case "ensure":
|
|
56
|
+
let key = try ensurePrivateKey(tag: tag)
|
|
57
|
+
result = try output(tag: tag, key: key, signature: nil)
|
|
58
|
+
case "public":
|
|
59
|
+
let key = try loadPrivateKey(tag: tag, prompt: nil, allowInteraction: false)
|
|
60
|
+
result = try output(tag: tag, key: key, signature: nil)
|
|
61
|
+
case "status":
|
|
62
|
+
let key = try? loadPrivateKey(tag: tag, prompt: nil, allowInteraction: false)
|
|
63
|
+
if let key {
|
|
64
|
+
result = try output(tag: tag, key: key, signature: nil)
|
|
65
|
+
} else {
|
|
66
|
+
result = Output(ok: false, provider: providerName, keyTag: tag, publicJwk: nil, signature: nil, secureEnclave: secureEnclaveAvailable())
|
|
67
|
+
}
|
|
68
|
+
case "sign":
|
|
69
|
+
let data = FileHandle.standardInput.readDataToEndOfFile()
|
|
70
|
+
guard !data.isEmpty, data.count <= 64 * 1024 else { throw BrokerError.invalidInput("signing input is empty or too large") }
|
|
71
|
+
guard String(data: data, encoding: .utf8) != nil else { throw BrokerError.invalidInput("signing input is not UTF-8") }
|
|
72
|
+
let key = try loadPrivateKey(tag: tag, prompt: reason, allowInteraction: true)
|
|
73
|
+
var error: Unmanaged<CFError>?
|
|
74
|
+
guard let der = SecKeyCreateSignature(key, .ecdsaSignatureMessageX962SHA256, data as CFData, &error) as Data? else {
|
|
75
|
+
throw error?.takeRetainedValue() ?? BrokerError.invalidKey("Secure Enclave signing failed")
|
|
76
|
+
}
|
|
77
|
+
let raw = try derToP1363(der)
|
|
78
|
+
result = try output(tag: tag, key: key, signature: raw.base64URLEncodedString())
|
|
79
|
+
case "delete":
|
|
80
|
+
let status = SecItemDelete(keyQuery(tag: tag, returnRef: false, allowInteraction: false, prompt: nil) as CFDictionary)
|
|
81
|
+
guard status == errSecSuccess || status == errSecItemNotFound else { throw BrokerError.security(status, "delete key") }
|
|
82
|
+
result = Output(ok: true, provider: providerName, keyTag: tag, publicJwk: nil, signature: nil, secureEnclave: secureEnclaveAvailable())
|
|
83
|
+
default:
|
|
84
|
+
throw BrokerError.usage("unknown action: \(action)")
|
|
85
|
+
}
|
|
86
|
+
try emit(result)
|
|
87
|
+
} catch {
|
|
88
|
+
let message = String(describing: error)
|
|
89
|
+
FileHandle.standardError.write(Data("machine-bridge-trust-broker: \(message)\n".utf8))
|
|
90
|
+
exit(1)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private func ensurePrivateKey(tag: String) throws -> SecKey {
|
|
96
|
+
if let existing = try? loadPrivateKey(tag: tag, prompt: nil, allowInteraction: false) { return existing }
|
|
97
|
+
guard secureEnclaveAvailable() else { throw BrokerError.invalidKey("Secure Enclave is unavailable") }
|
|
98
|
+
var accessError: Unmanaged<CFError>?
|
|
99
|
+
guard let access = SecAccessControlCreateWithFlags(
|
|
100
|
+
nil,
|
|
101
|
+
kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
|
102
|
+
[.privateKeyUsage, .userPresence],
|
|
103
|
+
&accessError
|
|
104
|
+
) else {
|
|
105
|
+
throw accessError?.takeRetainedValue() ?? BrokerError.invalidKey("could not create Secure Enclave access control")
|
|
106
|
+
}
|
|
107
|
+
let privateAttributes: [String: Any] = [
|
|
108
|
+
kSecAttrIsPermanent as String: true,
|
|
109
|
+
kSecAttrApplicationTag as String: Data(tag.utf8),
|
|
110
|
+
kSecAttrAccessControl as String: access,
|
|
111
|
+
]
|
|
112
|
+
let attributes: [String: Any] = [
|
|
113
|
+
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
|
|
114
|
+
kSecAttrKeySizeInBits as String: 256,
|
|
115
|
+
kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave,
|
|
116
|
+
kSecPrivateKeyAttrs as String: privateAttributes,
|
|
117
|
+
]
|
|
118
|
+
var keyError: Unmanaged<CFError>?
|
|
119
|
+
guard let key = SecKeyCreateRandomKey(attributes as CFDictionary, &keyError) else {
|
|
120
|
+
throw keyError?.takeRetainedValue() ?? BrokerError.invalidKey("could not create Secure Enclave key")
|
|
121
|
+
}
|
|
122
|
+
return key
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private func loadPrivateKey(tag: String, prompt: String?, allowInteraction: Bool) throws -> SecKey {
|
|
126
|
+
let query = keyQuery(tag: tag, returnRef: true, allowInteraction: allowInteraction, prompt: prompt)
|
|
127
|
+
var result: CFTypeRef?
|
|
128
|
+
let status = SecItemCopyMatching(query as CFDictionary, &result)
|
|
129
|
+
guard status == errSecSuccess, let key = result as! SecKey? else { throw BrokerError.security(status, "load Secure Enclave key") }
|
|
130
|
+
return key
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private func keyQuery(tag: String, returnRef: Bool, allowInteraction: Bool, prompt: String?) -> [String: Any] {
|
|
134
|
+
var query: [String: Any] = [
|
|
135
|
+
kSecClass as String: kSecClassKey,
|
|
136
|
+
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
|
|
137
|
+
kSecAttrApplicationTag as String: Data(tag.utf8),
|
|
138
|
+
kSecReturnRef as String: returnRef,
|
|
139
|
+
kSecMatchLimit as String: kSecMatchLimitOne,
|
|
140
|
+
]
|
|
141
|
+
let context = LAContext()
|
|
142
|
+
if allowInteraction {
|
|
143
|
+
context.localizedReason = prompt ?? "Authorize Machine Bridge startup"
|
|
144
|
+
} else {
|
|
145
|
+
context.interactionNotAllowed = true
|
|
146
|
+
}
|
|
147
|
+
query[kSecUseAuthenticationContext as String] = context
|
|
148
|
+
return query
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private func output(tag: String, key: SecKey, signature: String?) throws -> Output {
|
|
152
|
+
guard let publicKey = SecKeyCopyPublicKey(key) else { throw BrokerError.invalidKey("public key is unavailable") }
|
|
153
|
+
var error: Unmanaged<CFError>?
|
|
154
|
+
guard let external = SecKeyCopyExternalRepresentation(publicKey, &error) as Data? else {
|
|
155
|
+
throw error?.takeRetainedValue() ?? BrokerError.invalidKey("public key export failed")
|
|
156
|
+
}
|
|
157
|
+
guard external.count == 65, external.first == 0x04 else { throw BrokerError.invalidKey("public key representation is invalid") }
|
|
158
|
+
let x = external.subdata(in: 1..<33).base64URLEncodedString()
|
|
159
|
+
let y = external.subdata(in: 33..<65).base64URLEncodedString()
|
|
160
|
+
return Output(
|
|
161
|
+
ok: true,
|
|
162
|
+
provider: providerName,
|
|
163
|
+
keyTag: tag,
|
|
164
|
+
publicJwk: PublicJwk(kty: "EC", crv: "P-256", x: x, y: y),
|
|
165
|
+
signature: signature,
|
|
166
|
+
secureEnclave: true
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private func secureEnclaveAvailable() -> Bool {
|
|
171
|
+
let attributes: [String: Any] = [
|
|
172
|
+
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
|
|
173
|
+
kSecAttrKeySizeInBits as String: 256,
|
|
174
|
+
kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave,
|
|
175
|
+
kSecPrivateKeyAttrs as String: [kSecAttrIsPermanent as String: false],
|
|
176
|
+
]
|
|
177
|
+
var error: Unmanaged<CFError>?
|
|
178
|
+
return SecKeyCreateRandomKey(attributes as CFDictionary, &error) != nil
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private func derToP1363(_ data: Data) throws -> Data {
|
|
182
|
+
var index = 0
|
|
183
|
+
func readByte() throws -> UInt8 {
|
|
184
|
+
guard index < data.count else { throw BrokerError.invalidInput("ECDSA signature is truncated") }
|
|
185
|
+
defer { index += 1 }
|
|
186
|
+
return data[index]
|
|
187
|
+
}
|
|
188
|
+
func readLength() throws -> Int {
|
|
189
|
+
let first = try readByte()
|
|
190
|
+
if first < 0x80 { return Int(first) }
|
|
191
|
+
let count = Int(first & 0x7f)
|
|
192
|
+
guard count > 0, count <= 2 else { throw BrokerError.invalidInput("ECDSA signature length is invalid") }
|
|
193
|
+
var length = 0
|
|
194
|
+
for _ in 0..<count { length = (length << 8) | Int(try readByte()) }
|
|
195
|
+
return length
|
|
196
|
+
}
|
|
197
|
+
guard try readByte() == 0x30 else { throw BrokerError.invalidInput("ECDSA signature sequence is invalid") }
|
|
198
|
+
let sequenceLength = try readLength()
|
|
199
|
+
guard sequenceLength == data.count - index else { throw BrokerError.invalidInput("ECDSA signature sequence length is invalid") }
|
|
200
|
+
func readInteger() throws -> Data {
|
|
201
|
+
guard try readByte() == 0x02 else { throw BrokerError.invalidInput("ECDSA signature integer is invalid") }
|
|
202
|
+
let length = try readLength()
|
|
203
|
+
guard length > 0, index + length <= data.count else { throw BrokerError.invalidInput("ECDSA signature integer is truncated") }
|
|
204
|
+
var value = data.subdata(in: index..<(index + length))
|
|
205
|
+
index += length
|
|
206
|
+
while value.count > 32, value.first == 0 { value.removeFirst() }
|
|
207
|
+
guard value.count <= 32 else { throw BrokerError.invalidInput("ECDSA signature integer is too large") }
|
|
208
|
+
if value.count < 32 { value = Data(repeating: 0, count: 32 - value.count) + value }
|
|
209
|
+
return value
|
|
210
|
+
}
|
|
211
|
+
let r = try readInteger()
|
|
212
|
+
let s = try readInteger()
|
|
213
|
+
guard index == data.count else { throw BrokerError.invalidInput("ECDSA signature has trailing bytes") }
|
|
214
|
+
return r + s
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private func option(_ name: String, in arguments: [String]) -> String? {
|
|
218
|
+
guard let index = arguments.firstIndex(of: name), index + 1 < arguments.count else { return nil }
|
|
219
|
+
return arguments[index + 1]
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private func emit<T: Encodable>(_ value: T) throws {
|
|
223
|
+
let encoder = JSONEncoder()
|
|
224
|
+
encoder.outputFormatting = [.sortedKeys]
|
|
225
|
+
FileHandle.standardOutput.write(try encoder.encode(value))
|
|
226
|
+
FileHandle.standardOutput.write(Data("\n".utf8))
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private extension Data {
|
|
230
|
+
func base64URLEncodedString() -> String {
|
|
231
|
+
base64EncodedString().replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "")
|
|
232
|
+
}
|
|
233
|
+
}
|