@sleep2agi/commhub-server 0.9.0-preview.24 → 0.9.0-preview.240
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -9
- package/bin/commhub.ts +93 -9
- package/package.json +7 -7
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +50 -7
- package/src/admin-delete-network-http.test.ts +228 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +137 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +487 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +659 -0
- package/src/agent-acl-slice3-http.test.ts +654 -0
- package/src/agent-department-http.test.ts +215 -0
- package/src/agent-groups-perf.test.ts +223 -0
- package/src/agent-notice.ts +48 -0
- package/src/agent-team-whoami-http.test.ts +98 -0
- package/src/agent-teams-http.test.ts +330 -0
- package/src/agent-teams.ts +244 -0
- package/src/alias-baseline-2571.test.ts +162 -0
- package/src/alias-baseline.ts +113 -0
- package/src/alias-filter.test.ts +72 -0
- package/src/alias-filter.ts +42 -0
- package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
- package/src/api-host-supervisors-fallback.test.ts +6 -7
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +95 -4
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +250 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +609 -51
- package/src/avatar-validate.test.ts +52 -2
- package/src/avatar-validate.ts +35 -1
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +121 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/broadcast-fanout-2721.test.ts +149 -0
- package/src/broadcast-fanout.ts +117 -0
- package/src/chain-attempt-dedup.test.ts +447 -0
- package/src/clip.test.ts +36 -0
- package/src/clip.ts +48 -0
- package/src/clone-node.test.ts +248 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-validate.test.ts +188 -1
- package/src/config-apply-validate.ts +222 -3
- package/src/cors-origins.test.ts +73 -0
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +91 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +120 -0
- package/src/create-node-validate.test.ts +199 -7
- package/src/create-node-validate.ts +232 -15
- package/src/create-node-workdir.test.ts +289 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +434 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +236 -0
- package/src/dashboard-slash-routing-http.test.ts +109 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +377 -91
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +1264 -153
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +350 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +132 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +178 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/etag-of.test.ts +57 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +201 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +131 -0
- package/src/external-schedule-edits-http.test.ts +321 -0
- package/src/external-schedule-edits-timestamps.test.ts +64 -0
- package/src/external-schedule-edits.ts +327 -0
- package/src/file-download-authz-dev-open.test.ts +1 -1
- package/src/file-download-authz.test.ts +73 -18
- package/src/file-download-date-bucket.test.ts +436 -0
- package/src/file-network-scope-dev-open.test.ts +97 -0
- package/src/file-network-scope.test.ts +732 -0
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/group-messages-http.test.ts +482 -0
- package/src/group-messages.ts +297 -0
- package/src/health-anon-readers.ts +60 -0
- package/src/health-host-disk-2756.test.ts +71 -0
- package/src/health-redaction.test.ts +1443 -3
- package/src/host-supervisor-identity-http.test.ts +152 -0
- package/src/host-supervisor-identity.ts +92 -0
- package/src/host-supervisors-daemon-version.test.ts +137 -0
- package/src/http-gzip.test.ts +94 -0
- package/src/http-gzip.ts +112 -0
- package/src/hub-catalog-read-http.test.ts +564 -0
- package/src/hub-provider-config.test.ts +156 -0
- package/src/hub-provider-config.ts +202 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +310 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +6 -2
- package/src/license-state.test.ts +56 -0
- package/src/license-state.ts +48 -0
- package/src/list-host-supervisors.test.ts +15 -4
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +165 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +435 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +276 -0
- package/src/network-token-membership-http.test.ts +139 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs.test.ts +47 -2
- package/src/node-caller-identity-http.test.ts +1091 -0
- package/src/node-create-request-status-http.test.ts +105 -0
- package/src/node-daemon-bindings.test.ts +132 -0
- package/src/node-daemon-bindings.ts +135 -0
- package/src/node-failure-alert.test.ts +150 -0
- package/src/node-failure-alert.ts +177 -0
- package/src/node-files-transport.test.ts +344 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +335 -0
- package/src/node-health-guard.ts +121 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +250 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +263 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +728 -0
- package/src/node-permissions.ts +494 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-secret-rest-http.test.ts +190 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +212 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +102 -9
- package/src/observer-push.test.ts +76 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +356 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +457 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/poll-pacing.test.ts +74 -0
- package/src/poll-pacing.ts +123 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +373 -9
- package/src/redact-tokens.test.ts +86 -0
- package/src/redact-tokens.ts +43 -0
- package/src/rejection-counters.test.ts +72 -0
- package/src/rejection-counters.ts +173 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +295 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +309 -0
- package/src/requirement-tag-ops-http.test.ts +443 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-assignee-drift.test.ts +112 -0
- package/src/requirements-comment-http.test.ts +167 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +163 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-network-echo.test.ts +57 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2116 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +218 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +237 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +432 -0
- package/src/schedule-agent.ts +252 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +193 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +425 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +279 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +268 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +316 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +141 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +38 -38
- package/src/send-reply-alias-optional.test.ts +131 -0
- package/src/send-reply-attachments.test.ts +458 -0
- package/src/send_dedup.test.ts +54 -0
- package/src/send_dedup.ts +52 -6
- package/src/serve-error.test.ts +73 -0
- package/src/serve-error.ts +56 -0
- package/src/server.ts +5010 -429
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +10 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +142 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +264 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +129 -0
- package/src/stop-delete-node.test.ts +567 -1
- package/src/task-access-http.test.ts +402 -0
- package/src/task-access.ts +420 -0
- package/src/task-activity.ts +60 -0
- package/src/task-auth-origin-http.test.ts +93 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +490 -0
- package/src/task-diagnostic-http.test.ts +149 -0
- package/src/task-diagnostic.test.ts +115 -0
- package/src/task-diagnostic.ts +118 -0
- package/src/task-expiry-notice-http.test.ts +287 -0
- package/src/task-expiry-notice.ts +315 -0
- package/src/task-idempotency-mcp.test.ts +142 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +227 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +204 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-offline-delivery-2668.test.ts +119 -0
- package/src/task-offline-delivery.ts +81 -0
- package/src/task-orphan-http.test.ts +287 -0
- package/src/task-orphan.ts +119 -0
- package/src/task-queue-ahead-http.test.ts +309 -0
- package/src/task-queue-ahead-scale.test.ts +236 -0
- package/src/task-queue-ahead.ts +186 -0
- package/src/task-rejection-log-http.test.ts +160 -0
- package/src/task-rejection-log.ts +61 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +292 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tmux-allowlist.test.ts +48 -0
- package/src/tool-audience-http.test.ts +230 -0
- package/src/tool-audience.ts +127 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4310 -419
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +86 -7
- package/src/uploads.ts +103 -6
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +348 -0
- package/src/user-inbox-by-agent-http.test.ts +161 -0
- package/src/user-inbox-netid-notnull.test.ts +122 -0
- package/src/user-inbox-read-http.test.ts +223 -0
- package/src/user-inbox-schema.test.ts +54 -0
- package/src/version-stats.test.ts +66 -0
- package/src/version-stats.ts +109 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// REST from_session identity binding.
|
|
2
|
+
//
|
|
3
|
+
// Run: COMMHUB_DB=/tmp/rest-identity-test.db bun test src/rest-identity.test.ts
|
|
4
|
+
// (COMMHUB_DB must be set explicitly — unset defaults to the production DB.)
|
|
5
|
+
//
|
|
6
|
+
// Assertion discipline: results are compared with toEqual against the whole
|
|
7
|
+
// object, so a widened accepted-set fails instead of silently passing.
|
|
8
|
+
|
|
9
|
+
import { describe, expect, test } from "bun:test";
|
|
10
|
+
import { nodeAliasForToken, resolveRestFromSession } from "./rest-identity.js";
|
|
11
|
+
|
|
12
|
+
const NTOK = "ntok_abc123";
|
|
13
|
+
const UTOK = "utok_abc123";
|
|
14
|
+
|
|
15
|
+
describe("REST from_session identity binding", () => {
|
|
16
|
+
test("node token claiming another node's alias is refused", () => {
|
|
17
|
+
expect(resolveRestFromSession({
|
|
18
|
+
token: NTOK,
|
|
19
|
+
tokenName: "node:通信IM牛",
|
|
20
|
+
requestedFrom: "通信龙",
|
|
21
|
+
})).toEqual({
|
|
22
|
+
ok: false,
|
|
23
|
+
error: "from_session_identity_mismatch",
|
|
24
|
+
message: "network token from_session does not match token-bound node alias",
|
|
25
|
+
tokenAlias: "通信IM牛",
|
|
26
|
+
requestedFromSession: "通信龙",
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("node token omitting from is bound to its own alias, not 'api'", () => {
|
|
31
|
+
expect(resolveRestFromSession({
|
|
32
|
+
token: NTOK,
|
|
33
|
+
tokenName: "node:通信IM牛",
|
|
34
|
+
requestedFrom: undefined,
|
|
35
|
+
})).toEqual({ ok: true, fromSession: "通信IM牛" });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("node token claiming its own alias is accepted", () => {
|
|
39
|
+
expect(resolveRestFromSession({
|
|
40
|
+
token: NTOK,
|
|
41
|
+
tokenName: "node:通信IM牛",
|
|
42
|
+
requestedFrom: "通信IM牛",
|
|
43
|
+
})).toEqual({ ok: true, fromSession: "通信IM牛" });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("whitespace padding does not defeat the comparison", () => {
|
|
47
|
+
expect(resolveRestFromSession({
|
|
48
|
+
token: NTOK,
|
|
49
|
+
tokenName: "node:通信IM牛",
|
|
50
|
+
requestedFrom: " 通信IM牛 ",
|
|
51
|
+
})).toEqual({ ok: true, fromSession: "通信IM牛" });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// The Dashboard posts as its logged-in user; this path must not change.
|
|
55
|
+
test("user token explicitly naming the authenticated username is accepted", () => {
|
|
56
|
+
expect(resolveRestFromSession({
|
|
57
|
+
token: UTOK,
|
|
58
|
+
tokenName: "dashboard",
|
|
59
|
+
authenticatedUsername: "admin",
|
|
60
|
+
requestedFrom: "admin",
|
|
61
|
+
})).toEqual({ ok: true, fromSession: "admin" });
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("user token with no from defaults to the authenticated username", () => {
|
|
65
|
+
expect(resolveRestFromSession({
|
|
66
|
+
token: UTOK,
|
|
67
|
+
tokenName: null,
|
|
68
|
+
authenticatedUsername: "admin",
|
|
69
|
+
requestedFrom: undefined,
|
|
70
|
+
})).toEqual({ ok: true, fromSession: "admin" });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("legacy user auth without a username still falls back to 'api'", () => {
|
|
74
|
+
expect(resolveRestFromSession({
|
|
75
|
+
token: UTOK,
|
|
76
|
+
tokenName: null,
|
|
77
|
+
requestedFrom: undefined,
|
|
78
|
+
})).toEqual({ ok: true, fromSession: "api" });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// A non-string `from` must not be coerced into an identity.
|
|
82
|
+
test("non-string from is not treated as a claim", () => {
|
|
83
|
+
expect(resolveRestFromSession({
|
|
84
|
+
token: NTOK,
|
|
85
|
+
tokenName: "node:通信IM牛",
|
|
86
|
+
requestedFrom: { toString: () => "通信龙" },
|
|
87
|
+
})).toEqual({ ok: true, fromSession: "通信IM牛" });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// A network-bound token we cannot resolve to an alias must not be able to
|
|
91
|
+
// call itself anything. Found by independent review of this change.
|
|
92
|
+
describe("ntok_ with an unresolvable alias fails closed", () => {
|
|
93
|
+
for (const tokenName of ["node:", "node: ", "dashboard", null, undefined]) {
|
|
94
|
+
test(`claim is refused when tokenName is ${JSON.stringify(tokenName)}`, () => {
|
|
95
|
+
expect(resolveRestFromSession({
|
|
96
|
+
token: NTOK,
|
|
97
|
+
tokenName,
|
|
98
|
+
requestedFrom: "通信龙",
|
|
99
|
+
})).toEqual({
|
|
100
|
+
ok: false,
|
|
101
|
+
error: "from_session_identity_mismatch",
|
|
102
|
+
message: "network token has no resolvable node alias and may not claim a from_session",
|
|
103
|
+
tokenAlias: "",
|
|
104
|
+
requestedFromSession: "通信龙",
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
test("no claim at all still resolves to 'api'", () => {
|
|
110
|
+
expect(resolveRestFromSession({
|
|
111
|
+
token: NTOK,
|
|
112
|
+
tokenName: "node:",
|
|
113
|
+
requestedFrom: undefined,
|
|
114
|
+
})).toEqual({ ok: true, fromSession: "api" });
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("nodeAliasForToken", () => {
|
|
119
|
+
test("only ntok_ + node: prefix yields an alias", () => {
|
|
120
|
+
expect(nodeAliasForToken(NTOK, "node:X")).toBe("X");
|
|
121
|
+
// A user token whose name merely looks node-shaped must not bind.
|
|
122
|
+
expect(nodeAliasForToken(UTOK, "node:X")).toBeNull();
|
|
123
|
+
// A node token whose name is not node-scoped must not bind.
|
|
124
|
+
expect(nodeAliasForToken(NTOK, "dashboard")).toBeNull();
|
|
125
|
+
expect(nodeAliasForToken(NTOK, null)).toBeNull();
|
|
126
|
+
// `node:` with nothing after it is not an identity.
|
|
127
|
+
expect(nodeAliasForToken(NTOK, "node: ")).toBeNull();
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// REST from_session identity binding.
|
|
2
|
+
//
|
|
3
|
+
// Both transports resolve the same `api_tokens` row through resolveToken, and
|
|
4
|
+
// auth.ts already states the invariant:
|
|
5
|
+
//
|
|
6
|
+
// "Network-bound node tokens are an identity boundary: they must not spoof
|
|
7
|
+
// another node via from_session."
|
|
8
|
+
//
|
|
9
|
+
// tools.ts enforced it for MCP (defaultFrom + fromIdentityMismatchReply).
|
|
10
|
+
// POST /api/task did not — it took `body.from` verbatim, so any node holding
|
|
11
|
+
// its own ntok_ could claim another node's alias. That value is stored on the
|
|
12
|
+
// task row and, once sender labels reach the copresence TUIs, rendered to a
|
|
13
|
+
// human as an attribution.
|
|
14
|
+
//
|
|
15
|
+
// Production network tokens pass tokenId and resolve through resolveNodeCaller.
|
|
16
|
+
// A token whose name is not `node:` (not_a_node_token, the registration token
|
|
17
|
+
// before report_status) falls back to nodeAliasForToken, same as main.
|
|
18
|
+
// tokenId omitted keeps the name-only shape used by the unit cases below.
|
|
19
|
+
|
|
20
|
+
import { resolveNodeCaller } from "./create-node.js";
|
|
21
|
+
|
|
22
|
+
export interface RestIdentityInput {
|
|
23
|
+
/** Raw bearer token, only its prefix matters (`ntok_` = network-bound). */
|
|
24
|
+
token: string;
|
|
25
|
+
/** `api_tokens.name`; for node tokens it is `node:<alias>`. */
|
|
26
|
+
tokenName: string | null | undefined;
|
|
27
|
+
/** Set on the production path. Network tokens then ignore tokenName as authority. */
|
|
28
|
+
tokenId?: string | null;
|
|
29
|
+
/** Username resolved from the authenticated token. */
|
|
30
|
+
authenticatedUsername?: string | null;
|
|
31
|
+
/** Caller-supplied `body.from`. */
|
|
32
|
+
requestedFrom: unknown;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type RestIdentityResult =
|
|
36
|
+
| { ok: true; fromSession: string; nodeId?: string | null }
|
|
37
|
+
| {
|
|
38
|
+
ok: false;
|
|
39
|
+
error: "from_session_identity_mismatch";
|
|
40
|
+
message: string;
|
|
41
|
+
tokenAlias: string;
|
|
42
|
+
requestedFromSession: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Alias a token is bound to, or null when it is not node-scoped. */
|
|
46
|
+
export function nodeAliasForToken(token: string, tokenName: string | null | undefined): string | null {
|
|
47
|
+
if (!token.startsWith("ntok_")) return null;
|
|
48
|
+
if (typeof tokenName !== "string" || !tokenName.startsWith("node:")) return null;
|
|
49
|
+
const alias = tokenName.slice("node:".length).trim();
|
|
50
|
+
return alias || null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Human dispatches use the authenticated username; node credentials represent nodes. */
|
|
54
|
+
export function resolveUserTaskFromSession(username: string | null | undefined, requestedFrom: unknown): RestIdentityResult {
|
|
55
|
+
const fromSession = username?.trim() || "api";
|
|
56
|
+
const requested = typeof requestedFrom === "string" ? requestedFrom.trim() : "";
|
|
57
|
+
if (requested && requested !== fromSession) {
|
|
58
|
+
return {
|
|
59
|
+
ok: false,
|
|
60
|
+
error: "from_session_identity_mismatch",
|
|
61
|
+
message: "user token task sender must match the authenticated username",
|
|
62
|
+
tokenAlias: fromSession,
|
|
63
|
+
requestedFromSession: requested,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return { ok: true, fromSession };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function resolveRestFromSession(input: RestIdentityInput): RestIdentityResult {
|
|
70
|
+
const requested = typeof input.requestedFrom === "string" ? input.requestedFrom.trim() : "";
|
|
71
|
+
let alias: string | null;
|
|
72
|
+
let nodeId: string | null = null;
|
|
73
|
+
if (input.token.startsWith("ntok_") && input.tokenId) {
|
|
74
|
+
const resolved = resolveNodeCaller(input.tokenId);
|
|
75
|
+
if (resolved.ok) {
|
|
76
|
+
alias = resolved.alias;
|
|
77
|
+
nodeId = resolved.kind === "node" ? resolved.nodeId : null;
|
|
78
|
+
} else if (resolved.reason === "not_a_node_token") { // board679-plain-ntok-rest
|
|
79
|
+
// Name is not `node:` yet. Main signs that as api, and refuses a claim.
|
|
80
|
+
// not_owner / bound_node_missing / ambiguous stay the hard reject.
|
|
81
|
+
alias = nodeAliasForToken(input.token, input.tokenName);
|
|
82
|
+
} else {
|
|
83
|
+
return {
|
|
84
|
+
ok: false,
|
|
85
|
+
error: "from_session_identity_mismatch",
|
|
86
|
+
message: "network token from_session does not match token-bound node alias",
|
|
87
|
+
tokenAlias: "",
|
|
88
|
+
requestedFromSession: requested,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
alias = nodeAliasForToken(input.token, input.tokenName);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// A network-bound token whose name we cannot resolve to an alias (`node:`,
|
|
96
|
+
// `node: `, or a name that was never node-scoped) is the degenerate case.
|
|
97
|
+
// We know it IS a node credential; we just cannot tell WHICH node. Letting it
|
|
98
|
+
// claim anything is the worst available option, so refuse the claim outright
|
|
99
|
+
// rather than falling through to the unbound path.
|
|
100
|
+
//
|
|
101
|
+
// Production always writes `node:${nodeName}`, so this costs nothing real —
|
|
102
|
+
// but "we couldn't identify you, so you may call yourself anything" is
|
|
103
|
+
// exactly the fail-open shape this change exists to remove.
|
|
104
|
+
if (!alias && input.token.startsWith("ntok_")) {
|
|
105
|
+
if (requested) {
|
|
106
|
+
return {
|
|
107
|
+
ok: false,
|
|
108
|
+
error: "from_session_identity_mismatch",
|
|
109
|
+
message: "network token has no resolvable node alias and may not claim a from_session",
|
|
110
|
+
tokenAlias: "",
|
|
111
|
+
requestedFromSession: requested,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return { ok: true, fromSession: "api" };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Authenticated human callers are always represented by their username.
|
|
118
|
+
// Keep the unscoped development / legacy transport path separate.
|
|
119
|
+
if (!alias) {
|
|
120
|
+
const authenticatedUsername = typeof input.authenticatedUsername === "string"
|
|
121
|
+
? input.authenticatedUsername.trim()
|
|
122
|
+
: "";
|
|
123
|
+
if (authenticatedUsername) return resolveUserTaskFromSession(authenticatedUsername, input.requestedFrom);
|
|
124
|
+
return { ok: true, fromSession: requested || "api" };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Node token claiming somebody else — refuse rather than silently rewrite,
|
|
128
|
+
// so a misconfigured caller is visible instead of quietly relabelled.
|
|
129
|
+
if (requested && requested !== alias) {
|
|
130
|
+
return {
|
|
131
|
+
ok: false,
|
|
132
|
+
error: "from_session_identity_mismatch",
|
|
133
|
+
message: "network token from_session does not match token-bound node alias",
|
|
134
|
+
tokenAlias: alias,
|
|
135
|
+
requestedFromSession: requested,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Node token, no claim or a matching one: bind to the token's own alias.
|
|
140
|
+
return { ok: true, fromSession: alias, ...(input.tokenId ? { nodeId } : {}) };
|
|
141
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable REST row projections.
|
|
3
|
+
*
|
|
4
|
+
* These lists deliberately mirror the fields already exposed by the public
|
|
5
|
+
* endpoints before #311. Keeping them in one place makes the wire contract
|
|
6
|
+
* independent from future ALTER TABLE migrations: adding a storage-only
|
|
7
|
+
* column must never silently add a REST response key.
|
|
8
|
+
*/
|
|
9
|
+
export const NETWORK_REST_COLUMNS = [
|
|
10
|
+
"network_id", "network_name", "owner_id", "description", "settings",
|
|
11
|
+
"created_at", "updated_at", "visibility", "max_members",
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
export const SESSION_REST_COLUMNS = [
|
|
15
|
+
"resume_id", "alias", "tmux_name", "server", "ip", "hostname", "agent",
|
|
16
|
+
"project_dir", "version", "status", "task", "output", "progress", "score",
|
|
17
|
+
"cpu_load_1min", "cpu_cores", "mem_total_gb", "mem_used_gb", "mem_avail_gb",
|
|
18
|
+
"disk_total_gb", "disk_used_gb", "disk_avail_gb", "process_rss_bytes",
|
|
19
|
+
"process_rss_mb", "process_cpu_pct", "process_uptime_seconds",
|
|
20
|
+
"process_in_flight_count", "network_id", "registered_at", "updated_at",
|
|
21
|
+
"node_id", "session_id", "config_path", "channels", "last_seen_at", "model",
|
|
22
|
+
"external_schedules", "rules_file_capable", "skills_capable", "files_capable",
|
|
23
|
+
"logs_capable",
|
|
24
|
+
// #2211 — the node's last team-rules outcome (see team-rules-status.ts).
|
|
25
|
+
"team_rules_revision", "team_rules_status", "team_rules_at", "team_rules_reason",
|
|
26
|
+
] as const;
|
|
27
|
+
|
|
28
|
+
export const AUDIT_LOG_REST_COLUMNS = [
|
|
29
|
+
"id", "user_id", "username", "action", "target_type", "target_id", "detail",
|
|
30
|
+
"ip", "network_id", "created_at",
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
export const TASK_EVENT_REST_COLUMNS = [
|
|
34
|
+
"id", "task_id", "from_status", "to_status", "event_type", "actor", "detail",
|
|
35
|
+
"created_at", "network_id",
|
|
36
|
+
] as const;
|
|
37
|
+
|
|
38
|
+
export const TASK_REST_COLUMNS = [
|
|
39
|
+
"task_id", "from_node_id", "from_name", "to_node_id", "to_name", "priority",
|
|
40
|
+
"status", "content", "result", "in_reply_to", "requires_response", "scope",
|
|
41
|
+
"created_at", "delivered_at", "started_at", "runtime_submitted_at", "consumed_at", "completed_at", "expires_at",
|
|
42
|
+
"network_id", "parent_task_id", "meta_json", "thread_id", "turn_id",
|
|
43
|
+
] as const;
|
|
44
|
+
|
|
45
|
+
export const COMPLETION_REST_COLUMNS = [
|
|
46
|
+
"id", "session_name", "task", "result", "artifacts", "score",
|
|
47
|
+
"duration_minutes", "network_id", "completed_at",
|
|
48
|
+
] as const;
|
|
49
|
+
|
|
50
|
+
// Storage projection for scheduled-task rows. `created_by` and
|
|
51
|
+
// `schedule_json` are intentionally selected for server-side auth/decoding,
|
|
52
|
+
// then removed by decodeRow before the REST response is serialized.
|
|
53
|
+
export const SCHEDULED_TASK_STORAGE_COLUMNS = [
|
|
54
|
+
"schedule_id", "network_id", "created_by", "created_by_node_id", "name", "target_node_id",
|
|
55
|
+
"target_alias", "task_content", "priority", "schedule_type", "schedule_json",
|
|
56
|
+
"timezone", "overlap_policy", "misfire_policy", "status", "next_run_at",
|
|
57
|
+
"last_run_at", "revision", "created_at", "updated_at",
|
|
58
|
+
"no_change_backoff", "backoff_streak", "backoff_effective_seconds", "backoff_last_run_id",
|
|
59
|
+
"requirement_wake_json", "requirement_wake_cursor", "requirement_wake_last_event_id",
|
|
60
|
+
] as const;
|
|
61
|
+
|
|
62
|
+
export function sqlColumns(columns: readonly string[], qualifier?: string): string {
|
|
63
|
+
return columns.map((column) => qualifier ? `${qualifier}.${column}` : column).join(", ");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const NETWORK_REST_SELECT = sqlColumns(NETWORK_REST_COLUMNS);
|
|
67
|
+
export const SESSION_REST_SELECT = sqlColumns(SESSION_REST_COLUMNS);
|
|
68
|
+
export const AUDIT_LOG_REST_SELECT = sqlColumns(AUDIT_LOG_REST_COLUMNS);
|
|
69
|
+
export const TASK_EVENT_REST_SELECT = sqlColumns(TASK_EVENT_REST_COLUMNS);
|
|
70
|
+
export const TASK_REST_SELECT = sqlColumns(TASK_REST_COLUMNS);
|
|
71
|
+
export const COMPLETION_REST_SELECT = sqlColumns(COMPLETION_REST_COLUMNS);
|
|
72
|
+
export const SCHEDULED_TASK_STORAGE_SELECT = sqlColumns(SCHEDULED_TASK_STORAGE_COLUMNS);
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// #2720 复盘 —— 一次**我判断错了**的根因,钉成合同测试。
|
|
2
|
+
//
|
|
3
|
+
// 背景:我看 `/api/requirements/{tags,stats,people,people/directory}` 这四个只读端点
|
|
4
|
+
// 用的是 `resolveRestWriteNetworkId`(写解析器),于是一口咬定「非 admin 会员只要
|
|
5
|
+
// 请求里不带 network_id 就会 400 network_id_required」,还为此写了一个新的
|
|
6
|
+
// `resolveRestReadNetworkId` 想放宽它。
|
|
7
|
+
//
|
|
8
|
+
// 通信设计师 要求用**真实链路**复证,不要手搓 scope。复证结果:
|
|
9
|
+
//
|
|
10
|
+
// 请求不带 network_id,走 resolveRestNetworkScope(null, auth, false):
|
|
11
|
+
// 非 admin 单网络 → scope.networkIds = [NET_A] ⇒ 写解析器第一行的
|
|
12
|
+
// singleNetworkId(scope) 就已经返回 NET_A —— **根本不会 400**
|
|
13
|
+
// 非 admin 双网络 → scope.networkIds = [NET_A,NET_B] ⇒ null(歧义,确实 400)
|
|
14
|
+
// admin 单网络 → scope.networkIds = null ⇒ 回退成员关系,返回 NET_A
|
|
15
|
+
//
|
|
16
|
+
// ⇒ 那个「读解析器」在所有真实输入下与写解析器**结果完全相同**,是纯 no-op。
|
|
17
|
+
// 我当初的测试把非 admin 的 auth 配上 `{networkIds: null}`(真实链路不会产出的组合),
|
|
18
|
+
// 等于自己造故障再修故障 —— 所以那个 PR 已关闭,这里只留**真实链路**的断言。
|
|
19
|
+
//
|
|
20
|
+
// 这条测试的价值不是「证明代码对」,是**钉住真实的解析顺序**,让下一次想「放宽读权限」
|
|
21
|
+
// 的人先看到:单网络用户本来就没被拦,真正 400 的只有多网络且没显式指定的人。
|
|
22
|
+
//
|
|
23
|
+
// Run: COMMHUB_DB=/tmp/2720-real-chain.db bun test src/rest-read-network-real-chain.test.ts
|
|
24
|
+
|
|
25
|
+
import { afterAll, beforeEach, describe, expect, test } from "bun:test";
|
|
26
|
+
import { db } from "./db.js";
|
|
27
|
+
import { resolveRestNetworkScope, resolveRestWriteNetworkId } from "./network-scope.js";
|
|
28
|
+
|
|
29
|
+
const NET_A = "net_chain_a";
|
|
30
|
+
const NET_B = "net_chain_b";
|
|
31
|
+
const U_SINGLE = "u_chain_single"; // 只属于 NET_A
|
|
32
|
+
const U_MULTI = "u_chain_multi"; // 属于 NET_A + NET_B
|
|
33
|
+
|
|
34
|
+
function cleanup() {
|
|
35
|
+
for (const t of ["network_members", "networks"]) {
|
|
36
|
+
try { db.run(`DELETE FROM ${t} WHERE network_id IN (?1, ?2)`, [NET_A, NET_B]); } catch {}
|
|
37
|
+
}
|
|
38
|
+
for (const u of [U_SINGLE, U_MULTI]) {
|
|
39
|
+
try { db.run("DELETE FROM users WHERE user_id = ?1", [u]); } catch {}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function seed() {
|
|
44
|
+
for (const u of [U_SINGLE, U_MULTI]) {
|
|
45
|
+
db.run(
|
|
46
|
+
"INSERT INTO users (user_id, username, password_hash, role, created_at) VALUES (?1,?2,'x','user',datetime('now'))",
|
|
47
|
+
[u, u],
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
db.run("INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1,?1,?2,datetime('now'))", [NET_A, U_SINGLE]);
|
|
51
|
+
db.run("INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1,?1,?2,datetime('now'))", [NET_B, U_MULTI]);
|
|
52
|
+
db.run("INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1,?2,'owner',datetime('now'))", [U_SINGLE, NET_A]);
|
|
53
|
+
db.run("INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1,?2,'member',datetime('now'))", [U_MULTI, NET_A]);
|
|
54
|
+
db.run("INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1,?2,'owner',datetime('now'))", [U_MULTI, NET_B]);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
beforeEach(() => { cleanup(); seed(); });
|
|
58
|
+
afterAll(() => { cleanup(); });
|
|
59
|
+
|
|
60
|
+
/** 走真实链路:先按「请求里没有 network_id」解析作用域,再交给写解析器。 */
|
|
61
|
+
function resolveWithoutRequestedNetwork(userId: string, isAdmin: boolean) {
|
|
62
|
+
const authCtx = { userId, networkId: null as string | null };
|
|
63
|
+
const scope = resolveRestNetworkScope(null, authCtx, false ? null : isAdmin);
|
|
64
|
+
return { scope, networkId: resolveRestWriteNetworkId(scope, authCtx, isAdmin) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describe("#2720 真实链路:只读端点不会因为「没带 network_id」就把单网络会员打回 400", () => {
|
|
68
|
+
test("🔴 非 admin + 恰好 1 个成员关系 → 解析出 NET_A(不是 null)", () => {
|
|
69
|
+
const { scope, networkId } = resolveWithoutRequestedNetwork(U_SINGLE, false);
|
|
70
|
+
expect(scope.networkIds).toEqual([NET_A]);
|
|
71
|
+
expect(networkId).toBe(NET_A);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("非 admin + 2 个成员关系 → null(歧义时确实要显式指定,这条不能放宽)", () => {
|
|
75
|
+
const { scope, networkId } = resolveWithoutRequestedNetwork(U_MULTI, false);
|
|
76
|
+
expect(scope.networkIds?.length).toBe(2);
|
|
77
|
+
expect(networkId).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("admin + 1 个成员关系 → NET_A(回退分支)", () => {
|
|
81
|
+
const { networkId } = resolveWithoutRequestedNetwork(U_SINGLE, true);
|
|
82
|
+
expect(networkId).toBe(NET_A);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("请求里显式指定了自己所属的网络 → 用它(与角色无关)", () => {
|
|
86
|
+
const authCtx = { userId: U_MULTI, networkId: null as string | null };
|
|
87
|
+
const scope = resolveRestNetworkScope(NET_B, authCtx, false);
|
|
88
|
+
expect(resolveRestWriteNetworkId(scope, authCtx, false)).toBe(NET_B);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("请求里指定了一个自己不属于的网络 → scope 明确 denied,解析结果 null", () => {
|
|
92
|
+
const authCtx = { userId: U_SINGLE, networkId: null as string | null };
|
|
93
|
+
const scope = resolveRestNetworkScope(NET_B, authCtx, false);
|
|
94
|
+
expect(scope.denied).toBeTruthy();
|
|
95
|
+
expect(resolveRestWriteNetworkId(scope, authCtx, false)).toBeNull();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// #819 — `resolveRestWriteNetworkId` 是 network-scope.ts 里唯一没有任何测试点名的
|
|
2
|
+
// 导出函数。同文件的五个兄弟各有 1-2 个测试文件点名它们:
|
|
3
|
+
//
|
|
4
|
+
// resolveRestNetworkScope 1 canRestWriteNetwork 1
|
|
5
|
+
// singleNetworkId 2 addNetworkScope 1
|
|
6
|
+
// getUserNetworkIds 1 resolveRestWriteNetworkId 🔴 0
|
|
7
|
+
//
|
|
8
|
+
// 而它决定的是**一次 REST 写入落到哪个网络**。它的 docstring 说明了一条不显然的
|
|
9
|
+
// 规则:管理员的**读**作用域按设计是全局的(networkIds=null),所以它本身表达不了
|
|
10
|
+
// 「这个管理员恰好只属于一个网络」;而**写**不能继承这个歧义 —— 只有在确实只有
|
|
11
|
+
// 一个成员关系时才用它,0 个或 ≥2 个都必须显式指定网络。
|
|
12
|
+
//
|
|
13
|
+
// 🔴 这条规则的两半都要钉:
|
|
14
|
+
// - **放行那半**(admin + 恰好 1 个成员关系 → 用它)如果坏了,会退化成「管理员
|
|
15
|
+
// 写任何东西都要显式带 network_id」——很吵,但**安全**,所以没人会急着修;
|
|
16
|
+
// - **收紧那半**(admin + 0 或 ≥2 → null)如果坏了,一次写入会**落到一个没被
|
|
17
|
+
// 指定的网络里**,而调用方看到的是成功。
|
|
18
|
+
//
|
|
19
|
+
// 只写反向断言是不够的:一个「永远返回 null」的实现能通过所有「收紧」用例。
|
|
20
|
+
// 所以每一条收紧断言都配了正控。
|
|
21
|
+
//
|
|
22
|
+
// Run: COMMHUB_DB=/tmp/819-rest-write-scope.db bun test src/rest-write-network-resolution.test.ts
|
|
23
|
+
|
|
24
|
+
import { afterAll, beforeEach, describe, expect, test } from "bun:test";
|
|
25
|
+
import { db } from "./db.js";
|
|
26
|
+
import { resolveRestNetworkScope, resolveRestWriteNetworkId, type RestNetworkScope } from "./network-scope.js";
|
|
27
|
+
|
|
28
|
+
const NET_A = "net_819_a";
|
|
29
|
+
const NET_B = "net_819_b";
|
|
30
|
+
const U_SINGLE = "u819_single"; // 只属于 NET_A
|
|
31
|
+
const U_MULTI = "u819_multi"; // 属于 NET_A + NET_B
|
|
32
|
+
const U_NONE = "u819_none"; // 一个都不属于
|
|
33
|
+
const ALL_USERS = [U_SINGLE, U_MULTI, U_NONE];
|
|
34
|
+
|
|
35
|
+
function cleanup() {
|
|
36
|
+
try { db.run("DELETE FROM network_members WHERE network_id IN (?1, ?2)", [NET_A, NET_B]); } catch {}
|
|
37
|
+
try { db.run("DELETE FROM networks WHERE network_id IN (?1, ?2)", [NET_A, NET_B]); } catch {}
|
|
38
|
+
for (const u of ALL_USERS) {
|
|
39
|
+
try { db.run("DELETE FROM users WHERE user_id = ?1", [u]); } catch {}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function seed() {
|
|
44
|
+
for (const u of ALL_USERS) {
|
|
45
|
+
db.run(
|
|
46
|
+
`INSERT INTO users (user_id, username, password_hash, role, created_at)
|
|
47
|
+
VALUES (?1, ?2, 'x', 'user', datetime('now'))`,
|
|
48
|
+
[u, u],
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
db.run(`INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?1, ?2, datetime('now'))`, [NET_A, U_SINGLE]);
|
|
52
|
+
db.run(`INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?1, ?2, datetime('now'))`, [NET_B, U_MULTI]);
|
|
53
|
+
const member = (u: string, net: string, role: string) =>
|
|
54
|
+
db.run(`INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1, ?2, ?3, datetime('now'))`, [u, net, role]);
|
|
55
|
+
member(U_SINGLE, NET_A, "owner");
|
|
56
|
+
member(U_MULTI, NET_A, "member");
|
|
57
|
+
member(U_MULTI, NET_B, "owner");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
beforeEach(() => { cleanup(); seed(); });
|
|
61
|
+
afterAll(() => { cleanup(); });
|
|
62
|
+
|
|
63
|
+
/** 管理员的读作用域:全局(networkIds=null),这正是歧义的来源。 */
|
|
64
|
+
const ADMIN_SCOPE: RestNetworkScope = { networkIds: null };
|
|
65
|
+
const scopeOf = (...ids: string[]): RestNetworkScope => ({ networkIds: ids });
|
|
66
|
+
const ctx = (userId: string) => ({ userId, networkId: null });
|
|
67
|
+
|
|
68
|
+
describe("#819 resolveRestWriteNetworkId — 作用域里就能确定时,直接用它", () => {
|
|
69
|
+
test("作用域恰好一个网络 → 用那一个(与是不是 admin 无关)", () => {
|
|
70
|
+
expect(resolveRestWriteNetworkId(scopeOf(NET_A), ctx(U_MULTI), false)).toBe(NET_A);
|
|
71
|
+
expect(resolveRestWriteNetworkId(scopeOf(NET_A), ctx(U_MULTI), true)).toBe(NET_A);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("作用域两个网络 → 歧义,null(即使调用者是 admin)", () => {
|
|
75
|
+
expect(resolveRestWriteNetworkId(scopeOf(NET_A, NET_B), ctx(U_MULTI), true)).toBeNull();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 🔴 这一条我一开始断错了,留下来当记录:我以为「作用域为空数组 → null」,
|
|
79
|
+
// 实测是 NET_A。原因是 `[]` 过不了 singleNetworkId,于是落到 admin 的成员关系
|
|
80
|
+
// 回退分支,而 U_SINGLE 恰好只有一个成员关系。
|
|
81
|
+
//
|
|
82
|
+
// 而 `networkIds: []` 在 resolveRestNetworkScope 里有确切含义:`:46` 那一行,
|
|
83
|
+
// **非 admin 请求了一个自己没有角色的网络** —— 它带着 `denied: "access denied
|
|
84
|
+
// to requested network"`。也就是说 `[]` 不是「没指定」,是「明确被拒」。
|
|
85
|
+
//
|
|
86
|
+
// 现在**没有**问题,因为那一行只对非 admin 产生(admin 在 `:42` 就提前返回
|
|
87
|
+
// networkIds:null 了),而非 admin 走到这里必然 null —— 见下一条。
|
|
88
|
+
//
|
|
89
|
+
// 但它是一个潜伏的形状:如果将来有任何路径让 admin 拿到 networkIds: [],
|
|
90
|
+
// 这次写入会**忽略那条明确的拒绝**,回退到他的单一成员关系。所以两条都钉住:
|
|
91
|
+
// 当前行为,以及那条让它安全的前提。
|
|
92
|
+
test("作用域为空数组 + admin + 恰好一个成员关系 → 回退到成员关系(当前行为,记录)", () => {
|
|
93
|
+
expect(resolveRestWriteNetworkId(scopeOf(), ctx(U_SINGLE), true)).toBe(NET_A);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("🔴 让上一条安全的前提:空数组作用域只由非 admin 产生,而非 admin → null", () => {
|
|
97
|
+
expect(resolveRestWriteNetworkId(scopeOf(), ctx(U_SINGLE), false)).toBeNull();
|
|
98
|
+
// 前提本身也断一次:resolveRestNetworkScope 对 admin 从不产出空数组。
|
|
99
|
+
const adminScope = resolveRestNetworkScope(NET_B, ctx(U_NONE), true);
|
|
100
|
+
expect(adminScope.networkIds).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("#819 admin 的全局读作用域不能替写入决定网络", () => {
|
|
105
|
+
test("🔴 admin + 恰好 1 个成员关系 → 用那一个(放行那半)", () => {
|
|
106
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, ctx(U_SINGLE), true)).toBe(NET_A);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("🔴 admin + 2 个成员关系 → null,必须显式指定(收紧那半)", () => {
|
|
110
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, ctx(U_MULTI), true)).toBeNull();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("🔴 admin + 0 个成员关系 → null(不能因为读是全局的就随便挑一个)", () => {
|
|
114
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, ctx(U_NONE), true)).toBeNull();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("非 admin + 全局作用域 → null(这种组合本身就不该发生,fail closed)", () => {
|
|
118
|
+
// 正控在上面那条「admin + 单成员 → NET_A」:如果实现改成永远 null,那条会红。
|
|
119
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, ctx(U_SINGLE), false)).toBeNull();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("没有 authCtx + 全局作用域 → null", () => {
|
|
123
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, null, true)).toBeNull();
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("#819 作用域优先于成员关系回退", () => {
|
|
128
|
+
test("作用域说 NET_B,而该用户唯一的成员关系是 NET_A → 结果是 NET_B", () => {
|
|
129
|
+
// 归属由作用域决定,不由「他碰巧属于哪个网络」决定。
|
|
130
|
+
// 如果实现把两者的优先级搞反,这条会返回 NET_A。
|
|
131
|
+
expect(resolveRestWriteNetworkId(scopeOf(NET_B), ctx(U_SINGLE), true)).toBe(NET_B);
|
|
132
|
+
});
|
|
133
|
+
});
|