@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,110 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
import type {Request} from "express";
|
|
5
|
+
import {DateTime} from "luxon";
|
|
6
|
+
|
|
7
|
+
import {hmacSignature} from "./hmac";
|
|
8
|
+
|
|
9
|
+
const SECRET = "whsec_test";
|
|
10
|
+
const BODY = Buffer.from(`{"id":"evt_1"}`);
|
|
11
|
+
const VALID_HEX = "030fa3b2413d1993c551364bd53bb9b3edb5c0c34d55dba6ada6041245632811";
|
|
12
|
+
|
|
13
|
+
const hmacHex = (payload: Buffer | string): string => {
|
|
14
|
+
return crypto.createHmac("sha256", SECRET).update(payload).digest("hex");
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const fakeReq = ({
|
|
18
|
+
signature,
|
|
19
|
+
timestamp,
|
|
20
|
+
rawBody = BODY,
|
|
21
|
+
}: {
|
|
22
|
+
rawBody?: Buffer;
|
|
23
|
+
signature?: string;
|
|
24
|
+
timestamp?: string;
|
|
25
|
+
}): Request => {
|
|
26
|
+
const headers: Record<string, string> = {};
|
|
27
|
+
if (signature !== undefined) {
|
|
28
|
+
headers["x-webhook-signature"] = signature;
|
|
29
|
+
}
|
|
30
|
+
if (timestamp !== undefined) {
|
|
31
|
+
headers["x-webhook-timestamp"] = timestamp;
|
|
32
|
+
}
|
|
33
|
+
return {headers, rawBody} as Request;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe("hmacSignature", () => {
|
|
37
|
+
it("accepts a matching hex HMAC of the raw body", () => {
|
|
38
|
+
const verify = hmacSignature({header: "X-Webhook-Signature", secret: SECRET});
|
|
39
|
+
assert.isTrue(verify(fakeReq({signature: VALID_HEX})));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("rejects a shorter signature instead of throwing", () => {
|
|
43
|
+
const verify = hmacSignature({header: "X-Webhook-Signature", secret: SECRET});
|
|
44
|
+
assert.isFalse(verify(fakeReq({signature: VALID_HEX.slice(0, 8)})));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("rejects a timestamp outside the tolerance window", () => {
|
|
48
|
+
const verify = hmacSignature({
|
|
49
|
+
header: "X-Webhook-Signature",
|
|
50
|
+
secret: SECRET,
|
|
51
|
+
timestampHeader: "X-Webhook-Timestamp",
|
|
52
|
+
toleranceSec: 300,
|
|
53
|
+
});
|
|
54
|
+
const stale = String(DateTime.utc().toUnixInteger() - 301);
|
|
55
|
+
const signature = hmacHex(`${stale}.{"id":"evt_1"}`);
|
|
56
|
+
assert.isFalse(verify(fakeReq({signature, timestamp: stale})));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("accepts HMAC of timestamp plus raw body inside the tolerance window", () => {
|
|
60
|
+
const verify = hmacSignature({
|
|
61
|
+
header: "X-Webhook-Signature",
|
|
62
|
+
secret: SECRET,
|
|
63
|
+
timestampHeader: "X-Webhook-Timestamp",
|
|
64
|
+
toleranceSec: 300,
|
|
65
|
+
});
|
|
66
|
+
const now = String(DateTime.utc().toUnixInteger());
|
|
67
|
+
const signature = hmacHex(`${now}.{"id":"evt_1"}`);
|
|
68
|
+
assert.isTrue(verify(fakeReq({signature, timestamp: now})));
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("rejects a body-only HMAC when a timestamp header is required", () => {
|
|
72
|
+
const verify = hmacSignature({
|
|
73
|
+
header: "X-Webhook-Signature",
|
|
74
|
+
secret: SECRET,
|
|
75
|
+
timestampHeader: "X-Webhook-Timestamp",
|
|
76
|
+
toleranceSec: 300,
|
|
77
|
+
});
|
|
78
|
+
const now = String(DateTime.utc().toUnixInteger());
|
|
79
|
+
assert.isFalse(verify(fakeReq({signature: VALID_HEX, timestamp: now})));
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("rejects a missing raw body", () => {
|
|
83
|
+
const verify = hmacSignature({header: "X-Webhook-Signature", secret: SECRET});
|
|
84
|
+
const req = {headers: {"x-webhook-signature": VALID_HEX}} as Request;
|
|
85
|
+
assert.isFalse(verify(req));
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("rejects a missing signature", () => {
|
|
89
|
+
const verify = hmacSignature({header: "X-Webhook-Signature", secret: SECRET});
|
|
90
|
+
assert.isFalse(verify(fakeReq({})));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("rejects a missing timestamp when timestampHeader is set", () => {
|
|
94
|
+
const verify = hmacSignature({
|
|
95
|
+
header: "X-Webhook-Signature",
|
|
96
|
+
secret: SECRET,
|
|
97
|
+
timestampHeader: "X-Webhook-Timestamp",
|
|
98
|
+
});
|
|
99
|
+
assert.isFalse(verify(fakeReq({signature: VALID_HEX})));
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("rejects a non-numeric timestamp", () => {
|
|
103
|
+
const verify = hmacSignature({
|
|
104
|
+
header: "X-Webhook-Signature",
|
|
105
|
+
secret: SECRET,
|
|
106
|
+
timestampHeader: "X-Webhook-Timestamp",
|
|
107
|
+
});
|
|
108
|
+
assert.isFalse(verify(fakeReq({signature: VALID_HEX, timestamp: "not-a-number"})));
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import type {Request} from "express";
|
|
3
|
+
import {DateTime} from "luxon";
|
|
4
|
+
|
|
5
|
+
import {headerValue} from "../headerValue";
|
|
6
|
+
import {timingSafeEqualUtf8} from "../timingSafeEqual";
|
|
7
|
+
|
|
8
|
+
export interface HmacSignatureOptions {
|
|
9
|
+
algorithm?: "sha256" | "sha1" | "sha512";
|
|
10
|
+
encoding?: "hex" | "base64";
|
|
11
|
+
header: string;
|
|
12
|
+
secret: string;
|
|
13
|
+
timestampHeader?: string;
|
|
14
|
+
toleranceSec?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Verifies `header` against HMAC of `req.rawBody`.
|
|
19
|
+
* When `timestampHeader` is set, the MAC is HMAC(`${timestamp}.${rawBody}`) and
|
|
20
|
+
* timestamps outside `toleranceSec` (default 300) are rejected.
|
|
21
|
+
*/
|
|
22
|
+
export const hmacSignature = (options: HmacSignatureOptions): ((req: Request) => boolean) => {
|
|
23
|
+
const algorithm = options.algorithm ?? "sha256";
|
|
24
|
+
const encoding = options.encoding ?? "hex";
|
|
25
|
+
const toleranceSec = options.toleranceSec ?? 300;
|
|
26
|
+
|
|
27
|
+
return (req: Request): boolean => {
|
|
28
|
+
const provided = headerValue(req, options.header);
|
|
29
|
+
if (!provided) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (!req.rawBody) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const mac = crypto.createHmac(algorithm, options.secret);
|
|
36
|
+
if (options.timestampHeader) {
|
|
37
|
+
const timestampRaw = headerValue(req, options.timestampHeader);
|
|
38
|
+
if (!timestampRaw) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
const timestamp = Number(timestampRaw);
|
|
42
|
+
if (!Number.isFinite(timestamp)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const nowSec = DateTime.utc().toUnixInteger();
|
|
46
|
+
if (Math.abs(nowSec - timestamp) > toleranceSec) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
mac.update(`${timestampRaw}.`);
|
|
50
|
+
}
|
|
51
|
+
const expected = mac.update(req.rawBody).digest(encoding);
|
|
52
|
+
return timingSafeEqualUtf8({expected, provided});
|
|
53
|
+
};
|
|
54
|
+
};
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
import type {Application, Request} from "express";
|
|
5
|
+
import {DateTime} from "luxon";
|
|
6
|
+
import supertest from "supertest";
|
|
7
|
+
|
|
8
|
+
import type {UserModel as AuthUserModel} from "../../auth";
|
|
9
|
+
import {TerrenoApp} from "../../terrenoApp";
|
|
10
|
+
import {UserModel} from "../../tests";
|
|
11
|
+
import {WebhooksApp} from "../webhooksApp";
|
|
12
|
+
import {sendgridEventSignature} from "./sendgrid";
|
|
13
|
+
import {stripeSignature} from "./stripe";
|
|
14
|
+
import {twilioSignature} from "./twilio";
|
|
15
|
+
|
|
16
|
+
const STRIPE_SECRET = "whsec_stripe_test";
|
|
17
|
+
const STRIPE_BODY = `{"id":"evt_stripe_1"}`;
|
|
18
|
+
const TWILIO_TOKEN = "twilio_auth_token";
|
|
19
|
+
const TWILIO_URL = "https://api.example.com/comms/webhooks/twilio/status";
|
|
20
|
+
const TWILIO_BODY = "MessageSid=SM123&MessageStatus=delivered";
|
|
21
|
+
|
|
22
|
+
const buildApp = ({
|
|
23
|
+
path,
|
|
24
|
+
source,
|
|
25
|
+
verify,
|
|
26
|
+
}: {
|
|
27
|
+
path: string;
|
|
28
|
+
source: string;
|
|
29
|
+
verify: (req: Request) => boolean;
|
|
30
|
+
}): Application => {
|
|
31
|
+
const webhooks = new WebhooksApp({idempotency: {store: "memory"}});
|
|
32
|
+
webhooks.route({
|
|
33
|
+
eventId: () => "evt",
|
|
34
|
+
handler: () => undefined,
|
|
35
|
+
path,
|
|
36
|
+
source,
|
|
37
|
+
verify,
|
|
38
|
+
});
|
|
39
|
+
return new TerrenoApp({
|
|
40
|
+
logRequests: false,
|
|
41
|
+
skipListen: true,
|
|
42
|
+
userModel: UserModel as unknown as AuthUserModel,
|
|
43
|
+
})
|
|
44
|
+
.register(webhooks)
|
|
45
|
+
.build();
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const stripeHeader = ({payload, timestamp}: {payload: string; timestamp: number}): string => {
|
|
49
|
+
const v1 = crypto
|
|
50
|
+
.createHmac("sha256", STRIPE_SECRET)
|
|
51
|
+
.update(`${timestamp}.${payload}`)
|
|
52
|
+
.digest("hex");
|
|
53
|
+
return `t=${timestamp},v1=${v1}`;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const twilioHeader = ({body, url}: {body: string; url: string}): string => {
|
|
57
|
+
const params = Object.fromEntries(new URLSearchParams(body));
|
|
58
|
+
const keys = Object.keys(params).sort();
|
|
59
|
+
let data = url;
|
|
60
|
+
for (const key of keys) {
|
|
61
|
+
data += key + params[key];
|
|
62
|
+
}
|
|
63
|
+
return crypto.createHmac("sha1", TWILIO_TOKEN).update(data, "utf8").digest("base64");
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
describe("stripeSignature", () => {
|
|
67
|
+
it("accepts a fixture inside the timestamp window", async () => {
|
|
68
|
+
const app = buildApp({
|
|
69
|
+
path: "/billing/webhooks/stripe",
|
|
70
|
+
source: "stripe",
|
|
71
|
+
verify: stripeSignature({secret: STRIPE_SECRET}),
|
|
72
|
+
});
|
|
73
|
+
const timestamp = DateTime.utc().toUnixInteger();
|
|
74
|
+
const res = await supertest(app)
|
|
75
|
+
.post("/billing/webhooks/stripe")
|
|
76
|
+
.set("Content-Type", "application/json")
|
|
77
|
+
.set("Stripe-Signature", stripeHeader({payload: STRIPE_BODY, timestamp}))
|
|
78
|
+
.send(STRIPE_BODY);
|
|
79
|
+
|
|
80
|
+
assert.equal(res.status, 200);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("rejects a timestamp outside the tolerance window", async () => {
|
|
84
|
+
const app = buildApp({
|
|
85
|
+
path: "/billing/webhooks/stripe",
|
|
86
|
+
source: "stripe",
|
|
87
|
+
verify: stripeSignature({secret: STRIPE_SECRET, toleranceSec: 300}),
|
|
88
|
+
});
|
|
89
|
+
const timestamp = DateTime.utc().toUnixInteger() - 301;
|
|
90
|
+
const res = await supertest(app)
|
|
91
|
+
.post("/billing/webhooks/stripe")
|
|
92
|
+
.set("Content-Type", "application/json")
|
|
93
|
+
.set("Stripe-Signature", stripeHeader({payload: STRIPE_BODY, timestamp}))
|
|
94
|
+
.send(STRIPE_BODY);
|
|
95
|
+
|
|
96
|
+
assert.equal(res.status, 401);
|
|
97
|
+
assert.equal(res.body.code, "webhook-signature-invalid");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("rejects a mutated payload", async () => {
|
|
101
|
+
const app = buildApp({
|
|
102
|
+
path: "/billing/webhooks/stripe",
|
|
103
|
+
source: "stripe",
|
|
104
|
+
verify: stripeSignature({secret: STRIPE_SECRET}),
|
|
105
|
+
});
|
|
106
|
+
const timestamp = DateTime.utc().toUnixInteger();
|
|
107
|
+
const res = await supertest(app)
|
|
108
|
+
.post("/billing/webhooks/stripe")
|
|
109
|
+
.set("Content-Type", "application/json")
|
|
110
|
+
.set("Stripe-Signature", stripeHeader({payload: STRIPE_BODY, timestamp}))
|
|
111
|
+
.send(`{"id":"evt_mutated"}`);
|
|
112
|
+
|
|
113
|
+
assert.equal(res.status, 401);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("twilioSignature", () => {
|
|
118
|
+
it("accepts a form-urlencoded fixture", async () => {
|
|
119
|
+
const app = buildApp({
|
|
120
|
+
path: "/comms/webhooks/twilio/status",
|
|
121
|
+
source: "twilio",
|
|
122
|
+
verify: twilioSignature({authToken: TWILIO_TOKEN, url: TWILIO_URL}),
|
|
123
|
+
});
|
|
124
|
+
const res = await supertest(app)
|
|
125
|
+
.post("/comms/webhooks/twilio/status")
|
|
126
|
+
.set("Content-Type", "application/x-www-form-urlencoded")
|
|
127
|
+
.set("X-Twilio-Signature", twilioHeader({body: TWILIO_BODY, url: TWILIO_URL}))
|
|
128
|
+
.send(TWILIO_BODY);
|
|
129
|
+
|
|
130
|
+
assert.equal(res.status, 200);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("rejects JSON on the Twilio route", async () => {
|
|
134
|
+
const app = buildApp({
|
|
135
|
+
path: "/comms/webhooks/twilio/status",
|
|
136
|
+
source: "twilio",
|
|
137
|
+
verify: twilioSignature({authToken: TWILIO_TOKEN, url: TWILIO_URL}),
|
|
138
|
+
});
|
|
139
|
+
const jsonBody = `{"MessageSid":"SM123","MessageStatus":"delivered"}`;
|
|
140
|
+
const res = await supertest(app)
|
|
141
|
+
.post("/comms/webhooks/twilio/status")
|
|
142
|
+
.set("Content-Type", "application/json")
|
|
143
|
+
.set("X-Twilio-Signature", twilioHeader({body: TWILIO_BODY, url: TWILIO_URL}))
|
|
144
|
+
.send(jsonBody);
|
|
145
|
+
|
|
146
|
+
assert.equal(res.status, 401);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("rejects a mutated form body", async () => {
|
|
150
|
+
const app = buildApp({
|
|
151
|
+
path: "/comms/webhooks/twilio/status",
|
|
152
|
+
source: "twilio",
|
|
153
|
+
verify: twilioSignature({authToken: TWILIO_TOKEN, url: TWILIO_URL}),
|
|
154
|
+
});
|
|
155
|
+
const res = await supertest(app)
|
|
156
|
+
.post("/comms/webhooks/twilio/status")
|
|
157
|
+
.set("Content-Type", "application/x-www-form-urlencoded")
|
|
158
|
+
.set("X-Twilio-Signature", twilioHeader({body: TWILIO_BODY, url: TWILIO_URL}))
|
|
159
|
+
.send("MessageSid=SM123&MessageStatus=failed");
|
|
160
|
+
|
|
161
|
+
assert.equal(res.status, 401);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("resolves the callback url from a function", async () => {
|
|
165
|
+
const app = buildApp({
|
|
166
|
+
path: "/comms/webhooks/twilio/status",
|
|
167
|
+
source: "twilio",
|
|
168
|
+
verify: twilioSignature({
|
|
169
|
+
authToken: TWILIO_TOKEN,
|
|
170
|
+
url: (req) => `https://api.example.com${req.originalUrl}`,
|
|
171
|
+
}),
|
|
172
|
+
});
|
|
173
|
+
const res = await supertest(app)
|
|
174
|
+
.post("/comms/webhooks/twilio/status")
|
|
175
|
+
.set("Content-Type", "application/x-www-form-urlencoded")
|
|
176
|
+
.set(
|
|
177
|
+
"X-Twilio-Signature",
|
|
178
|
+
twilioHeader({
|
|
179
|
+
body: TWILIO_BODY,
|
|
180
|
+
url: "https://api.example.com/comms/webhooks/twilio/status",
|
|
181
|
+
})
|
|
182
|
+
)
|
|
183
|
+
.send(TWILIO_BODY);
|
|
184
|
+
|
|
185
|
+
assert.equal(res.status, 200);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("rejects when no callback url is configured", async () => {
|
|
189
|
+
const app = buildApp({
|
|
190
|
+
path: "/comms/webhooks/twilio/status",
|
|
191
|
+
source: "twilio",
|
|
192
|
+
verify: twilioSignature({authToken: TWILIO_TOKEN}),
|
|
193
|
+
});
|
|
194
|
+
const res = await supertest(app)
|
|
195
|
+
.post("/comms/webhooks/twilio/status")
|
|
196
|
+
.set("Content-Type", "application/x-www-form-urlencoded")
|
|
197
|
+
.set("X-Twilio-Signature", twilioHeader({body: TWILIO_BODY, url: TWILIO_URL}))
|
|
198
|
+
.send(TWILIO_BODY);
|
|
199
|
+
|
|
200
|
+
assert.equal(res.status, 401);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("rejects when an empty url is configured", async () => {
|
|
204
|
+
const app = buildApp({
|
|
205
|
+
path: "/comms/webhooks/twilio/status",
|
|
206
|
+
source: "twilio",
|
|
207
|
+
verify: twilioSignature({authToken: TWILIO_TOKEN, url: ""}),
|
|
208
|
+
});
|
|
209
|
+
const res = await supertest(app)
|
|
210
|
+
.post("/comms/webhooks/twilio/status")
|
|
211
|
+
.set("Content-Type", "application/x-www-form-urlencoded")
|
|
212
|
+
.set("X-Twilio-Signature", twilioHeader({body: TWILIO_BODY, url: TWILIO_URL}))
|
|
213
|
+
.send(TWILIO_BODY);
|
|
214
|
+
|
|
215
|
+
assert.equal(res.status, 401);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("rejects a missing signature header", async () => {
|
|
219
|
+
const app = buildApp({
|
|
220
|
+
path: "/comms/webhooks/twilio/status",
|
|
221
|
+
source: "twilio",
|
|
222
|
+
verify: twilioSignature({authToken: TWILIO_TOKEN, url: TWILIO_URL}),
|
|
223
|
+
});
|
|
224
|
+
const res = await supertest(app)
|
|
225
|
+
.post("/comms/webhooks/twilio/status")
|
|
226
|
+
.set("Content-Type", "application/x-www-form-urlencoded")
|
|
227
|
+
.send(TWILIO_BODY);
|
|
228
|
+
|
|
229
|
+
assert.equal(res.status, 401);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("rejects repeated form fields that do not decode to strings", async () => {
|
|
233
|
+
const app = buildApp({
|
|
234
|
+
path: "/comms/webhooks/twilio/status",
|
|
235
|
+
source: "twilio",
|
|
236
|
+
verify: twilioSignature({authToken: TWILIO_TOKEN, url: TWILIO_URL}),
|
|
237
|
+
});
|
|
238
|
+
const body = "MessageSid=SM123&MessageSid=SM456";
|
|
239
|
+
const res = await supertest(app)
|
|
240
|
+
.post("/comms/webhooks/twilio/status")
|
|
241
|
+
.set("Content-Type", "application/x-www-form-urlencoded")
|
|
242
|
+
.set("X-Twilio-Signature", twilioHeader({body, url: TWILIO_URL}))
|
|
243
|
+
.send(body);
|
|
244
|
+
|
|
245
|
+
assert.equal(res.status, 401);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe("sendgridEventSignature", () => {
|
|
250
|
+
it("accepts an ECDSA fixture and rejects a mutated payload", async () => {
|
|
251
|
+
const {publicKey, privateKey} = crypto.generateKeyPairSync("ec", {
|
|
252
|
+
namedCurve: "prime256v1",
|
|
253
|
+
});
|
|
254
|
+
const pem = publicKey.export({format: "pem", type: "spki"}).toString();
|
|
255
|
+
const app = buildApp({
|
|
256
|
+
path: "/comms/webhooks/sendgrid",
|
|
257
|
+
source: "sendgrid",
|
|
258
|
+
verify: sendgridEventSignature({publicKey: pem}),
|
|
259
|
+
});
|
|
260
|
+
const payload = `[{"sg_event_id":"sg_1","event":"delivered"}]`;
|
|
261
|
+
const timestamp = String(DateTime.utc().toUnixInteger());
|
|
262
|
+
const signer = crypto.createSign("SHA256");
|
|
263
|
+
signer.update(timestamp);
|
|
264
|
+
signer.update(payload);
|
|
265
|
+
signer.end();
|
|
266
|
+
const signature = signer.sign(privateKey, "base64");
|
|
267
|
+
|
|
268
|
+
const accepted = await supertest(app)
|
|
269
|
+
.post("/comms/webhooks/sendgrid")
|
|
270
|
+
.set("Content-Type", "application/json")
|
|
271
|
+
.set("X-Twilio-Email-Event-Webhook-Timestamp", timestamp)
|
|
272
|
+
.set("X-Twilio-Email-Event-Webhook-Signature", signature)
|
|
273
|
+
.send(payload);
|
|
274
|
+
assert.equal(accepted.status, 200);
|
|
275
|
+
|
|
276
|
+
const rejected = await supertest(app)
|
|
277
|
+
.post("/comms/webhooks/sendgrid")
|
|
278
|
+
.set("Content-Type", "application/json")
|
|
279
|
+
.set("X-Twilio-Email-Event-Webhook-Timestamp", timestamp)
|
|
280
|
+
.set("X-Twilio-Email-Event-Webhook-Signature", signature)
|
|
281
|
+
.send(`[{"sg_event_id":"sg_1","event":"bounce"}]`);
|
|
282
|
+
assert.equal(rejected.status, 401);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("accepts a raw public key without PEM headers", async () => {
|
|
286
|
+
const {publicKey, privateKey} = crypto.generateKeyPairSync("ec", {
|
|
287
|
+
namedCurve: "prime256v1",
|
|
288
|
+
});
|
|
289
|
+
const raw = publicKey
|
|
290
|
+
.export({format: "pem", type: "spki"})
|
|
291
|
+
.toString()
|
|
292
|
+
.replace("-----BEGIN PUBLIC KEY-----", "")
|
|
293
|
+
.replace("-----END PUBLIC KEY-----", "")
|
|
294
|
+
.replace(/\s+/g, "");
|
|
295
|
+
const app = buildApp({
|
|
296
|
+
path: "/comms/webhooks/sendgrid",
|
|
297
|
+
source: "sendgrid",
|
|
298
|
+
verify: sendgridEventSignature({publicKey: raw}),
|
|
299
|
+
});
|
|
300
|
+
const payload = `[{"sg_event_id":"sg_raw","event":"delivered"}]`;
|
|
301
|
+
const timestamp = String(DateTime.utc().toUnixInteger());
|
|
302
|
+
const signer = crypto.createSign("SHA256");
|
|
303
|
+
signer.update(timestamp);
|
|
304
|
+
signer.update(payload);
|
|
305
|
+
signer.end();
|
|
306
|
+
const signature = signer.sign(privateKey, "base64");
|
|
307
|
+
const res = await supertest(app)
|
|
308
|
+
.post("/comms/webhooks/sendgrid")
|
|
309
|
+
.set("Content-Type", "application/json")
|
|
310
|
+
.set("X-Twilio-Email-Event-Webhook-Timestamp", timestamp)
|
|
311
|
+
.set("X-Twilio-Email-Event-Webhook-Signature", signature)
|
|
312
|
+
.send(payload);
|
|
313
|
+
assert.equal(res.status, 200);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("rejects a missing timestamp header", async () => {
|
|
317
|
+
const {publicKey} = crypto.generateKeyPairSync("ec", {namedCurve: "prime256v1"});
|
|
318
|
+
const pem = publicKey.export({format: "pem", type: "spki"}).toString();
|
|
319
|
+
const app = buildApp({
|
|
320
|
+
path: "/comms/webhooks/sendgrid",
|
|
321
|
+
source: "sendgrid",
|
|
322
|
+
verify: sendgridEventSignature({publicKey: pem}),
|
|
323
|
+
});
|
|
324
|
+
const res = await supertest(app)
|
|
325
|
+
.post("/comms/webhooks/sendgrid")
|
|
326
|
+
.set("Content-Type", "application/json")
|
|
327
|
+
.set("X-Twilio-Email-Event-Webhook-Signature", "sig")
|
|
328
|
+
.send(`[{"sg_event_id":"sg_1","event":"delivered"}]`);
|
|
329
|
+
assert.equal(res.status, 401);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it("rejects a missing signature header", async () => {
|
|
333
|
+
const {publicKey} = crypto.generateKeyPairSync("ec", {namedCurve: "prime256v1"});
|
|
334
|
+
const pem = publicKey.export({format: "pem", type: "spki"}).toString();
|
|
335
|
+
const app = buildApp({
|
|
336
|
+
path: "/comms/webhooks/sendgrid",
|
|
337
|
+
source: "sendgrid",
|
|
338
|
+
verify: sendgridEventSignature({publicKey: pem}),
|
|
339
|
+
});
|
|
340
|
+
const res = await supertest(app)
|
|
341
|
+
.post("/comms/webhooks/sendgrid")
|
|
342
|
+
.set("Content-Type", "application/json")
|
|
343
|
+
.set("X-Twilio-Email-Event-Webhook-Timestamp", String(DateTime.utc().toUnixInteger()))
|
|
344
|
+
.send(`[{"sg_event_id":"sg_1","event":"delivered"}]`);
|
|
345
|
+
assert.equal(res.status, 401);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("rejects a timestamp outside the tolerance window", async () => {
|
|
349
|
+
const {publicKey, privateKey} = crypto.generateKeyPairSync("ec", {
|
|
350
|
+
namedCurve: "prime256v1",
|
|
351
|
+
});
|
|
352
|
+
const pem = publicKey.export({format: "pem", type: "spki"}).toString();
|
|
353
|
+
const app = buildApp({
|
|
354
|
+
path: "/comms/webhooks/sendgrid",
|
|
355
|
+
source: "sendgrid",
|
|
356
|
+
verify: sendgridEventSignature({publicKey: pem, toleranceSec: 300}),
|
|
357
|
+
});
|
|
358
|
+
const payload = `[{"sg_event_id":"sg_stale","event":"delivered"}]`;
|
|
359
|
+
const timestamp = String(DateTime.utc().toUnixInteger() - 301);
|
|
360
|
+
const signer = crypto.createSign("SHA256");
|
|
361
|
+
signer.update(timestamp);
|
|
362
|
+
signer.update(payload);
|
|
363
|
+
signer.end();
|
|
364
|
+
const signature = signer.sign(privateKey, "base64");
|
|
365
|
+
const res = await supertest(app)
|
|
366
|
+
.post("/comms/webhooks/sendgrid")
|
|
367
|
+
.set("Content-Type", "application/json")
|
|
368
|
+
.set("X-Twilio-Email-Event-Webhook-Timestamp", timestamp)
|
|
369
|
+
.set("X-Twilio-Email-Event-Webhook-Signature", signature)
|
|
370
|
+
.send(payload);
|
|
371
|
+
assert.equal(res.status, 401);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("rejects an invalid public key", async () => {
|
|
375
|
+
const app = buildApp({
|
|
376
|
+
path: "/comms/webhooks/sendgrid",
|
|
377
|
+
source: "sendgrid",
|
|
378
|
+
verify: sendgridEventSignature({publicKey: "not-a-key"}),
|
|
379
|
+
});
|
|
380
|
+
const res = await supertest(app)
|
|
381
|
+
.post("/comms/webhooks/sendgrid")
|
|
382
|
+
.set("Content-Type", "application/json")
|
|
383
|
+
.set("X-Twilio-Email-Event-Webhook-Timestamp", String(DateTime.utc().toUnixInteger()))
|
|
384
|
+
.set("X-Twilio-Email-Event-Webhook-Signature", "AAAA")
|
|
385
|
+
.send(`[{"sg_event_id":"sg_1","event":"delivered"}]`);
|
|
386
|
+
assert.equal(res.status, 401);
|
|
387
|
+
});
|
|
388
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import type {Request} from "express";
|
|
3
|
+
import {DateTime} from "luxon";
|
|
4
|
+
|
|
5
|
+
import {headerValue} from "../headerValue";
|
|
6
|
+
|
|
7
|
+
export interface SendgridEventSignatureOptions {
|
|
8
|
+
publicKey: string;
|
|
9
|
+
toleranceSec?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const PEM_HEADER = "-----BEGIN PUBLIC KEY-----";
|
|
13
|
+
const PEM_FOOTER = "-----END PUBLIC KEY-----";
|
|
14
|
+
|
|
15
|
+
const toPem = (publicKey: string): string => {
|
|
16
|
+
const trimmed = publicKey.trim();
|
|
17
|
+
if (trimmed.includes(PEM_HEADER)) {
|
|
18
|
+
return trimmed;
|
|
19
|
+
}
|
|
20
|
+
const body = trimmed.replace(/\s+/g, "");
|
|
21
|
+
const wrapped = body.match(/.{1,64}/g)?.join("\n") ?? body;
|
|
22
|
+
return `${PEM_HEADER}\n${wrapped}\n${PEM_FOOTER}`;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Verifies SendGrid Event Webhook ECDSA P-256 over `${timestamp}${rawBody}`.
|
|
27
|
+
* Timestamps outside `toleranceSec` (default 300) are rejected.
|
|
28
|
+
*/
|
|
29
|
+
export const sendgridEventSignature = (
|
|
30
|
+
options: SendgridEventSignatureOptions
|
|
31
|
+
): ((req: Request) => boolean) => {
|
|
32
|
+
const pem = toPem(options.publicKey);
|
|
33
|
+
const toleranceSec = options.toleranceSec ?? 300;
|
|
34
|
+
|
|
35
|
+
return (req: Request): boolean => {
|
|
36
|
+
const signature = headerValue(req, "X-Twilio-Email-Event-Webhook-Signature");
|
|
37
|
+
const timestampRaw = headerValue(req, "X-Twilio-Email-Event-Webhook-Timestamp");
|
|
38
|
+
if (!signature || !timestampRaw || !req.rawBody) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
const timestamp = Number(timestampRaw);
|
|
42
|
+
if (!Number.isFinite(timestamp)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const nowSec = DateTime.utc().toUnixInteger();
|
|
46
|
+
if (Math.abs(nowSec - timestamp) > toleranceSec) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const verifier = crypto.createVerify("SHA256");
|
|
51
|
+
verifier.update(timestampRaw);
|
|
52
|
+
verifier.update(req.rawBody);
|
|
53
|
+
verifier.end();
|
|
54
|
+
return verifier.verify(pem, signature, "base64");
|
|
55
|
+
} catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import type {Request} from "express";
|
|
3
|
+
import {DateTime} from "luxon";
|
|
4
|
+
|
|
5
|
+
import {headerValue} from "../headerValue";
|
|
6
|
+
import {timingSafeEqualUtf8} from "../timingSafeEqual";
|
|
7
|
+
|
|
8
|
+
export interface StripeSignatureOptions {
|
|
9
|
+
secret: string;
|
|
10
|
+
toleranceSec?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const parseStripeSignature = (header: string): {timestamp: string | undefined; v1: string[]} => {
|
|
14
|
+
let timestamp: string | undefined;
|
|
15
|
+
const v1: string[] = [];
|
|
16
|
+
for (const part of header.split(",")) {
|
|
17
|
+
const separator = part.indexOf("=");
|
|
18
|
+
if (separator <= 0) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const key = part.slice(0, separator).trim();
|
|
22
|
+
const value = part.slice(separator + 1).trim();
|
|
23
|
+
if (key === "t") {
|
|
24
|
+
timestamp = value;
|
|
25
|
+
} else if (key === "v1") {
|
|
26
|
+
v1.push(value);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return {timestamp, v1};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Verifies Stripe `Stripe-Signature` (`t=` / `v1=`) against HMAC-SHA256 of `${t}.${rawBody}`.
|
|
34
|
+
*/
|
|
35
|
+
export const stripeSignature = (options: StripeSignatureOptions): ((req: Request) => boolean) => {
|
|
36
|
+
const toleranceSec = options.toleranceSec ?? 300;
|
|
37
|
+
|
|
38
|
+
return (req: Request): boolean => {
|
|
39
|
+
const header = headerValue(req, "Stripe-Signature");
|
|
40
|
+
if (!header || !req.rawBody) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
const parsed = parseStripeSignature(header);
|
|
44
|
+
if (!parsed.timestamp || parsed.v1.length === 0) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
const timestamp = Number(parsed.timestamp);
|
|
48
|
+
if (!Number.isFinite(timestamp)) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const nowSec = DateTime.utc().toUnixInteger();
|
|
52
|
+
if (Math.abs(nowSec - timestamp) > toleranceSec) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const expected = crypto
|
|
56
|
+
.createHmac("sha256", options.secret)
|
|
57
|
+
.update(`${parsed.timestamp}.`)
|
|
58
|
+
.update(req.rawBody)
|
|
59
|
+
.digest("hex");
|
|
60
|
+
for (const provided of parsed.v1) {
|
|
61
|
+
if (timingSafeEqualUtf8({expected, provided})) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
};
|
|
67
|
+
};
|