Skip to content

Commit

Permalink
Add WPT tests for feature policy frame policy
Browse files Browse the repository at this point in the history
1. Without specifying allow attribute, frame policy inherits correctly.
2. With allow attribute, frame policy inherits from and overrides header policy
   correctly. Updating allowfullscreen and allowpaymentrequest correctly updates
   frame policy.
3. Frame policy is not affected by the frame's document policy.

Bug: 732003
Change-Id: Ib41f883a779f11c564c91cfc03ff1224330108f5
  • Loading branch information
loonybear authored and chromium-wpt-export-bot committed Feb 27, 2018
1 parent 1305210 commit 2758922
Show file tree
Hide file tree
Showing 9 changed files with 377 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/feature-policy/resources/featurepolicy.js></script>
<!-- Feature-Policy: fullscreen *; -->
<script>
'use strict';
var same_origin = 'https://{{domains[]}}:{{ports[https][0]}}';
var cross_origin = 'https://{{domains[www]}}:{{ports[https][0]}}';
var same_origin_src = '/feature-policy/resources/feature-policy-allowedfeatures.html';
var cross_origin_src = cross_origin + same_origin_src;
var policies = [
{allow: "*", sameOriginTestExpect: true, crossOriginTestExpect: true},
{allow: "'self'", sameOriginTestExpect: true, crossOriginTestExpect: false},
{allow: "'none'", sameOriginTestExpect: false, crossOriginTestExpect: false},
{allow: "'self' " + cross_origin + " https://www.example.com", sameOriginTestExpect: true, crossOriginTestExpect: true}];
var pipe_front = '?pipe=sub|header(Feature-Policy,fullscreen ';
var pipe_end = ';)';
var header_policies = ["*", "'self'", "'none'"];

// Test that frame.policy inherits from parent's header policy when allow
// attribute is not specified.
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, true);
}, 'Test frame policy on same origin iframe inherit from header policy.');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, true);
}, 'Test frame policy on cross origin iframe inherit from header policy.');

// Test that frame policy with allow attribute.
for (var i = 0; i < policies.length; i++) {
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow + '".');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow + '".');
}

// Test that the header policy of the iframe document does not change the
// frame policy.
for (var i = 0; i < policies.length; i++) {
for (var j = 0; j < header_policies.length; j++) {
test(function() {
test_frame_policy(
'fullscreen',
same_origin_src + pipe_front + header_policies[j] + pipe_end,
policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow +
'" and header policy = "Feature-Policy: fullscreen ' + header_policies[j] + ';".');
test(function() {
test_frame_policy(
'fullscreen',
cross_origin_src + pipe_front + header_policies[j] + pipe_end,
policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow +
'" and header policy = "Feature-Policy: fullscreen ' + header_policies[j] + ';".');
}
}

// Test that the allow attribute overwrite allowfullscreen.
for (var i = 0; i < policies.length; i++) {
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';', true);
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow +
'" and allowfullscreen.');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';', true);
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow +
'" and allowfullscreen.');
}
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Feature-Policy: fullscreen *;
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/feature-policy/resources/featurepolicy.js></script>
<!-- Feature-Policy: fullscreen 'self'; -->
<script>
'use strict';
var same_origin = 'https://{{domains[]}}:{{ports[https][0]}}';
var cross_origin = 'https://{{domains[www]}}:{{ports[https][0]}}';
var same_origin_src = '/feature-policy/resources/feature-policy-allowedfeatures.html';
var cross_origin_src = cross_origin + same_origin_src;
var policies = [
{allow: "*", sameOriginTestExpect: true, crossOriginTestExpect: true},
{allow: "'self'", sameOriginTestExpect: true, crossOriginTestExpect: false},
{allow: "'none'", sameOriginTestExpect: false, crossOriginTestExpect: false},
{allow: "'self' " + cross_origin + " https://www.example.com", sameOriginTestExpect: true, crossOriginTestExpect: true}];
var pipe_front = '?pipe=sub|header(Feature-Policy,fullscreen ';
var pipe_end = ';)';
var header_policies = ["*", "'self'", "'none'"];

