From 66d5a0aaab19ba02784330e22d23b7af173dc659 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Fri, 20 Apr 2018 15:24:18 +0200 Subject: [PATCH 1/8] Parsing Content-Length --- fetch/content-length/parsing.window.js | 19 +++++++++++ .../resources/content-length.py | 9 +++++ .../resources/content-lengths.json | 34 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 fetch/content-length/parsing.window.js create mode 100644 fetch/content-length/resources/content-length.py create mode 100644 fetch/content-length/resources/content-lengths.json diff --git a/fetch/content-length/parsing.window.js b/fetch/content-length/parsing.window.js new file mode 100644 index 00000000000000..ab503033bc6385 --- /dev/null +++ b/fetch/content-length/parsing.window.js @@ -0,0 +1,19 @@ +promise_test(() => { + return fetch("resources/content-lengths.json").then(res => res.json()).then(runTests); +}, "Loading JSONā€¦"); + +function runTests(testUnits) { + testUnits.forEach(testUnit => { + const input = encodeURIComponent(testUnit.input); + promise_test(t => { + const result = fetch("resources/content-length.py?length=" + input); + if (testUnit.output === null) { + return promise_rejects(t, new TypeError(), result); + } else { + return result.then(res => res.text()).then(text => { + assert_equals(text.length, testUnit.output); + }); + } + }, "Content-Length: " + input); + }); +} diff --git a/fetch/content-length/resources/content-length.py b/fetch/content-length/resources/content-length.py new file mode 100644 index 00000000000000..2365d2d9570863 --- /dev/null +++ b/fetch/content-length/resources/content-length.py @@ -0,0 +1,9 @@ +def main(request, response): + response.add_required_headers = False + output = "HTTP/1.1 200 OK\r\n" + output += "Content-Type: text/plain;charset=UTF-8\r\n" + output += request.GET.first("length") + "\r\n" + output += "\r\n" + output += "Fact: this is really forty-two bytes long." + response.writer.write(output) + response.write() diff --git a/fetch/content-length/resources/content-lengths.json b/fetch/content-length/resources/content-lengths.json new file mode 100644 index 00000000000000..6e3a5d6faf49fc --- /dev/null +++ b/fetch/content-length/resources/content-lengths.json @@ -0,0 +1,34 @@ +[ + { + "input": "Content-Length: 42", + "output": 42 + }, + { + "input": "Content-Length: 42,42", + "output": 42 + }, + { + "input": "Content-Length: 42\r\nContent-Length: 42", + "output": 42 + }, + { + "input": "Content-Length: 30", + "output": 30 + }, + { + "input": "Content-Length: 42,30", + "output": null + }, + { + "input": "Content-Length: 42\r\nContent-Length: 30", + "output": null + }, + { + "input": "Content-Length: aaaah", + "output": null + }, + { + "input": "Content-Length: 42s", + "output": 42 + } +] From 48c084d1b9d3400ee2c24eb95ef789f42a867a62 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Fri, 27 Apr 2018 13:46:49 +0200 Subject: [PATCH 2/8] correct script and add more tests --- .../resources/content-length.py | 2 +- .../resources/content-lengths.json | 34 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/fetch/content-length/resources/content-length.py b/fetch/content-length/resources/content-length.py index 2365d2d9570863..77d114660b1e9d 100644 --- a/fetch/content-length/resources/content-length.py +++ b/fetch/content-length/resources/content-length.py @@ -6,4 +6,4 @@ def main(request, response): output += "\r\n" output += "Fact: this is really forty-two bytes long." response.writer.write(output) - response.write() + response.close_connection = True diff --git a/fetch/content-length/resources/content-lengths.json b/fetch/content-length/resources/content-lengths.json index 6e3a5d6faf49fc..994937f37c7f6d 100644 --- a/fetch/content-length/resources/content-lengths.json +++ b/fetch/content-length/resources/content-lengths.json @@ -11,6 +11,10 @@ "input": "Content-Length: 42\r\nContent-Length: 42", "output": 42 }, + { + "input": "Content-Length: 42\r\nContent-Length: 42,42", + "output": 42 + }, { "input": "Content-Length: 30", "output": 30 @@ -19,16 +23,44 @@ "input": "Content-Length: 42,30", "output": null }, + { + "input": "Content-Length: 30,42", + "output": null + }, { "input": "Content-Length: 42\r\nContent-Length: 30", "output": null }, { - "input": "Content-Length: aaaah", + "input": "Content-Length: 30\r\nContent-Length: 42", + "output": null + }, + { + "input": "Content-Length: aaaah\r\nContent-Length: nah", + "output": null + }, + { + "input": "Content-Length: aaaah, nah", "output": null }, + { + "input": "Content-Length: aaaah\r\nContent-Length: aaaah", + "output": 42 + }, + { + "input": "Content-Length: aaaah, aaaah", + "output": 42 + }, + { + "input": "Content-Length: aaaah", + "output": 42 + }, { "input": "Content-Length: 42s", "output": 42 + }, + { + "input": "Content-Length: -1", + "output": 42 } ] From 3ca383746a4962a3bea20382107177aebf1f7e95 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Mon, 15 Oct 2018 11:02:56 +0200 Subject: [PATCH 3/8] more tests --- .../resources/content-lengths.json | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/fetch/content-length/resources/content-lengths.json b/fetch/content-length/resources/content-lengths.json index 994937f37c7f6d..482749ef0aa193 100644 --- a/fetch/content-length/resources/content-lengths.json +++ b/fetch/content-length/resources/content-lengths.json @@ -35,6 +35,22 @@ "input": "Content-Length: 30\r\nContent-Length: 42", "output": null }, + { + "input": "Content-Length: 42,", + "output": null + }, + { + "input": "Content-Length: ,42", + "output": null + }, + { + "input": "Content-Length: 42\r\nContent-Length: \t", + "output": null + }, + { + "input": "Content-Length: \r\nContent-Length: 42", + "output": null + }, { "input": "Content-Length: aaaah\r\nContent-Length: nah", "output": null @@ -59,6 +75,10 @@ "input": "Content-Length: 42s", "output": 42 }, + { + "input": "Content-Length: 30s", + "output": 42 + }, { "input": "Content-Length: -1", "output": 42 From 4837ae378782923ebc14ae0d7666df851169ff80 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Mon, 15 Oct 2018 11:08:25 +0200 Subject: [PATCH 4/8] test more with 30 --- .../resources/content-lengths.json | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/fetch/content-length/resources/content-lengths.json b/fetch/content-length/resources/content-lengths.json index 482749ef0aa193..8a07ca5ec84174 100644 --- a/fetch/content-length/resources/content-lengths.json +++ b/fetch/content-length/resources/content-lengths.json @@ -19,6 +19,18 @@ "input": "Content-Length: 30", "output": 30 }, + { + "input": "Content-Length: 30,30", + "output": 30 + }, + { + "input": "Content-Length: 30\r\nContent-Length: 30", + "output": 30 + }, + { + "input": "Content-Length: 30\r\nContent-Length: 30,30", + "output": 30 + }, { "input": "Content-Length: 42,30", "output": null @@ -36,19 +48,19 @@ "output": null }, { - "input": "Content-Length: 42,", + "input": "Content-Length: 30,", "output": null }, { - "input": "Content-Length: ,42", + "input": "Content-Length: ,30", "output": null }, { - "input": "Content-Length: 42\r\nContent-Length: \t", + "input": "Content-Length: 30\r\nContent-Length: \t", "output": null }, { - "input": "Content-Length: \r\nContent-Length: 42", + "input": "Content-Length: \r\nContent-Length: 30", "output": null }, { From 36c7bd65d491626cceefe8df0d39e6f8283a6909 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Mon, 15 Oct 2018 11:37:30 +0200 Subject: [PATCH 5/8] no hex or oct digits --- .../resources/content-lengths.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/fetch/content-length/resources/content-lengths.json b/fetch/content-length/resources/content-lengths.json index 8a07ca5ec84174..336da37fe751f3 100644 --- a/fetch/content-length/resources/content-lengths.json +++ b/fetch/content-length/resources/content-lengths.json @@ -94,5 +94,21 @@ { "input": "Content-Length: -1", "output": 42 + }, + { + "input": "Content-Length: 0x20", + "output": 42 + }, + { + "input": "Content-Length: 030", + "output": 30 + }, + { + "input": "Content-Length: 030\r\nContent-Length: 30", + "output": null + }, + { + "input": "Content-Length: 030, 30", + "output": null } ] From cb1bb1dcc40140d2ada14b6016922a494692a9fe Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Fri, 26 Feb 2021 17:32:21 +0100 Subject: [PATCH 6/8] add two more tests --- fetch/content-length/parsing.window.js | 2 +- fetch/content-length/resources/content-length.py | 10 +++++----- fetch/content-length/resources/content-lengths.json | 8 ++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/fetch/content-length/parsing.window.js b/fetch/content-length/parsing.window.js index ab503033bc6385..b8b66394591861 100644 --- a/fetch/content-length/parsing.window.js +++ b/fetch/content-length/parsing.window.js @@ -8,7 +8,7 @@ function runTests(testUnits) { promise_test(t => { const result = fetch("resources/content-length.py?length=" + input); if (testUnit.output === null) { - return promise_rejects(t, new TypeError(), result); + return promise_rejects_js(t, TypeError, result); } else { return result.then(res => res.text()).then(text => { assert_equals(text.length, testUnit.output); diff --git a/fetch/content-length/resources/content-length.py b/fetch/content-length/resources/content-length.py index 77d114660b1e9d..b4583b8da37197 100644 --- a/fetch/content-length/resources/content-length.py +++ b/fetch/content-length/resources/content-length.py @@ -1,9 +1,9 @@ def main(request, response): response.add_required_headers = False - output = "HTTP/1.1 200 OK\r\n" - output += "Content-Type: text/plain;charset=UTF-8\r\n" - output += request.GET.first("length") + "\r\n" - output += "\r\n" - output += "Fact: this is really forty-two bytes long." + output = b"HTTP/1.1 200 OK\r\n" + output += b"Content-Type: text/plain;charset=UTF-8\r\n" + output += request.GET.first(b"length") + b"\r\n" + output += b"\r\n" + output += b"Fact: this is really forty-two bytes long." response.writer.write(output) response.close_connection = True diff --git a/fetch/content-length/resources/content-lengths.json b/fetch/content-length/resources/content-lengths.json index 336da37fe751f3..f594ecb187f248 100644 --- a/fetch/content-length/resources/content-lengths.json +++ b/fetch/content-length/resources/content-lengths.json @@ -110,5 +110,13 @@ { "input": "Content-Length: 030, 30", "output": null + }, + { + "input": "Content-Length: \"30\"", + "output": null + }, + { + "input": "Content-Length:30\r\nContent-Length:,\r\nContent-Length:30", + "output": null } ] From 95215601825742362b8f452671b6cbbc5e34af70 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Fri, 26 Feb 2021 18:17:57 +0100 Subject: [PATCH 7/8] single header bogus value is ignored --- fetch/content-length/resources/content-lengths.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch/content-length/resources/content-lengths.json b/fetch/content-length/resources/content-lengths.json index f594ecb187f248..bf9863af229df3 100644 --- a/fetch/content-length/resources/content-lengths.json +++ b/fetch/content-length/resources/content-lengths.json @@ -113,7 +113,7 @@ }, { "input": "Content-Length: \"30\"", - "output": null + "output": 42 }, { "input": "Content-Length:30\r\nContent-Length:,\r\nContent-Length:30", From 2b763a08fb05478387fe0721d4fe2bc8c9d199c7 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Mon, 1 Mar 2021 16:52:56 +0100 Subject: [PATCH 8/8] more tests \o/ --- fetch/content-length/parsing.window.js | 2 +- fetch/content-length/resources/content-lengths.json | 4 ++++ fetch/content-length/too-long.window.js | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 fetch/content-length/too-long.window.js diff --git a/fetch/content-length/parsing.window.js b/fetch/content-length/parsing.window.js index b8b66394591861..73fbb482e042c1 100644 --- a/fetch/content-length/parsing.window.js +++ b/fetch/content-length/parsing.window.js @@ -14,6 +14,6 @@ function runTests(testUnits) { assert_equals(text.length, testUnit.output); }); } - }, "Content-Length: " + input); + }, input); }); } diff --git a/fetch/content-length/resources/content-lengths.json b/fetch/content-length/resources/content-lengths.json index bf9863af229df3..dac9c82dc09784 100644 --- a/fetch/content-length/resources/content-lengths.json +++ b/fetch/content-length/resources/content-lengths.json @@ -118,5 +118,9 @@ { "input": "Content-Length:30\r\nContent-Length:,\r\nContent-Length:30", "output": null + }, + { + "input": "Content-Length: ", + "output": 42 } ] diff --git a/fetch/content-length/too-long.window.js b/fetch/content-length/too-long.window.js new file mode 100644 index 00000000000000..f8cefaa9c23fbf --- /dev/null +++ b/fetch/content-length/too-long.window.js @@ -0,0 +1,4 @@ +promise_test(async t => { + const result = await fetch(`resources/content-length.py?length=${encodeURIComponent("Content-Length: 50")}`); + await promise_rejects_js(t, TypeError, result.text()); +}, "Content-Length header value of network response exceeds response body");