|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { get } from '@/tests/helpers/e2etest' |
| 3 | + |
| 4 | +describe('malformed URLs', () => { |
| 5 | + test('blocks URLs with %FF sequences', async () => { |
| 6 | + const res = await get('/en/site-policy/other-site-policies/github-account-%FFqrlkuciqll-policy') |
| 7 | + |
| 8 | + expect(res.statusCode).toBe(400) |
| 9 | + expect(res.headers['content-type']).toMatch('text/plain') |
| 10 | + expect(res.body).toBe('Bad Request: Malformed URL') |
| 11 | + }) |
| 12 | + |
| 13 | + test('blocks URLs with %FE sequences', async () => { |
| 14 | + const res = await get('/en/some-page-%FE-test') |
| 15 | + expect(res.statusCode).toBe(400) |
| 16 | + expect(res.headers['content-type']).toMatch('text/plain') |
| 17 | + expect(res.body).toBe('Bad Request: Malformed URL') |
| 18 | + }) |
| 19 | + |
| 20 | + test('blocks URLs with overlong encoding %C0%80', async () => { |
| 21 | + const res = await get('/en/test-%C0%80-page') |
| 22 | + expect(res.statusCode).toBe(400) |
| 23 | + expect(res.headers['content-type']).toMatch('text/plain') |
| 24 | + expect(res.body).toBe('Bad Request: Malformed URL') |
| 25 | + }) |
| 26 | + |
| 27 | + test('blocks URLs with invalid UTF-8 continuation sequences', async () => { |
| 28 | + const res = await get('/en/test-%80%80-page') |
| 29 | + expect(res.statusCode).toBe(400) |
| 30 | + expect(res.headers['content-type']).toMatch('text/plain') |
| 31 | + expect(res.body).toBe('Bad Request: Malformed URL') |
| 32 | + }) |
| 33 | + |
| 34 | + test('allows URLs with control characters (valid UTF-8)', async () => { |
| 35 | + const res = await get('/en/test-%01-page') |
| 36 | + expect(res.statusCode).toBe(404) // Should be 404 since page doesn't exist, not 400 |
| 37 | + // Control characters like %01 are valid UTF-8 and don't cause decoding errors |
| 38 | + }) |
| 39 | + |
| 40 | + test('allows valid URLs with proper encoding', async () => { |
| 41 | + const res = await get('/en/get-started') |
| 42 | + expect(res.statusCode).not.toBe(400) |
| 43 | + // Should not be blocked by malformed URL middleware |
| 44 | + }) |
| 45 | + |
| 46 | + test('allows valid URLs with proper percent encoding', async () => { |
| 47 | + const res = await get('/en/search?q=test%20query') |
| 48 | + expect(res.statusCode).not.toBe(400) |
| 49 | + // Should not be blocked by malformed URL middleware |
| 50 | + }) |
| 51 | + |
| 52 | + test('blocks malformed query parameters', async () => { |
| 53 | + // This is caught by checking originalUrl which contains the raw, unparsed URL |
| 54 | + const res = await get('/en/search?q=test%FF') |
| 55 | + expect(res.statusCode).toBe(400) |
| 56 | + expect(res.headers['content-type']).toMatch('text/plain') |
| 57 | + expect(res.body).toBe('Bad Request: Malformed URL') |
| 58 | + }) |
| 59 | + |
| 60 | + test('properly caches malformed URL responses', async () => { |
| 61 | + const res = await get('/en/malformed-%FF-url') |
| 62 | + expect(res.statusCode).toBe(400) |
| 63 | + expect(res.headers['cache-control']).toBeDefined() |
| 64 | + }) |
| 65 | + |
| 66 | + test('handles multiple malformed sequences', async () => { |
| 67 | + const res = await get('/en/test-%FF%FE%80-page') |
| 68 | + expect(res.statusCode).toBe(400) |
| 69 | + expect(res.headers['content-type']).toMatch('text/plain') |
| 70 | + expect(res.body).toBe('Bad Request: Malformed URL') |
| 71 | + }) |
| 72 | +}) |
0 commit comments