Skip to content

Commit

Permalink
Digital Credentials: implement digital-credentials-get permission policy
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=275783
rdar://130821690

Reviewed by NOBODY (OOPS!).

Implements the digital-credentials-get permission policy directive.
This directive allows a site to control whether the .get() method
can be used in the Digital Credential API.

Related spec change:
WICG/digital-credentials#132

* LayoutTests/http/tests/identity/allow-attribute.https-expected.txt: Added.
* LayoutTests/http/tests/identity/allow-attribute.https.html: Added.
* LayoutTests/http/tests/identity/resources/iframe.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/default-permissions-policy.https.sub-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/default-permissions-policy.https.sub.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/disabled-by-permissions-policy.https.sub-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/disabled-by-permissions-policy.https.sub.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/disabled-by-permissions-policy.https.sub.html.headers: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/enabled-on-self-origin-by-permissions-policy.https.sub-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/enabled-on-self-origin-by-permissions-policy.https.sub.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/enabled-on-self-origin-by-permissions-policy.https.sub.html.headers: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/override-permissions-policy.https.sub-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/override-permissions-policy.https.sub.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/digital-credentials/override-permissions-policy.https.sub.html.headers: Added.
* LayoutTests/imported/w3c/web-platform-tests/permissions-policy/resources/digital-credentials-get.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/permissions-policy/resources/permissions-policy.js:
(test_feature_availability):
* LayoutTests/platform/glib/TestExpectations:
* LayoutTests/platform/mac-site-isolation/TestExpectations:
* LayoutTests/platform/mac-wk1/TestExpectations:
* LayoutTests/platform/win/TestExpectations:
* Source/WebCore/Modules/identity/IdentityCredentialsContainer.cpp:
(WebCore::IdentityCredentialsContainer::get):
* Source/WebCore/html/PermissionsPolicy.cpp:
(WebCore::toFeatureNameForLogging):
(WebCore::readFeatureIdentifier):
(WebCore::defaultAllowlistValue):
* Source/WebCore/html/PermissionsPolicy.h:
* Source/WebKit/Shared/WebCoreArgumentCoders.serialization.in:
  • Loading branch information
marcoscaceres committed Jul 16, 2024
1 parent aadfa72 commit 4711fec
Show file tree
Hide file tree
Showing 24 changed files with 435 additions and 2 deletions.
13 changes: 13 additions & 0 deletions LayoutTests/http/tests/identity/allow-attribute.https-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
PASS <iframe src="https://127.0.0.1:8443/identity/resources/iframe.html" data-is-allowed="true"></iframe> is allowed to call get()
PASS <iframe src="https://localhost:8443/identity/resources/iframe.html" data-is-allowed="false"></iframe> is not allowed to call get()
PASS <iframe allow="digital-credentials-get" src="https://127.0.0.1:8443/identity/resources/iframe.html" data-is-allowed="true"></iframe> is allowed to call get()
PASS <iframe allow="digital-credentials-get" src="https://localhost:8443/identity/resources/iframe.html" data-is-allowed="true"></iframe> is allowed to call get()
PASS <iframe allow="digital-credentials-get *" src="https://localhost:8443/identity/resources/iframe.html" data-is-allowed="true"></iframe> is allowed to call get()
PASS <iframe allow="digital-credentials-get *" src="https://127.0.0.1:8443/identity/resources/iframe.html" data-is-allowed="true"></iframe> is allowed to call get()
PASS <iframe allow="digital-credentials-get 'none'" src="https://localhost:8443/identity/resources/iframe.html" data-is-allowed="false"></iframe> is not allowed to call get()
PASS <iframe allow="digital-credentials-get 'none'" src="https://127.0.0.1:8443/identity/resources/iframe.html" data-is-allowed="false"></iframe> is not allowed to call get()
PASS <iframe allow="digital-credentials-get 'self'" src="https://localhost:8443/identity/resources/iframe.html" data-is-allowed="false"></iframe> is not allowed to call get()
PASS <iframe allow="digital-credentials-get 'self'" src="https://127.0.0.1:8443/identity/resources/iframe.html" data-is-allowed="true"></iframe> is allowed to call get()
PASS <iframe allow="digital-credentials-get https://localhost:8443" src="https://localhost:8443/identity/resources/iframe.html" data-is-allowed="true"></iframe> is allowed to call get()
PASS <iframe allow="digital-credentials-get https://localhost:8443" src="https://127.0.0.1:8443/identity/resources/iframe.html" data-is-allowed="false"></iframe> is not allowed to call get()

133 changes: 133 additions & 0 deletions LayoutTests/http/tests/identity/allow-attribute.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<!DOCTYPE html>
<html>
<head>
<title>
Test allow attribute with "digital-credentials-get" and
CredentialsContainer's .get() method
</title>
<script src="/resources/js-test-pre.js"></script>
<script>
testRunner?.dumpAsText();
testRunner?.waitUntilDone();

