-
Notifications
You must be signed in to change notification settings - Fork 1
fix(web): stop leaking upstream and Stripe error details to clients #1381
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1adbed6
fix(web): stop leaking upstream and Stripe error details to clients
groupthinking 672bac3
docs(web): correct two inaccurate security comments flagged in review
claude ae2be83
fix(web): return the static 502 when the Realtime upstream is unreach…
claude 5e69b7e
fix(web): complete the error-code contract across the patched routes
claude f5cb509
fix(web): annotate remaining helper return types and cover the Turnst…
claude 090f297
fix(web): stop verifyTurnstileToken rejecting into an unstructured 500
claude e185c4b
fix(web): complete the error-code contract on the transcribe route
claude 36645ad
fix(web): stop the SSRF guard leaking DNS resolver text to the caller
claude 8628b5e
Merge branch 'main' into groupthinking-fix-upstream-error-leakage
groupthinking 020fad8
fix(web): close the last DNS oracle in the SSRF guard
claude e9bf62d
Merge branch 'main' into groupthinking-fix-upstream-error-leakage
groupthinking 18a787a
fix(web): stop /api/transcribe leaking probe status and key config
claude 8d756b6
test: use the repo's standard fixture video ID
claude e511781
fix(web): stop forwarding the SSRF guard's rejection reason
claude 8e22f20
Merge pull request #1440 from claude/clever-heisenberg-cjf3tn
groupthinking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
apps/web/src/app/api/__tests__/billing-activate-route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { describe, it, expect, afterEach, vi } from 'vitest'; | ||
| import { NextRequest } from 'next/server'; | ||
| import { POST } from '@/app/api/billing/activate/route'; | ||
| import { getKaizenTraces, resetKaizenTracesForTests } from '@/lib/billing/kaizen-trace'; | ||
|
|
||
| vi.mock('@/lib/billing/stripe-checkout', () => ({ | ||
| getCheckoutSession: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('@/lib/billing/checkout-session-store', () => ({ | ||
| getCheckoutActivation: vi.fn().mockResolvedValue(null), | ||
| })); | ||
|
|
||
| vi.mock('@/lib/billing/subscription-events', () => ({ | ||
| activateFromCheckoutSession: vi.fn(), | ||
| })); | ||
|
|
||
| import { getCheckoutSession } from '@/lib/billing/stripe-checkout'; | ||
| import { activateFromCheckoutSession } from '@/lib/billing/subscription-events'; | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| resetKaizenTracesForTests(); | ||
| }); | ||
|
|
||
| function activateRequest(sessionId: string): NextRequest { | ||
| return new NextRequest('http://localhost/api/billing/activate', { | ||
| method: 'POST', | ||
| headers: { 'content-type': 'application/json' }, | ||
| body: JSON.stringify({ sessionId }), | ||
| }); | ||
| } | ||
|
|
||
| // This route is on PUBLIC_API_EXACT — unauthenticated, so any internet caller | ||
| // can trigger the failure branch and read the response. | ||
| describe('POST /api/billing/activate error leakage', () => { | ||
| it('never returns raw Stripe SDK error text', async () => { | ||
| const stripeMessage = | ||
| "No such checkout.session: 'cs_live_secret'; Invalid API Key provided: sk_live_****ABCD"; | ||
| vi.mocked(getCheckoutSession).mockRejectedValue(new Error(stripeMessage)); | ||
| const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| vi.spyOn(console, 'info').mockImplementation(() => {}); | ||
|
|
||
| const res = await POST(activateRequest('cs_live_secret')); | ||
| const raw = await res.text(); | ||
|
|
||
| expect(res.status).toBe(500); | ||
| expect(JSON.parse(raw)).toEqual({ error: 'activation_failed', code: 'activation_failed' }); | ||
| for (const token of ['sk_live', 'No such checkout.session', 'Invalid API Key']) { | ||
| expect(raw).not.toContain(token); | ||
| } | ||
|
|
||
| // Raw detail is retained for operators only. | ||
| expect(JSON.stringify(consoleError.mock.calls)).toContain('sk_live_****ABCD'); | ||
| expect(getKaizenTraces('billing').some((t) => t.observation === stripeMessage)).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects a malformed body without echoing parser internals', async () => { | ||
| const req = new NextRequest('http://localhost/api/billing/activate', { | ||
| method: 'POST', | ||
| headers: { 'content-type': 'application/json' }, | ||
| body: '{not json', | ||
| }); | ||
| const res = await POST(req); | ||
| expect(res.status).toBe(400); | ||
| expect(await res.json()).toEqual({ error: 'invalid_json', code: 'invalid_json' }); | ||
| }); | ||
| }); | ||
|
|
||
| // Every error branch must carry a stable machine-readable `code`, not only the | ||
| // two that were sanitized for CWE-209 — otherwise a client cannot branch on the | ||
| // contract this route declares. | ||
| describe('POST /api/billing/activate error-code contract', () => { | ||
| it('returns session_id_required with a matching code', async () => { | ||
| const res = await POST(activateRequest(' ')); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(await res.json()).toEqual({ | ||
| error: 'session_id_required', | ||
| code: 'session_id_required', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns not_eligible with a matching code', async () => { | ||
| vi.mocked(getCheckoutSession).mockResolvedValue({ id: 'cs_free' } as never); | ||
| vi.mocked(activateFromCheckoutSession).mockResolvedValue(null); | ||
| vi.spyOn(console, 'info').mockImplementation(() => {}); | ||
|
|
||
| const res = await POST(activateRequest('cs_free')); | ||
|
|
||
| expect(res.status).toBe(402); | ||
| expect(await res.json()).toEqual({ | ||
| error: 'not_eligible', | ||
| code: 'not_eligible', | ||
| sessionId: 'cs_free', | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
apps/web/src/app/api/__tests__/billing-webhook-route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { describe, it, expect, afterEach, beforeEach, vi } from 'vitest'; | ||
| import { NextRequest } from 'next/server'; | ||
| import { POST } from '@/app/api/billing/webhook/route'; | ||
| import { getKaizenTraces, resetKaizenTracesForTests } from '@/lib/billing/kaizen-trace'; | ||
|
|
||
| const constructEvent = vi.fn(); | ||
|
|
||
| vi.mock('@/lib/billing/stripe-checkout', () => ({ | ||
| getStripeClient: () => ({ webhooks: { constructEvent } }), | ||
| })); | ||
|
|
||
| vi.mock('@/lib/billing/subscription-events', () => ({ | ||
| activateFromCheckoutSession: vi.fn(), | ||
| syncFromSubscription: vi.fn(), | ||
| })); | ||
|
|
||
| import { activateFromCheckoutSession } from '@/lib/billing/subscription-events'; | ||
|
|
||
| const ORIGINAL_SECRET = process.env.STRIPE_WEBHOOK_SECRET; | ||
|
|
||
| beforeEach(() => { | ||
| process.env.STRIPE_WEBHOOK_SECRET = 'whsec_test'; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (ORIGINAL_SECRET === undefined) delete process.env.STRIPE_WEBHOOK_SECRET; | ||
| else process.env.STRIPE_WEBHOOK_SECRET = ORIGINAL_SECRET; | ||
| constructEvent.mockReset(); | ||
| vi.restoreAllMocks(); | ||
| resetKaizenTracesForTests(); | ||
| }); | ||
|
|
||
| function webhookRequest(body: string): NextRequest { | ||
| return new NextRequest('http://localhost/api/billing/webhook', { | ||
| method: 'POST', | ||
| headers: { 'stripe-signature': 't=1,v1=deadbeef' }, | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| // This route is on PUBLIC_API_EXACT — unauthenticated, so any internet caller | ||
| // can trigger both failure branches and read the response. | ||
| describe('POST /api/billing/webhook error leakage', () => { | ||
| it('never returns signature-verification internals', async () => { | ||
| const stripeMessage = | ||
| 'No signatures found matching the expected signature for payload (tolerance 300s, scheme v1, key sk_live_****ABCD)'; | ||
| constructEvent.mockImplementation(() => { | ||
| throw new Error(stripeMessage); | ||
| }); | ||
| const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| vi.spyOn(console, 'info').mockImplementation(() => {}); | ||
|
|
||
| const res = await POST(webhookRequest('{"id":"evt_1"}')); | ||
| const raw = await res.text(); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(JSON.parse(raw)).toEqual({ error: 'invalid_payload', code: 'invalid_payload' }); | ||
| for (const token of ['sk_live', 'tolerance', 'scheme v1', 'No signatures found']) { | ||
| expect(raw).not.toContain(token); | ||
| } | ||
|
|
||
| // Raw detail is retained for operators only. | ||
| expect(JSON.stringify(consoleError.mock.calls)).toContain('sk_live_****ABCD'); | ||
| expect(getKaizenTraces('billing').some((t) => t.observation === stripeMessage)).toBe(true); | ||
| }); | ||
|
|
||
| it('never returns handler internals', async () => { | ||
| const handlerMessage = 'Redis connection refused at 10.0.3.14:6379 for customer cus_live_secret'; | ||
| constructEvent.mockReturnValue({ | ||
| type: 'checkout.session.completed', | ||
| data: { object: { id: 'cs_test_1' } }, | ||
| }); | ||
| vi.mocked(activateFromCheckoutSession).mockRejectedValue(new Error(handlerMessage)); | ||
| const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| vi.spyOn(console, 'info').mockImplementation(() => {}); | ||
|
|
||
| const res = await POST(webhookRequest('{"id":"evt_2"}')); | ||
| const raw = await res.text(); | ||
|
|
||
| expect(res.status).toBe(500); | ||
| expect(JSON.parse(raw)).toEqual({ | ||
| error: 'webhook_handler_failed', | ||
| code: 'webhook_handler_failed', | ||
| }); | ||
| for (const token of ['10.0.3.14', 'cus_live_secret', 'Redis connection refused']) { | ||
| expect(raw).not.toContain(token); | ||
| } | ||
|
|
||
| expect(JSON.stringify(consoleError.mock.calls)).toContain('10.0.3.14:6379'); | ||
| expect(getKaizenTraces('billing').some((t) => t.observation === handlerMessage)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| // The two pre-flight rejections short-circuit before Stripe is ever called, so | ||
| // they never carried leaked text — but they are part of the same error contract | ||
| // and must return a stable `code` like every other branch. | ||
| describe('POST /api/billing/webhook error-code contract', () => { | ||
| it('returns webhook_not_configured with a matching code', async () => { | ||
| delete process.env.STRIPE_WEBHOOK_SECRET; | ||
|
|
||
| const res = await POST(webhookRequest('{"id":"evt_3"}')); | ||
|
|
||
| expect(res.status).toBe(503); | ||
| expect(await res.json()).toEqual({ | ||
| error: 'webhook_not_configured', | ||
| code: 'webhook_not_configured', | ||
| }); | ||
| expect(constructEvent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns missing_signature with a matching code', async () => { | ||
| const req = new NextRequest('http://localhost/api/billing/webhook', { | ||
| method: 'POST', | ||
| body: '{"id":"evt_4"}', | ||
| }); | ||
|
|
||
| const res = await POST(req); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(await res.json()).toEqual({ | ||
| error: 'missing_signature', | ||
| code: 'missing_signature', | ||
| }); | ||
| expect(constructEvent).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.