urlPolicy.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, it } from 'vitest'
  2. import { isAppUrl, isSafeExternalUrl, isServablePreview } from './urlPolicy'
  3. describe('isSafeExternalUrl', () => {
  4. it('lets web links, steam and mail through', () => {
  5. for (const url of ['https://steamcommunity.com/id/x', 'http://example.org/a?b=c', 'steam://url/CommunityFilePage/1', 'mailto:a@b.c']) {
  6. expect(isSafeExternalUrl(url)).toBe(true)
  7. }
  8. })
  9. it('refuses what would start a program or read a file', () => {
  10. for (const url of ['file:///etc/passwd', 'javascript:alert(1)', 'data:text/html,hi', 'smb://host/share', 'ssh://host', 'vscode://x', 'ftp://host', '', 'not a url', '//host/path']) {
  11. expect(isSafeExternalUrl(url)).toBe(false)
  12. }
  13. })
  14. })
  15. describe('isAppUrl', () => {
  16. const fileApp = 'file:///opt/we/out/renderer/index.html'
  17. it('accepts the app page, with or without a query or hash', () => {
  18. expect(isAppUrl(fileApp, fileApp)).toBe(true)
  19. expect(isAppUrl(`${fileApp}#/library`, fileApp)).toBe(true)
  20. expect(isAppUrl(`${fileApp}?x=1`, fileApp)).toBe(true)
  21. })
  22. it('refuses another file, another scheme and a dropped file', () => {
  23. expect(isAppUrl('file:///etc/passwd', fileApp)).toBe(false)
  24. expect(isAppUrl('file:///opt/we/out/renderer/other.html', fileApp)).toBe(false)
  25. expect(isAppUrl('https://example.org/', fileApp)).toBe(false)
  26. expect(isAppUrl('file:///home/u/Downloads/a%20b.png', fileApp)).toBe(false)
  27. expect(isAppUrl('garbage', fileApp)).toBe(false)
  28. })
  29. it('follows the origin of the dev server', () => {
  30. const dev = 'http://localhost:5173/'
  31. expect(isAppUrl('http://localhost:5173/index.html', dev)).toBe(true)
  32. expect(isAppUrl('http://localhost:5174/', dev)).toBe(false)
  33. expect(isAppUrl('http://evil.example/', dev)).toBe(false)
  34. expect(isAppUrl('file:///opt/we/out/renderer/index.html', dev)).toBe(false)
  35. })
  36. })
  37. describe('isServablePreview', () => {
  38. const roots = ['/mnt/steam/workshop/content/431960', '/home/u/.config/we-manager/thumbnails']
  39. it('serves media inside a root', () => {
  40. expect(isServablePreview('/mnt/steam/workshop/content/431960/123/preview.jpg', roots)).toBe(true)
  41. expect(isServablePreview('/mnt/steam/workshop/content/431960/123/sub/preview.GIF', roots)).toBe(true)
  42. expect(isServablePreview('/home/u/.config/we-manager/thumbnails/123.png', roots)).toBe(true)
  43. })
  44. it('refuses a path outside the roots, however it is spelled', () => {
  45. expect(isServablePreview('/etc/passwd', roots)).toBe(false)
  46. expect(isServablePreview('/home/u/.ssh/id_rsa.png', roots)).toBe(false)
  47. expect(isServablePreview('/mnt/steam/workshop/content/431960/../../../../../etc/shadow.png', roots)).toBe(false)
  48. expect(isServablePreview('/mnt/steam/workshop/content/4319601/x.png', roots)).toBe(false)
  49. expect(isServablePreview('/mnt/steam/workshop/content/431960', roots)).toBe(false)
  50. })
  51. it('refuses what is not a picture or video, even inside a root', () => {
  52. expect(isServablePreview('/mnt/steam/workshop/content/431960/123/project.json', roots)).toBe(false)
  53. expect(isServablePreview('/mnt/steam/workshop/content/431960/123/noextension', roots)).toBe(false)
  54. expect(isServablePreview('/mnt/steam/workshop/content/431960/123/page.html', roots)).toBe(false)
  55. })
  56. it('refuses relative paths and null bytes', () => {
  57. expect(isServablePreview('preview.jpg', roots)).toBe(false)
  58. expect(isServablePreview('/mnt/steam/workshop/content/431960/a\0.png', roots)).toBe(false)
  59. expect(isServablePreview('/mnt/steam/workshop/content/431960/123/preview.jpg', [])).toBe(false)
  60. })
  61. })