Skip to content
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

http: decode username and password before encoding #31450

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,10 @@ This can be overridden for servers and client requests by passing the
<!-- YAML
added: v0.3.6
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/31450
description: When used with a `URL` object, the authentication fields will
now be percent-decoded.
- version: v13.3.0
pr-url: https://github.com/nodejs/node/pull/30570
description: The `maxHeaderSize` option is supported now.
Expand Down
11 changes: 10 additions & 1 deletion doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ the URL, use the [`url.search`][] setter. See [`URLSearchParams`][]
documentation for details.

#### `url.username`
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/31450
description: When used with `http.request()`, this field will now be
percent-decoded.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing closing -->.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The section on http.request() in http.md seems like a more logical place to me for this note.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The section on http.request() in http.md seems like a more logical place to me for this note.

Why not both? :)

-->

* {string}

Expand All @@ -488,7 +495,8 @@ console.log(myURL.href);
Any invalid URL characters appearing in the value assigned the `username`
property will be [percent-encoded][]. The selection of which
characters to percent-encode may vary somewhat from what the [`url.parse()`][]
and [`url.format()`][] methods would produce.
and [`url.format()`][] methods would produce. When used with
[`http.request()`][], this field will be percent-decoded.

#### `url.toString()`

Expand Down Expand Up @@ -1316,6 +1324,7 @@ console.log(myURL.origin);
[`TypeError`]: errors.html#errors_class_typeerror
[`URLSearchParams`]: #url_class_urlsearchparams
[`array.toString()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
[`http.request()`]: http.html#http_http_request_options_callback
[`new URL()`]: #url_constructor_new_url_input_base
[`querystring`]: querystring.html
[`require('url').format()`]: #url_url_format_url_options
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,10 @@ function domainToUnicode(domain) {
return _domainToUnicode(`${domain}`);
}

function decodeAuth(str) {
return decodeURIComponent(str).replace(':', '%3A').replace('/', '%2F');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you need to use global replace at least ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax This is still unaddressed (and, apparently, untested.)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be unexpected outcome with undefined url.password or url.username

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for url.username to be undefined? Isn't it an empty string instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/addaleax/node/blob/5ebbb79a3bd56b99821081c23e1f5db9c879fce8/lib/internal/url.js#L1296

const forwardSlashRegEx = /\//g;
+const colonRegEx = /:/g;
Suggested change
return decodeURIComponent(str).replace(':', '%3A').replace('/', '%2F');
return decodeURIComponent(str).replace(colonRegEx, '%3A').replace(forwardSlashRegEx, '%2F');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only thing todo here I think...

}

// Utility function that converts a URL object into an ordinary
// options object as expected by the http.request and https.request
// APIs.
Expand All @@ -1284,7 +1288,7 @@ function urlToOptions(url) {
options.port = Number(url.port);
}
if (url.username || url.password) {
options.auth = `${url.username}:${url.password}`;
options.auth = `${decodeAuth(url.username)}:${decodeAuth(url.password)}`;
}
return options;
}
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-http-url-username.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const MakeDuplexPair = require('../common/duplexpair');

// Test that usernames from URLs are URL-decoded, as they should be.

{
const url = new URL('http://localhost');
url.username = 'test@test"';
url.password = '123456^';

const server = http.createServer(
common.mustCall((req, res) => {
assert.strictEqual(
req.headers.authorization,
'Basic ' + Buffer.from('test@test":123456^').toString('base64'));
res.statusCode = 200;
res.end();
}));

const { clientSide, serverSide } = MakeDuplexPair();
server.emit('connection', serverSide);

const req = http.request(url, {
createConnection: common.mustCall(() => clientSide)
}, common.mustCall((res) => {
res.resume(); // We don’t actually care about contents.
res.on('end', common.mustCall());
}));
req.end();
}