function waitFor(target, eventName) {
return new Promise((resolve) => {
target.addEventListener(eventName, resolve, { once: true });
});
}
const crossorigin = `https://localhost:8443`;

const iframeDetails = [
{
policy: null,
crossOrigin: false,
isAllowed: true,
},
{
policy: null,
crossOrigin: true,
isAllowed: false,
},
{
policy: "digital-credentials-get",
crossOrigin: false,
isAllowed: true,
},
{
policy: "digital-credentials-get",
crossOrigin: true,
isAllowed: true,
},
{
policy: "digital-credentials-get *",
crossOrigin: true,
isAllowed: true,
},
{
policy: "digital-credentials-get *",
crossOrigin: false,
isAllowed: true,
},
{
policy: "digital-credentials-get 'none'",
crossOrigin: true,
isAllowed: false,
},
{
policy: "digital-credentials-get 'none'",
crossOrigin: false,
isAllowed: false,
},
{
policy: "digital-credentials-get 'self'",
crossOrigin: true,
isAllowed: false,
},
{
policy: "digital-credentials-get 'self'",
crossOrigin: false,
isAllowed: true,
},
{
policy: `digital-credentials-get ${crossorigin}`,
crossOrigin: true,
isAllowed: true,
},
{
policy: `digital-credentials-get ${crossorigin}`,
crossOrigin: false,
isAllowed: false,
},
];

async function loadIframe({ policy, crossOrigin, isAllowed }) {
const origin = crossOrigin ? crossorigin : location.origin;
return new Promise((resolve) => {
const iframe = document.createElement("iframe");
if (policy !== null) {
iframe.allow = policy;
}

iframe.src = new URL(
"/identity/resources/iframe.html",
origin
).href;
iframe.onload = () => resolve(iframe);
iframe.dataset.isAllowed = isAllowed;
document.body.appendChild(iframe);
});
}

async function runTests() {
for (const details of iframeDetails) {
const iframe = await loadIframe(details);
const { isAllowed } = details;
const action = "call get()";
iframe.contentWindow.postMessage({ action }, "*");
const {
data: { exceptionMessage, exceptionName, result },
} = await waitFor(window, "message");
switch (true) {
case isAllowed && exceptionName === "TypeError":
testPassed(
`${iframe.outerHTML} is allowed to ${action}`
);
break;
case exceptionName === "NotAllowedError" && !isAllowed:
testPassed(
`${iframe.outerHTML} is not allowed to ${action}`
);
break;
default:
testFailed(
`${iframe.outerHTML}. ${exceptionName} ${exceptionMessage} - was allowed? ${isAllowed}`
);
}
iframe.remove();
}
testRunner.notifyDone();
}
</script>
</head>
<body onload="runTests()"></body>
</html>
33 changes: 33 additions & 0 deletions LayoutTests/http/tests/identity/resources/iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<meta viewport="width=device-width, initial-scale=1.0" />
<h1>Digital Credentials API</h1>
<script>
window.addEventListener("message", async (event) => {
const { action } = event.data;
let result = null;
let exceptionMessage = "";
let exceptionName = "";
try {
switch (action) {
case "call get()":
// Resutls in TypeError
await navigator.identity.get({
digital: { providers: [] },
});
break;
default:
throw new Error(`Unknown action: ${action}`);
}
} catch (e) {
exceptionMessage = e.message;
exceptionName = e.name;
result = "exception";
} finally {
event.source.postMessage(
{ action, exceptionMessage, exceptionName },
event.origin
);
}
});
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

PASS Permissions-Policy is by default 'self'.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/permissions-policy/resources/permissions-policy.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<body></body>
<script>
"use strict";
const { HTTPS_REMOTE_ORIGIN } = get_host_info();
const same_origin_src =
"/permissions-policy/resources/digital-credentials-get.html";
const cross_origin_src = new URL(same_origin_src, HTTPS_REMOTE_ORIGIN).href;

promise_test(async (test) => {
await test_driver.bless("use activation");
await promise_rejects_js(
test,
TypeError,
navigator.identity.get({ digital: { providers: [] } })
);

await test_feature_availability({
feature_description: "Digital Credential API",
test,
src: same_origin_src,
expect_feature_available: expect_feature_available_default,
is_promise_test: true,
});

await test_feature_availability({
feature_description: "Digital Credential API",
test,
src: cross_origin_src,
expect_feature_available: expect_feature_unavailable_default,
feature_name: "digital-credentials-get",
is_promise_test: true,
});
}, "Permissions-Policy is by default 'self'.");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


