Skip to content

Truly independent web browser

LibWeb: Split SRI metadata on ASCII whitespace

Shannon Boothb1476b891b6f
diff --git a/Libraries/LibWeb/SRI/SRI.cpp b/Libraries/LibWeb/SRI/SRI.cpp
index 5007f5a1677..b76f03ec36e 100644
--- a/Libraries/LibWeb/SRI/SRI.cpp
+++ b/Libraries/LibWeb/SRI/SRI.cpp
@@ -8,10 +8,12 @@
#include <AK/Base64.h>
#include <AK/CharacterTypes.h>
#include <AK/Find.h>
+#include <AK/GenericLexer.h>
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
#include <LibCrypto/Hash/SHA2.h>
#include <LibWeb/SRI/SRI.h>
+#include <LibWebCommon/Infra/CharacterTypes.h>
namespace Web::SRI {
@@ -88,7 +90,15 @@ ErrorOr<Vector<Metadata>> parse_metadata(Utf16View metadata)
Vector<Metadata> result;
// 2. For each item returned by splitting metadata on spaces:
- for (auto item : metadata.split_view(u' ', SplitBehavior::Nothing)) {
+ // AD-HOC: "spaces" is undefined, so split on ASCII whitespace.
+ // https://github.com/w3c/webappsec-subresource-integrity/issues/84
+ Utf16GenericLexer lexer { metadata };
+ while (!lexer.is_eof()) {
+ lexer.ignore_while(Infra::is_ascii_whitespace);
+ auto item = lexer.consume_until(Infra::is_ascii_whitespace);
+ if (item.is_empty())
+ continue;
+
// 1. Let expression-and-options be the result of splitting item on U+003F (?).
// 2. Let algorithm-expression be expression-and-options[0].
auto algorithm_expression = item.substring_view(0, item.find_code_unit_offset(u'?').value_or(item.length_in_code_units()));
diff --git a/Tests/LibWeb/Text/expected/Fetch/integrity-metadata.txt b/Tests/LibWeb/Text/expected/Fetch/integrity-metadata.txt
index 2ce8d354555..c3f5d837b04 100644
--- a/Tests/LibWeb/Text/expected/Fetch/integrity-metadata.txt
+++ b/Tests/LibWeb/Text/expected/Fetch/integrity-metadata.txt
@@ -44,3 +44,7 @@ Mismatching mixed-case SHA-384: PASS
Invalid uppercase stronger digest: PASS
Valid mixed-case strongest digest: PASS
Matching mixed-case digest at same strength: PASS
+Tab-separated mismatching digests: PASS
+Mismatching digest followed by a newline: PASS
+Mismatching digest surrounded by ASCII whitespace: PASS
+Newline-separated valid stronger digest: PASS
diff --git a/Tests/LibWeb/Text/input/Fetch/integrity-metadata.html b/Tests/LibWeb/Text/input/Fetch/integrity-metadata.html
index 8ede054c5cb..b069e466d55 100644
--- a/Tests/LibWeb/Text/input/Fetch/integrity-metadata.html
+++ b/Tests/LibWeb/Text/input/Fetch/integrity-metadata.html
@@ -67,6 +67,10 @@
["Invalid uppercase stronger digest", `${sha256} ${uppercase(wrong(sha384))}`, false],
["Valid mixed-case strongest digest", `${uppercase(wrong(sha384))} ${mixedCase(sha512)}`, true],
["Matching mixed-case digest at same strength", `${wrong(sha256)} ${mixedCase(sha256)}`, true],
+ ["Tab-separated mismatching digests", `${wrong(sha256)}\t${wrong(sha384)}`, false],
+ ["Mismatching digest followed by a newline", `${wrong(sha256)}\n`, false],
+ ["Mismatching digest surrounded by ASCII whitespace", `\t\n\f\r ${wrong(sha256)} \r\n`, false],
+ ["Newline-separated valid stronger digest", `${wrong(sha256)}\n${sha384}`, true],
];
for (const [label, integrity, shouldPass] of cases) {
let response;