|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require('assert'); |
| 4 | +var http = require('http'); |
| 5 | +var urllib = require('../'); |
| 6 | + |
| 7 | +// On a cross-origin redirect, credential-bearing request headers |
| 8 | +// (Authorization, Cookie, Proxy-Authorization) and the `auth` option must NOT |
| 9 | +// be forwarded to the new origin. Same-origin redirects should keep them. |
| 10 | +describe('test/redirect-cross-origin.test.js', function() { |
| 11 | + var serverA; |
| 12 | + var serverB; |
| 13 | + var portA; |
| 14 | + var portB; |
| 15 | + |
| 16 | + before(function(done) { |
| 17 | + serverB = http.createServer(function(req, res) { |
| 18 | + res.statusCode = 200; |
| 19 | + res.setHeader('Content-Type', 'application/json'); |
| 20 | + res.end(JSON.stringify(req.headers)); |
| 21 | + }); |
| 22 | + serverB.listen(0, function() { |
| 23 | + portB = serverB.address().port; |
| 24 | + serverA = http.createServer(function(req, res) { |
| 25 | + if (req.url === '/start-cross') { |
| 26 | + res.statusCode = 302; |
| 27 | + res.setHeader('Location', 'http://127.0.0.1:' + portB + '/captured'); |
| 28 | + return res.end('redirect to B'); |
| 29 | + } |
| 30 | + if (req.url === '/start-same') { |
| 31 | + res.statusCode = 302; |
| 32 | + res.setHeader('Location', '/captured'); |
| 33 | + return res.end('redirect to self'); |
| 34 | + } |
| 35 | + res.statusCode = 200; |
| 36 | + res.setHeader('Content-Type', 'application/json'); |
| 37 | + res.end(JSON.stringify(req.headers)); |
| 38 | + }); |
| 39 | + serverA.listen(0, function() { |
| 40 | + portA = serverA.address().port; |
| 41 | + done(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + after(function() { |
| 47 | + if (serverA) { |
| 48 | + serverA.close(); |
| 49 | + } |
| 50 | + if (serverB) { |
| 51 | + serverB.close(); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + function credentialHeaders() { |
| 56 | + return { |
| 57 | + Authorization: 'Bearer LIVE-AUTH', |
| 58 | + Cookie: 'session=LIVE-COOKIE', |
| 59 | + 'Proxy-Authorization': 'Bearer proxy-LIVE', |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + it('should NOT forward credential headers on cross-origin redirect', function(done) { |
| 64 | + urllib.request('http://127.0.0.1:' + portA + '/start-cross', { |
| 65 | + followRedirect: true, |
| 66 | + headers: credentialHeaders(), |
| 67 | + }, function(err, data, res) { |
| 68 | + assert(!err); |
| 69 | + assert.equal(res.statusCode, 200); |
| 70 | + assert.equal(res.requestUrls.length, 2); |
| 71 | + var received = JSON.parse(data.toString()); |
| 72 | + assert.equal(received.authorization, undefined); |
| 73 | + assert.equal(received.cookie, undefined); |
| 74 | + assert.equal(received['proxy-authorization'], undefined); |
| 75 | + done(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should keep credential headers on same-origin redirect', function(done) { |
| 80 | + urllib.request('http://127.0.0.1:' + portA + '/start-same', { |
| 81 | + followRedirect: true, |
| 82 | + headers: credentialHeaders(), |
| 83 | + }, function(err, data, res) { |
| 84 | + assert(!err); |
| 85 | + assert.equal(res.statusCode, 200); |
| 86 | + assert.equal(res.requestUrls.length, 2); |
| 87 | + var received = JSON.parse(data.toString()); |
| 88 | + assert.equal(received.authorization, 'Bearer LIVE-AUTH'); |
| 89 | + assert.equal(received.cookie, 'session=LIVE-COOKIE'); |
| 90 | + assert.equal(received['proxy-authorization'], 'Bearer proxy-LIVE'); |
| 91 | + done(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should NOT re-inject Basic auth (options.auth) on cross-origin redirect', function(done) { |
| 96 | + urllib.request('http://127.0.0.1:' + portA + '/start-cross', { |
| 97 | + followRedirect: true, |
| 98 | + auth: 'user:passwd', |
| 99 | + }, function(err, data, res) { |
| 100 | + assert(!err); |
| 101 | + assert.equal(res.statusCode, 200); |
| 102 | + var received = JSON.parse(data.toString()); |
| 103 | + assert.equal(received.authorization, undefined); |
| 104 | + done(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should keep Basic auth (options.auth) on same-origin redirect', function(done) { |
| 109 | + urllib.request('http://127.0.0.1:' + portA + '/start-same', { |
| 110 | + followRedirect: true, |
| 111 | + auth: 'user:passwd', |
| 112 | + }, function(err, data, res) { |
| 113 | + assert(!err); |
| 114 | + assert.equal(res.statusCode, 200); |
| 115 | + var received = JSON.parse(data.toString()); |
| 116 | + assert.equal(received.authorization, 'Basic ' + Buffer.from('user:passwd').toString('base64')); |
| 117 | + done(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should not mutate the caller options object on cross-origin redirect', function(done) { |
| 122 | + var headers = credentialHeaders(); |
| 123 | + var opts = { followRedirect: true, auth: 'user:passwd', digestAuth: 'user:passwd', headers: headers }; |
| 124 | + urllib.request('http://127.0.0.1:' + portA + '/start-cross', opts, function(err, data, res) { |
| 125 | + assert(!err); |
| 126 | + assert.equal(res.statusCode, 200); |
| 127 | + // credentials were stripped on the wire |
| 128 | + var received = JSON.parse(data.toString()); |
| 129 | + assert.equal(received.authorization, undefined); |
| 130 | + // but the caller's reusable options object is untouched |
| 131 | + assert.equal(opts.auth, 'user:passwd'); |
| 132 | + assert.equal(opts.digestAuth, 'user:passwd'); |
| 133 | + assert.equal(opts.headers, headers); |
| 134 | + assert.equal(opts.headers.Authorization, 'Bearer LIVE-AUTH'); |
| 135 | + done(); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments