test.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. var expect = require('chai').expect;
  3. var sanitizeUrl = require('../').sanitizeUrl;
  4. describe('sanitizeUrl', function () {
  5. it('replaces javascript urls with about:blank', function () {
  6. expect(sanitizeUrl('javascript:alert(document.domain)')).to.equal('about:blank');
  7. });
  8. it('disregards capitalization for JavaScript urls', function () {
  9. expect(sanitizeUrl('jAvasCrIPT:alert(document.domain)')).to.equal('about:blank');
  10. });
  11. it('ignores ctrl characters in javascript urls', function () {
  12. expect(sanitizeUrl(decodeURIComponent('JaVaScRiP%0at:alert(document.domain)'))).to.equal('about:blank');
  13. });
  14. it('replaces javascript urls with about:blank when javascript url begins with %20', function () {
  15. expect(sanitizeUrl('%20%20%20%20javascript:alert(document.domain)')).to.equal('about:blank');
  16. });
  17. it('replaces javascript urls with about:blank when javascript url begins with \s', function () {
  18. expect(sanitizeUrl(' javascript:alert(document.domain)')).to.equal('about:blank');
  19. });
  20. it('does not replace javascript: if it is not in the scheme of the URL', function () {
  21. expect(sanitizeUrl('http://example.com#myjavascript:foo')).to.equal('http://example.com#myjavascript:foo');
  22. });
  23. it('replaces data urls with about:blank', function () {
  24. expect(sanitizeUrl('data:text/html;base64,PH%3Cscript%3Ealert(document.domain)%3C/script%3E')).to.equal('about:blank');
  25. });
  26. it('replaces data urls with about:blank when data url begins with %20', function () {
  27. expect(sanitizeUrl('%20%20%20%20data:text/html;base64,PH%3Cscript%3Ealert(document.domain)%3C/script%3E')).to.equal('about:blank');
  28. });
  29. it('replaces data urls with about:blank when data url begins with \s', function () {
  30. expect(sanitizeUrl(' data:text/html;base64,PH%3Cscript%3Ealert(document.domain)%3C/script%3E')).to.equal('about:blank');
  31. });
  32. it('disregards capitalization for data urls', function () {
  33. expect(sanitizeUrl('dAtA:text/html;base64,PH%3Cscript%3Ealert(document.domain)%3C/script%3E')).to.equal('about:blank');
  34. });
  35. it('ignores ctrl characters in data urls', function () {
  36. expect(sanitizeUrl(decodeURIComponent('dat%0aa:text/html;base64,PH%3Cscript%3Ealert(document.domain)%3C/script%3E'))).to.equal('about:blank');
  37. });
  38. it('does not alter http URLs', function () {
  39. expect(sanitizeUrl('http://example.com/path/to:something')).to.equal('http://example.com/path/to:something');
  40. });
  41. it('does not alter http URLs with ports', function () {
  42. expect(sanitizeUrl('http://example.com:4567/path/to:something')).to.equal('http://example.com:4567/path/to:something');
  43. });
  44. it('does not alter https URLs', function () {
  45. expect(sanitizeUrl('https://example.com')).to.equal('https://example.com');
  46. });
  47. it('does not alter https URLs with ports', function () {
  48. expect(sanitizeUrl('https://example.com:4567/path/to:something')).to.equal('https://example.com:4567/path/to:something');
  49. });
  50. it('does not alter relative-path reference URLs', function () {
  51. expect(sanitizeUrl('./path/to/my.json')).to.equal('./path/to/my.json');
  52. });
  53. it('does not alter absolute-path reference URLs', function () {
  54. expect(sanitizeUrl('/path/to/my.json')).to.equal('/path/to/my.json');
  55. });
  56. it('does not alter network-path relative URLs', function () {
  57. expect(sanitizeUrl('//google.com/robots.txt')).to.equal('//google.com/robots.txt');
  58. });
  59. it('does not alter deep-link urls', function () {
  60. expect(sanitizeUrl('com.braintreepayments.demo://example')).to.equal('com.braintreepayments.demo://example');
  61. });
  62. it('does not alter mailto urls', function () {
  63. expect(sanitizeUrl('mailto:test@example.com?subject=hello+world')).to.equal('mailto:test@example.com?subject=hello+world');
  64. });
  65. it('replaces blank urls with about:blank', function () {
  66. expect(sanitizeUrl('')).to.equal('about:blank');
  67. });
  68. it('replaces null values with about:blank', function () {
  69. expect(sanitizeUrl(null)).to.equal('about:blank');
  70. });
  71. it('removes whitespace from urls', function () {
  72. expect(sanitizeUrl(' http://example.com/path/to:something ')).to.equal('http://example.com/path/to:something');
  73. });
  74. });