Skip to content

Commit 1116c83

Browse files
[Language Detector] Integrate Permissions Policy
Updates the Language Detector API from being accessed from third-party iframes, unless there is a permission policy being delefated from the top-level Window. Spec: webmachinelearning/translation-api#45 Bug: 402166942 Change-Id: Ifbdabc3c8ee1098530dfabddb0d0c157aa40beb2 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6468598 Reviewed-by: Nathan Memmott <[email protected]> Commit-Queue: Christine Hollingsworth <[email protected]> Cr-Commit-Position: refs/heads/main@{#1448992}
1 parent 7f33a33 commit 1116c83

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<script src="/common/get-host-info.sub.js"></script>
5+
<body></body>
6+
<script>
7+
'use strict';
8+
9+
const { HTTPS_ORIGIN, HTTPS_NOTSAMESITE_ORIGIN } = get_host_info();
10+
const PATH = location.pathname.substring(0, location.pathname.lastIndexOf('/') + 1);
11+
const IFRAME_PATH = PATH + 'resources/iframe-helper.html';
12+
13+
function run_iframe_test(iframe, test_name) {
14+
iframe.contentWindow.postMessage({type: test_name}, '*');
15+
const {promise, resolve, reject} = Promise.withResolvers();
16+
17+
window.onmessage = message => {
18+
if (message.data.success) {
19+
resolve(message.data.success);
20+
} else {
21+
reject(message.data.err)
22+
}
23+
}
24+
25+
return promise;
26+
}
27+
28+
function load_iframe(src, permission_policy) {
29+
let iframe = document.createElement('iframe');
30+
return new Promise((resolve, reject) => {
31+
iframe.onload = () => {
32+
resolve(iframe);
33+
}
34+
iframe.src = src;
35+
iframe.allow = permission_policy;
36+
document.body.appendChild(iframe);
37+
});
38+
}
39+
40+
promise_test(async t => {
41+
const src = HTTPS_NOTSAMESITE_ORIGIN + IFRAME_PATH;
42+
const iframe = await load_iframe(src, /*permission_policy=*/"");
43+
await promise_rejects_dom(t, 'NotAllowedError',
44+
run_iframe_test(iframe, "LanguageDetectorCreate"));
45+
}, "Throw a 'NotAllowedError' when creating Language Detector within cross-origin iframe");
46+
47+
promise_test(async t => {
48+
const src = HTTPS_NOTSAMESITE_ORIGIN + IFRAME_PATH;
49+
const iframe = await load_iframe(src, "languageDetector");
50+
51+
assert_equals(
52+
await run_iframe_test(iframe, "LanguageDetectorCreate"), 'Success');
53+
}, "Language Detector can be created within cross-origin iframe with permission policy");
54+
55+
promise_test(async t => {
56+
const src = HTTPS_ORIGIN + IFRAME_PATH;
57+
const iframe = await load_iframe(src, /*permission_policy=*/"");
58+
59+
assert_equals(
60+
await run_iframe_test(iframe, "LanguageDetectorCreate"), 'Success');
61+
}, "Language Detector can be used within same-origin iframe");
62+
63+
promise_test(async t => {
64+
const src = HTTPS_NOTSAMESITE_ORIGIN + IFRAME_PATH;
65+
const iframe = await load_iframe(src, /*permission_policy=*/"");
66+
67+
assert_equals(
68+
await run_iframe_test(iframe, "LanguageDetectorAvailability"), 'unavailable');
69+
}, "Language Detector is unavailable within cross-origin iframe");
70+
71+
promise_test(async t => {
72+
const src = HTTPS_NOTSAMESITE_ORIGIN + IFRAME_PATH;
73+
const iframe = await load_iframe(src, "languageDetector");
74+
75+
assert_in_array(
76+
await run_iframe_test(iframe, "LanguageDetectorAvailability"),
77+
['downloadable', 'downloading', 'available']);
78+
}, "Language Detector is available within cross-origin iframe with permission policy");
79+
80+
promise_test(async t => {
81+
const src = HTTPS_ORIGIN + IFRAME_PATH;
82+
const iframe = await load_iframe(src, /*permission_policy=*/"");
83+
84+
assert_in_array(
85+
await run_iframe_test(iframe, "LanguageDetectorAvailability"),
86+
['downloadable', 'downloading', 'available']);
87+
}, "LanguageDetector is available within same-origin iframe");
88+
89+
</script>

Diff for: ai/language_detection/resources/iframe-helper.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE HTML>
2+
<meta charset="utf-8">
3+
<script>
4+
window.onmessage = async message => {
5+
switch (message.data.type) {
6+
case 'LanguageDetectorCreate':
7+
LanguageDetector.create()
8+
.then(t => parent.postMessage({success: 'Success'}))
9+
.catch(err => parent.postMessage({err}));
10+
break;
11+
case 'LanguageDetectorAvailability':
12+
LanguageDetector.availability()
13+
.then(t => parent.postMessage({success: availability}))
14+
.catch(err => parent.postMessage({err}));
15+
break;
16+
};
17+
};
18+
</script>

0 commit comments

Comments
 (0)