Skip to content

Commit

Permalink
Merge pull request #3176 from cloudflare/yagiz/url-parser-href-setter
Browse files Browse the repository at this point in the history
make sure href setter throws if invalid
  • Loading branch information
anonrig authored Nov 26, 2024
2 parents 7cae939 + b31604c commit d9695ca
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/workerd/api/url-standard.c++
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ kj::ArrayPtr<const char> URL::getHref() {
return inner.getHref();
}

void URL::setHref(kj::String value) {
inner.setHref(value);
void URL::setHref(jsg::Lock& js, kj::String value) {
// Href setter is the only place in URL parser that is allowed to throw except the constructor.
if (!inner.setHref(value)) {
auto context = jsg::TypeErrorContext::setterArgument(typeid(URL), "href");
jsg::throwTypeError(js.v8Isolate, context, "valid URL string");
}
KJ_IF_SOME(searchParams, maybeSearchParams) {
searchParams->reset();
}
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/url-standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class URL: public jsg::Object {
}

kj::ArrayPtr<const char> getHref();
void setHref(kj::String value);
void setHref(jsg::Lock& js, kj::String value);

kj::Array<const char> getOrigin();

Expand Down
1 change: 0 additions & 1 deletion src/workerd/api/wpt/url-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const idnaTestV2Window = run('IdnaTestV2.window.js');
export const historical = run('historical.any.js', {
expectedFailures: [
'Constructor only takes strings',
"Setting URL's href attribute and base URLs",
'URL: no structured serialize/deserialize support',
'URLSearchParams: no structured serialize/deserialize support',
],
Expand Down
19 changes: 15 additions & 4 deletions src/wpt/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ globalThis.fetch = async (url) => {
};
};

globalThis.GLOBAL = { isWindow: () => false };
globalThis.self = globalThis;
globalThis.GLOBAL = {
isWindow() {
return false;
},
};

globalThis.done = () => undefined;

Expand Down Expand Up @@ -303,16 +308,22 @@ function sanitizeMessage(message) {
);
}

async function validate(options) {
function validate(testFileName, options) {
const expectedFailures = new Set(options.expectedFailures ?? []);

let failing = false;
for (const err of globalThis.errors) {
if (!expectedFailures.delete(err.message)) {
err.message = sanitizeMessage(err.message);
throw err;
console.error(err);
failing = true;
}
}

if (failing) {
throw new Error(`${testFileName} failed`);
}

if (expectedFailures.size > 0) {
console.info('Expected failures', expectedFailures);
throw new Error(
Expand All @@ -326,7 +337,7 @@ export function run(file, options = {}) {
async test() {
prepare(options);
await import(file);
await validate(options);
validate(file, options);
},
};
}

0 comments on commit d9695ca

Please sign in to comment.