Skip to content

Commit

Permalink
Add baseURL to the URLPattern condition while routing rule registration
Browse files Browse the repository at this point in the history
This behavior change was originally started in
whatwg/urlpattern#182, and follows the spec
change in whatwg/urlpattern#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
  • Loading branch information
sisidovski authored and chromium-wpt-export-bot committed Nov 27, 2023
1 parent 51c42fc commit b4e1417
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const routerRules = {
'condition-url-pattern-source-network': [
{
condition: {
urlPattern: new URLPattern({
pathname: '/**/direct.txt'
})
},
source: 'network'
},

'condition-urlpattern-constructed-source-network': [{
condition: {urlPattern: new URLPattern({pathname: '/**/direct.txt'})},
source: 'network'
}],
'condition-urlpattern-urlpatterninit-source-network': [
{condition: {urlPattern: {pathname: '/**/direct.txt'}}, source: 'network'},
],
'condition-request-source-network': [
{
condition: {
requestMode: 'no-cors'
},
source: 'network'
}
],
'condition-or-source-network': [
{
condition: {
or: [
{
or: [
{
urlPattern: '/**/or-test/direct1.*??*'
}
],
},
{
urlPattern: '/**/or-test/direct2.*??*'
}
]
},
source: 'network'
}
'condition-urlpattern-string-source-network': [
{condition: {urlPattern: '/**/direct.txt'}, source: 'network'},
],
'condition-request-source-network':
[{condition: {requestMode: 'no-cors'}, source: 'network'}],
'condition-or-source-network': [{
condition: {
or: [
{
or: [{urlPattern: '/**/or-test/direct1.*??*'}],
},
{urlPattern: '/**/or-test/direct2.*??*'}
]
},
source: 'network'
}],
};

export {routerRules};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body>
<script>
const SCRIPT = 'resources/static-router-sw.js';
const ROUTER_RULE_KEY = 'condition-url-pattern-source-network'
const ROUTER_RULE_KEY = 'condition-urlpattern-constructed-source-network';
const SCOPE = 'resources/';
const REGISTERED_ROUTE = 'resources/direct.txt';
const NON_REGISTERED_ROUTE = 'resources/simple.html';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<body>
<script>
const SCRIPT = 'resources/static-router-sw.js';
const ROUTER_RULE_KEY_URL_PATTERN = 'condition-url-pattern-source-network'
const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED =
'condition-urlpattern-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 = 'condition-request-source-network'
const ROUTER_RULE_KEY_OR = 'condition-or-source-network'
const SCOPE = 'resources/';
Expand All @@ -29,11 +35,12 @@
const reg = await service_worker_unregister_and_register(
t, swURL, SCOPE, {type: 'module'});
add_completion_callback(() => reg.unregister());
await wait_for_state(t, reg.installing, 'activated');
const worker = reg.installing;
await wait_for_state(t, worker, 'activated');
const iframe = await with_iframe(url);
const iwin = iframe.contentWindow;
t.add_cleanup(() => iframe.remove());
await callback(t, iwin);
await callback(t, iwin, worker);
}, name);
}

Expand All @@ -45,18 +52,83 @@
return result;
}

iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN, async (t, iwin) => {
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_CONSTRUCTED, async (t, iwin) => {
const rnd = randomString();
const response = await iwin.fetch('?nonce=' + rnd);
assert_equals(await response.text(), rnd);
}, 'Subresource load not matched with URLPattern condition');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN, async (t, iwin) => {
iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, 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');

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 (t, iwin) => {
const rnd = randomString();
const response = await iwin.fetch('?nonce=' + rnd, { mode: 'no-cors' });
Expand Down

0 comments on commit b4e1417

Please sign in to comment.