FAIL Permissions-Policy header digital-credentials-get=() disallows the top-level document. promise_rejects_dom: function "function() { throw e }" threw object "TypeError: At least one provider must be specified." that is not a DOMException NotAllowedError: property "code" is equal to undefined, expected 0
FAIL Permissions-Policy header digital-credentials-get=() disallows same-origin iframes. assert_false: Digital Credential API expected false got true

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/permissions-policy/resources/permissions-policy.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<body></body>
<script>
"use strict";
const { HTTPS_REMOTE_ORIGIN } = get_host_info();
const same_origin_src =
"/permissions-policy/resources/digital-credentials-get.html";
const cross_origin_src = new URL(same_origin_src, HTTPS_REMOTE_ORIGIN).href;

promise_test(async (test) => {
await test_driver.bless("user activation");
await promise_rejects_dom(
test,
"NotAllowedError",
navigator.identity.get({ digital: { providers: [] } })
);
}, "Permissions-Policy header digital-credentials-get=() disallows the top-level document.");

promise_test(async (test) => {
await test_feature_availability({
feature_description: "Digital Credential API",
test,
src: same_origin_src,
expect_feature_available: expect_feature_unavailable_default,
is_promise_test: true,
needs_focus: true,
});
}, "Permissions-Policy header digital-credentials-get=() disallows same-origin iframes.");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Permissions-Policy: digital-credentials-get=()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

PASS Permissions-Policy header digital-credentials-get=(self) allows the top-level document.
PASS Permissions-Policy header digital-credentials-get=(self) allows same-origin iframes.
PASS Permissions-Policy header digital-credentials-get=(self) disallows cross-origin iframes.
PASS Permissions-Policy header digital-credentials-get=(self) gets overridden by allow attribute.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/permissions-policy/resources/permissions-policy.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<body></body>
<script>
"use strict";
const { HTTPS_REMOTE_ORIGIN } = get_host_info();
const same_origin_src =
"/permissions-policy/resources/digital-credentials-get.html";
const cross_origin_src = new URL(same_origin_src, HTTPS_REMOTE_ORIGIN).href;

promise_test(async (test) => {
await test_driver.bless("use activation");
await promise_rejects_js(
test,
TypeError,
navigator.identity.get({ digital: { providers: [] } })
);
}, "Permissions-Policy header digital-credentials-get=(self) allows the top-level document.");

promise_test(async (test) => {
await test_feature_availability({
feature_description: "Digital Credential API",
test,
src: same_origin_src,
expect_feature_available: expect_feature_available_default,
is_promise_test: true,
needs_focus: true,
});
}, "Permissions-Policy header digital-credentials-get=(self) allows same-origin iframes.");

promise_test(async (test) => {
await test_feature_availability({
feature_description: "Digital Credential API",
test,
src: cross_origin_src,
expect_feature_available: expect_feature_unavailable_default,
is_promise_test: true,
needs_focus: true,
});
}, "Permissions-Policy header digital-credentials-get=(self) disallows cross-origin iframes.");

promise_test(async (test) => {
await test_feature_availability({
feature_description: "Digital Credential API",
test,
src: cross_origin_src,
expect_feature_available: expect_feature_available_default,
feature_name: "digital-credentials-get",
is_promise_test: true,
needs_focus: true,
});
}, "Permissions-Policy header digital-credentials-get=(self) gets overridden by allow attribute.");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Permissions-Policy: digital-credentials-get=(self)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


PASS Header-set policy is overridden in cross-origin iframe using allow attribute.
FAIL Setting digital-credentials-get=(self) disallows the API in same-origin iframes. assert_false: Digital Credential API expected false got true

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/permissions-policy/resources/permissions-policy.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<body></body>
<script>
"use strict";
const { HTTPS_REMOTE_ORIGIN } = get_host_info();
const same_origin_src =
"/permissions-policy/resources/digital-credentials-get.html";
const cross_origin_src = new URL(same_origin_src, HTTPS_REMOTE_ORIGIN).href;

promise_test(async (test) => {
await test_feature_availability({
feature_description: "Digital Credential API",
test,
src: cross_origin_src,
expect_feature_available: expect_feature_available_default,
feature_name: "digital-credentials-get",
is_promise_test: true,
needs_focus: true,
});
}, "Header-set policy is overridden in cross-origin iframe using allow attribute.");

promise_test(async (test) => {
await test_feature_availability({
feature_description: "Digital Credential API",
test,
src: same_origin_src,
expect_feature_available: expect_feature_unavailable_default,
is_promise_test: true,
needs_focus: true,
});
}, "Setting digital-credentials-get=(self) disallows the API in same-origin iframes.");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Permissions-Policy: digital-credentials-get=()
Loading

0 comments on commit 4711fec

Please sign in to comment.