-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe another There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm only checking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yep, not worth a full on abort ;) |
||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this is right, since this would affect more than just the WHATWG URL implementation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
EDIT: Objection withdrawn. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I've explained in 424722af4541cd0eec1bf299aa8afcdff6284f52, this flag is actually a no-op. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you're right, I missed that it's the UTF-16 version. Objection withdrawn. |
||
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}:`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left-over debug code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it was intentional, since it seems like after the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, yes, to patch the error message we would have to extend the Error class, something like: class WPTError extends Error {
constructor(desc, err) {
super(`In ${desc}, ${err.name}: ${err.message}`);
Error.captureStackTrace(this, WPTError);
}
}
throw new WPTError(desc, err); But then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm.. it appears to: > var m = new Error('test')
undefined
> m.message = 'foo'
'foo'
> m
Error: foo
at repl:1:9
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at REPLServer.defaultEval (repl.js:340:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:537:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:189:7)
at REPLServer.Interface._onLine (readline.js:238:10)
at REPLServer.Interface._line (readline.js:582:8) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, looks like it's the diff --git a/test/common.js b/test/common.js
index 5f7dc25..f74d5f0 100644
--- a/test/common.js
+++ b/test/common.js
@@ -598,8 +598,10 @@ exports.WPT = {
try {
fn();
} catch (err) {
- if (err instanceof Error)
+ if (err instanceof Error) {
err.message = `In ${desc}:\n ${err.message}`;
+ Error.captureStackTrace(err);
+ }
throw err;
}
}, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joyeecheung, calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, yes, this probably deserves another PR to sort out. I am fine with a |
||
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.