|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { renderSSRHead } from '../../../src/server' |
| 3 | +import { createServerHeadWithContext } from '../../util' |
| 4 | + |
| 5 | +describe('prototype pollution', () => { |
| 6 | + it('strips __proto__ from meta props', async () => { |
| 7 | + const head = createServerHeadWithContext() |
| 8 | + head.push({ |
| 9 | + meta: [ |
| 10 | + JSON.parse('{"name":"description","content":"safe","__proto__":{"polluted":true}}'), |
| 11 | + ], |
| 12 | + }) |
| 13 | + await renderSSRHead(head) |
| 14 | + expect(({} as any).polluted).toBeUndefined() |
| 15 | + }) |
| 16 | + |
| 17 | + it('strips constructor from meta props', async () => { |
| 18 | + const head = createServerHeadWithContext() |
| 19 | + head.push({ |
| 20 | + meta: [ |
| 21 | + { name: 'description', content: 'safe', constructor: { prototype: { polluted: true } } } as any, |
| 22 | + ], |
| 23 | + }) |
| 24 | + await renderSSRHead(head) |
| 25 | + expect(({} as any).polluted).toBeUndefined() |
| 26 | + }) |
| 27 | + |
| 28 | + it('strips prototype from meta props', async () => { |
| 29 | + const head = createServerHeadWithContext() |
| 30 | + head.push({ |
| 31 | + meta: [ |
| 32 | + { name: 'description', content: 'safe', prototype: { polluted: true } } as any, |
| 33 | + ], |
| 34 | + }) |
| 35 | + await renderSSRHead(head) |
| 36 | + expect(({} as any).polluted).toBeUndefined() |
| 37 | + }) |
| 38 | + |
| 39 | + it('strips __proto__ from htmlAttrs', async () => { |
| 40 | + const head = createServerHeadWithContext() |
| 41 | + head.push({ |
| 42 | + htmlAttrs: JSON.parse('{"lang":"en","__proto__":{"polluted":true}}'), |
| 43 | + }) |
| 44 | + await renderSSRHead(head) |
| 45 | + expect(({} as any).polluted).toBeUndefined() |
| 46 | + }) |
| 47 | + |
| 48 | + it('strips __proto__ from bodyAttrs', async () => { |
| 49 | + const head = createServerHeadWithContext() |
| 50 | + head.push({ |
| 51 | + bodyAttrs: JSON.parse('{"class":"dark","__proto__":{"polluted":true}}'), |
| 52 | + }) |
| 53 | + await renderSSRHead(head) |
| 54 | + expect(({} as any).polluted).toBeUndefined() |
| 55 | + }) |
| 56 | + |
| 57 | + it('strips __proto__ from script props', async () => { |
| 58 | + const head = createServerHeadWithContext() |
| 59 | + head.push({ |
| 60 | + script: [ |
| 61 | + JSON.parse('{"src":"https://example.com/app.js","__proto__":{"polluted":true}}'), |
| 62 | + ], |
| 63 | + }) |
| 64 | + await renderSSRHead(head) |
| 65 | + expect(({} as any).polluted).toBeUndefined() |
| 66 | + }) |
| 67 | + |
| 68 | + it('strips __proto__ from link props', async () => { |
| 69 | + const head = createServerHeadWithContext() |
| 70 | + head.push({ |
| 71 | + link: [ |
| 72 | + JSON.parse('{"rel":"stylesheet","href":"/style.css","__proto__":{"polluted":true}}'), |
| 73 | + ], |
| 74 | + }) |
| 75 | + await renderSSRHead(head) |
| 76 | + expect(({} as any).polluted).toBeUndefined() |
| 77 | + }) |
| 78 | + |
| 79 | + it('preserves valid props while stripping dangerous keys', async () => { |
| 80 | + const head = createServerHeadWithContext() |
| 81 | + head.push({ |
| 82 | + meta: [ |
| 83 | + JSON.parse('{"name":"description","content":"hello","__proto__":{"polluted":true}}'), |
| 84 | + ], |
| 85 | + }) |
| 86 | + const ctx = await renderSSRHead(head) |
| 87 | + expect(ctx.headTags).toContain('name="description"') |
| 88 | + expect(ctx.headTags).toContain('content="hello"') |
| 89 | + expect(ctx.headTags).not.toContain('__proto__') |
| 90 | + expect(ctx.headTags).not.toContain('polluted') |
| 91 | + }) |
| 92 | + |
| 93 | + it('strips nested __proto__ in style objects', async () => { |
| 94 | + const head = createServerHeadWithContext() |
| 95 | + head.push({ |
| 96 | + htmlAttrs: { |
| 97 | + style: JSON.parse('{"color":"red","__proto__":{"polluted":true}}'), |
| 98 | + } as any, |
| 99 | + }) |
| 100 | + await renderSSRHead(head) |
| 101 | + expect(({} as any).polluted).toBeUndefined() |
| 102 | + }) |
| 103 | +}) |
0 commit comments