get-return-to-path.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { describe, expect, it } from 'vitest'
  2. import { DEFAULT_FALLBACK_PATH, getReturnToPath } from '@/lib/gotrue'
  3. describe(`getReturnToPath`, () => {
  4. it(`returns to ${DEFAULT_FALLBACK_PATH} when no fallback is provided`, () => {
  5. expect(getReturnToPath()).toBe(DEFAULT_FALLBACK_PATH)
  6. })
  7. it(`returns to /custom when fallback is provided`, () => {
  8. expect(getReturnToPath('/custom')).toBe('/custom')
  9. })
  10. it(`returns to /custom`, () => {
  11. // @ts-ignore
  12. delete window.location
  13. // @ts-ignore
  14. window.location = { search: `?returnTo=/custom` }
  15. expect(getReturnToPath()).toBe('/custom')
  16. })
  17. it(`returns to /custom?foo=bar`, () => {
  18. // @ts-ignore
  19. delete window.location
  20. // @ts-ignore
  21. window.location = { search: `?returnTo=/custom?foo=bar` }
  22. expect(getReturnToPath()).toBe('/custom?foo=bar')
  23. })
  24. it(`does not return to https://google.com`, () => {
  25. // @ts-ignore
  26. delete window.location
  27. // @ts-ignore
  28. window.location = { search: `?returnTo=https://google.com` }
  29. expect(getReturnToPath()).toBe(DEFAULT_FALLBACK_PATH)
  30. })
  31. it(`does not allow XSS`, () => {
  32. // @ts-ignore
  33. delete window.location
  34. // @ts-ignore
  35. window.location = { search: `?returnTo=javascript:alert(1)` }
  36. expect(getReturnToPath()).toBe(DEFAULT_FALLBACK_PATH)
  37. })
  38. it(`does not allow XSS with encoded characters`, () => {
  39. // @ts-ignore
  40. delete window.location
  41. // @ts-ignore
  42. window.location = { search: `?returnTo=javascript%3Aalert%281%29` }
  43. expect(getReturnToPath()).toBe(DEFAULT_FALLBACK_PATH)
  44. })
  45. })