Skip to content

Commit

Permalink
Add JSON modules tests for charset, integrity, and MIME type parameters
Browse files Browse the repository at this point in the history
Add JSON modules tests for the following:
- The <script> element's integrity attribute is respected.
- JSON modules are always decoded as UTF-8 regardless of Content-Type
  or the document's encoding.  The existing coverage we had on this in
  utf8.tenative.html was renamed to charset.html, refactored, and
  expanded.
- Adding parameters to the MIME type doesn't prevent it from being
  evaluated as a JSON MIME type.

Note, some of the existing MIME type tests for */*+json still fail
because it seems the spec issue on these at
whatwg/mimesniff#112 hasn't been fully
resolved. I'll follow up with that separately.

Bug: 1132413
Change-Id: I682de01bbb14b53214fcc16f427a4875f8cfed7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2841103
Reviewed-by: Domenic Denicola <domenic@chromium.org>
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Commit-Queue: Dan Clark <daniec@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#875274}
GitOrigin-RevId: 5d7b584b349398b5d42204b977fc0fca102aebe2
  • Loading branch information
dandclark authored and copybara-github committed Apr 22, 2021
1 parent aeee286 commit 58791d7
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<meta charset="windows-1250">
<title>JSON modules: UTF-8 decoding</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script type="module">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json" assert { type: "json"};
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 even though document's encoding is windows-1250");
</script>
<script type="module">
import json from "../serve-with-content-type.py?fn=json-module/windows-1250.json&ct=text/json%3Bcharset=windows-1250" assert { type: "json"};
test(() => {
assert_not_equals(json.data, "śćążź",
'Should be decoded as UTF-8');
}, "JSON module should be loaded as utf-8 even if it is encoded in windows-1250 and served with a windows-1250 charset response header, and this document's encoding is windows-1250");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>JSON modules: UTF-8 decoding</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json&ct=text/json%3Bcharset=utf-8" assert { type: "json"};
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 when charset=utf8 is specified");
</script>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json&ct=text/json%3Bcharset=shift-jis" assert { type: "json"};
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 when charset=shift-jis is specified");
</script>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json&ct=text/json%3Bcharset=windows-1252" assert { type: "json"};
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 when charset=windows-1252 is specified");
</script>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json&ct=text/json%3Bcharset=utf-7" assert { type: "json"};;
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 when charset=utf-7 is specified");
</script>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/windows-1250.json&ct=text/json%3Bcharset=windows-1250" assert { type: "json"};
test(() => {
assert_not_equals(json.data, "śćążź",
'Should be decoded as UTF-8');
}, "JSON module should be loaded as utf-8 even if it is encoded in windows-1250 and served with a windows-1250 charset response header");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import json from "./data.json" assert { type: "json" };
window.matchesLog.push(`integrity-matches,json:${json.answer}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import json "./data.json" assert { type: "json" };
window.mismatchesLog.push(`integrity-mismatches,json:${json.answer}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>&lt;script> integrity=""</title>
<link rel="help" href="https://html.spec.whatwg.org/multipage/#prepare-a-script">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
window.matchesLog = [];
window.matchesEvents = [];

window.mismatchesLog = [];
window.mismatchesEvents = [];
</script>
<script type="module" src="integrity-matches.js" integrity="sha384-VmQQfGzBiLKdyzw4FA4kL4ohu4tyujV68ddgW1aN/1v3cBZNNBn2gDFdVQxfL7+a" onload="window.matchesEvents.push('load');" onerror="window.matchesEvents.push('error')"></script>
<script type="module" src="integrity-mismatches.js" integrity="sha384-doesnotmatch" onload="window.mismatchesEvents.push('load');" onerror="window.mismatchesEvents.push('error')"></script>

<script type="module">
test(() => {
assert_array_equals(window.matchesLog, ["integrity-matches,json:42"], "The module and its dependency must have executed");
assert_array_equals(window.matchesEvents, ["load"], "The load event must have fired");
}, "The integrity attribute must be verified on the top-level of a module loading a JSON module and allow it to execute when it matches");

test(() => {
assert_array_equals(window.mismatchesLog, [], "The module and its dependency must not have executed");
assert_array_equals(window.mismatchesEvents, ["error"], "The error event must have fired");
}, "The integrity attribute must be verified on the top-level of a module loading a JSON module and not allow it to execute when there's a mismatch");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"data": "śćążź",
"comment": "The data above are five Polish letters, similar to scazz. It can be read correctly only with utf-8 encoding."
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ PASS text/json
PASS application/json
FAIL text/html+json assert_unreached: [object Event] Reached unreachable code
FAIL image/svg+json assert_unreached: [object Event] Reached unreachable code
PASS text/json;boundary=something
PASS text/json;foo=bar
Harness: the test ran to completion.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
const t2 = async_test("application/json");
const t3 = async_test("text/html+json");
const t4 = async_test("image/svg+json");
const t5 = async_test("text/json;boundary=something");
const t6 = async_test("text/json;foo=bar");
</script>
<script type="module" onerror="t1.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=text/json" assert { type: "json"};
Expand All @@ -34,3 +36,11 @@
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=image/svg+json" assert { type: "json"};
check(t4, v);
</script>
<script type="module" onerror="t5.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=text/json;boundary=something" assert { type: "json"};
check(t5, v);
</script>
<script type="module" onerror="t6.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=text/json;foo=bar" assert { type: "json"};
check(t6, v);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"data": "œæ¹¿Ÿ",
"comment": "The data above are five Polish letters, similar to scazz. It can be read correctly only with windows1250 encoding."
}

0 comments on commit 58791d7

Please sign in to comment.