@open-mercato/webhooks 0.6.6-develop.6431.1.g1037269810 → 0.6.6-develop.6451.1.5b071f47fc
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/dist/modules/webhooks/api/inbound/[endpointId]/route.js +6 -1
- package/dist/modules/webhooks/api/inbound/[endpointId]/route.js.map +2 -2
- package/package.json +6 -6
- package/src/modules/webhooks/api/inbound/[endpointId]/__tests__/route.test.ts +25 -1
- package/src/modules/webhooks/api/inbound/[endpointId]/route.ts +6 -1
|
@@ -30,7 +30,7 @@ async function POST(request, context) {
|
|
|
30
30
|
const rateLimitResponse = await checkRateLimit(
|
|
31
31
|
rateLimiterService,
|
|
32
32
|
{ points: 60, duration: 60, keyPrefix: `webhooks:inbound:${params.endpointId}` },
|
|
33
|
-
|
|
33
|
+
buildInboundRateLimitKey(params.endpointId, request, rateLimiterService.trustProxyDepth),
|
|
34
34
|
translate(RATE_LIMIT_ERROR_KEY, RATE_LIMIT_ERROR_FALLBACK)
|
|
35
35
|
);
|
|
36
36
|
if (rateLimitResponse) return rateLimitResponse;
|
|
@@ -122,6 +122,10 @@ function isUniqueViolation(error) {
|
|
|
122
122
|
if (!maybeError.cause || typeof maybeError.cause !== "object") return false;
|
|
123
123
|
return maybeError.cause.code === "23505";
|
|
124
124
|
}
|
|
125
|
+
function buildInboundRateLimitKey(endpointId, request, trustProxyDepth) {
|
|
126
|
+
const clientIp = getClientIp(request, trustProxyDepth);
|
|
127
|
+
return clientIp ? `${endpointId}:ip:${clientIp}` : `${endpointId}:global`;
|
|
128
|
+
}
|
|
125
129
|
function resolveInboundReceiptMessageId(input) {
|
|
126
130
|
const explicitMessageId = input.headers["webhook-id"] ?? input.headers["svix-id"] ?? null;
|
|
127
131
|
if (typeof explicitMessageId === "string" && explicitMessageId.trim().length > 0) {
|
|
@@ -137,6 +141,7 @@ function resolveInboundReceiptMessageId(input) {
|
|
|
137
141
|
}
|
|
138
142
|
export {
|
|
139
143
|
POST,
|
|
144
|
+
buildInboundRateLimitKey,
|
|
140
145
|
metadata,
|
|
141
146
|
openApi,
|
|
142
147
|
resolveInboundReceiptMessageId
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../../src/modules/webhooks/api/inbound/%5BendpointId%5D/route.ts"],
|
|
4
|
-
"sourcesContent": ["import { z } from 'zod'\nimport { createHash } from 'node:crypto'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { checkRateLimit, getClientIp, RATE_LIMIT_ERROR_FALLBACK, RATE_LIMIT_ERROR_KEY } from '@open-mercato/shared/lib/ratelimit/helpers'\nimport type { RateLimiterService } from '@open-mercato/shared/lib/ratelimit/service'\nimport { emitWebhooksEvent } from '../../../events'\nimport { getWebhookEndpointAdapter } from '../../../lib/adapter-registry'\nimport { isWebhookIntegrationEnabled } from '../../../lib/integration-state'\nimport { json } from '../../helpers'\nimport { WebhookInboundReceiptEntity } from '../../../data/entities'\n\nexport const metadata = {\n POST: { requireAuth: false },\n}\n\ninterface RouteContext {\n params: Promise<{ endpointId: string }>\n}\n\nconst inboundResponseSchema = z.object({\n ok: z.boolean(),\n duplicate: z.boolean().optional(),\n})\n\nconst errorSchema = z.object({ error: z.string() })\n\nexport async function POST(request: Request, context: RouteContext): Promise<Response> {\n const params = await context.params\n const adapter = getWebhookEndpointAdapter(params.endpointId)\n const { translate } = await resolveTranslations()\n\n if (!adapter) {\n return json({ error: 'Webhook endpoint not found' }, { status: 404 })\n }\n\n const container = await createRequestContainer()\n const em = (container.resolve('em') as EntityManager).fork()\n const rateLimiterService = tryResolve<RateLimiterService>(container, 'rateLimiterService')\n\n if (rateLimiterService) {\n const rateLimitResponse = await checkRateLimit(\n rateLimiterService,\n { points: 60, duration: 60, keyPrefix: `webhooks:inbound:${params.endpointId}` },\n
|
|
5
|
-
"mappings": "AAAA,SAAS,SAAS;AAClB,SAAS,kBAAkB;AAG3B,SAAS,8BAA8B;AACvC,SAAS,2BAA2B;AACpC,SAAS,gBAAgB,aAAa,2BAA2B,4BAA4B;AAE7F,SAAS,yBAAyB;AAClC,SAAS,iCAAiC;AAC1C,SAAS,mCAAmC;AAC5C,SAAS,YAAY;AACrB,SAAS,mCAAmC;AAErC,MAAM,WAAW;AAAA,EACtB,MAAM,EAAE,aAAa,MAAM;AAC7B;AAMA,MAAM,wBAAwB,EAAE,OAAO;AAAA,EACrC,IAAI,EAAE,QAAQ;AAAA,EACd,WAAW,EAAE,QAAQ,EAAE,SAAS;AAClC,CAAC;AAED,MAAM,cAAc,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAElD,eAAsB,KAAK,SAAkB,SAA0C;AACrF,QAAM,SAAS,MAAM,QAAQ;AAC7B,QAAM,UAAU,0BAA0B,OAAO,UAAU;AAC3D,QAAM,EAAE,UAAU,IAAI,MAAM,oBAAoB;AAEhD,MAAI,CAAC,SAAS;AACZ,WAAO,KAAK,EAAE,OAAO,6BAA6B,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,EACtE;AAEA,QAAM,YAAY,MAAM,uBAAuB;AAC/C,QAAM,KAAM,UAAU,QAAQ,IAAI,EAAoB,KAAK;AAC3D,QAAM,qBAAqB,WAA+B,WAAW,oBAAoB;AAEzF,MAAI,oBAAoB;AACtB,UAAM,oBAAoB,MAAM;AAAA,MAC9B;AAAA,MACA,EAAE,QAAQ,IAAI,UAAU,IAAI,WAAW,oBAAoB,OAAO,UAAU,GAAG;AAAA,MAC/E,
|
|
4
|
+
"sourcesContent": ["import { z } from 'zod'\nimport { createHash } from 'node:crypto'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { checkRateLimit, getClientIp, RATE_LIMIT_ERROR_FALLBACK, RATE_LIMIT_ERROR_KEY } from '@open-mercato/shared/lib/ratelimit/helpers'\nimport type { RateLimiterService } from '@open-mercato/shared/lib/ratelimit/service'\nimport { emitWebhooksEvent } from '../../../events'\nimport { getWebhookEndpointAdapter } from '../../../lib/adapter-registry'\nimport { isWebhookIntegrationEnabled } from '../../../lib/integration-state'\nimport { json } from '../../helpers'\nimport { WebhookInboundReceiptEntity } from '../../../data/entities'\n\nexport const metadata = {\n POST: { requireAuth: false },\n}\n\ninterface RouteContext {\n params: Promise<{ endpointId: string }>\n}\n\nconst inboundResponseSchema = z.object({\n ok: z.boolean(),\n duplicate: z.boolean().optional(),\n})\n\nconst errorSchema = z.object({ error: z.string() })\n\nexport async function POST(request: Request, context: RouteContext): Promise<Response> {\n const params = await context.params\n const adapter = getWebhookEndpointAdapter(params.endpointId)\n const { translate } = await resolveTranslations()\n\n if (!adapter) {\n return json({ error: 'Webhook endpoint not found' }, { status: 404 })\n }\n\n const container = await createRequestContainer()\n const em = (container.resolve('em') as EntityManager).fork()\n const rateLimiterService = tryResolve<RateLimiterService>(container, 'rateLimiterService')\n\n if (rateLimiterService) {\n const rateLimitResponse = await checkRateLimit(\n rateLimiterService,\n { points: 60, duration: 60, keyPrefix: `webhooks:inbound:${params.endpointId}` },\n buildInboundRateLimitKey(params.endpointId, request, rateLimiterService.trustProxyDepth),\n translate(RATE_LIMIT_ERROR_KEY, RATE_LIMIT_ERROR_FALLBACK),\n )\n\n if (rateLimitResponse) return rateLimitResponse\n }\n\n const body = await request.text()\n const headers = Object.fromEntries(request.headers.entries())\n let verified: Awaited<ReturnType<typeof adapter.verifyWebhook>>\n try {\n verified = await adapter.verifyWebhook({\n headers,\n body,\n method: request.method,\n })\n } catch {\n return json({ error: 'Verification failed' }, { status: 400 })\n }\n\n if (verified.tenantId && verified.organizationId) {\n const integrationEnabled = await isWebhookIntegrationEnabled(em, {\n tenantId: verified.tenantId,\n organizationId: verified.organizationId,\n })\n\n if (!integrationEnabled) {\n return json({ error: 'Custom Webhooks integration is disabled' }, { status: 503 })\n }\n }\n\n const messageId = resolveInboundReceiptMessageId({\n endpointId: params.endpointId,\n providerKey: adapter.providerKey,\n headers,\n body,\n })\n try {\n em.persist(em.create(WebhookInboundReceiptEntity, {\n endpointId: params.endpointId,\n messageId,\n providerKey: adapter.providerKey,\n eventType: verified.eventType,\n tenantId: verified.tenantId ?? null,\n organizationId: verified.organizationId ?? null,\n createdAt: new Date(),\n }))\n await em.flush()\n } catch (error) {\n if (isUniqueViolation(error)) {\n return json({ ok: true, duplicate: true })\n }\n throw error\n }\n\n await emitWebhooksEvent('webhooks.inbound.received', {\n providerKey: adapter.providerKey,\n endpointId: params.endpointId,\n messageId,\n eventType: verified.eventType,\n payload: verified.payload,\n tenantId: verified.tenantId ?? null,\n organizationId: verified.organizationId ?? null,\n }, { persistent: true })\n\n return json({ ok: true })\n}\n\nexport const openApi: OpenApiRouteDoc = {\n summary: 'Receive inbound webhook',\n description: 'Verifies, rate limits, deduplicates, and processes an inbound webhook using the registered endpoint adapter.',\n methods: {\n POST: {\n summary: 'Receive inbound webhook',\n description: 'Endpoint ids currently resolve to registered adapter provider keys.',\n pathParams: z.object({ endpointId: z.string().min(1) }),\n responses: [{ status: 200, description: 'Inbound webhook accepted', schema: inboundResponseSchema }],\n errors: [\n { status: 400, description: 'Verification failed', schema: errorSchema },\n { status: 404, description: 'Endpoint not found', schema: errorSchema },\n { status: 429, description: 'Rate limit exceeded', schema: errorSchema },\n { status: 503, description: 'Webhook integration disabled', schema: errorSchema },\n ],\n },\n },\n}\n\nfunction tryResolve<T>(container: { resolve: (name: string) => unknown }, name: string): T | null {\n try {\n return container.resolve(name) as T\n } catch {\n return null\n }\n}\n\nfunction isUniqueViolation(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false\n const maybeError = error as { code?: string; cause?: unknown }\n if (maybeError.code === '23505') return true\n if (!maybeError.cause || typeof maybeError.cause !== 'object') return false\n return (maybeError.cause as { code?: string }).code === '23505'\n}\n\ntype ResolveInboundReceiptMessageIdInput = {\n endpointId: string\n providerKey: string\n headers: Record<string, string>\n body: string\n}\n\nexport function buildInboundRateLimitKey(endpointId: string, request: Request, trustProxyDepth: number): string {\n const clientIp = getClientIp(request, trustProxyDepth)\n return clientIp ? `${endpointId}:ip:${clientIp}` : `${endpointId}:global`\n}\n\nexport function resolveInboundReceiptMessageId(\n input: ResolveInboundReceiptMessageIdInput\n): string {\n const explicitMessageId = input.headers['webhook-id'] ?? input.headers['svix-id'] ?? null\n if (typeof explicitMessageId === 'string' && explicitMessageId.trim().length > 0) {\n return explicitMessageId.trim()\n }\n\n const timestamp =\n input.headers['webhook-timestamp'] ??\n input.headers['svix-timestamp'] ??\n null\n\n if (typeof timestamp === 'string' && timestamp.trim().length > 0) {\n const digest = createHash('sha256')\n .update(input.providerKey)\n .update(':')\n .update(input.endpointId)\n .update(':')\n .update(timestamp.trim())\n .update(':')\n .update(input.body)\n .digest('hex')\n\n return `derived:${timestamp.trim()}:${digest}`\n }\n\n const digest = createHash('sha256')\n .update(input.providerKey)\n .update(':')\n .update(input.endpointId)\n .update(':')\n .update(input.body)\n .digest('hex')\n\n return `derived:no-timestamp:${digest}`\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,SAAS;AAClB,SAAS,kBAAkB;AAG3B,SAAS,8BAA8B;AACvC,SAAS,2BAA2B;AACpC,SAAS,gBAAgB,aAAa,2BAA2B,4BAA4B;AAE7F,SAAS,yBAAyB;AAClC,SAAS,iCAAiC;AAC1C,SAAS,mCAAmC;AAC5C,SAAS,YAAY;AACrB,SAAS,mCAAmC;AAErC,MAAM,WAAW;AAAA,EACtB,MAAM,EAAE,aAAa,MAAM;AAC7B;AAMA,MAAM,wBAAwB,EAAE,OAAO;AAAA,EACrC,IAAI,EAAE,QAAQ;AAAA,EAC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UAAU,EACvB,OAAO,GAAG,EACV,OAAO,UAAU,KAAK,CAAC,EACvB,OAAO,GAAG,EACV,OAAO,MAAM,IAAI,EACjB,OAAO,KAAK;AAEf,WAAO,WAAW,UAAU,KAAK,CAAC,IAAIA,OAAM;AAAA,EAC9C;AAEA,QAAM,SAAS,WAAW,QAAQ,EAC/B,OAAO,MAAM,WAAW,EACxB,OAAO,GAAG,EACV,OAAO,MAAM,UAAU,EACvB,OAAO,GAAG,EACV,OAAO,MAAM,IAAI,EACjB,OAAO,KAAK;AAEf,SAAO,wBAAwB,MAAM;AACvC;",
|
|
6
6
|
"names": ["digest"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/webhooks",
|
|
3
|
-
"version": "0.6.6-develop.
|
|
3
|
+
"version": "0.6.6-develop.6451.1.5b071f47fc",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Webhooks module for Open Mercato — Standard Webhooks compliant outbound/inbound delivery",
|
|
6
6
|
"type": "module",
|
|
@@ -70,19 +70,19 @@
|
|
|
70
70
|
}
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@open-mercato/core": "0.6.6-develop.
|
|
74
|
-
"@open-mercato/queue": "0.6.6-develop.
|
|
75
|
-
"@open-mercato/ui": "0.6.6-develop.
|
|
73
|
+
"@open-mercato/core": "0.6.6-develop.6451.1.5b071f47fc",
|
|
74
|
+
"@open-mercato/queue": "0.6.6-develop.6451.1.5b071f47fc",
|
|
75
|
+
"@open-mercato/ui": "0.6.6-develop.6451.1.5b071f47fc",
|
|
76
76
|
"svix": "^1.96.1"
|
|
77
77
|
},
|
|
78
78
|
"peerDependencies": {
|
|
79
79
|
"@mikro-orm/postgresql": "^7.0.14",
|
|
80
|
-
"@open-mercato/shared": "0.6.6-develop.
|
|
80
|
+
"@open-mercato/shared": "0.6.6-develop.6451.1.5b071f47fc",
|
|
81
81
|
"react": "^19.0.0",
|
|
82
82
|
"react-dom": "^19.0.0"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@open-mercato/shared": "0.6.6-develop.
|
|
85
|
+
"@open-mercato/shared": "0.6.6-develop.6451.1.5b071f47fc",
|
|
86
86
|
"@types/jest": "^30.0.0",
|
|
87
87
|
"@types/react": "^19.2.17",
|
|
88
88
|
"@types/react-dom": "^19.2.3",
|
|
@@ -1,4 +1,28 @@
|
|
|
1
|
-
import { resolveInboundReceiptMessageId } from '../route'
|
|
1
|
+
import { buildInboundRateLimitKey, resolveInboundReceiptMessageId } from '../route'
|
|
2
|
+
|
|
3
|
+
describe('buildInboundRateLimitKey', () => {
|
|
4
|
+
it('uses an endpoint-global bucket when proxy headers are untrusted', () => {
|
|
5
|
+
const request = new Request('http://localhost/api/webhooks/inbound/mock', {
|
|
6
|
+
headers: {
|
|
7
|
+
'x-forwarded-for': '203.0.113.1',
|
|
8
|
+
'x-real-ip': '198.51.100.10',
|
|
9
|
+
},
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
expect(buildInboundRateLimitKey('mock_inbound', request, 0)).toBe('mock_inbound:global')
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('uses a trusted proxy-derived IP when trust depth is configured', () => {
|
|
16
|
+
const request = new Request('http://localhost/api/webhooks/inbound/mock', {
|
|
17
|
+
headers: {
|
|
18
|
+
'x-forwarded-for': '203.0.113.1, 198.51.100.10',
|
|
19
|
+
'x-real-ip': '192.0.2.5',
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
expect(buildInboundRateLimitKey('mock_inbound', request, 1)).toBe('mock_inbound:ip:198.51.100.10')
|
|
24
|
+
})
|
|
25
|
+
})
|
|
2
26
|
|
|
3
27
|
describe('resolveInboundReceiptMessageId', () => {
|
|
4
28
|
it('prefers explicit webhook ids when present', () => {
|
|
@@ -44,7 +44,7 @@ export async function POST(request: Request, context: RouteContext): Promise<Res
|
|
|
44
44
|
const rateLimitResponse = await checkRateLimit(
|
|
45
45
|
rateLimiterService,
|
|
46
46
|
{ points: 60, duration: 60, keyPrefix: `webhooks:inbound:${params.endpointId}` },
|
|
47
|
-
|
|
47
|
+
buildInboundRateLimitKey(params.endpointId, request, rateLimiterService.trustProxyDepth),
|
|
48
48
|
translate(RATE_LIMIT_ERROR_KEY, RATE_LIMIT_ERROR_FALLBACK),
|
|
49
49
|
)
|
|
50
50
|
|
|
@@ -154,6 +154,11 @@ type ResolveInboundReceiptMessageIdInput = {
|
|
|
154
154
|
body: string
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
+
export function buildInboundRateLimitKey(endpointId: string, request: Request, trustProxyDepth: number): string {
|
|
158
|
+
const clientIp = getClientIp(request, trustProxyDepth)
|
|
159
|
+
return clientIp ? `${endpointId}:ip:${clientIp}` : `${endpointId}:global`
|
|
160
|
+
}
|
|
161
|
+
|
|
157
162
|
export function resolveInboundReceiptMessageId(
|
|
158
163
|
input: ResolveInboundReceiptMessageIdInput
|
|
159
164
|
): string {
|