// Test that frame.policy inherits from parent's header policy when allow
// attribute is not specified.
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, true);
}, 'Test frame policy on same origin iframe inherit from header policy.');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, false);
}, 'Test frame policy on cross origin iframe inherit from header policy.');

// Test that frame policy with allow attribute.
for (var i = 0; i < policies.length; i++) {
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow + '".');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow + '".');
}

// Test that the header policy of the iframe document does not change the
// frame policy.
for (var i = 0; i < policies.length; i++) {
for (var j = 0; j < header_policies.length; j++) {
test(function() {
test_frame_policy(
'fullscreen',
same_origin_src + pipe_front + header_policies[j] + pipe_end,
policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow +
'" and header policy = "Feature-Policy: fullscreen ' + header_policies[j] + ';".');
test(function() {
test_frame_policy(
'fullscreen',
cross_origin_src + pipe_front + header_policies[j] + pipe_end,
policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow +
'" and header policy = "Feature-Policy: fullscreen ' + header_policies[j] + ';".');
}
}

// Test that the allow attribute overwrite allowfullscreen.
for (var i = 0; i < policies.length; i++) {
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';', true);
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow +
'" and allowfullscreen.');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';', true);
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow +
'" and allowfullscreen.');
}
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Feature-Policy: fullscreen 'self';
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/feature-policy/resources/featurepolicy.js></script>
<!-- Feature-Policy: fullscreen 'self' cross_origin https://www.example.com; -->
<script>
'use strict';
var same_origin = 'https://{{domains[]}}:{{ports[https][0]}}';
var cross_origin = 'https://{{domains[www]}}:{{ports[https][0]}}';
var same_origin_src = '/feature-policy/resources/feature-policy-allowedfeatures.html';
var cross_origin_src = cross_origin + same_origin_src;
var policies = [
{allow: "*", sameOriginTestExpect: true, crossOriginTestExpect: true},
{allow: "'self'", sameOriginTestExpect: true, crossOriginTestExpect: false},
{allow: "'none'", sameOriginTestExpect: false, crossOriginTestExpect: false},
{allow: "'self' " + cross_origin + " https://www.example.com", sameOriginTestExpect: true, crossOriginTestExpect: true}];
var pipe_front = '?pipe=sub|header(Feature-Policy,fullscreen ';
var pipe_end = ';)';
var header_policies = ["*", "'self'", "'none'"];

// Test that frame.policy inherits from parent's header policy when allow
// attribute is not specified.
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, true);
}, 'Test frame policy on same origin iframe inherit from header policy.');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, true);
}, 'Test frame policy on cross origin iframe inherit from header policy.');

// Test that frame policy with allow attribute.
for (var i = 0; i < policies.length; i++) {
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow + '".');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow + '".');
}

// Test that the header policy of the iframe document does not change the
// frame policy.
for (var i = 0; i < policies.length; i++) {
for (var j = 0; j < header_policies.length; j++) {
test(function() {
test_frame_policy(
'fullscreen',
same_origin_src + pipe_front + header_policies[j] + pipe_end,
policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow +
'" and header policy = "Feature-Policy: fullscreen ' + header_policies[j] + ';".');
test(function() {
test_frame_policy(
'fullscreen',
cross_origin_src + pipe_front + header_policies[j] + pipe_end,
policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow +
'" and header policy = "Feature-Policy: fullscreen ' + header_policies[j] + ';".');
}
}

// Test that the allow attribute overwrite allowfullscreen.
for (var i = 0; i < policies.length; i++) {
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';', true);
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow +
'" and allowfullscreen.');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';', true);
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow +
'" and allowfullscreen.');
}
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Feature-Policy: fullscreen 'self' https://{{domains[www]}}:{{ports[https][0]}} https://www.example.com;
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/feature-policy/resources/featurepolicy.js></script>
<!-- Feature-Policy: fullscreen 'none'; -->
<script>
'use strict';
var same_origin = 'https://{{domains[]}}:{{ports[https][0]}}';
var cross_origin = 'https://{{domains[www]}}:{{ports[https][0]}}';
var same_origin_src = '/feature-policy/resources/feature-policy-allowedfeatures.html';
var cross_origin_src = cross_origin + same_origin_src;
var policies = [
{allow: "*", sameOriginTestExpect: false, crossOriginTestExpect: false},
{allow: "'self'", sameOriginTestExpect: false, crossOriginTestExpect: false},
{allow: "'none'", sameOriginTestExpect: false, crossOriginTestExpect: false},
{allow: "'self' " + cross_origin + " https://www.example.com", sameOriginTestExpect: false, crossOriginTestExpect: false}];
var pipe_front = '?pipe=sub|header(Feature-Policy,fullscreen ';
var pipe_end = ';)';
var header_policies = ["*", "'self'", "'none'"];

