http-agent.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const nock = require('nock')
  2. const { FORBIDDEN_IP_ADDRESS } = require('../../src/server/helpers/request')
  3. const { getProtectedGot } = require('../../src/server/helpers/request')
  4. afterAll(() => {
  5. nock.cleanAll()
  6. nock.restore()
  7. })
  8. describe('test protected request Agent', () => {
  9. test('allows URLs without IP addresses', async () => {
  10. nock('https://transloadit.com').get('/').reply(200)
  11. const url = 'https://transloadit.com'
  12. return (await getProtectedGot({ allowLocalIPs: false })).get(url)
  13. })
  14. test('blocks url that resolves to forbidden IP', async () => {
  15. const url = 'https://localhost'
  16. const promise = getProtectedGot({ allowLocalIPs: false }).then(got => got.get(url))
  17. await expect(promise).rejects.toThrow(/^Forbidden resolved IP address/)
  18. })
  19. test('blocks private http IP address', async () => {
  20. const url = 'http://172.20.10.4:8090'
  21. const promise = getProtectedGot({ allowLocalIPs: false }).then(got => got.get(url))
  22. await expect(promise).rejects.toThrow(new Error(FORBIDDEN_IP_ADDRESS))
  23. })
  24. test('blocks private https IP address', async () => {
  25. const url = 'https://172.20.10.4:8090'
  26. const promise = getProtectedGot({ allowLocalIPs: false }).then(got => got.get(url))
  27. await expect(promise).rejects.toThrow(new Error(FORBIDDEN_IP_ADDRESS))
  28. })
  29. test('blocks various private IP addresses', async () => {
  30. // eslint-disable-next-line max-len
  31. // taken from: https://github.com/transloadit/uppy/blob/4aeef4dac0490ebb1d1fccd5582ba42c6c0fb87d/packages/%40uppy/companion/src/server/helpers/request.js#L14
  32. const ipv4s = [
  33. '0.0.0.0',
  34. '0.0.0.1',
  35. '127.0.0.1',
  36. '127.16.0.1',
  37. '192.168.1.1',
  38. '169.254.1.1',
  39. '10.0.0.1',
  40. ]
  41. const ipv6s = [
  42. 'fd80::1234:5678:abcd:0123',
  43. 'fe80::1234:5678:abcd:0123',
  44. 'ff00::1234',
  45. '::ffff:192.168.1.10',
  46. '::1',
  47. '0:0:0:0:0:0:0:1',
  48. 'fda1:3f9f:dbf7::1c8d',
  49. ]
  50. for (const ip of ipv4s) {
  51. const url = `http://${ip}:8090`
  52. const promise = getProtectedGot({ allowLocalIPs: false }).then(got => got.get(url))
  53. await expect(promise).rejects.toThrow(new Error(FORBIDDEN_IP_ADDRESS))
  54. }
  55. for (const ip of ipv6s) {
  56. const url = `http://[${ip}]:8090`
  57. const promise = getProtectedGot({ allowLocalIPs: false }).then(got => got.get(url))
  58. await expect(promise).rejects.toThrow(new Error(FORBIDDEN_IP_ADDRESS))
  59. }
  60. })
  61. })