safe-redirect.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* eslint-disable arrow-body-style */
  2. describe('safeRedirect', () => {
  3. let safeRedirect;
  4. const whitelistOfHosts = [
  5. 'white1.example.com:8080',
  6. 'white2.example.com',
  7. ];
  8. beforeEach(async(done) => {
  9. safeRedirect = require('@server/middleware/safe-redirect')(whitelistOfHosts);
  10. done();
  11. });
  12. describe('res.safeRedirect', () => {
  13. // setup req/res/next
  14. const req = {
  15. protocol: 'http',
  16. hostname: 'example.com',
  17. get: jest.fn().mockReturnValue('example.com'),
  18. };
  19. const res = {
  20. redirect: jest.fn().mockReturnValue('redirect'),
  21. };
  22. const next = jest.fn();
  23. test('redirects to \'/\' because specified url causes open redirect vulnerability', () => {
  24. safeRedirect(req, res, next);
  25. const result = res.safeRedirect('//evil.example.com');
  26. expect(next).toHaveBeenCalledTimes(1);
  27. expect(req.get).toHaveBeenCalledTimes(1);
  28. expect(req.get).toHaveBeenCalledWith('host');
  29. expect(res.redirect).toHaveBeenCalledTimes(1);
  30. expect(res.redirect).toHaveBeenCalledWith('/');
  31. expect(result).toBe('redirect');
  32. });
  33. test('redirects to \'/\' because specified host without port is not in whitelist', () => {
  34. safeRedirect(req, res, next);
  35. const result = res.safeRedirect('http://white1.example.com/path/to/page');
  36. expect(next).toHaveBeenCalledTimes(1);
  37. expect(req.get).toHaveBeenCalledTimes(1);
  38. expect(req.get).toHaveBeenCalledWith('host');
  39. expect(res.redirect).toHaveBeenCalledTimes(1);
  40. expect(res.redirect).toHaveBeenCalledWith('/');
  41. expect(result).toBe('redirect');
  42. });
  43. test('redirects to the specified local url', () => {
  44. safeRedirect(req, res, next);
  45. const result = res.safeRedirect('/path/to/page');
  46. expect(next).toHaveBeenCalledTimes(1);
  47. expect(req.get).toHaveBeenCalledTimes(1);
  48. expect(req.get).toHaveBeenCalledWith('host');
  49. expect(res.redirect).toHaveBeenCalledTimes(1);
  50. expect(res.redirect).toHaveBeenCalledWith('http://example.com/path/to/page');
  51. expect(result).toBe('redirect');
  52. });
  53. test('redirects to the specified local url (fqdn)', () => {
  54. safeRedirect(req, res, next);
  55. const result = res.safeRedirect('http://example.com/path/to/page');
  56. expect(next).toHaveBeenCalledTimes(1);
  57. expect(req.get).toHaveBeenCalledTimes(1);
  58. expect(req.get).toHaveBeenCalledWith('host');
  59. expect(res.redirect).toHaveBeenCalledTimes(1);
  60. expect(res.redirect).toHaveBeenCalledWith('http://example.com/path/to/page');
  61. expect(result).toBe('redirect');
  62. });
  63. test('redirects to the specified whitelisted url (white1.example.com:8080)', () => {
  64. safeRedirect(req, res, next);
  65. const result = res.safeRedirect('http://white1.example.com:8080/path/to/page');
  66. expect(next).toHaveBeenCalledTimes(1);
  67. expect(req.get).toHaveBeenCalledTimes(1);
  68. expect(req.get).toHaveBeenCalledWith('host');
  69. expect(res.redirect).toHaveBeenCalledTimes(1);
  70. expect(res.redirect).toHaveBeenCalledWith('http://white1.example.com:8080/path/to/page');
  71. expect(result).toBe('redirect');
  72. });
  73. test('redirects to the specified whitelisted url (white2.example.com:8080)', () => {
  74. safeRedirect(req, res, next);
  75. const result = res.safeRedirect('http://white2.example.com:8080/path/to/page');
  76. expect(next).toHaveBeenCalledTimes(1);
  77. expect(req.get).toHaveBeenCalledTimes(1);
  78. expect(req.get).toHaveBeenCalledWith('host');
  79. expect(res.redirect).toHaveBeenCalledTimes(1);
  80. expect(res.redirect).toHaveBeenCalledWith('http://white2.example.com:8080/path/to/page');
  81. expect(result).toBe('redirect');
  82. });
  83. });
  84. });