// Test that frame.policy inherits from parent's header policy when allow
// attribute is not specified.
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, false);
}, 'Test frame policy on same origin iframe inherit from header policy.');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, false);
}, 'Test frame policy on cross origin iframe inherit from header policy.');

// Test that frame policy with allow attribute.
for (var i = 0; i < policies.length; i++) {
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow + '".');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow + '".');
}

// Test that the header policy of the iframe document does not change the
// frame policy.
for (var i = 0; i < policies.length; i++) {
for (var j = 0; j < header_policies.length; j++) {
test(function() {
test_frame_policy(
'fullscreen',
same_origin_src + pipe_front + header_policies[j] + pipe_end,
policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow +
'" and header policy = "Feature-Policy: fullscreen ' + header_policies[j] + ';".');
test(function() {
test_frame_policy(
'fullscreen',
cross_origin_src + pipe_front + header_policies[j] + pipe_end,
policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';');
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow +
'" and header policy = "Feature-Policy: fullscreen ' + header_policies[j] + ';".');
}
}

// Test that the allow attribute overwrite allowfullscreen.
for (var i = 0; i < policies.length; i++) {
test(function() {
test_frame_policy(
'fullscreen', same_origin_src, policies[i].sameOriginTestExpect,
'fullscreen ' + policies[i].allow + ';', true);
}, 'Test frame policy on same origin iframe with allow = "' + policies[i].allow +
'" and allowfullscreen.');
test(function() {
test_frame_policy(
'fullscreen', cross_origin_src, policies[i].crossOriginTestExpect,
'fullscreen ' + policies[i].allow + ';', true);
}, 'Test frame policy on cross origin iframe with allow = "' + policies[i].allow +
'" and allowfullscreen.');
}
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Feature-Policy: fullscreen 'none';
29 changes: 29 additions & 0 deletions feature-policy/resources/featurepolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,32 @@ function test_subframe_header_policy(
});
}, test_name);
}

// This function tests that frame policy allows a given feature correctly. A
// feature is allowed in a frame either through inherited policy or specified
// by iframe allow attribute.
// Arguments:
// feature: feature name.
// src: the URL to load in the frame.
// test_expect: boolean value of either the feature should be allowed.
// allow: the allow attribute (container policy) of the iframe.
// allowfullscreen: boolean value of allowfullscreen attribute.
function test_frame_policy(
feature, src, test_expect, allow, allowfullscreen) {
let frame = document.createElement('iframe');
document.body.appendChild(frame);
var frame_policy = frame.policy;
if (typeof allow !== 'undefined') {
frame.setAttribute('allow', allow);
}
if (typeof allowfullscreen !== 'undefined') {
frame.setAttribute('allowfullscreen', allowfullscreen);
// Dynamically update allow attribute update frame.policy
}
frame.src = src;
if (test_expect) {
assert_true(frame_policy.allowedFeatures().includes(feature));
} else {
assert_false(frame_policy.allowedFeatures().includes(feature));
}
}

0 comments on commit 2758922

Please sign in to comment.