-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
url: enforce valid UTF-8 in WHATWG parser #11436
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
|
||
const inputs = { | ||
valid: 'adsfadsfadsf', | ||
validsurr: '\uda23\ude23\uda1f\udfaa\ud800\udfff\uda23\ude23\uda1f\udfaa' + | ||
'\ud800\udfff', | ||
someinvalid: 'asasfdfasd\uda23', | ||
allinvalid: '\udc45\uda23 \udf00\udc00 \udfaa\uda12 \udc00\udfaa', | ||
nonstring: { toString() { return 'asdf'; } } | ||
}; | ||
const bench = common.createBenchmark(main, { | ||
input: Object.keys(inputs), | ||
n: [5e7] | ||
}, { | ||
flags: ['--expose-internals'] | ||
}); | ||
|
||
function main(conf) { | ||
const { toUSVString } = require('internal/url'); | ||
const str = inputs[conf.input]; | ||
const n = conf.n | 0; | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i++) | ||
toUSVString(str); | ||
bench.end(n); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
#include <unicode/utf.h> | ||
#endif | ||
|
||
#define UNICODE_REPLACEMENT_CHARACTER 0xFFFD | ||
|
||
namespace node { | ||
|
||
using v8::Array; | ||
|
@@ -143,6 +145,21 @@ namespace url { | |
} | ||
#endif | ||
|
||
// If a UTF-16 character is a low/trailing surrogate. | ||
static inline bool IsUnicodeTrail(uint16_t c) { | ||
return (c & 0xFC00) == 0xDC00; | ||
} | ||
|
||
// If a UTF-16 character is a surrogate. | ||
static inline bool IsUnicodeSurrogate(uint16_t c) { | ||
return (c & 0xF800) == 0xD800; | ||
} | ||
|
||
// If a UTF-16 surrogate is a low/trailing one. | ||
static inline bool IsUnicodeSurrogateTrail(uint16_t c) { | ||
return (c & 0x400) != 0; | ||
} | ||
|
||
static url_host_type ParseIPv6Host(url_host* host, | ||
const char* input, | ||
size_t length) { | ||
|
@@ -1351,6 +1368,41 @@ namespace url { | |
v8::NewStringType::kNormal).ToLocalChecked()); | ||
} | ||
|
||
static void ToUSVString(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
CHECK_GE(args.Length(), 2); | ||
CHECK(args[0]->IsString()); | ||
CHECK(args[1]->IsNumber()); | ||
|
||
TwoByteValue value(env->isolate(), args[0]); | ||
const size_t n = value.length(); | ||
|
||
const int64_t start = args[1]->IntegerValue(env->context()).FromJust(); | ||
CHECK_GE(start, 0); | ||
|
||
|
||
for (size_t i = start; i < n; i++) { | ||
uint16_t c = value[i]; | ||
if (!IsUnicodeSurrogate(c)) { | ||
continue; | ||
} else if (IsUnicodeSurrogateTrail(c) || i == n - 1) { | ||
value[i] = UNICODE_REPLACEMENT_CHARACTER; | ||
} else { | ||
uint16_t d = value[i + 1]; | ||
if (IsUnicodeTrail(d)) { | ||
i++; | ||
} else { | ||
value[i] = UNICODE_REPLACEMENT_CHARACTER; | ||
} | ||
} | ||
} | ||
|
||
args.GetReturnValue().Set( | ||
String::NewFromTwoByte(env->isolate(), | ||
*value, | ||
v8::NewStringType::kNormal, | ||
n).ToLocalChecked()); | ||
} | ||
|
||
static void DomainToASCII(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
CHECK_GE(args.Length(), 1); | ||
|
@@ -1398,6 +1450,7 @@ namespace url { | |
Environment* env = Environment::GetCurrent(context); | ||
env->SetMethod(target, "parse", Parse); | ||
env->SetMethod(target, "encodeAuth", EncodeAuthSet); | ||
env->SetMethod(target, "toUSVString", ToUSVString); | ||
env->SetMethod(target, "domainToASCII", DomainToASCII); | ||
env->SetMethod(target, "domainToUnicode", DomainToUnicode); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,8 +48,7 @@ TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) { | |
const size_t storage = string->Length() + 1; | ||
AllocateSufficientStorage(storage); | ||
|
||
const int flags = | ||
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8; | ||
const int flags = String::NO_NULL_TERMINATION; | ||
|
||
const int length = string->Write(out(), 0, storage, flags); | ||
SetLengthAndZeroTerminate(length); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -604,8 +604,7 @@ exports.WPT = { | |
try { | ||
fn(); | ||
} catch (err) { | ||
if (err instanceof Error) | ||
err.message = `In ${desc}:\n ${err.message}`; | ||
console.error(`In ${desc}:`); | ||
|
||
throw err; | ||
} | ||
}, | ||
|
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.
CHECK_EQ
?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.
All the existing functions use
CHECK_GE
, and I don't see a reason to be stricter than what this function actually uses.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.
Yeah there are functions in other files doing EQ...not sure if we have a convention or not, just think GE is implying there could be more args, which doesn't seem to be the case for this one, though I don't feel very strongly about this.