|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import type { AllowList } from '../uploads/types.js' |
| 4 | + |
| 5 | +import { isURLAllowed } from './isURLAllowed.js' |
| 6 | + |
| 7 | +describe('isURLAllowed', () => { |
| 8 | + describe('hostname matching', () => { |
| 9 | + const allowList: AllowList = [{ hostname: 'cdn.example.com' }] |
| 10 | + |
| 11 | + it('should allow an exactly matching hostname', () => { |
| 12 | + expect(isURLAllowed('https://cdn.example.com/file.png', allowList)).toBe(true) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should deny a different hostname', () => { |
| 16 | + expect(isURLAllowed('https://attacker.com/file.png', allowList)).toBe(false) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should deny a userinfo `@` trick (hostname is the real authority)', () => { |
| 20 | + expect(isURLAllowed('https://cdn.example.com@attacker.com/file.png', allowList)).toBe(false) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should deny an invalid URL', () => { |
| 24 | + expect(isURLAllowed('not a url', allowList)).toBe(false) |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + describe('pathname matching', () => { |
| 29 | + it('should treat a literal dot as a literal, not a wildcard', () => { |
| 30 | + const allowList: AllowList = [{ hostname: 'cdn.example.com', pathname: '/files/report.json' }] |
| 31 | + |
| 32 | + expect(isURLAllowed('https://cdn.example.com/files/report.json', allowList)).toBe(true) |
| 33 | + // Previously the unescaped `.` matched any character, widening the allow-list. |
| 34 | + expect(isURLAllowed('https://cdn.example.com/files/reportXjson', allowList)).toBe(false) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should not let other regex metacharacters broaden the match', () => { |
| 38 | + const allowList: AllowList = [{ hostname: 'cdn.example.com', pathname: '/a+b/(c)' }] |
| 39 | + |
| 40 | + expect(isURLAllowed('https://cdn.example.com/a+b/(c)', allowList)).toBe(true) |
| 41 | + expect(isURLAllowed('https://cdn.example.com/aaab/c', allowList)).toBe(false) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should match a single segment with `*` but not across slashes', () => { |
| 45 | + const allowList: AllowList = [{ hostname: 'cdn.example.com', pathname: '/uploads/*' }] |
| 46 | + |
| 47 | + expect(isURLAllowed('https://cdn.example.com/uploads/photo.png', allowList)).toBe(true) |
| 48 | + expect(isURLAllowed('https://cdn.example.com/uploads/nested/photo.png', allowList)).toBe( |
| 49 | + false, |
| 50 | + ) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should match across slashes with `**`', () => { |
| 54 | + const allowList: AllowList = [{ hostname: 'cdn.example.com', pathname: '/uploads/**' }] |
| 55 | + |
| 56 | + expect(isURLAllowed('https://cdn.example.com/uploads/photo.png', allowList)).toBe(true) |
| 57 | + expect(isURLAllowed('https://cdn.example.com/uploads/nested/photo.png', allowList)).toBe(true) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should allow an optional trailing slash', () => { |
| 61 | + const allowList: AllowList = [{ hostname: 'cdn.example.com', pathname: '/assets/' }] |
| 62 | + |
| 63 | + expect(isURLAllowed('https://cdn.example.com/assets', allowList)).toBe(true) |
| 64 | + expect(isURLAllowed('https://cdn.example.com/assets/', allowList)).toBe(true) |
| 65 | + }) |
| 66 | + }) |
| 67 | +}) |
0 commit comments