UrlHelperTests.js 1.3 KB

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