Skip to content

Commit d4ff80f

Browse files
xiaochen-zchromium-wpt-export-bot
authored andcommitted
[Connection-Allowlist] Add web platform tests for connection allowlist enforcement on PaymentRequest API: service worker registration
The web platform tests cover the service worker registration behavior when there is a connection allowlist on the initiator frame of PaymentRequest API. Requests flow: 1. PaymentRequest API sends a HEAD request to payment url. 2. A response with Link header pointing to the app manifest is sent back. 3. The API downloads the app manifest. 4. The app registers a service worker. The test sets up allowlist to either block or allow the registration. About CA: https://github.com/WICG/connection-allowlists Bug: 519915328, 447954811, 506480941 Change-Id: I6f7b8f39e6227404242c22d24fee5c2992c50f10 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8012329 Commit-Queue: Xiaochen Zhou <xiaochenzh@chromium.org> Reviewed-by: Andrew Verge <averge@chromium.org> Cr-Commit-Position: refs/heads/main@{#1668628}
1 parent 4832db4 commit d4ff80f

7 files changed

Lines changed: 160 additions & 0 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"default_applications": ["payment-app-manifest.json"],
3+
"name": "Test Payment App",
4+
"icons": [
5+
{
6+
"src": "/images/rgrg-256x256.png",
7+
"sizes": "256x256",
8+
"type": "image/png"
9+
}
10+
],
11+
"serviceworker": {
12+
"src": "payment-app-sw.js",
13+
"scope": "./"
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Link: </connection-allowlist/tentative/payment-app-manifest.json>; rel="payment-method-manifest"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
self.addEventListener('canmakepayment', event => {
2+
event.respondWith(true);
3+
});
4+
5+
self.addEventListener('paymentrequest', event => {
6+
event.respondWith({
7+
methodName: event.methodData[0].supportedMethods,
8+
details: {status: 'success'},
9+
});
10+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// META: script=/resources/testdriver.js
2+
// META: script=/resources/testdriver-vendor.js
3+
//
4+
// The test assumes the connection allowlist has been set:
5+
// Connection-Allowlist: (response-origin)
6+
//
7+
// It allows same-origin network requests, so the JIT payment app installation
8+
// (manifest, icon, service worker registration) is allowed. It also allows the
9+
// test to communicate with the test runner (testharness.js and testdriver.js)
10+
//
11+
// 1. Trigger PaymentRequest JIT installation with same-origin payment manifest.
12+
// 2. JIT succeeds, payment app registers a same-origin service worker.
13+
// 3. Verify the service worker is registered.
14+
15+
const pay_url = window.location.origin +
16+
'/connection-allowlist/tentative/payment-app-manifest.json';
17+
18+
const defaultDetails = {
19+
total: {
20+
label: 'Total',
21+
amount: {
22+
currency: 'USD',
23+
value: '0.01',
24+
},
25+
},
26+
};
27+
28+
promise_test(
29+
async t => {
30+
// Ensure any registered service workers are cleaned up after the test,
31+
// regardless of whether the test passes, fails, or throws.
32+
t.add_cleanup(async () => {
33+
const regs = await navigator.serviceWorker.getRegistrations();
34+
for (const reg of regs) {
35+
if (reg.scope.includes('connection-allowlist/tentative/')) {
36+
await reg.unregister();
37+
}
38+
}
39+
});
40+
41+
const request =
42+
new PaymentRequest([{supportedMethods: pay_url}], defaultDetails);
43+
44+
const response =
45+
await test_driver.bless('installing a payment app', () => {
46+
return request.show();
47+
});
48+
await response.complete('success');
49+
50+
const regs = await navigator.serviceWorker.getRegistrations();
51+
const found = regs.some(
52+
reg => reg.scope.includes('connection-allowlist/tentative/'));
53+
assert_true(found, 'Service worker should be registered');
54+
},
55+
'Payment Request API payment app service worker registration is allowed ' +
56+
'by the connection allowlist.');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Connection-Allowlist: (response-origin)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// META: script=/resources/testdriver.js
2+
// META: script=/resources/testdriver-vendor.js
3+
//
4+
// The test assumes the connection allowlist has been set:
5+
// Connection-Allowlist: (
6+
// "*://*:*/connection-allowlist/tentative/payment-app-manifest.json"
7+
// "*://*:*/images/rgrg-256x256.png"
8+
// "*://*:*/connection-allowlist/tentative/payment-request-sw-registration-blocked.https.window.js"
9+
// "*://*:*/resources/testharness.js"
10+
// "*://*:*/resources/testharnessreport.js"
11+
// "*://*:*/resources/testdriver.js"
12+
// "*://*:*/resources/testdriver-vendor.js"
13+
// )
14+
//
15+
// Note: The allowlist explicitly allows required test runner scripts, the
16+
// payment app manifest, and the icon, but does not allow the service worker
17+
// script ("payment-app-sw.js").
18+
//
19+
// 1. Trigger PaymentRequest JIT installation with same-origin payment manifest.
20+
// 2. The manifest and icon downloads are allowed, but the service worker script
21+
// is not allowed by the connection allowlist.
22+
// 3. Verify the service worker registration fails.
23+
24+
const pay_url = window.location.origin +
25+
'/connection-allowlist/tentative/payment-app-manifest.json';
26+
27+
const defaultDetails = {
28+
total: {
29+
label: 'Total',
30+
amount: {
31+
currency: 'USD',
32+
value: '0.01',
33+
},
34+
},
35+
};
36+
37+
promise_test(
38+
async t => {
39+
// Ensure any registered service workers are cleaned up after the test,
40+
// regardless of whether the test passes, fails, or throws.
41+
t.add_cleanup(async () => {
42+
const regs = await navigator.serviceWorker.getRegistrations();
43+
for (const reg of regs) {
44+
if (reg.scope.includes('connection-allowlist/tentative/')) {
45+
await reg.unregister();
46+
}
47+
}
48+
});
49+
50+
const request =
51+
new PaymentRequest([{supportedMethods: pay_url}], defaultDetails);
52+
53+
test_driver.bless('installing a payment app', () => {
54+
// request.show() initiates JIT payment app installation asynchronously.
55+
// We do not await this promise because if JIT installation fails, an
56+
// error dialog is displayed that keeps the promise pending indefinitely
57+
// until closed.
58+
request.show().catch(() => {});
59+
});
60+
61+
// Wait for some time to allow Payment Request API to attempt fetching and
62+
// registering `payment-app-sw.js` which gets blocked by the connection
63+
// allowlist.
64+
await new Promise(resolve => t.step_timeout(resolve, 2000));
65+
66+
const regs = await navigator.serviceWorker.getRegistrations();
67+
let found = false;
68+
for (const reg of regs) {
69+
if (reg.scope.includes('connection-allowlist/tentative/')) {
70+
found = true;
71+
}
72+
}
73+
assert_false(found, 'Service worker should not be registered');
74+
},
75+
'Payment Request API payment app service worker registration is blocked ' +
76+
'by the connection allowlist.');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Connection-Allowlist: ("*://*:*/connection-allowlist/tentative/payment-app-manifest.json" "*://*:*/images/rgrg-256x256.png" "*://*:*/connection-allowlist/tentative/payment-request-sw-registration-blocked.https.window.js" "*://*:*/resources/testharness.js" "*://*:*/resources/testharnessreport.js" "*://*:*/resources/testdriver.js" "*://*:*/resources/testdriver-vendor.js")

0 commit comments

Comments
 (0)