Skip to content

Commit

Permalink
Migrate iframe-based Client Hint WPTs to import client hints lists
Browse files Browse the repository at this point in the history
Introduce two new files into "client-hints/resources" to centralize the
dependencies of client hint token lists across javascript and python
tests:

  - export.js
  - clienthintslist.py

Eventually all Client Hints tests will be migrated to using these lists
to pare down the number of tests touched when creating new hints; this
CL pertains to the tests that depend on
"client-hints/resources/expect-client-hints-headers-iframe.py"

One of change is the query strings fed to
"expect-client-hints-headers-iframe.py" is now generated in
"client-hints/resources/feature-policy-navigation.js".

Another change is fixing tests related to `sec-ch-ua-platform` since we
added platform to client hints returned by default in
getHighEntropyValues in https://crrev.com/c/3389295.

Bug: 1304740
Change-Id: Id41ddbe1ee29c10f7b7c96a283f7eca2abec438b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3549556
Reviewed-by: Ali Beyad <[email protected]>
Commit-Queue: Victor Tan <[email protected]>
Cr-Commit-Position: refs/heads/main@{#993338}
  • Loading branch information
Tanych authored and chromium-wpt-export-bot committed Apr 19, 2022
1 parent b941021 commit 0f27025
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Accept-CH: Sec-CH-Device-Memory, Device-Memory, Sec-CH-DPR, DPR, Sec-CH-Viewport-Width, Viewport-Width
Accept-CH: device-memory, dpr, width, viewport-width, rtt, downlink, ect, sec-ch-ua, sec-ch-ua-arch, sec-ch-ua-platform, sec-ch-ua-model, sec-ch-ua-mobile, sec-ch-ua-full-version, sec-ch-ua-platform-version, sec-ch-prefers-color-scheme, sec-ch-ua-bitness, sec-ch-viewport-height, sec-ch-device-memory, sec-ch-dpr, sec-ch-width, sec-ch-viewport-width, sec-ch-ua-full-version-list, sec-ch-ua-wow64
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="/client-hints/resources/feature-policy-navigation.js"></script>
<script>
(async () => {
await test_frame(
"HTTPS_REMOTE_ORIGIN",
"sec-ch-device-memory=true&device-memory=true&sec-ch-dpr=false&dpr=false&sec-ch-viewport-width=false&viewport-width=false&sec-ch-ua=false&sec-ch-ua-mobile=false",
cross_origin_client_hints,
"",
"Client hints loaded on cross-origin iframe request with feature policy.");
await test_frame(
"HTTPS_ORIGIN",
"sec-ch-device-memory=true&device-memory=true&sec-ch-dpr=false&dpr=false&sec-ch-viewport-width=true&viewport-width=true&sec-ch-ua=true&sec-ch-ua-mobile=false",
same_origin_client_hints,
"",
"Client hints loaded on same-origin iframe request with feature policy.");
await test_frame(
"HTTPS_REMOTE_ORIGIN",
"sec-ch-device-memory=true&device-memory=true&sec-ch-dpr=false&dpr=false&sec-ch-viewport-width=false&viewport-width=false&sec-ch-ua=false&sec-ch-ua-mobile=false",
cross_origin_client_hints,
"",
"Client hints loaded on cross-origin iframe request with feature policy after attempting to set independently.");
})();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Feature-Policy: ch-device-memory *; ch-dpr 'none'; ch-viewport-width 'self'; ch-ua 'self'; ch-ua-mobile 'none'; ch-prefers-color-scheme 'none'
Feature-Policy: ch-device-memory *; ch-dpr 'none'; ch-viewport-width 'self'; ch-ua 'self'; ch-ua-mobile 'none'
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="/client-hints/resources/feature-policy-navigation.js"></script>
<script>
(async () => {
await test_frame(
"HTTPS_REMOTE_ORIGIN",
"sec-ch-device-memory=false&device-memory=false&sec-ch-dpr=false&dpr=false&sec-ch-viewport-width=false&viewport-width=false&sec-ch-ua=true&sec-ch-ua-mobile=true",
expect_iframe_no_hints,
"",
"Client hints not loaded on cross-origin iframe request with no feature policy.");
await test_frame(
"HTTPS_ORIGIN",
"sec-ch-device-memory=true&device-memory=true&sec-ch-dpr=true&dpr=true&sec-ch-viewport-width=true&viewport-width=true&sec-ch-ua=true&sec-ch-ua-mobile=true",
expect_iframe_hints,
"",
"Client hints loaded on same-origin iframe request with no feature policy.");

let allow = "ch-device-memory *; ch-dpr 'src'; ch-viewport-width 'self'; ch-prefers-color-scheme 'none'";
await test_frame(
let allow = "ch-device-memory *; ch-dpr 'none'; ch-viewport-width 'self'; ch-ua 'self'; ch-ua-mobile 'none';"; await test_frame(
"HTTPS_REMOTE_ORIGIN",
"sec-ch-device-memory=true&device-memory=true&sec-ch-dpr=true&dpr=true&sec-ch-viewport-width=false&viewport-width=false&sec-ch-ua=true&sec-ch-ua-mobile=true",
cross_origin_client_hints,
allow,
"Client hints loaded on cross-origin iframe request with allow list.");
await test_frame(
"HTTPS_ORIGIN",
"sec-ch-device-memory=true&device-memory=true&sec-ch-dpr=true&dpr=true&sec-ch-viewport-width=true&viewport-width=true&sec-ch-ua=true&sec-ch-ua-mobile=true",
same_origin_client_hints,
allow,
"Client hints loaded on same-origin iframe request with allow list.");
})();
Expand Down
5 changes: 3 additions & 2 deletions client-hints/http-equiv-accept-ch-iframe.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/feature-policy-navigation.js"></script>
<script>
(async () => {
await test_frame(
"HTTPS_ORIGIN",
"sec-ch-device-memory=false&device-memory=false&sec-ch-dpr=false&dpr=false&sec-ch-viewport-width=false&viewport-width=false&sec-ch-ua=true&sec-ch-ua-mobile=true",
expect_iframe_no_hints,
"",
"Client hints loaded on same-origin iframe should include hints with a default permissions policy of" +
"self and *, but the http-equiv meta tag has a bug and it doesn't impact iframes.");
await test_frame(
"HTTPS_REMOTE_ORIGIN",
"sec-ch-device-memory=false&device-memory=false&sec-ch-dpr=false&dpr=false&sec-ch-viewport-width=false&viewport-width=false&sec-ch-ua=true&sec-ch-ua-mobile=true",
expect_iframe_no_hints,
"",
"Client hints loaded on cross-origin iframe only include hints with a default permissions policy of *.");
})();
Expand Down
5 changes: 3 additions & 2 deletions client-hints/meta-name-accept-ch-iframe.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/feature-policy-navigation.js"></script>
<script>
(async () => {
await test_frame(
"HTTPS_ORIGIN",
"sec-ch-device-memory=true&device-memory=true&sec-ch-dpr=true&dpr=true&sec-ch-viewport-width=true&viewport-width=true&sec-ch-ua=true&sec-ch-ua-mobile=true",
meta_name_client_hints,
"",
"Client hints loaded on same-origin iframe include hints with a default permissions policy of self and *.");
await test_frame(
"HTTPS_REMOTE_ORIGIN",
"sec-ch-device-memory=false&device-memory=false&sec-ch-dpr=false&dpr=false&sec-ch-viewport-width=false&viewport-width=false&sec-ch-ua=true&sec-ch-ua-mobile=true",
expect_iframe_no_hints,
"",
"Client hints loaded on cross-origin iframe only include hints with a default permissions policy of *.");
})();
Expand Down
25 changes: 25 additions & 0 deletions client-hints/resources/clienthintslist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def client_hints_list():
return [b"device-memory",
b"dpr",
# b"width", (Only available for images)
b"viewport-width",
b"rtt",
b"downlink",
b"ect",
b"sec-ch-ua",
b"sec-ch-ua-arch",
b"sec-ch-ua-platform",
b"sec-ch-ua-model",
b"sec-ch-ua-mobile",
b"sec-ch-ua-full-version",
b"sec-ch-ua-platform-version",
b"sec-ch-prefers-color-scheme",
b"sec-ch-ua-bitness",
b"sec-ch-viewport-height",
b"sec-ch-device-memory",
b"sec-ch-dpr",
# b"sec-ch-width", (Only available for images)
b"sec-ch-viewport-width",
b"sec-ch-ua-full-version-list",
b"sec-ch-ua-wow64",
]
6 changes: 4 additions & 2 deletions client-hints/resources/expect-client-hints-headers-iframe.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from wptserve.utils import isomorphic_decode

import importlib
client_hints_list = importlib.import_module("client-hints.resources.clienthintslist").client_hints_list

def main(request, response):
"""
Simple handler that returns an HTML response that passes when the required
Client Hints are received as request headers.
"""
values = [b"Sec-CH-Device-Memory", b"Device-Memory", b"Sec-CH-DPR", b"DPR", b"Sec-CH-Viewport-Width", b"Viewport-Width", b"Sec-CH-UA", b"Sec-CH-UA-Mobile"]

result = u"PASS"
log = u""
for value in values:
for value in client_hints_list():
should = (request.GET[value.lower()] == b"true")
present = request.headers.get(value.lower()) or request.headers.get(value)
if present:
Expand Down
46 changes: 46 additions & 0 deletions client-hints/resources/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const client_hints_list = [
"device-memory",
"dpr",
// "width", (only available for images)
"viewport-width",
"rtt",
"downlink",
"ect",
"sec-ch-ua",
"sec-ch-ua-arch",
"sec-ch-ua-platform",
"sec-ch-ua-model",
"sec-ch-ua-mobile",
"sec-ch-ua-full-version",
"sec-ch-ua-platform-version",
"sec-ch-prefers-color-scheme",
"sec-ch-ua-bitness",
"sec-ch-viewport-height",
"sec-ch-device-memory",
"sec-ch-dpr",
// "sec-ch-width", (Only available for images)
"sec-ch-viewport-width",
"sec-ch-ua-full-version-list",
"sec-ch-ua-wow64",
];

const default_on_client_hints = [
"sec-ch-ua",
"sec-ch-ua-mobile",
"sec-ch-ua-platform",
];

const iframe_src =
"/client-hints/resources/expect-client-hints-headers-iframe.py?";

const expect_iframe_no_hints = iframe_src +
client_hints_list.map((e) => {
if(default_on_client_hints.includes(e)) {
return e+"=true";
} else {
return e+"=false";
}
}).join("&");

const expect_iframe_hints = iframe_src +
client_hints_list.map(e => e+"=true").join("&");
55 changes: 53 additions & 2 deletions client-hints/resources/feature-policy-navigation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
const test_frame = (origin, hints, allow, message, url = "/client-hints/resources/expect-client-hints-headers-iframe.py?") => {
const meta_name_enabled = [
"sec-ch-device-memory",
"device-memory",
"sec-ch-dpr",
"dpr",
"sec-ch-viewport-width",
"viewport-width",
"sec-ch-ua",
"sec-ch-ua-mobile",
"sec-ch-ua-platform",
];

const meta_name_client_hints = iframe_src +
client_hints_list.map((e) => {
if(meta_name_enabled.includes(e)) {
return e+"=true";
} else {
return e+"=false";
}
}).join("&");

const cross_origin_enabled = [
"device-memory",
"sec-ch-device-memory",
"sec-ch-ua-platform",
];

const cross_origin_client_hints = iframe_src +
client_hints_list.map((e) => {
if(cross_origin_enabled.includes(e)) {
return e+"=true";
} else {
return e+"=false";
}
}).join("&");

const same_origin_disabled = [
"dpr",
"sec-ch-dpr",
"sec-ch-ua-mobile",
];

const same_origin_client_hints = iframe_src +
client_hints_list.map((e) => {
if(same_origin_disabled.includes(e)) {
return e+"=false";
} else {
return e+"=true";
}
}).join("&");

const test_frame = (origin, url, allow, message) => {
promise_test(() => {
return new Promise((resolve, reject) => {
let frame = document.createElement('iframe');
Expand All @@ -15,7 +66,7 @@ const test_frame = (origin, hints, allow, message, url = "/client-hints/resource
document.body.appendChild(frame);
// Writing to |frame.src| triggers the navigation, so
// everything else need to happen first.
frame.src = get_host_info()[origin] + url + hints;
frame.src = get_host_info()[origin] + url;
});
}, message);
}
2 changes: 1 addition & 1 deletion client-hints/sandbox/__dir__.headers
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Accept-CH: sec-ch-device-memory, device-memory, sec-ch-dpr, dpr, sec-ch-viewport-width, viewport-width, rtt, downlink, ect
Accept-CH: device-memory, dpr, width, viewport-width, rtt, downlink, ect, sec-ch-ua, sec-ch-ua-arch, sec-ch-ua-platform, sec-ch-ua-model, sec-ch-ua-mobile, sec-ch-ua-full-version, sec-ch-ua-platform-version, sec-ch-prefers-color-scheme, sec-ch-ua-bitness, sec-ch-viewport-height, sec-ch-device-memory, sec-ch-dpr, sec-ch-width, sec-ch-viewport-width, sec-ch-ua-full-version-list, sec-ch-ua-wow64
1 change: 1 addition & 0 deletions client-hints/sandbox/iframe-csp-same-origin.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/util.js"></script>

<script>
Expand Down
1 change: 1 addition & 0 deletions client-hints/sandbox/iframe-csp.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/util.js"></script>

<script>
Expand Down
1 change: 1 addition & 0 deletions client-hints/sandbox/iframe-iframe-popups.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/util.js"></script>

<script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/util.js"></script>

<script>
Expand Down
1 change: 1 addition & 0 deletions client-hints/sandbox/iframe-popups.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/util.js"></script>

<script>
Expand Down
1 change: 1 addition & 0 deletions client-hints/sandbox/iframe-same-origin.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/util.js"></script>

<script>
Expand Down
1 change: 1 addition & 0 deletions client-hints/sandbox/iframe.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/client-hints/resources/export.js"></script>
<script src="resources/util.js"></script>

<script>
Expand Down
23 changes: 0 additions & 23 deletions client-hints/sandbox/resources/util.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
const iframe_src =
"/client-hints/resources/expect-client-hints-headers-iframe.py";

const expect_iframe_no_hints = iframe_src +
"?sec-ch-device-memory=false" +
"&device-memory=false" +
"&sec-ch-dpr=false" +
"&dpr=false" +
"&sec-ch-viewport-width=false" +
"&viewport-width=false" +
"&sec-ch-ua=true" +
"&sec-ch-ua-mobile=true";

const expect_iframe_hints = iframe_src +
"?sec-ch-device-memory=true" +
"&device-memory=true" +
"&sec-ch-dpr=true" +
"&dpr=true" +
"&sec-ch-viewport-width=true" +
"&viewport-width=true" +
"&sec-ch-ua=true" +
"&sec-ch-ua-mobile=true";

function sandbox_iframe_test(sandbox, src, title) {
return promise_test(async t => {
const iframe = document.createElement("iframe");
Expand Down

0 comments on commit 0f27025

Please sign in to comment.