csp.test.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { describe, expect, it } from 'vitest'
  2. import { RENDERER_CSP } from './csp'
  3. function directives(policy: string): Map<string, string[]> {
  4. return new Map(
  5. policy.split(';').map((part) => {
  6. const [name, ...sources] = part.trim().split(/\s+/)
  7. return [name, sources]
  8. })
  9. )
  10. }
  11. describe('RENDERER_CSP', () => {
  12. const policy = directives(RENDERER_CSP)
  13. it('runs only the bundled scripts', () => {
  14. expect(policy.get('script-src')).toEqual(["'self'"])
  15. expect(RENDERER_CSP).not.toMatch(/unsafe-eval|unsafe-hashes|\*(?!\.)/)
  16. })
  17. it('blocks plugins, frames and form posts', () => {
  18. expect(policy.get('object-src')).toEqual(["'none'"])
  19. expect(policy.get('frame-src')).toEqual(["'none'"])
  20. expect(policy.get('form-action')).toEqual(["'none'"])
  21. expect(policy.get('base-uri')).toEqual(["'none'"])
  22. })
  23. it('lets previews load from the app scheme and from Steam', () => {
  24. expect(policy.get('img-src')).toEqual(expect.arrayContaining(['wallpaper:', 'https:', 'data:']))
  25. expect(policy.get('media-src')).toContain('wallpaper:')
  26. expect(policy.get('connect-src')).toContain('wallpaper:')
  27. })
  28. it('has no repeated directive', () => {
  29. const names = RENDERER_CSP.split(';').map((part) => part.trim().split(/\s+/)[0])
  30. expect(new Set(names).size).toBe(names.length)
  31. })
  32. })