UrlHelper.test.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { vi, expect } from 'vitest'
  2. import path from 'node:path'
  3. const modulePath = path.join(
  4. import.meta.dirname,
  5. '../../../../app/src/Features/Helpers/UrlHelper.mjs'
  6. )
  7. describe('UrlHelper', function () {
  8. beforeEach(async function (ctx) {
  9. ctx.settings = {
  10. apis: { linkedUrlProxy: { url: undefined } },
  11. siteUrl: 'http://127.0.0.1:3000',
  12. }
  13. vi.doMock('@overleaf/settings', () => ({
  14. default: ctx.settings,
  15. }))
  16. ctx.UrlHelper = (await import(modulePath)).default
  17. })
  18. describe('getSafeRedirectPath', function () {
  19. it('sanitize redirect path to prevent open redirects', function (ctx) {
  20. expect(ctx.UrlHelper.getSafeRedirectPath('https://evil.com')).to.be
  21. .undefined
  22. expect(ctx.UrlHelper.getSafeRedirectPath('//evil.com')).to.be.undefined
  23. expect(ctx.UrlHelper.getSafeRedirectPath('//ol.com/evil')).to.equal(
  24. '/evil'
  25. )
  26. expect(ctx.UrlHelper.getSafeRedirectPath('////evil.com')).to.be.undefined
  27. expect(ctx.UrlHelper.getSafeRedirectPath('%2F%2Fevil.com')).to.equal(
  28. '/%2F%2Fevil.com'
  29. )
  30. expect(
  31. ctx.UrlHelper.getSafeRedirectPath('http://foo.com//evil.com/bad')
  32. ).to.equal('/evil.com/bad')
  33. return expect(ctx.UrlHelper.getSafeRedirectPath('.evil.com')).to.equal(
  34. '/.evil.com'
  35. )
  36. })
  37. })
  38. })