From 8df3a22ddc19c7170c07d1144dea30068a534c04 Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Thu, 7 Dec 2023 15:47:44 +0000 Subject: [PATCH] Bug 1866956 [wpt PR 43272] - Add baseURL to the URLPattern condition while routing rule registration, a=testonly Automatic update from web-platform-tests Add baseURL to the URLPattern condition while routing rule registration This behavior change was originally started in https://github.com/whatwg/urlpattern/issues/182, and follows the spec change in https://github.com/whatwg/urlpattern/pull/199. This CL changes the behavior of the router rule registration in the ServiceWorker Static Routing API, especially when the |urlPattern| condition receives URLPatternInit or USVString. Before this CL, the URLPatternInit input was accepted as it is, that means any unspecified fields are resulted in the wildcards (*). This behavior is inconsistent with the case when |urlPattern| accepts a string. When a string is passed, missing fields are complemented by baseURL, the SW script URL is internally treated as baseURL. After this CL, the URLPatternInit input also internally uses the SW script URL as a baseURL if it's not explicitly provided. This is achieved by the helper method `URLPattern::From()`, which was added in [1]. This change doesn't affect the case when the input is URLPattern, which means the input is the object constructed via `new URLPattern()`. [1] crrev.com/c/5053645 Bug: 1371756 Change-Id: I5cce80fde05cf18237c8b6412b00e017ff5aad5b Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5039680 Reviewed-by: Takashi Toyoshima Reviewed-by: Kouhei Ueno Commit-Queue: Shunya Shishido Reviewed-by: Yoshisato Yanagisawa Auto-Submit: Shunya Shishido Cr-Commit-Position: refs/heads/main{#1229724} -- wpt-commits: ffe06ef95d4d48be147e640fd9d1c489b810f929 wpt-pr: 43272 UltraBlame original commit: 8cba5e465039e56ad23ca65c4e4b1c307f2f8379 --- .../static-router/resources/router-rules.js | 80 +- .../static-router-main-resource.https.html | 5 +- .../static-router-subresource.https.html | 793 +++++++++++++++++- 3 files changed, 866 insertions(+), 12 deletions(-) diff --git a/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/resources/router-rules.js b/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/resources/router-rules.js index 1a1020efcc2e7..cd6dfa69896ca 100644 --- a/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/resources/router-rules.js +++ b/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/resources/router-rules.js @@ -38,9 +38,9 @@ routerRules ' condition - -url +urlpattern - -pattern +constructed - source - @@ -82,6 +82,82 @@ network ' condition - +urlpattern +- +urlpatterninit +- +source +- +network +' +: +[ +{ +condition +: +{ +urlPattern +: +{ +pathname +: +' +/ +* +* +/ +direct +. +txt +' +} +} +source +: +' +network +' +} +] +' +condition +- +urlpattern +- +string +- +source +- +network +' +: +[ +{ +condition +: +{ +urlPattern +: +' +/ +* +* +/ +direct +. +txt +' +} +source +: +' +network +' +} +] +' +condition +- request - source diff --git a/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html b/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html index 59ae240e853a2..c1a97e0592014 100644 --- a/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html +++ b/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html @@ -137,14 +137,15 @@ ' condition - -url +urlpattern - -pattern +constructed - source - network ' +; const SCOPE = diff --git a/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-subresource.https.html b/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-subresource.https.html index 7afcc9f563360..8491e375b8d61 100644 --- a/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-subresource.https.html +++ b/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-subresource.https.html @@ -85,6 +85,29 @@ script > < +script +src += +" +/ +common +/ +get +- +host +- +info +. +sub +. +js +" +> +< +/ +script +> +< body > < @@ -106,19 +129,50 @@ ' ; const -ROUTER_RULE_KEY_URL_PATTERN +ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED = ' condition - -url +urlpattern - -pattern +constructed +- +source +- +network +' +; +const +ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT += +' +condition +- +urlpattern +- +urlpatterninit +- +source +- +network +' +; +const +ROUTER_RULE_KEY_URL_PATTERN_STRING += +' +condition +- +urlpattern +- +string - source - network ' +; const ROUTER_RULE_KEY_REQUEST = @@ -332,13 +386,18 @@ ) ) ; +const +worker += +reg +. +installing +; await wait_for_state ( t -reg -. -installing +worker ' activated ' @@ -380,6 +439,7 @@ ( t iwin +worker ) ; } @@ -442,10 +502,73 @@ result ; } +function +get_fetched_urls +( +worker +) +{ +return +new +Promise +( +function +( +resolve +) +{ +var +channel += +new +MessageChannel +( +) +; +channel +. +port1 +. +onmessage += +function +( +msg +) +{ +resolve +( +msg +) +; +} +; +worker +. +postMessage +( +{ +port +: +channel +. +port2 +} +[ +channel +. +port2 +] +) +; +} +) +; +} iframeTest ( HTML_FILE -ROUTER_RULE_KEY_URL_PATTERN +ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED async ( t @@ -504,7 +627,7 @@ iframeTest ( TXT_FILE -ROUTER_RULE_KEY_URL_PATTERN +ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED async ( t @@ -565,6 +688,660 @@ ; iframeTest ( +TXT_FILE +ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED +async +( +t +iwin +worker +) += +> +{ +const +rnd += +randomString +( +) +; +/ +/ +Confirm +that +the +given +URLPatternInit +has +a +wildcard +pattern +for +the +/ +/ +hostname +. +Also +if +| +urlPattern +| +is +a +consutructed +URLPattern +object +/ +/ +baseURL +won +' +t +be +set +while +adding +router +rules +thus +it +matches +the +cross +/ +/ +origin +request +as +far +as +other +components +matches +. +So +expecting +the +direct +/ +/ +network +request +and +the +fetch +handler +doesn +' +t +capture +the +response +. +/ +/ +The +response +is +going +to +be +a +opaque +. +const +origin += +get_host_info +( +) +. +HTTPS_REMOTE_ORIGIN +; +const +response += +await +iwin +. +fetch +( +{ +origin +} +/ +{ +TXT_FILE +} +? +nonce += +{ +rnd +} +{ +mode +: +' +no +- +cors +' +} +) +; +const +fetched_urls += +await +get_fetched_urls +( +worker +) +; +const +{ +requests +} += +fetched_urls +. +data +; +assert_equals +( +requests +. +length +0 +) +; +assert_equals +( +response +. +type +' +opaque +' +) +; +} +' +Subresource +cross +origin +load +matched +with +URLPattern +condition +via +constructed +object +' +) +; +iframeTest +( +TXT_FILE +ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT +async +( +t +iwin +) += +> +{ +const +rnd += +randomString +( +) +; +const +response += +await +iwin +. +fetch +( +' +? +nonce += +' ++ +rnd +) +; +assert_equals +( +await +response +. +text +( +) +" +Network +\ +n +" +) +; +} +' +Subresource +load +matched +with +URLPattern +condition +via +URLPatternInit +' +) +; +iframeTest +( +TXT_FILE +ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT +async +( +t +iwin +worker +) += +> +{ +/ +/ +The +SW +script +URL +is +added +as +a +baseURL +when +| +urlPattern +| +is +passed +via +/ +/ +URLPatternInit +and +there +is +not +| +baseURL +| +in +it +. +Cross +origin +request +will +/ +/ +go +through +the +fetch +handler +because +| +baseURL +| +info +complements +hostname +/ +/ +with +the +hostname +of +the +SW +script +. +const +rnd += +randomString +( +) +; +const +origin += +get_host_info +( +) +. +HTTPS_REMOTE_ORIGIN +; +const +response += +await +iwin +. +fetch +( +{ +origin +} +/ +{ +TXT_FILE +} +? +nonce += +{ +rnd +} +) +; +const +fetched_urls += +await +get_fetched_urls +( +worker +) +; +const +{ +requests +} += +fetched_urls +. +data +; +assert_equals +( +requests +. +length +1 +) +; +assert_equals +( +await +response +. +text +( +) +rnd +) +; +} +' +Subresource +cross +origin +load +not +matched +with +URLPattern +condition +via +URLPatternInit +' +) +; +iframeTest +( +TXT_FILE +ROUTER_RULE_KEY_URL_PATTERN_STRING +async +( +t +iwin +) += +> +{ +const +rnd += +randomString +( +) +; +const +response += +await +iwin +. +fetch +( +' +? +nonce += +' ++ +rnd +) +; +assert_equals +( +await +response +. +text +( +) +" +Network +\ +n +" +) +; +} +' +Subresource +load +matched +with +URLPattern +condition +via +string +' +) +; +iframeTest +( +TXT_FILE +ROUTER_RULE_KEY_URL_PATTERN_STRING +async +( +t +iwin +worker +) += +> +{ +/ +/ +The +SW +script +URL +is +added +as +a +baseURL +when +| +urlPattern +| +is +passed +via +/ +/ +string +and +there +is +not +| +baseURL +| +in +it +. +Cross +origin +request +will +go +/ +/ +through +the +fetch +handler +because +| +baseURL +| +info +complements +hostname +with +/ +/ +the +hostname +of +the +SW +script +. +const +rnd += +randomString +( +) +; +const +origin += +get_host_info +( +) +. +HTTPS_REMOTE_ORIGIN +; +const +response += +await +iwin +. +fetch +( +{ +origin +} +/ +{ +TXT_FILE +} +? +nonce += +{ +rnd +} +) +; +const +fetched_urls += +await +get_fetched_urls +( +worker +) +; +const +{ +requests +} += +fetched_urls +. +data +; +assert_equals +( +requests +. +length +1 +) +; +assert_equals +( +await +response +. +text +( +) +rnd +) +; +} +' +Subresource +cross +origin +load +not +matched +with +URLPattern +condition +via +string +' +) +; +iframeTest +( CSV_FILE ROUTER_RULE_KEY_REQUEST async