UrlHelperTests.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { expect } = require('chai')
  2. const SandboxedModule = require('sandboxed-module')
  3. const modulePath = require('path').join(
  4. __dirname,
  5. '../../../../app/src/Features/Helpers/UrlHelper.js'
  6. )
  7. describe('UrlHelper', function () {
  8. beforeEach(function () {
  9. this.settings = {
  10. apis: { linkedUrlProxy: { url: undefined } },
  11. siteUrl: 'http://127.0.0.1:3000',
  12. }
  13. this.UrlHelper = SandboxedModule.require(modulePath, {
  14. requires: { '@overleaf/settings': this.settings },
  15. })
  16. })
  17. describe('getSafeRedirectPath', function () {
  18. it('sanitize redirect path to prevent open redirects', function () {
  19. expect(this.UrlHelper.getSafeRedirectPath('https://evil.com')).to.be
  20. .undefined
  21. expect(this.UrlHelper.getSafeRedirectPath('//evil.com')).to.be.undefined
  22. expect(this.UrlHelper.getSafeRedirectPath('//ol.com/evil')).to.equal(
  23. '/evil'
  24. )
  25. expect(this.UrlHelper.getSafeRedirectPath('////evil.com')).to.be.undefined
  26. expect(this.UrlHelper.getSafeRedirectPath('%2F%2Fevil.com')).to.equal(
  27. '/%2F%2Fevil.com'
  28. )
  29. expect(
  30. this.UrlHelper.getSafeRedirectPath('http://foo.com//evil.com/bad')
  31. ).to.equal('/evil.com/bad')
  32. return expect(this.UrlHelper.getSafeRedirectPath('.evil.com')).to.equal(
  33. '/.evil.com'
  34. )
  35. })
  36. })
  37. })