Skip to content

Commit

Permalink
service worker: Add tests for canvas tainting from video.
Browse files Browse the repository at this point in the history
R=horo

Bug: 780435
Change-Id: Ie8ae9cf8dca55f122a7b4a984ca0a96035b0099f
Reviewed-on: https://chromium-review.googlesource.com/892683
Reviewed-by: Tsuyoshi Horo <horo@chromium.org>
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532814}
  • Loading branch information
mfalken authored and chromium-wpt-export-bot committed Jan 30, 2018
1 parent db29e4e commit 0edd6ae
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 24 deletions.
@@ -1,4 +1,5 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Service Worker: canvas tainting of the fetched image using cached responses</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
Expand Down
@@ -1,4 +1,5 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Service Worker: canvas tainting of the fetched image</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
Expand Down
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Service Worker: canvas tainting of the fetched video using cache responses</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js?pipe=sub"></script>
<script src="resources/fetch-canvas-tainting-tests.js"></script>
<body>
<script>
do_canvas_tainting_tests({
resource_path: base_path() + 'resources/fetch-access-control.py?VIDEO',
cache: true
});
</script>
</body>
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Service Worker: canvas tainting of the fetched video</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js?pipe=sub"></script>
<script src="resources/fetch-canvas-tainting-tests.js"></script>
<body>
<script>
do_canvas_tainting_tests({
resource_path: base_path() + 'resources/fetch-access-control.py?VIDEO',
cache: false
});
</script>
</body>
@@ -1,5 +1,7 @@
import base64
import json
import os
import sys

def main(request, response):
headers = []
Expand Down Expand Up @@ -31,6 +33,10 @@ def main(request, response):
"jBoAAqMGDLwBDAwAEsoCTFWunmQAAAAASUVORK5CYII=")
return headers, body

if "VIDEO" in request.GET:
headers.append(("Content-Type", "video/webm"))
body = open(os.path.join(request.doc_root, "media", "movie_5.ogv"), "rb").read()
return headers, body

username = request.auth.username if request.auth.username else "undefined"
password = request.auth.password if request.auth.username else "undefined"
Expand Down
Expand Up @@ -5,34 +5,65 @@
const TAINTED = 'TAINTED';
const LOAD_ERROR = 'LOAD_ERROR';

// Creates an image element with src=|url| and an optional |cross_origin|
// attibute. Tries to read from the image using a canvas element. Returns
// NOT_TAINTED if the could be read, TAINTED if it could not be read, and
// LOAD_ERROR if loading the image failed.
// Creates an image/video element with src=|url| and an optional |cross_origin|
// attibute. Tries to read from the image/video using a canvas element. Returns
// NOT_TAINTED if it could be read, TAINTED if it could not be read, and
// LOAD_ERROR if loading the image/video failed.
function create_test_case_promise(url, cross_origin) {
return new Promise(resolve => {
const img = document.createElement('img');
if (cross_origin != '') {
img.crossOrigin = cross_origin;
if (url.indexOf('PNGIMAGE') != -1) {
const img = document.createElement('img');
if (cross_origin != '') {
img.crossOrigin = cross_origin;
}
img.onload = function() {
try {
const canvas = document.createElement('canvas');
canvas.width = 100;
canvas.height = 100;
const context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
context.getImageData(0, 0, 100, 100);
resolve(NOT_TAINTED);
} catch (e) {
resolve(TAINTED);
}
};
img.onerror = function() {
resolve(LOAD_ERROR);
}
img.src = url;
return;
}
img.onload = function() {
try {
const canvas = document.createElement('canvas');
canvas.width = 100;
canvas.height = 100;
const context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
context.getImageData(0, 0, 100, 100);
resolve(NOT_TAINTED);
} catch (e) {
resolve(TAINTED);

if (url.indexOf('VIDEO') != -1) {
const video = document.createElement('video');
video.autoplay = true;
if (cross_origin != '') {
video.crossOrigin = cross_origin;
}
};
img.onerror = function() {
resolve(LOAD_ERROR);
video.onplay = function() {
try {
const canvas = document.createElement('canvas');
canvas.width = 100;
canvas.height = 100;
const context = canvas.getContext('2d');
context.drawImage(video, 0, 0);
context.getImageData(0, 0, 100, 100);
resolve(NOT_TAINTED);
} catch (e) {
resolve(TAINTED);
}
};
video.onerror = function() {
resolve(LOAD_ERROR);
}
video.src = url;
return;
}
img.src = url;
});

resolve('unknown resource type');
});
}
</script>
</html>
Expand Up @@ -18,7 +18,7 @@ function canvas_taint_test(url, cross_origin, expected_result) {


// Runs all the tests. The given |params| has these properties:
// * |resource_path|: the relative path to the (image) resource to test.
// * |resource_path|: the relative path to the (image/video) resource to test.
// * |cache|: when true, the service worker bounces responses into
// Cache Storage and back out before responding with them.
function do_canvas_tainting_tests(params) {
Expand Down

0 comments on commit 0edd6ae

Please sign in to comment.