Skip to content

Commit a6a0a02

Browse files
committed
Test that BufferSource arguments reject shared buffers
BufferSource in Media Source Extensions, Web Cryptography, Encrypted Media Extensions, and WebTransport is not [AllowShared].
1 parent 8e18278 commit a6a0a02

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// META: title=WebCryptoAPI: BufferSource arguments and members reject shared buffers
2+
3+
// None of the BufferSource arguments of SubtleCrypto, or of the dictionaries it
4+
// takes, are [AllowShared], so a SharedArrayBuffer, or a view onto one, has to
5+
// throw a TypeError.
6+
//
7+
// See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`.
8+
const sharedBuffer = new WebAssembly.Memory({ shared: true, initial: 1, maximum: 1 }).buffer;
9+
10+
function generateAesGcmKey() {
11+
return crypto.subtle.generateKey({ name: "AES-GCM", length: 128 }, false, ["encrypt"]);
12+
}
13+
14+
for (const [kind, buffer] of [["SharedArrayBuffer", sharedBuffer],
15+
["Uint8Array(SharedArrayBuffer)", new Uint8Array(sharedBuffer)]]) {
16+
promise_test(t => {
17+
return promise_rejects_js(t, TypeError, crypto.subtle.digest("SHA-256", buffer));
18+
}, `digest() data is a ${kind}`);
19+
20+
promise_test(async t => {
21+
const key = await generateAesGcmKey();
22+
await promise_rejects_js(t, TypeError,
23+
crypto.subtle.encrypt({ name: "AES-GCM", iv: new Uint8Array(12) }, key, buffer));
24+
}, `encrypt() data is a ${kind}`);
25+
26+
promise_test(async t => {
27+
const key = await generateAesGcmKey();
28+
await promise_rejects_js(t, TypeError,
29+
crypto.subtle.encrypt({ name: "AES-GCM", iv: buffer }, key, new Uint8Array(1)));
30+
}, `encrypt() AesGcmParams.iv is a ${kind}`);
31+
32+
promise_test(async t => {
33+
const key = await generateAesGcmKey();
34+
await promise_rejects_js(t, TypeError,
35+
crypto.subtle.encrypt({ name: "AES-GCM", iv: new Uint8Array(12), additionalData: buffer },
36+
key, new Uint8Array(1)));
37+
}, `encrypt() AesGcmParams.additionalData is a ${kind}`);
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// META: title=Encrypted Media Extensions: Test handling of shared buffers
2+
// META: script=/encrypted-media/util/utils.js
3+
4+
// None of the BufferSource arguments in Encrypted Media Extensions are
5+
// [AllowShared], so a SharedArrayBuffer, or a view onto one, has to throw a
6+
// TypeError.
7+
//
8+
// See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`.
9+
const sharedBuffer = new WebAssembly.Memory({ shared: true, initial: 1, maximum: 1 }).buffer;
10+
11+
async function createMediaKeys() {
12+
const access = await navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration());
13+
return access.createMediaKeys();
14+
}
15+
16+
for (const [kind, buffer] of [['SharedArrayBuffer', sharedBuffer],
17+
['Uint8Array(SharedArrayBuffer)', new Uint8Array(sharedBuffer)]]) {
18+
// Converting the argument precedes the check for server certificate
19+
// support, which Clear Key does not have, so a user agent that wrongly
20+
// accepts the shared buffer resolves with false instead.
21+
promise_test(async t => {
22+
const mediaKeys = await createMediaKeys();
23+
await promise_rejects_js(t, TypeError, mediaKeys.setServerCertificate(buffer));
24+
}, `setServerCertificate() serverCertificate is a ${kind}`);
25+
26+
promise_test(async t => {
27+
const keyStatuses = (await createMediaKeys()).createSession().keyStatuses;
28+
assert_throws_js(TypeError, () => keyStatuses.has(buffer));
29+
}, `MediaKeyStatusMap.has() keyId is a ${kind}`);
30+
31+
promise_test(async t => {
32+
const keyStatuses = (await createMediaKeys()).createSession().keyStatuses;
33+
assert_throws_js(TypeError, () => keyStatuses.get(buffer));
34+
}, `MediaKeyStatusMap.get() keyId is a ${kind}`);
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// META: title=SourceBuffer.appendBuffer() shared buffer test cases
2+
// META: script=mediasource-util.js
3+
4+
// The data argument of appendBuffer() is not [AllowShared], so a
5+
// SharedArrayBuffer, or a view onto one, has to throw a TypeError.
6+
//
7+
// See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`.
8+
const sharedBuffer = new WebAssembly.Memory({ shared: true, initial: 1, maximum: 1 }).buffer;
9+
10+
for (const [kind, data] of [["SharedArrayBuffer", sharedBuffer],
11+
["Uint8Array(SharedArrayBuffer)", new Uint8Array(sharedBuffer)]]) {
12+
mediasource_test((test, mediaElement, mediaSource) => {
13+
const sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
14+
assert_throws_js(TypeError, () => sourceBuffer.appendBuffer(data),
15+
`appendBuffer() threw an exception when passed a ${kind}.`);
16+
test.done();
17+
}, `Test appendBuffer() with a ${kind}`);
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// META: global=window,worker
2+
// META: script=resources/webtransport-test-helpers.sub.js
3+
4+
// The value member of WebTransportHash is not [AllowShared], so a
5+
// SharedArrayBuffer, or a view onto one, has to throw a TypeError.
6+
//
7+
// See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`.
8+
const sharedBuffer = new WebAssembly.Memory({ shared: true, initial: 1, maximum: 1 }).buffer;
9+
10+
for (const [kind, value] of [['SharedArrayBuffer', sharedBuffer],
11+
['Uint8Array(SharedArrayBuffer)', new Uint8Array(sharedBuffer)]]) {
12+
test(() => {
13+
assert_throws_js(TypeError, () => {
14+
const wt = new WebTransport(
15+
webtransport_url('echo.py'),
16+
{ serverCertificateHashes: [{ algorithm: 'sha-256', value }] });
17+
// Only reached when the shared buffer was wrongly accepted. Close the
18+
// session so the test does not leave a connection attempt running.
19+
wt.ready.catch(() => {});
20+
wt.closed.catch(() => {});
21+
wt.close();
22+
});
23+
}, `WebTransport constructor should reject serverCertificateHashes value of type '${kind}'`);
24+
}

0 commit comments

Comments
 (0)