Skip to content

Commit 7040121

Browse files
committed
Test webrtc/content-security-policy integration
...as specified in: - w3c/webappsec-csp#457 - w3c/webrtc-extensions#81
1 parent db7bc43 commit 7040121

File tree

6 files changed

+158
-0
lines changed

6 files changed

+158
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta
6+
http-equiv="Content-Security-Policy"
7+
content="default-src 'none'; script-src 'self' 'unsafe-inline'">
8+
<title>webrtc allowed with default-src 'none'</title>
9+
<script src="/resources/testharness.js"></script>
10+
<script src="/resources/testharnessreport.js"></script>
11+
<script src="webrtc.js"></script>
12+
</head>
13+
14+
<body>
15+
<script>
16+
expectAllow();
17+
</script>
18+
<div id="log"></div>
19+
</body>
20+
21+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta http-equiv="Content-Security-Policy" content="webrtc 'allow';">
6+
<title>webrtc allowed with an explicit webrtc allowed policy</title>
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
<script src="webrtc.js"></script>
10+
</head>
11+
12+
<body>
13+
<script>
14+
expectAllow();
15+
</script>
16+
<div id="log"></div>
17+
</body>
18+
19+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>webrtc allowed with no policy</title>
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
<script src="webrtc.js"></script>
9+
</head>
10+
11+
<body>
12+
<script>
13+
expectAllow();
14+
</script>
15+
<div id="log"></div>
16+
</body>
17+
18+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta http-equiv="Content-Security-Policy" content="webrtc 'block';">
6+
<title>webrtc blocked with an explicit webrtc blocked policy</title>
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
<script src="webrtc.js"></script>
10+
</head>
11+
12+
<body>
13+
<script>
14+
expectBlock();
15+
</script>
16+
<div id="log"></div>
17+
</body>
18+
19+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta http-equiv="Content-Security-Policy" content="webrtc 'unrecognized';">
6+
<title>webrtc blocked with an unrecognized explicit webrtc policy</title>
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
<script src="webrtc.js"></script>
10+
</head>
11+
12+
<body>
13+
<script>
14+
expectBlock();
15+
</script>
16+
<div id="log"></div>
17+
</body>
18+
19+
</html>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
// Creates two RTCPeerConnection and tries to connect them. Returns
3+
// "allowed" if the connection is permitted, "blocked" if it is
4+
// blocked on both sides and "inconsistent" in the even that the
5+
// result is the same on both sides (should never happen).
6+
async function tryConnect() {
7+
const iceServers = [{urls: "stun:stun.l.google.com:19302"}];
8+
const pc1 = new RTCPeerConnection({iceServers});
9+
const pc2 = new RTCPeerConnection({iceServers});
10+
11+
// Returns a promise which resolves to a boolean which is true
12+
// if and only if pc.iceConnectionState settles in the "failed"
13+
// state, and never transitions to any state other than "new"
14+
// or "failed."
15+
const pcFailed = (pc) => {
16+
pc.onicecandidate = ({candidate}) => {
17+
pc.addIceCandidate(candidate);
18+
};
19+
return new Promise((resolve, _reject) => {
20+
pc.onicegatheringstatechange = (e) => {
21+
if(pc.iceGatheringState === "complete") {
22+
resolve(pc.iceConnectionState === "failed");
23+
} else if(pc.iceConnectionState !== "new") {
24+
resolve(false);
25+
}
26+
};
27+
});
28+
}
29+
pc1Failed = pcFailed(pc1);
30+
pc2Failed = pcFailed(pc2);
31+
32+
// Creating a data channel is necessary to induce negotiation:
33+
const channel = pc1.createDataChannel('test');
34+
35+
// Usual webrtc signaling dance:
36+
const offer = await pc1.createOffer();
37+
await pc1.setLocalDescription(offer);
38+
await pc2.setRemoteDescription(pc1.localDescription);
39+
const answer = await pc2.createAnswer();
40+
await pc2.setLocalDescription(answer);
41+
await pc1.setRemoteDescription(pc2.localDescription);
42+
43+
const failed1 = await pc1Failed;
44+
const failed2 = await pc2Failed;
45+
if(failed1 && failed2) {
46+
return 'blocked';
47+
} else if(!failed1 && !failed2) {
48+
return 'allowed';
49+
} else {
50+
return 'inconsistent';
51+
}
52+
}
53+
54+
async function expectAllow() {
55+
promise_test(async () => assert_equals(await tryConnect(), 'allowed'));
56+
}
57+
58+
async function expectBlock() {
59+
promise_test(async () => assert_equals(await tryConnect(), 'blocked'));
60+
}
61+
62+
// vim: set ts=4 sw=4 et :

0 commit comments

Comments
 (0)