-
Notifications
You must be signed in to change notification settings - Fork 126
fix: do not forward credential headers on cross-origin redirect #813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1e81b92
fix: do not forward credential headers on cross-origin redirect
fengmk2 d59b619
test: skip external-network tests that are flaky in CI
fengmk2 e967cbf
ci: drop Node.js 8 from the 2.x test matrix
fengmk2 44c1935
fix: address review feedback on cross-origin redirect sanitization
fengmk2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| 'use strict'; | ||
|
|
||
| var assert = require('assert'); | ||
| var http = require('http'); | ||
| var urllib = require('../'); | ||
|
|
||
| // On a cross-origin redirect, credential-bearing request headers | ||
| // (Authorization, Cookie, Proxy-Authorization) and the `auth` option must NOT | ||
| // be forwarded to the new origin. Same-origin redirects should keep them. | ||
| describe('test/redirect-cross-origin.test.js', function() { | ||
| var serverA; | ||
| var serverB; | ||
| var portA; | ||
| var portB; | ||
|
|
||
| before(function(done) { | ||
| serverB = http.createServer(function(req, res) { | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify(req.headers)); | ||
| }); | ||
| serverB.listen(0, function() { | ||
| portB = serverB.address().port; | ||
| serverA = http.createServer(function(req, res) { | ||
| if (req.url === '/start-cross') { | ||
| res.statusCode = 302; | ||
| res.setHeader('Location', 'http://127.0.0.1:' + portB + '/captured'); | ||
| return res.end('redirect to B'); | ||
| } | ||
| if (req.url === '/start-same') { | ||
| res.statusCode = 302; | ||
| res.setHeader('Location', '/captured'); | ||
| return res.end('redirect to self'); | ||
| } | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify(req.headers)); | ||
| }); | ||
| serverA.listen(0, function() { | ||
| portA = serverA.address().port; | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| after(function() { | ||
| if (serverA) { | ||
| serverA.close(); | ||
| } | ||
| if (serverB) { | ||
| serverB.close(); | ||
| } | ||
| }); | ||
|
|
||
| function credentialHeaders() { | ||
| return { | ||
| Authorization: 'Bearer LIVE-AUTH', | ||
| Cookie: 'session=LIVE-COOKIE', | ||
| 'Proxy-Authorization': 'Bearer proxy-LIVE', | ||
| }; | ||
| } | ||
|
|
||
| it('should NOT forward credential headers on cross-origin redirect', function(done) { | ||
| urllib.request('http://127.0.0.1:' + portA + '/start-cross', { | ||
| followRedirect: true, | ||
| headers: credentialHeaders(), | ||
| }, function(err, data, res) { | ||
| assert(!err); | ||
| assert.equal(res.statusCode, 200); | ||
| assert.equal(res.requestUrls.length, 2); | ||
| var received = JSON.parse(data.toString()); | ||
| assert.equal(received.authorization, undefined); | ||
| assert.equal(received.cookie, undefined); | ||
| assert.equal(received['proxy-authorization'], undefined); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should keep credential headers on same-origin redirect', function(done) { | ||
| urllib.request('http://127.0.0.1:' + portA + '/start-same', { | ||
| followRedirect: true, | ||
| headers: credentialHeaders(), | ||
| }, function(err, data, res) { | ||
| assert(!err); | ||
| assert.equal(res.statusCode, 200); | ||
| assert.equal(res.requestUrls.length, 2); | ||
| var received = JSON.parse(data.toString()); | ||
| assert.equal(received.authorization, 'Bearer LIVE-AUTH'); | ||
| assert.equal(received.cookie, 'session=LIVE-COOKIE'); | ||
| assert.equal(received['proxy-authorization'], 'Bearer proxy-LIVE'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should NOT re-inject Basic auth (options.auth) on cross-origin redirect', function(done) { | ||
| urllib.request('http://127.0.0.1:' + portA + '/start-cross', { | ||
| followRedirect: true, | ||
| auth: 'user:passwd', | ||
| }, function(err, data, res) { | ||
| assert(!err); | ||
| assert.equal(res.statusCode, 200); | ||
| var received = JSON.parse(data.toString()); | ||
| assert.equal(received.authorization, undefined); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should keep Basic auth (options.auth) on same-origin redirect', function(done) { | ||
| urllib.request('http://127.0.0.1:' + portA + '/start-same', { | ||
| followRedirect: true, | ||
| auth: 'user:passwd', | ||
| }, function(err, data, res) { | ||
| assert(!err); | ||
| assert.equal(res.statusCode, 200); | ||
| var received = JSON.parse(data.toString()); | ||
| assert.equal(received.authorization, 'Basic ' + Buffer.from('user:passwd').toString('base64')); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not mutate the caller options object on cross-origin redirect', function(done) { | ||
| var headers = credentialHeaders(); | ||
| var opts = { followRedirect: true, auth: 'user:passwd', digestAuth: 'user:passwd', headers: headers }; | ||
| urllib.request('http://127.0.0.1:' + portA + '/start-cross', opts, function(err, data, res) { | ||
| assert(!err); | ||
| assert.equal(res.statusCode, 200); | ||
| // credentials were stripped on the wire | ||
| var received = JSON.parse(data.toString()); | ||
| assert.equal(received.authorization, undefined); | ||
| // but the caller's reusable options object is untouched | ||
| assert.equal(opts.auth, 'user:passwd'); | ||
| assert.equal(opts.digestAuth, 'user:passwd'); | ||
| assert.equal(opts.headers, headers); | ||
| assert.equal(opts.headers.Authorization, 'Bearer LIVE-AUTH'); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For proxied requests where the caller pins a proxy with
args.proxy/URLLIB_PROXYand suppliesProxy-Authorizationin the request headers, a redirect to another origin still goes through that same authenticated proxy, but this list removes the proxy credential before the recursive request. In that context the header authenticates the proxy rather than the redirected origin, so authenticated proxies can start returning 407 on any cross-origin redirect; only strip it when it is actually being sent as an origin header rather than while proxying.Useful? React with 👍 / 👎.