Skip to content

Commit

Permalink
src: set HAS_USERNAME/PASSWORD more strictly
Browse files Browse the repository at this point in the history
Fixes: #24211

PR-URL: #24495
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
TimothyGu authored and BridgeAR committed Dec 5, 2018
1 parent 96e6873 commit 0e88f44
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1209,21 +1209,33 @@ inline url_data HarvestBase(Environment* env, Local<Object> base_obj) {
base_obj->Get(env->context(), env->scheme_string()).ToLocalChecked();
base.scheme = Utf8Value(env->isolate(), scheme).out();

auto GetStr = [&](std::string url_data::* member,
auto GetStr = [&](std::string url_data::*member,
int flag,
Local<String> name) {
Local<String> name,
bool empty_as_present) {
Local<Value> value = base_obj->Get(env->context(), name).ToLocalChecked();
if (value->IsString()) {
Utf8Value utf8value(env->isolate(), value.As<String>());
(base.*member).assign(*utf8value, utf8value.length());
base.flags |= flag;
if (empty_as_present || value.As<String>()->Length() != 0) {
base.flags |= flag;
}
}
};
GetStr(&url_data::username, URL_FLAGS_HAS_USERNAME, env->username_string());
GetStr(&url_data::password, URL_FLAGS_HAS_PASSWORD, env->password_string());
GetStr(&url_data::host, URL_FLAGS_HAS_HOST, env->host_string());
GetStr(&url_data::query, URL_FLAGS_HAS_QUERY, env->query_string());
GetStr(&url_data::fragment, URL_FLAGS_HAS_FRAGMENT, env->fragment_string());
GetStr(&url_data::username,
URL_FLAGS_HAS_USERNAME,
env->username_string(),
false);
GetStr(&url_data::password,
URL_FLAGS_HAS_PASSWORD,
env->password_string(),
false);
GetStr(&url_data::host, URL_FLAGS_HAS_HOST, env->host_string(), true);
GetStr(&url_data::query, URL_FLAGS_HAS_QUERY, env->query_string(), true);
GetStr(&url_data::fragment,
URL_FLAGS_HAS_FRAGMENT,
env->fragment_string(),
true);

Local<Value> port =
base_obj->Get(env->context(), env->port_string()).ToLocalChecked();
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-whatwg-url-custom-deepequal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
// This tests that the internal flags in URL objects are consistent, as manifest
// through assert libraries.
// See https://github.com/nodejs/node/issues/24211

// Tests below are not from WPT.

require('../common');
const assert = require('assert');

assert.deepStrictEqual(
new URL('./foo', 'https://example.com/'),
new URL('https://example.com/foo')
);
assert.deepStrictEqual(
new URL('./foo', 'https://user:[email protected]/'),
new URL('https://user:[email protected]/foo')
);

0 comments on commit 0e88f44

Please sign in to comment.