Skip to content

Commit 04478db

Browse files
committed
fix(api): add CSP header to generic error SVGs
1 parent 99c2907 commit 04478db

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

app/api/streak/route.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,17 @@ describe('GET /api/streak', () => {
670670
expect(response.headers.get('Cache-Control')).toBe('no-store');
671671
});
672672

673+
it('sets the SVG Content-Security-Policy header on generic error responses', async () => {
674+
vi.mocked(fetchGitHubContributions).mockRejectedValue(new Error('Network failure'));
675+
676+
const response = await GET(makeRequest({ user: 'octocat' }));
677+
const csp = response.headers.get('Content-Security-Policy');
678+
679+
expect(response.status).toBe(500);
680+
expect(csp).toContain("default-src 'none'");
681+
expect(csp).not.toContain('script-src');
682+
});
683+
673684
it('returns 429 with no-cache headers and rate limit SVG when rate limited', async () => {
674685
vi.mocked(fetchGitHubContributions).mockRejectedValue(new Error('API Rate Limit Exceeded'));
675686

app/api/streak/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ function buildErrorResponse(error: unknown, parseResult: ParseResult): NextRespo
336336
headers: {
337337
'Content-Type': 'image/svg+xml',
338338
'Cache-Control': 'no-store',
339+
'Content-Security-Policy': SVG_CSP_HEADER,
339340
},
340341
});
341342
}

0 commit comments

Comments
 (0)