From b31604ce015ba9b332097ea33b24d6186730486b Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 26 Nov 2024 13:46:44 -0500 Subject: [PATCH] make sure href setter throws if invalid --- src/workerd/api/url-standard.c++ | 8 ++++++-- src/workerd/api/url-standard.h | 2 +- src/workerd/api/wpt/url-test.js | 1 - src/wpt/harness.js | 19 +++++++++++++++---- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/workerd/api/url-standard.c++ b/src/workerd/api/url-standard.c++ index 91659da229c..69730d7a76d 100644 --- a/src/workerd/api/url-standard.c++ +++ b/src/workerd/api/url-standard.c++ @@ -79,8 +79,12 @@ kj::ArrayPtr 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(); } diff --git a/src/workerd/api/url-standard.h b/src/workerd/api/url-standard.h index 411f402ab41..8f998476b25 100644 --- a/src/workerd/api/url-standard.h +++ b/src/workerd/api/url-standard.h @@ -162,7 +162,7 @@ class URL: public jsg::Object { } kj::ArrayPtr getHref(); - void setHref(kj::String value); + void setHref(jsg::Lock& js, kj::String value); kj::Array getOrigin(); diff --git a/src/workerd/api/wpt/url-test.js b/src/workerd/api/wpt/url-test.js index c2272abbd4b..40b34d19583 100644 --- a/src/workerd/api/wpt/url-test.js +++ b/src/workerd/api/wpt/url-test.js @@ -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', ], diff --git a/src/wpt/harness.js b/src/wpt/harness.js index 6e6e8256a7e..8e9f6850989 100644 --- a/src/wpt/harness.js +++ b/src/wpt/harness.js @@ -50,7 +50,12 @@ globalThis.fetch = async (url) => { }; }; -globalThis.GLOBAL = { isWindow: () => false }; +globalThis.self = globalThis; +globalThis.GLOBAL = { + isWindow() { + return false; + }, +}; globalThis.done = () => undefined; @@ -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( @@ -326,7 +337,7 @@ export function run(file, options = {}) { async test() { prepare(options); await import(file); - await validate(options); + validate(file, options); }, }; }