@terreno/api 57.2.0 → 57.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ai/skills/mongoose-schema-safety/SKILL.md +23 -10
- package/README.md +23 -7
- package/dist/actions.js +19 -7
- package/dist/adminTypes.d.ts +7 -0
- package/dist/adminTypes.js +6 -1
- package/dist/api.d.ts +14 -0
- package/dist/api.js +111 -60
- package/dist/audit/auditApp.d.ts +30 -0
- package/dist/audit/auditApp.js +155 -0
- package/dist/audit/auditEventModel.d.ts +29 -0
- package/dist/audit/auditEventModel.js +102 -0
- package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
- package/dist/audit/cloudTasksEnqueue.js +162 -0
- package/dist/audit/diff.d.ts +10 -0
- package/dist/audit/diff.js +230 -0
- package/dist/audit/processQueue.d.ts +6 -0
- package/dist/audit/processQueue.js +101 -0
- package/dist/audit/rbacSink.d.ts +2 -0
- package/dist/audit/rbacSink.js +82 -0
- package/dist/audit/record.d.ts +56 -0
- package/dist/audit/record.js +373 -0
- package/dist/auth.d.ts +23 -10
- package/dist/auth.js +97 -32
- package/dist/authRecovery.d.ts +5 -0
- package/dist/authRecovery.js +350 -0
- package/dist/authTokens.d.ts +8 -0
- package/dist/authTokens.js +162 -0
- package/dist/betterAuth.d.ts +39 -0
- package/dist/betterAuthApp.d.ts +4 -0
- package/dist/betterAuthApp.js +26 -8
- package/dist/betterAuthSetup.d.ts +44 -2
- package/dist/betterAuthSetup.js +288 -18
- package/dist/collectionRegistry.d.ts +40 -0
- package/dist/collectionRegistry.js +97 -0
- package/dist/configurationApp.d.ts +11 -2
- package/dist/configurationApp.js +2 -4
- package/dist/docLoader.js +35 -6
- package/dist/expressServer.d.ts +26 -3
- package/dist/expressServer.js +79 -11
- package/dist/index.d.ts +42 -1
- package/dist/index.js +84 -2
- package/dist/mcp/auth.d.ts +1 -0
- package/dist/mcp/auth.js +47 -25
- package/dist/mcp/handlers.d.ts +1 -1
- package/dist/mcp/handlers.js +67 -233
- package/dist/mcp/query.d.ts +2 -15
- package/dist/mcp/query.js +8 -8
- package/dist/mcp/registry.d.ts +1 -5
- package/dist/mcp/registry.js +31 -20
- package/dist/mcp/schemaGenerator.d.ts +2 -7
- package/dist/mcp/schemaGenerator.js +41 -116
- package/dist/mcp/server.d.ts +1 -0
- package/dist/mcp/server.js +1 -0
- package/dist/mcp/serviceTokens.d.ts +16 -0
- package/dist/mcp/serviceTokens.js +345 -0
- package/dist/mcp/types.d.ts +2 -2
- package/dist/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +49 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +49 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +45 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +45 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.d.ts +2 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.js +5 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.d.ts +3 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +45 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +49 -0
- package/dist/migrations/generate.d.ts +17 -0
- package/dist/migrations/generate.js +277 -0
- package/dist/migrations/load.d.ts +20 -0
- package/dist/migrations/load.js +236 -0
- package/dist/migrations/lock.d.ts +13 -0
- package/dist/migrations/lock.js +145 -0
- package/dist/migrations/runner.d.ts +42 -0
- package/dist/migrations/runner.js +503 -0
- package/dist/migrations/schemaCatalog.d.ts +44 -0
- package/dist/migrations/schemaCatalog.js +326 -0
- package/dist/migrations/types.d.ts +87 -0
- package/dist/migrations/types.js +7 -0
- package/dist/models/mcpServiceToken.d.ts +3 -0
- package/dist/models/mcpServiceToken.js +208 -0
- package/dist/models/notification.d.ts +2 -0
- package/dist/models/notification.js +62 -0
- package/dist/models/notificationPreference.d.ts +2 -0
- package/dist/models/notificationPreference.js +49 -0
- package/dist/notifications/notificationService.d.ts +40 -0
- package/dist/notifications/notificationService.js +323 -0
- package/dist/notifications/notificationsApp.d.ts +18 -0
- package/dist/notifications/notificationsApp.js +250 -0
- package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
- package/dist/notifications/notificationsBeforeSend.js +77 -0
- package/dist/openApi.d.ts +1 -7
- package/dist/openApi.js +7 -6
- package/dist/openApiBuilder.d.ts +19 -7
- package/dist/openApiBuilder.js +61 -8
- package/dist/openApiCompat.js +1 -1
- package/dist/openApiValidator.d.ts +13 -1
- package/dist/openApiValidator.js +200 -38
- package/dist/orgs/orgContext.d.ts +23 -0
- package/dist/orgs/orgContext.js +198 -0
- package/dist/orgs/orgPermissions.d.ts +4 -0
- package/dist/orgs/orgPermissions.js +77 -0
- package/dist/orgs/orgPlugin.d.ts +13 -0
- package/dist/orgs/orgPlugin.js +84 -0
- package/dist/orgs/organizationModel.d.ts +13 -0
- package/dist/orgs/organizationModel.js +301 -0
- package/dist/orgs/organizationSettings.d.ts +19 -0
- package/dist/orgs/organizationSettings.js +120 -0
- package/dist/orgs/orgsApp.d.ts +28 -0
- package/dist/orgs/orgsApp.js +678 -0
- package/dist/permissions.d.ts +7 -13
- package/dist/permissions.js +8 -31
- package/dist/plugins.d.ts +14 -1
- package/dist/plugins.js +17 -6
- package/dist/populate.d.ts +2 -2
- package/dist/populate.js +47 -42
- package/dist/rateLimit/applyTrustProxy.d.ts +3 -0
- package/dist/rateLimit/applyTrustProxy.js +17 -0
- package/dist/rateLimit/createStore.d.ts +2 -0
- package/dist/rateLimit/createStore.js +28 -0
- package/dist/rateLimit/memoryStore.d.ts +2 -0
- package/dist/rateLimit/memoryStore.js +108 -0
- package/dist/rateLimit/middleware.d.ts +3 -0
- package/dist/rateLimit/middleware.js +109 -0
- package/dist/rateLimit/mongoStore.d.ts +4 -0
- package/dist/rateLimit/mongoStore.js +123 -0
- package/dist/rateLimit/policies.d.ts +10 -0
- package/dist/rateLimit/policies.js +149 -0
- package/dist/rateLimit/redisStore.d.ts +4 -0
- package/dist/rateLimit/redisStore.js +156 -0
- package/dist/rateLimit/types.d.ts +54 -0
- package/dist/rateLimit/types.js +9 -0
- package/dist/rbac/access.js +35 -14
- package/dist/rbac/assertAllowed.d.ts +9 -0
- package/dist/rbac/assertAllowed.js +182 -0
- package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
- package/dist/rbac/membershipOrgAdminSource.js +122 -0
- package/dist/rbac/modelRouterAccess.d.ts +4 -0
- package/dist/rbac/modelRouterAccess.js +11 -4
- package/dist/rbac/permissionUtils.js +3 -2
- package/dist/rbac/resolve.js +65 -35
- package/dist/rbac/roleModel.d.ts +2 -5
- package/dist/rbac/roleModel.js +18 -4
- package/dist/rbac/routes.js +15 -6
- package/dist/rbac/statements.d.ts +10 -1
- package/dist/rbac/statements.js +19 -2
- package/dist/rbac/types.d.ts +5 -0
- package/dist/realtime/changeStreamWatcher.d.ts +10 -4
- package/dist/realtime/changeStreamWatcher.js +110 -64
- package/dist/realtime/deprecation.d.ts +13 -0
- package/dist/realtime/deprecation.js +33 -0
- package/dist/realtime/realtimeApp.d.ts +2 -1
- package/dist/realtime/realtimeApp.js +11 -11
- package/dist/realtime/registry.d.ts +0 -31
- package/dist/realtime/registry.js +44 -31
- package/dist/realtime/socketAuth.d.ts +5 -5
- package/dist/realtime/socketAuth.js +11 -1
- package/dist/realtime/socketUser.d.ts +5 -1
- package/dist/realtime/types.d.ts +14 -4
- package/dist/schemaMetadata.d.ts +56 -0
- package/dist/schemaMetadata.js +524 -0
- package/dist/seedRunner.d.ts +88 -0
- package/dist/seedRunner.js +627 -0
- package/dist/sync/adminBroadcastScope.d.ts +40 -0
- package/dist/sync/adminBroadcastScope.js +194 -0
- package/dist/sync/adminWindowAccess.d.ts +13 -0
- package/dist/sync/adminWindowAccess.js +96 -0
- package/dist/sync/adminWindowMutation.d.ts +88 -0
- package/dist/sync/adminWindowMutation.js +247 -0
- package/dist/sync/executors.d.ts +19 -3
- package/dist/sync/executors.js +194 -165
- package/dist/sync/loadHarness.js +28 -19
- package/dist/sync/models.js +26 -24
- package/dist/sync/mutationHandler.d.ts +14 -2
- package/dist/sync/mutationHandler.js +201 -76
- package/dist/sync/registrationSideEffects.d.ts +35 -0
- package/dist/sync/registrationSideEffects.js +151 -0
- package/dist/sync/registry.d.ts +2 -52
- package/dist/sync/registry.js +42 -187
- package/dist/sync/routes.d.ts +13 -0
- package/dist/sync/routes.js +116 -58
- package/dist/sync/scripts/compactTombstones.js +0 -2
- package/dist/sync/socketHandlers.d.ts +8 -2
- package/dist/sync/socketHandlers.js +144 -50
- package/dist/sync/streams.d.ts +2 -0
- package/dist/sync/streams.js +7 -1
- package/dist/sync/syncApp.d.ts +4 -3
- package/dist/sync/syncApp.js +5 -4
- package/dist/sync/syncBatch.d.ts +5 -2
- package/dist/sync/syncBatch.js +7 -6
- package/dist/sync/syncSeqPlugin.d.ts +1 -1
- package/dist/sync/syncSeqPlugin.js +20 -16
- package/dist/sync/types.d.ts +18 -0
- package/dist/terrenoApp.d.ts +48 -5
- package/dist/terrenoApp.js +157 -72
- package/dist/tests/createTestData.d.ts +1 -3
- package/dist/tests/createTestData.js +3 -5
- package/dist/tests/models.d.ts +3 -0
- package/dist/tests/models.js +15 -2
- package/dist/tests/testHelper.d.ts +0 -5
- package/dist/tests/testHelper.js +2 -9
- package/dist/types/authToken.d.ts +33 -0
- package/dist/types/authToken.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +41 -0
- package/dist/types/mcpServiceToken.js +2 -0
- package/dist/types/membership.d.ts +23 -0
- package/dist/types/membership.js +2 -0
- package/dist/types/notification.d.ts +28 -0
- package/dist/types/notification.js +2 -0
- package/dist/types/notificationPreference.d.ts +20 -0
- package/dist/types/notificationPreference.js +2 -0
- package/dist/types/organization.d.ts +36 -0
- package/dist/types/organization.js +2 -0
- package/dist/webhooks/headerValue.d.ts +2 -0
- package/dist/webhooks/headerValue.js +14 -0
- package/dist/webhooks/idempotency/memoryStore.d.ts +10 -0
- package/dist/webhooks/idempotency/memoryStore.js +65 -0
- package/dist/webhooks/idempotency/mongoStore.d.ts +4 -0
- package/dist/webhooks/idempotency/mongoStore.js +150 -0
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +17 -0
- package/dist/webhooks/idempotency/webhookReceipt.js +39 -0
- package/dist/webhooks/rawBody.d.ts +15 -0
- package/dist/webhooks/rawBody.js +23 -0
- package/dist/webhooks/timingSafeEqual.d.ts +4 -0
- package/dist/webhooks/timingSafeEqual.js +17 -0
- package/dist/webhooks/verifiers/hmac.d.ts +15 -0
- package/dist/webhooks/verifiers/hmac.js +49 -0
- package/dist/webhooks/verifiers/sendgrid.d.ts +10 -0
- package/dist/webhooks/verifiers/sendgrid.js +56 -0
- package/dist/webhooks/verifiers/stripe.d.ts +9 -0
- package/dist/webhooks/verifiers/stripe.js +99 -0
- package/dist/webhooks/verifiers/twilio.d.ts +9 -0
- package/dist/webhooks/verifiers/twilio.js +112 -0
- package/dist/webhooks/webhooksApp.d.ts +33 -0
- package/dist/webhooks/webhooksApp.js +195 -0
- package/package.json +32 -27
- package/src/__snapshots__/openApi.test.ts.snap +444 -36
- package/src/__snapshots__/openApiBuilder.test.ts.snap +24 -24
- package/src/__snapshots__/schemaMetadata.test.ts.snap +196 -0
- package/src/actions.ts +20 -13
- package/src/adminModelRouter.test.ts +133 -0
- package/src/adminTypes.ts +8 -0
- package/src/api.arrayOperations.test.ts +43 -31
- package/src/api.hooks.test.ts +38 -29
- package/src/api.query.test.ts +31 -31
- package/src/api.test.ts +133 -97
- package/src/api.ts +84 -27
- package/src/audit/auditApp.test.ts +243 -0
- package/src/audit/auditApp.ts +139 -0
- package/src/audit/auditEventModel.ts +156 -0
- package/src/audit/auditRouter.test.ts +456 -0
- package/src/audit/cloudTasksEnqueue.test.ts +124 -0
- package/src/audit/cloudTasksEnqueue.ts +104 -0
- package/src/audit/diff.ts +163 -0
- package/src/audit/processQueue.ts +69 -0
- package/src/audit/rbacSink.test.ts +86 -0
- package/src/audit/rbacSink.ts +42 -0
- package/src/audit/record.test.ts +402 -0
- package/src/audit/record.ts +328 -0
- package/src/auth.test.ts +108 -52
- package/src/auth.ts +98 -29
- package/src/authRecovery.test.ts +281 -0
- package/src/authRecovery.ts +279 -0
- package/src/authTokens.test.ts +94 -0
- package/src/authTokens.ts +118 -0
- package/src/authVerification.test.ts +293 -0
- package/src/betterAuth.ts +31 -0
- package/src/betterAuthApp.test.ts +1 -0
- package/src/betterAuthApp.ts +27 -9
- package/src/betterAuthSetup.test.ts +595 -38
- package/src/betterAuthSetup.ts +261 -5
- package/src/collectionRegistry.test.ts +164 -0
- package/src/collectionRegistry.ts +135 -0
- package/src/configurationApp.ts +13 -7
- package/src/docLoader.test.ts +45 -1
- package/src/docLoader.ts +37 -6
- package/src/express.d.ts +3 -0
- package/src/expressServer.test.ts +180 -68
- package/src/expressServer.ts +75 -19
- package/src/githubAuth.test.ts +60 -55
- package/src/index.ts +127 -0
- package/src/mcp/auth.test.ts +47 -0
- package/src/mcp/auth.ts +32 -11
- package/src/mcp/handlers.test.ts +275 -0
- package/src/mcp/handlers.ts +45 -172
- package/src/mcp/integration.test.ts +59 -9
- package/src/mcp/query.ts +5 -7
- package/src/mcp/registry.ts +29 -20
- package/src/mcp/schemaGenerator.test.ts +8 -0
- package/src/mcp/schemaGenerator.ts +49 -131
- package/src/mcp/server.ts +2 -0
- package/src/mcp/serviceTokens.test.ts +330 -0
- package/src/mcp/serviceTokens.ts +307 -0
- package/src/mcp/toolGenerator.test.ts +82 -1
- package/src/mcp/types.ts +2 -6
- package/src/migrations/cli.test.ts +376 -0
- package/src/migrations/cli.ts +283 -0
- package/src/migrations/fixtures/all-down/20260910120000-one.ts +6 -0
- package/src/migrations/fixtures/all-down/20260910120001-two.ts +6 -0
- package/src/migrations/fixtures/bad-id/20260910120000-mismatch.ts +4 -0
- package/src/migrations/fixtures/bad-name/not-a-migration.ts +4 -0
- package/src/migrations/fixtures/missing-up/20260910120000-noup.ts +2 -0
- package/src/migrations/fixtures/valid/20260910120000-alpha.ts +4 -0
- package/src/migrations/fixtures/valid/20260910120001-beta.ts +6 -0
- package/src/migrations/generate.test.ts +333 -0
- package/src/migrations/generate.ts +216 -0
- package/src/migrations/load.test.ts +103 -0
- package/src/migrations/load.ts +124 -0
- package/src/migrations/lock.test.ts +133 -0
- package/src/migrations/lock.ts +118 -0
- package/src/migrations/runner.test.ts +504 -0
- package/src/migrations/runner.ts +358 -0
- package/src/migrations/schemaCatalog.test.ts +159 -0
- package/src/migrations/schemaCatalog.ts +262 -0
- package/src/migrations/types.ts +94 -0
- package/src/models/mcpServiceToken.test.ts +129 -0
- package/src/models/mcpServiceToken.ts +158 -0
- package/src/models/notification.ts +68 -0
- package/src/models/notificationPreference.ts +62 -0
- package/src/notifications/notificationService.ts +254 -0
- package/src/notifications/notificationsApp.test.ts +505 -0
- package/src/notifications/notificationsApp.ts +200 -0
- package/src/notifications/notificationsBeforeSend.ts +51 -0
- package/src/openApi.test.ts +80 -65
- package/src/openApi.ts +2 -1
- package/src/openApiBuilder.test.ts +95 -7
- package/src/openApiBuilder.ts +60 -12
- package/src/openApiCompat.ts +1 -1
- package/src/openApiValidator.test.ts +270 -56
- package/src/openApiValidator.ts +219 -36
- package/src/orgs/orgAccessOptIn.test.ts +87 -0
- package/src/orgs/orgContext.test.ts +259 -0
- package/src/orgs/orgContext.ts +151 -0
- package/src/orgs/orgPermissions.test.ts +105 -0
- package/src/orgs/orgPermissions.ts +40 -0
- package/src/orgs/orgPlugin.test.ts +130 -0
- package/src/orgs/orgPlugin.ts +70 -0
- package/src/orgs/orgRbac.test.ts +267 -0
- package/src/orgs/orgSecurityDefects.test.ts +415 -0
- package/src/orgs/organizationModel.test.ts +205 -0
- package/src/orgs/organizationModel.ts +257 -0
- package/src/orgs/organizationSettings.test.ts +113 -0
- package/src/orgs/organizationSettings.ts +126 -0
- package/src/orgs/orgsApp.test.ts +382 -0
- package/src/orgs/orgsApp.ts +563 -0
- package/src/permissions.test.ts +62 -36
- package/src/permissions.ts +8 -30
- package/src/plugins.test.ts +14 -0
- package/src/plugins.ts +34 -9
- package/src/populate.test.ts +34 -0
- package/src/populate.ts +40 -29
- package/src/rateLimit/applyTrustProxy.ts +17 -0
- package/src/rateLimit/createStore.ts +25 -0
- package/src/rateLimit/memoryStore.ts +40 -0
- package/src/rateLimit/middleware.ts +79 -0
- package/src/rateLimit/mongoStore.test.ts +80 -0
- package/src/rateLimit/mongoStore.ts +77 -0
- package/src/rateLimit/policies.ts +130 -0
- package/src/rateLimit/rateLimit.test.ts +328 -0
- package/src/rateLimit/redisStore.ioredis.test.ts +29 -0
- package/src/rateLimit/redisStore.test.ts +114 -0
- package/src/rateLimit/redisStore.ts +86 -0
- package/src/rateLimit/types.ts +64 -0
- package/src/rbac/access.ts +16 -4
- package/src/rbac/assertAllowed.test.ts +325 -0
- package/src/rbac/assertAllowed.ts +157 -0
- package/src/rbac/backfillAdmins.test.ts +124 -0
- package/src/rbac/fieldViews.test.ts +89 -0
- package/src/rbac/membershipOrgAdminSource.ts +59 -0
- package/src/rbac/modelRouterAccess.test.ts +126 -0
- package/src/rbac/modelRouterAccess.ts +8 -2
- package/src/rbac/permissionUtils.test.ts +26 -5
- package/src/rbac/permissionUtils.ts +3 -2
- package/src/rbac/realtimeAccess.test.ts +109 -1
- package/src/rbac/resolve.test.ts +34 -2
- package/src/rbac/resolve.ts +31 -11
- package/src/rbac/roleManager.test.ts +169 -0
- package/src/rbac/roleModel.test.ts +34 -1
- package/src/rbac/roleModel.ts +17 -1
- package/src/rbac/routes.test.ts +233 -0
- package/src/rbac/routes.ts +16 -6
- package/src/rbac/statements.test.ts +19 -0
- package/src/rbac/statements.ts +19 -1
- package/src/rbac/types.ts +5 -0
- package/src/realtime/changeStreamWatcher.ts +73 -33
- package/src/realtime/deprecation.test.ts +60 -0
- package/src/realtime/deprecation.ts +33 -0
- package/src/realtime/realtime.test.ts +365 -189
- package/src/realtime/realtimeApp.ts +11 -11
- package/src/realtime/realtimeAppSetup.test.ts +32 -5
- package/src/realtime/registry.ts +48 -42
- package/src/realtime/sessionRevalidation.test.ts +57 -44
- package/src/realtime/socketAuth.test.ts +75 -23
- package/src/realtime/socketAuth.ts +17 -6
- package/src/realtime/socketUser.ts +5 -3
- package/src/realtime/types.ts +14 -4
- package/src/schemaMetadata.test.ts +419 -0
- package/src/schemaMetadata.ts +593 -0
- package/src/scrubAdminFields.test.ts +105 -0
- package/src/seedRunner.test.ts +591 -0
- package/src/seedRunner.ts +464 -0
- package/src/sync/adminBroadcastConfig.test.ts +96 -0
- package/src/sync/adminBroadcastScope.test.ts +155 -0
- package/src/sync/adminBroadcastScope.ts +123 -0
- package/src/sync/adminWindowAccess.test.ts +57 -0
- package/src/sync/adminWindowAccess.ts +32 -0
- package/src/sync/adminWindowMutation.test.ts +450 -0
- package/src/sync/adminWindowMutation.ts +283 -0
- package/src/sync/docMatchesMutationData.test.ts +16 -0
- package/src/sync/ensureSyncIndexes.test.ts +54 -15
- package/src/sync/executors.test.ts +40 -0
- package/src/sync/executors.ts +57 -18
- package/src/sync/integration.test.ts +15 -7
- package/src/sync/loadHarness.ts +28 -19
- package/src/sync/models.ts +32 -18
- package/src/sync/mutationHandler.test.ts +293 -17
- package/src/sync/mutationHandler.ts +180 -36
- package/src/sync/registrationSideEffects.test.ts +126 -0
- package/src/sync/registrationSideEffects.ts +131 -0
- package/src/sync/registry.ts +49 -171
- package/src/sync/routes.ts +55 -10
- package/src/sync/scripts/compactTombstones.ts +0 -2
- package/src/sync/sessionRevalidationIntegration.test.ts +12 -11
- package/src/sync/socketHandlers.ts +154 -58
- package/src/sync/streams.ts +6 -0
- package/src/sync/sync.test.ts +28 -25
- package/src/sync/syncApp.ts +5 -4
- package/src/sync/syncBatch.ts +10 -4
- package/src/sync/syncFrontier.test.ts +17 -15
- package/src/sync/syncPhaseC.test.ts +54 -43
- package/src/sync/syncRoutes.test.ts +347 -38
- package/src/sync/syncSeqPlugin.ts +66 -44
- package/src/sync/syncSocket.test.ts +341 -81
- package/src/sync/types.ts +20 -0
- package/src/terrenoApp.mcpServiceTokens.test.ts +173 -0
- package/src/terrenoApp.migrations.test.ts +114 -0
- package/src/terrenoApp.test.ts +41 -0
- package/src/terrenoApp.ts +181 -46
- package/src/tests/createTestData.ts +2 -2
- package/src/tests/models.ts +24 -3
- package/src/tests/testHelper.ts +1 -7
- package/src/types/authToken.ts +41 -0
- package/src/types/mcpServiceToken.ts +48 -0
- package/src/types/membership.ts +48 -0
- package/src/types/notification.ts +34 -0
- package/src/types/notificationPreference.ts +28 -0
- package/src/types/organization.ts +49 -0
- package/src/webhooks/headerValue.test.ts +22 -0
- package/src/webhooks/headerValue.ts +12 -0
- package/src/webhooks/idempotency/memoryStore.ts +34 -0
- package/src/webhooks/idempotency/mongoStore.test.ts +274 -0
- package/src/webhooks/idempotency/mongoStore.ts +83 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +67 -0
- package/src/webhooks/rawBody.test.ts +107 -0
- package/src/webhooks/rawBody.ts +24 -0
- package/src/webhooks/timingSafeEqual.ts +16 -0
- package/src/webhooks/verifiers/hmac.test.ts +110 -0
- package/src/webhooks/verifiers/hmac.ts +54 -0
- package/src/webhooks/verifiers/providerVerifiers.test.ts +388 -0
- package/src/webhooks/verifiers/sendgrid.ts +59 -0
- package/src/webhooks/verifiers/stripe.ts +67 -0
- package/src/webhooks/verifiers/twilio.ts +69 -0
- package/src/webhooks/webhooksApp.test.ts +221 -0
- package/src/webhooks/webhooksApp.ts +144 -0
- package/tsconfig.json +1 -1
- package/tsconfig.typedoc.json +2 -1
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.d.ts +0 -2
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
- package/src/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import {beforeEach, describe, it} from "bun:test";
|
|
2
|
+
import {authAsUser as loginWithPassword} from "@terreno/test";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
import {type Application, Router} from "express";
|
|
5
|
+
import mongoose, {model, Schema} from "mongoose";
|
|
6
|
+
import type {PassportLocalMongooseDocument} from "passport-local-mongoose";
|
|
7
|
+
import qs from "qs";
|
|
8
|
+
import {modelRouter} from "../api";
|
|
9
|
+
import {
|
|
10
|
+
type UserModel as AuthUserModel,
|
|
11
|
+
addAuthRoutes,
|
|
12
|
+
authenticateMiddleware,
|
|
13
|
+
setupAuth,
|
|
14
|
+
} from "../auth";
|
|
15
|
+
import {apiErrorMiddleware, apiUnauthorizedMiddleware} from "../errors";
|
|
16
|
+
import {Permissions} from "../permissions";
|
|
17
|
+
import {getBaseServer} from "../tests";
|
|
18
|
+
import {type User, UserModel} from "../tests/models";
|
|
19
|
+
import {Membership, Organization} from "./organizationModel";
|
|
20
|
+
import {isPlatformOrgActor, orgContextMiddleware, resolveOrgContext} from "./orgContext";
|
|
21
|
+
import {OrgQueryFilter} from "./orgPermissions";
|
|
22
|
+
import {orgScopedPlugin} from "./orgPlugin";
|
|
23
|
+
|
|
24
|
+
interface OrgProject {
|
|
25
|
+
organizationId: mongoose.Types.ObjectId;
|
|
26
|
+
title: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const projectSchema = new Schema<OrgProject>(
|
|
30
|
+
{
|
|
31
|
+
title: {description: "Project title", required: true, type: String},
|
|
32
|
+
},
|
|
33
|
+
{strict: "throw"}
|
|
34
|
+
);
|
|
35
|
+
projectSchema.plugin(orgScopedPlugin);
|
|
36
|
+
|
|
37
|
+
const OrgProjectModel =
|
|
38
|
+
(mongoose.models.OrgProject as mongoose.Model<OrgProject> | undefined) ??
|
|
39
|
+
model<OrgProject>("OrgProject", projectSchema);
|
|
40
|
+
|
|
41
|
+
const PASSWORD = "testpassword123";
|
|
42
|
+
|
|
43
|
+
const setPassword = async (user: User, password: string): Promise<void> => {
|
|
44
|
+
await (user as unknown as PassportLocalMongooseDocument).setPassword(password);
|
|
45
|
+
await (user as unknown as {save: () => Promise<void>}).save();
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const createUser = async (args: {email: string; roles?: string[]}): Promise<User> => {
|
|
49
|
+
const user = await UserModel.create({
|
|
50
|
+
email: args.email,
|
|
51
|
+
name: args.email,
|
|
52
|
+
roles: args.roles ?? [],
|
|
53
|
+
});
|
|
54
|
+
await setPassword(user, PASSWORD);
|
|
55
|
+
return user;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
describe("org context middleware and OrgQueryFilter", () => {
|
|
59
|
+
let app: Application;
|
|
60
|
+
|
|
61
|
+
beforeEach(async () => {
|
|
62
|
+
await Promise.all([
|
|
63
|
+
Membership.deleteMany({}),
|
|
64
|
+
Organization.deleteMany({}),
|
|
65
|
+
OrgProjectModel.deleteMany({}),
|
|
66
|
+
UserModel.deleteMany({}),
|
|
67
|
+
]);
|
|
68
|
+
await OrgProjectModel.syncIndexes();
|
|
69
|
+
|
|
70
|
+
app = getBaseServer();
|
|
71
|
+
setupAuth(app, UserModel as unknown as AuthUserModel);
|
|
72
|
+
addAuthRoutes(app, UserModel as unknown as AuthUserModel);
|
|
73
|
+
app.use(apiUnauthorizedMiddleware);
|
|
74
|
+
|
|
75
|
+
const tenant = Router();
|
|
76
|
+
tenant.use(authenticateMiddleware());
|
|
77
|
+
tenant.use(orgContextMiddleware({required: true}));
|
|
78
|
+
tenant.use(
|
|
79
|
+
modelRouter(OrgProjectModel, {
|
|
80
|
+
permissions: {
|
|
81
|
+
create: [Permissions.IsAuthenticated],
|
|
82
|
+
delete: [],
|
|
83
|
+
list: [Permissions.IsAuthenticated],
|
|
84
|
+
read: [Permissions.IsOrganizationMember],
|
|
85
|
+
update: [],
|
|
86
|
+
},
|
|
87
|
+
queryFields: ["title", "organizationId"],
|
|
88
|
+
queryFilter: OrgQueryFilter,
|
|
89
|
+
})
|
|
90
|
+
);
|
|
91
|
+
app.use("/projects", tenant);
|
|
92
|
+
app.use(apiErrorMiddleware);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("returns 400 when an operator omits X-Organization-Id on a scoped list", async () => {
|
|
96
|
+
await createUser({email: "operator@example.com", roles: ["operator"]});
|
|
97
|
+
const agent = await loginWithPassword(app, {
|
|
98
|
+
email: "operator@example.com",
|
|
99
|
+
password: PASSWORD,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const res = await agent.get("/projects");
|
|
103
|
+
assert.equal(res.status, 400);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("scopes an org-admin of exactly one org when the header is omitted", async () => {
|
|
107
|
+
const admin = await createUser({email: "orgadmin@example.com"});
|
|
108
|
+
const orgA = await Organization.create({name: "Org A", ownerId: admin._id});
|
|
109
|
+
const orgB = await Organization.create({name: "Org B", ownerId: admin._id});
|
|
110
|
+
await Membership.create({
|
|
111
|
+
organizationId: orgA._id,
|
|
112
|
+
roleName: "org-admin",
|
|
113
|
+
userId: admin._id,
|
|
114
|
+
});
|
|
115
|
+
await OrgProjectModel.create({organizationId: orgA._id, title: "Alpha"});
|
|
116
|
+
await OrgProjectModel.create({organizationId: orgB._id, title: "Bravo"});
|
|
117
|
+
|
|
118
|
+
const agent = await loginWithPassword(app, {
|
|
119
|
+
email: "orgadmin@example.com",
|
|
120
|
+
password: PASSWORD,
|
|
121
|
+
});
|
|
122
|
+
const res = await agent.get("/projects");
|
|
123
|
+
assert.equal(res.status, 200);
|
|
124
|
+
assert.equal(res.body.data.length, 1);
|
|
125
|
+
assert.equal(res.body.data[0].title, "Alpha");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("does not return org B rows for an org A member including $or and query-param attacks", async () => {
|
|
129
|
+
const member = await createUser({email: "member@example.com"});
|
|
130
|
+
const orgA = await Organization.create({name: "Tenant A", ownerId: member._id});
|
|
131
|
+
const orgB = await Organization.create({name: "Tenant B", ownerId: member._id});
|
|
132
|
+
await Membership.create({
|
|
133
|
+
organizationId: orgA._id,
|
|
134
|
+
roleName: "member",
|
|
135
|
+
userId: member._id,
|
|
136
|
+
});
|
|
137
|
+
await OrgProjectModel.create({organizationId: orgA._id, title: "Keep"});
|
|
138
|
+
await OrgProjectModel.create({organizationId: orgB._id, title: "Leak"});
|
|
139
|
+
|
|
140
|
+
const agent = await loginWithPassword(app, {
|
|
141
|
+
email: "member@example.com",
|
|
142
|
+
password: PASSWORD,
|
|
143
|
+
});
|
|
144
|
+
const headers = {"X-Organization-Id": orgA._id.toString()};
|
|
145
|
+
|
|
146
|
+
const listed = await agent.get("/projects").set(headers);
|
|
147
|
+
assert.equal(listed.status, 200);
|
|
148
|
+
assert.deepEqual(
|
|
149
|
+
listed.body.data.map((row: {title: string}) => row.title),
|
|
150
|
+
["Keep"]
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const byParam = await agent
|
|
154
|
+
.get("/projects")
|
|
155
|
+
.query({organizationId: orgB._id.toString()})
|
|
156
|
+
.set(headers);
|
|
157
|
+
assert.equal(byParam.status, 200);
|
|
158
|
+
assert.deepEqual(
|
|
159
|
+
byParam.body.data.map((row: {title: string}) => row.title),
|
|
160
|
+
["Keep"]
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
const orAttack = await agent
|
|
164
|
+
.get(`/projects?${qs.stringify({$or: [{organizationId: orgB._id.toString()}]})}`)
|
|
165
|
+
.set(headers);
|
|
166
|
+
assert.equal(orAttack.status, 200);
|
|
167
|
+
const leaked = (orAttack.body.data as Array<{title: string}>).some((row) => {
|
|
168
|
+
return row.title === "Leak";
|
|
169
|
+
});
|
|
170
|
+
assert.isFalse(leaked);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("returns 403 when a non-member sends X-Organization-Id", async () => {
|
|
174
|
+
const outsider = await createUser({email: "outsider@example.com"});
|
|
175
|
+
const org = await Organization.create({name: "Closed", ownerId: outsider._id});
|
|
176
|
+
const agent = await loginWithPassword(app, {
|
|
177
|
+
email: "outsider@example.com",
|
|
178
|
+
password: PASSWORD,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const res = await agent.get("/projects").set("X-Organization-Id", org._id.toString());
|
|
182
|
+
assert.equal(res.status, 403);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("returns 400 when an org-admin of many orgs omits the header", async () => {
|
|
186
|
+
const admin = await createUser({email: "multiadmin@example.com"});
|
|
187
|
+
const orgA = await Organization.create({name: "Multi A", ownerId: admin._id});
|
|
188
|
+
const orgB = await Organization.create({name: "Multi B", ownerId: admin._id});
|
|
189
|
+
await Membership.create({
|
|
190
|
+
organizationId: orgA._id,
|
|
191
|
+
roleName: "org-admin",
|
|
192
|
+
userId: admin._id,
|
|
193
|
+
});
|
|
194
|
+
await Membership.create({
|
|
195
|
+
organizationId: orgB._id,
|
|
196
|
+
roleName: "org-admin",
|
|
197
|
+
userId: admin._id,
|
|
198
|
+
});
|
|
199
|
+
const agent = await loginWithPassword(app, {
|
|
200
|
+
email: "multiadmin@example.com",
|
|
201
|
+
password: PASSWORD,
|
|
202
|
+
});
|
|
203
|
+
const res = await agent.get("/projects");
|
|
204
|
+
assert.equal(res.status, 400);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe("orgContext helpers", () => {
|
|
209
|
+
beforeEach(async () => {
|
|
210
|
+
await Promise.all([
|
|
211
|
+
Membership.deleteMany({}),
|
|
212
|
+
Organization.deleteMany({}),
|
|
213
|
+
UserModel.deleteMany({}),
|
|
214
|
+
]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("isPlatformOrgActor returns false for undefined", () => {
|
|
218
|
+
assert.isFalse(isPlatformOrgActor(undefined));
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("resolveOrgContext throws 401 when user is missing and required is true", async () => {
|
|
222
|
+
let error: unknown;
|
|
223
|
+
try {
|
|
224
|
+
await resolveOrgContext({required: true});
|
|
225
|
+
} catch (caught) {
|
|
226
|
+
error = caught;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
assert.isDefined(error);
|
|
230
|
+
assert.equal((error as {status?: number}).status, 401);
|
|
231
|
+
assert.equal((error as {title?: string}).title, "Unauthorized");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("resolveOrgContext returns empty context when user is missing and required is false", async () => {
|
|
235
|
+
const context = await resolveOrgContext({required: false});
|
|
236
|
+
|
|
237
|
+
assert.deepEqual(context, {});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("resolveOrgContext returns empty context when user has no id and required is false", async () => {
|
|
241
|
+
const context = await resolveOrgContext({
|
|
242
|
+
required: false,
|
|
243
|
+
user: {} as import("../auth").User,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
assert.deepEqual(context, {});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("resolveOrgContext returns empty context for authenticated user when org is not required", async () => {
|
|
250
|
+
const user = await createUser({email: "optional-context@example.com"});
|
|
251
|
+
|
|
252
|
+
const context = await resolveOrgContext({
|
|
253
|
+
required: false,
|
|
254
|
+
user: user as unknown as import("../auth").User,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
assert.deepEqual(context, {});
|
|
258
|
+
});
|
|
259
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {AsyncLocalStorage} from "node:async_hooks";
|
|
2
|
+
import type express from "express";
|
|
3
|
+
import type {NextFunction, RequestHandler} from "express";
|
|
4
|
+
|
|
5
|
+
import type {User} from "../auth";
|
|
6
|
+
import {APIError, BadRequestError, ForbiddenError, NotFoundError} from "../errors";
|
|
7
|
+
import type {MembershipDocument} from "../types/membership";
|
|
8
|
+
import type {OrganizationDocument} from "../types/organization";
|
|
9
|
+
import {isValidObjectId} from "../utils";
|
|
10
|
+
import {Membership, Organization} from "./organizationModel";
|
|
11
|
+
|
|
12
|
+
export const ORGANIZATION_ID_HEADER = "x-organization-id";
|
|
13
|
+
|
|
14
|
+
export const ORGANIZATION_DISABLED_TITLE = "Organization is disabled";
|
|
15
|
+
|
|
16
|
+
export const PLATFORM_ORG_ROLE_NAMES = ["operator", "superadmin"] as const;
|
|
17
|
+
|
|
18
|
+
export interface ResolvedOrgContext {
|
|
19
|
+
membership?: MembershipDocument;
|
|
20
|
+
organization?: OrganizationDocument;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const orgContextAls = new AsyncLocalStorage<ResolvedOrgContext>();
|
|
24
|
+
|
|
25
|
+
export const getOrgContext = (): ResolvedOrgContext | undefined => {
|
|
26
|
+
return orgContextAls.getStore();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const runWithOrgContext = <T>(context: ResolvedOrgContext, fn: () => T): T => {
|
|
30
|
+
return orgContextAls.run(context, fn);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const isPlatformOrgActor = (user?: User): boolean => {
|
|
34
|
+
if (!user) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const roles = (user as User & {roles?: string[]}).roles ?? [];
|
|
38
|
+
return roles.includes("operator") || roles.includes("superadmin");
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const readOrganizationIdHeader = (req: express.Request): string | undefined => {
|
|
42
|
+
const raw = req.header("X-Organization-Id");
|
|
43
|
+
if (typeof raw !== "string") {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
const trimmed = raw.trim();
|
|
47
|
+
if (!trimmed) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
return trimmed;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const userIdForMembership = (user: User): string => {
|
|
54
|
+
if (user._id) {
|
|
55
|
+
return String(user._id);
|
|
56
|
+
}
|
|
57
|
+
return user.id;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const loadOrganization = async (id: string): Promise<OrganizationDocument> => {
|
|
61
|
+
if (!isValidObjectId(id)) {
|
|
62
|
+
throw new BadRequestError("Invalid organization id");
|
|
63
|
+
}
|
|
64
|
+
const organization = await Organization.findOneOrNone({_id: id});
|
|
65
|
+
if (!organization) {
|
|
66
|
+
throw new NotFoundError("Organization not found");
|
|
67
|
+
}
|
|
68
|
+
return organization;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const assertOrganizationEnabled = (organization: OrganizationDocument): void => {
|
|
72
|
+
if (organization.disabled) {
|
|
73
|
+
throw new ForbiddenError(ORGANIZATION_DISABLED_TITLE);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const resolveOrgContext = async (args: {
|
|
78
|
+
organizationId?: string;
|
|
79
|
+
required: boolean;
|
|
80
|
+
user?: User;
|
|
81
|
+
}): Promise<ResolvedOrgContext> => {
|
|
82
|
+
const user = args.user;
|
|
83
|
+
if (!user?.id) {
|
|
84
|
+
if (args.required) {
|
|
85
|
+
throw new APIError({status: 401, title: "Unauthorized"});
|
|
86
|
+
}
|
|
87
|
+
return {};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const platform = isPlatformOrgActor(user);
|
|
91
|
+
|
|
92
|
+
if (args.organizationId) {
|
|
93
|
+
const organization = await loadOrganization(args.organizationId);
|
|
94
|
+
assertOrganizationEnabled(organization);
|
|
95
|
+
const membership = await Membership.findOneOrNone({
|
|
96
|
+
organizationId: organization._id,
|
|
97
|
+
status: "active",
|
|
98
|
+
userId: userIdForMembership(user),
|
|
99
|
+
});
|
|
100
|
+
if (!platform && !membership) {
|
|
101
|
+
throw new ForbiddenError("Not a member of this organization");
|
|
102
|
+
}
|
|
103
|
+
return {membership: membership ?? undefined, organization};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!args.required) {
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (platform) {
|
|
111
|
+
throw new BadRequestError("Organization context required");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const active = await Membership.findActiveForUser(userIdForMembership(user));
|
|
115
|
+
const adminMemberships = active.filter((row) => {
|
|
116
|
+
return row.roleName === "org-admin";
|
|
117
|
+
});
|
|
118
|
+
if (adminMemberships.length === 1) {
|
|
119
|
+
const membership = adminMemberships[0];
|
|
120
|
+
if (!membership) {
|
|
121
|
+
throw new ForbiddenError("Organization context required");
|
|
122
|
+
}
|
|
123
|
+
const organization = await Organization.findExactlyOne({_id: membership.organizationId});
|
|
124
|
+
assertOrganizationEnabled(organization);
|
|
125
|
+
return {membership, organization};
|
|
126
|
+
}
|
|
127
|
+
if (adminMemberships.length > 1) {
|
|
128
|
+
throw new BadRequestError("Select an organization");
|
|
129
|
+
}
|
|
130
|
+
throw new ForbiddenError("Organization context required");
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const orgContextMiddleware = (options?: {required?: boolean}): RequestHandler => {
|
|
134
|
+
const required = options?.required ?? false;
|
|
135
|
+
return async (req: express.Request, _res: express.Response, next: NextFunction) => {
|
|
136
|
+
try {
|
|
137
|
+
const context = await resolveOrgContext({
|
|
138
|
+
organizationId: readOrganizationIdHeader(req),
|
|
139
|
+
required,
|
|
140
|
+
user: req.user,
|
|
141
|
+
});
|
|
142
|
+
req.organization = context.organization;
|
|
143
|
+
req.membership = context.membership;
|
|
144
|
+
orgContextAls.run(context, () => {
|
|
145
|
+
next();
|
|
146
|
+
});
|
|
147
|
+
} catch (error) {
|
|
148
|
+
next(error);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import mongoose from "mongoose";
|
|
4
|
+
|
|
5
|
+
import type {User} from "../auth";
|
|
6
|
+
import {runWithOrgContext} from "./orgContext";
|
|
7
|
+
import {isOrgMemberPermission, OrgQueryFilter} from "./orgPermissions";
|
|
8
|
+
|
|
9
|
+
const platformUser = (roles: string[]): User =>
|
|
10
|
+
({
|
|
11
|
+
admin: false,
|
|
12
|
+
id: new mongoose.Types.ObjectId().toString(),
|
|
13
|
+
roles,
|
|
14
|
+
}) as User;
|
|
15
|
+
|
|
16
|
+
const memberUser = (): User =>
|
|
17
|
+
({
|
|
18
|
+
admin: false,
|
|
19
|
+
id: new mongoose.Types.ObjectId().toString(),
|
|
20
|
+
roles: [],
|
|
21
|
+
}) as User;
|
|
22
|
+
|
|
23
|
+
describe("OrgQueryFilter", () => {
|
|
24
|
+
it("throws when organization context is missing", () => {
|
|
25
|
+
assert.throws(() => OrgQueryFilter(memberUser()), "Organization context required");
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("isOrgMemberPermission", () => {
|
|
30
|
+
it("returns true when no object is provided", async () => {
|
|
31
|
+
const allowed = await isOrgMemberPermission("list", memberUser(), undefined);
|
|
32
|
+
|
|
33
|
+
assert.isTrue(allowed);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("returns false when there is no user", async () => {
|
|
37
|
+
const allowed = await isOrgMemberPermission("read", undefined, {
|
|
38
|
+
organizationId: new mongoose.Types.ObjectId(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
assert.isFalse(allowed);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("returns false when the document has no organizationId", async () => {
|
|
45
|
+
const allowed = await isOrgMemberPermission("read", memberUser(), {});
|
|
46
|
+
|
|
47
|
+
assert.isFalse(allowed);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("returns false when ALS org context does not match for a non-platform member", async () => {
|
|
51
|
+
const contextOrganizationId = new mongoose.Types.ObjectId();
|
|
52
|
+
const documentOrganizationId = new mongoose.Types.ObjectId();
|
|
53
|
+
const user = memberUser();
|
|
54
|
+
|
|
55
|
+
const allowed = await runWithOrgContext(
|
|
56
|
+
{organization: {_id: contextOrganizationId} as never},
|
|
57
|
+
() => isOrgMemberPermission("read", user, {organizationId: documentOrganizationId})
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
assert.isFalse(await allowed);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("returns false when ALS org context is missing for a non-platform member", async () => {
|
|
64
|
+
const allowed = await isOrgMemberPermission("read", memberUser(), {
|
|
65
|
+
organizationId: new mongoose.Types.ObjectId(),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
assert.isFalse(allowed);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("isOrgMemberPermission platform actors", () => {
|
|
73
|
+
it("returns false for a platform actor without ALS org context", async () => {
|
|
74
|
+
const user = platformUser(["operator"]);
|
|
75
|
+
const organizationId = new mongoose.Types.ObjectId();
|
|
76
|
+
|
|
77
|
+
const allowed = await isOrgMemberPermission("read", user, {organizationId});
|
|
78
|
+
|
|
79
|
+
assert.isFalse(allowed);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("returns true when ALS org context matches the document organization", async () => {
|
|
83
|
+
const organizationId = new mongoose.Types.ObjectId();
|
|
84
|
+
const user = platformUser(["superadmin"]);
|
|
85
|
+
|
|
86
|
+
const allowed = await runWithOrgContext({organization: {_id: organizationId} as never}, () =>
|
|
87
|
+
isOrgMemberPermission("update", user, {organizationId})
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
assert.isTrue(await allowed);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("returns false when ALS org context does not match the document organization", async () => {
|
|
94
|
+
const contextOrganizationId = new mongoose.Types.ObjectId();
|
|
95
|
+
const documentOrganizationId = new mongoose.Types.ObjectId();
|
|
96
|
+
const user = platformUser(["operator"]);
|
|
97
|
+
|
|
98
|
+
const allowed = await runWithOrgContext(
|
|
99
|
+
{organization: {_id: contextOrganizationId} as never},
|
|
100
|
+
() => isOrgMemberPermission("read", user, {organizationId: documentOrganizationId})
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
assert.isFalse(await allowed);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type {User} from "../auth";
|
|
2
|
+
import {BadRequestError} from "../errors";
|
|
3
|
+
import type {PermissionMethod} from "../permissions";
|
|
4
|
+
import {Membership} from "./organizationModel";
|
|
5
|
+
import {getOrgContext, isPlatformOrgActor} from "./orgContext";
|
|
6
|
+
|
|
7
|
+
export const OrgQueryFilter = (
|
|
8
|
+
_user?: User,
|
|
9
|
+
_query?: Record<string, unknown>
|
|
10
|
+
): Record<string, unknown> => {
|
|
11
|
+
const context = getOrgContext();
|
|
12
|
+
if (!context?.organization) {
|
|
13
|
+
throw new BadRequestError("Organization context required");
|
|
14
|
+
}
|
|
15
|
+
return {organizationId: context.organization._id};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const isOrgMemberPermission: PermissionMethod<unknown> = async (_method, user, obj) => {
|
|
19
|
+
if (!obj) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
if (!user) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const organizationId = (obj as {organizationId?: unknown}).organizationId;
|
|
26
|
+
if (!organizationId) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
const context = getOrgContext();
|
|
30
|
+
if (!context?.organization) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
if (String(context.organization._id) !== String(organizationId)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
if (isPlatformOrgActor(user)) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return Membership.isMember(user.id, String(organizationId));
|
|
40
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {beforeEach, describe, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import mongoose, {model, Schema} from "mongoose";
|
|
4
|
+
|
|
5
|
+
import {orgScopedPlugin} from "./orgPlugin";
|
|
6
|
+
|
|
7
|
+
interface OrgScopedWidget {
|
|
8
|
+
formerOrganizationId?: mongoose.Types.ObjectId;
|
|
9
|
+
organizationId: mongoose.Types.ObjectId;
|
|
10
|
+
title: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const widgetSchema = new Schema<OrgScopedWidget>(
|
|
14
|
+
{
|
|
15
|
+
formerOrganizationId: {
|
|
16
|
+
description: "An unrelated organization reference used to test rename protection",
|
|
17
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
18
|
+
},
|
|
19
|
+
title: {description: "Widget title", type: String},
|
|
20
|
+
},
|
|
21
|
+
{strict: "throw"}
|
|
22
|
+
);
|
|
23
|
+
widgetSchema.plugin(orgScopedPlugin);
|
|
24
|
+
|
|
25
|
+
const OrgScopedWidgetModel =
|
|
26
|
+
(mongoose.models.OrgScopedWidget as mongoose.Model<OrgScopedWidget> | undefined) ??
|
|
27
|
+
model<OrgScopedWidget>("OrgScopedWidget", widgetSchema);
|
|
28
|
+
|
|
29
|
+
describe("orgScopedPlugin", () => {
|
|
30
|
+
beforeEach(async () => {
|
|
31
|
+
await OrgScopedWidgetModel.deleteMany({});
|
|
32
|
+
await OrgScopedWidgetModel.syncIndexes();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("rejects a save when organizationId is missing", async () => {
|
|
36
|
+
let error: unknown;
|
|
37
|
+
try {
|
|
38
|
+
await OrgScopedWidgetModel.create({title: "No org"});
|
|
39
|
+
} catch (caughtError) {
|
|
40
|
+
error = caughtError;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
assert.instanceOf(error, Error);
|
|
44
|
+
assert.match((error as Error).message, /organizationId/i);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("indexes organizationId on the consumer collection", async () => {
|
|
48
|
+
const indexes = await OrgScopedWidgetModel.collection.indexes();
|
|
49
|
+
const orgIndex = indexes.find((index) => {
|
|
50
|
+
return index.key.organizationId === 1;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
assert.isDefined(orgIndex);
|
|
54
|
+
|
|
55
|
+
const saved = await OrgScopedWidgetModel.create({
|
|
56
|
+
organizationId: new mongoose.Types.ObjectId(),
|
|
57
|
+
title: "Scoped",
|
|
58
|
+
});
|
|
59
|
+
assert.isDefined(saved.organizationId);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("rejects changing organizationId after creation on save", async () => {
|
|
63
|
+
const orgA = new mongoose.Types.ObjectId();
|
|
64
|
+
const orgB = new mongoose.Types.ObjectId();
|
|
65
|
+
const widget = await OrgScopedWidgetModel.create({organizationId: orgA, title: "Pinned"});
|
|
66
|
+
|
|
67
|
+
widget.organizationId = orgB;
|
|
68
|
+
let error: unknown;
|
|
69
|
+
try {
|
|
70
|
+
await widget.save();
|
|
71
|
+
} catch (caughtError) {
|
|
72
|
+
error = caughtError;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
assert.isDefined(error);
|
|
76
|
+
assert.equal((error as {status?: number}).status, 400);
|
|
77
|
+
assert.equal((error as {title?: string}).title, "organizationId cannot be changed");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("rejects changing organizationId through updateOne", async () => {
|
|
81
|
+
const orgA = new mongoose.Types.ObjectId();
|
|
82
|
+
const orgB = new mongoose.Types.ObjectId();
|
|
83
|
+
const widget = await OrgScopedWidgetModel.create({organizationId: orgA, title: "Pinned"});
|
|
84
|
+
|
|
85
|
+
let error: unknown;
|
|
86
|
+
try {
|
|
87
|
+
await OrgScopedWidgetModel.updateOne({_id: widget._id}, {$set: {organizationId: orgB}});
|
|
88
|
+
} catch (caughtError) {
|
|
89
|
+
error = caughtError;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
assert.isDefined(error);
|
|
93
|
+
assert.equal((error as {status?: number}).status, 400);
|
|
94
|
+
assert.equal((error as {title?: string}).title, "organizationId cannot be changed");
|
|
95
|
+
|
|
96
|
+
const reloaded = await OrgScopedWidgetModel.findById(widget._id);
|
|
97
|
+
assert.equal(String(reloaded?.organizationId), String(orgA));
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("rejects removing or renaming organizationId through query updates", async () => {
|
|
101
|
+
const orgA = new mongoose.Types.ObjectId();
|
|
102
|
+
const orgB = new mongoose.Types.ObjectId();
|
|
103
|
+
const widget = await OrgScopedWidgetModel.create({
|
|
104
|
+
formerOrganizationId: orgB,
|
|
105
|
+
organizationId: orgA,
|
|
106
|
+
title: "Pinned",
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
for (const update of [
|
|
110
|
+
{$unset: {organizationId: 1}},
|
|
111
|
+
{$rename: {organizationId: "formerOrganizationId"}},
|
|
112
|
+
{$rename: {formerOrganizationId: "organizationId"}},
|
|
113
|
+
]) {
|
|
114
|
+
let error: unknown;
|
|
115
|
+
try {
|
|
116
|
+
await OrgScopedWidgetModel.updateOne({_id: widget._id}, update);
|
|
117
|
+
} catch (caughtError) {
|
|
118
|
+
error = caughtError;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (error) {
|
|
122
|
+
assert.equal((error as {status?: number}).status, 400);
|
|
123
|
+
assert.equal((error as {title?: string}).title, "organizationId cannot be changed");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const reloaded = await OrgScopedWidgetModel.findById(widget._id);
|
|
127
|
+
assert.equal(String(reloaded?.organizationId), String(orgA));
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|