Repository navigation
feat(sdk): webhook receiver hardening — auth, rate limit, provider-shape (#304) #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||
| /** | ||||||
| * In-process token-bucket rate limiter for the local webhook receiver (#304). | ||||||
| * | ||||||
| * Slice E's receiver binds loopback and is documented as development-only, so | ||||||
| * a single-process in-memory bucket is sufficient. Cluster deployments must | ||||||
| * layer a shared store in a follow-up slice; the taxonomy stays the same | ||||||
| * (`webhook_rate_limited`) so client behavior does not change. | ||||||
| */ | ||||||
| export interface RateLimitConfig { | ||||||
| /** Tokens replenished per second. */ | ||||||
| readonly ratePerSecond: number; | ||||||
| /** Maximum burst capacity. */ | ||||||
| readonly burst: number; | ||||||
| } | ||||||
|
|
||||||
| export interface RateLimitDecision { | ||||||
| readonly allowed: boolean; | ||||||
| readonly retryAfterMs: number; | ||||||
| } | ||||||
|
|
||||||
| interface Bucket { | ||||||
| tokens: number; | ||||||
| refilledAtMs: number; | ||||||
| } | ||||||
|
|
||||||
| export class TokenBucketLimiter { | ||||||
| private readonly buckets = new Map<string, Bucket>(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Every unique route/source key remains in Prompt for AI agents |
||||||
|
|
||||||
| constructor( | ||||||
| private readonly config: RateLimitConfig, | ||||||
| private readonly clock: () => number = Date.now, | ||||||
| ) { | ||||||
| if (config.burst <= 0 || config.ratePerSecond < 0) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: When a caller supplies Prompt for AI agents |
||||||
| throw new Error('rate limiter: burst > 0 and ratePerSecond >= 0 required'); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| consume(key: string, cost = 1): RateLimitDecision { | ||||||
| const now = this.clock(); | ||||||
| const bucket = this.buckets.get(key) ?? { tokens: this.config.burst, refilledAtMs: now }; | ||||||
| const elapsedSeconds = Math.max(0, (now - bucket.refilledAtMs) / 1000); | ||||||
| bucket.tokens = Math.min(this.config.burst, bucket.tokens + elapsedSeconds * this.config.ratePerSecond); | ||||||
| bucket.refilledAtMs = now; | ||||||
| if (bucket.tokens >= cost) { | ||||||
| bucket.tokens -= cost; | ||||||
| this.buckets.set(key, bucket); | ||||||
| return { allowed: true, retryAfterMs: 0 }; | ||||||
| } | ||||||
| this.buckets.set(key, bucket); | ||||||
| const missingTokens = cost - bucket.tokens; | ||||||
| const retryAfterMs = this.config.ratePerSecond > 0 | ||||||
| ? Math.ceil((missingTokens / this.config.ratePerSecond) * 1000) | ||||||
| : Number.POSITIVE_INFINITY; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zero rate emits invalid Retry-AfterLow Severity When Additional Locations (1)Reviewed by Cursor Bugbot for commit 4fe4f6e. Configure here. |
||||||
| return { allowed: false, retryAfterMs }; | ||||||
| } | ||||||
|
|
||||||
| /** Test/inspection helper — production code should only call {@link consume}. */ | ||||||
| size(): number { | ||||||
| return this.buckets.size; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export function keyFor( | ||||||
| provider: string | undefined, | ||||||
| name: string, | ||||||
| sourceAddress: string, | ||||||
| ): string { | ||||||
| return `${provider ?? 'raw'}:${name}:${sourceAddress}`; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: When a generic Prompt for AI agents
Suggested change
|
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { createHmac, timingSafeEqual } from 'node:crypto'; | ||
|
|
||
| /** | ||
| * Per-provider HMAC signature schemes for webhook payload authentication (#304). | ||
| * | ||
| * The receiver only invokes verification when a secret is configured for the | ||
| * provider (via `env[WEBHOOK_SECRET_<PROVIDER>]`). When no secret is set the | ||
| * receiver runs unsigned, matching the initial loopback-only development mode | ||
| * documented in #301. Callers deploying to public ingress MUST set the secret. | ||
| */ | ||
| export type SignatureScheme = { | ||
| header: string; | ||
| compute(rawBody: Buffer, secret: string): string; | ||
| }; | ||
|
|
||
| const HEX = /^[0-9a-f]+$/i; | ||
|
|
||
| const github: SignatureScheme = { | ||
| header: 'x-hub-signature-256', | ||
| compute(rawBody, secret) { | ||
| return 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex'); | ||
| }, | ||
| }; | ||
|
|
||
| const slack: SignatureScheme = { | ||
| header: 'x-slack-signature', | ||
| compute(rawBody, secret) { | ||
| // Slack's v0 scheme is HMAC(SHA256, secret) over `v0:{ts}:{body}`; a | ||
| // deployment that wants Slack signature verification must set the secret | ||
| // to include the timestamp prefix or use provider-specific integration. | ||
| return 'v0=' + createHmac('sha256', secret).update(rawBody).digest('hex'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: When Prompt for AI agents |
||
| }, | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slack signature scheme is incorrectMedium Severity The Additional Locations (1)Reviewed by Cursor Bugbot for commit 4fe4f6e. Configure here. |
||
|
|
||
| const generic: SignatureScheme = { | ||
| header: 'x-flows-signature-256', | ||
| compute(rawBody, secret) { | ||
| return 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex'); | ||
| }, | ||
| }; | ||
|
|
||
| export const SIGNATURE_SCHEMES: Record<string, SignatureScheme> = { | ||
| github, | ||
| slack, | ||
| }; | ||
|
|
||
| export function schemeFor(provider: string | undefined): SignatureScheme { | ||
| if (provider && Object.hasOwn(SIGNATURE_SCHEMES, provider)) { | ||
| return SIGNATURE_SCHEMES[provider]!; | ||
| } | ||
| return generic; | ||
| } | ||
|
|
||
| /** Constant-time verification of a hex-encoded signature against the expected. */ | ||
| export function verifySignature( | ||
| provider: string | undefined, | ||
| rawBody: Buffer, | ||
| headerValue: string | undefined, | ||
| secret: string, | ||
| ): { ok: true } | { ok: false; reason: 'missing' | 'malformed' | 'mismatch' } { | ||
| const scheme = schemeFor(provider); | ||
| const supplied = headerValue?.trim(); | ||
| if (!supplied) return { ok: false, reason: 'missing' }; | ||
| const expected = scheme.compute(rawBody, secret); | ||
| if (supplied.length !== expected.length) return { ok: false, reason: 'malformed' }; | ||
| const [suppliedHex, expectedHex] = [supplied.replace(/^[^=]+=/, ''), expected.replace(/^[^=]+=/, '')]; | ||
| if (!HEX.test(suppliedHex) || suppliedHex.length !== expectedHex.length) { | ||
| return { ok: false, reason: 'malformed' }; | ||
| } | ||
| const suppliedBytes = Buffer.from(suppliedHex, 'hex'); | ||
| const expectedBytes = Buffer.from(expectedHex, 'hex'); | ||
| if (suppliedBytes.length !== expectedBytes.length) return { ok: false, reason: 'malformed' }; | ||
| return timingSafeEqual(suppliedBytes, expectedBytes) ? { ok: true } : { ok: false, reason: 'mismatch' }; | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: When
ratePerSecondis zero, this emitsRetry-After: InfinityandretryAfterMs: null, neither of which is valid retry metadata. Omit the retry header and field for a non-refilling bucket, or define a finite retry policy.Prompt for AI agents