Skip to content

Truly independent web browser

LibWeb: Accept SRI hash algorithm tokens in any ASCII case

Jelle Raaijmakerse3f3f42c4e26
diff --git a/Libraries/LibWeb/SRI/SRI.cpp b/Libraries/LibWeb/SRI/SRI.cpp
index f904fe82193..03df9749259 100644
--- a/Libraries/LibWeb/SRI/SRI.cpp
+++ b/Libraries/LibWeb/SRI/SRI.cpp
@@ -22,10 +22,13 @@ constexpr Array supported_hash_functions {
"sha512"sv,
};
-static bool is_supported_hash_function(Utf16View algorithm)
+// https://w3c.github.io/webappsec-subresource-integrity/#valid-sri-hash-algorithm-token
+static bool is_valid_sri_hash_algorithm_token(Utf16View string)
{
+ // A string is a valid SRI hash algorithm token if its ASCII lowercase is contained in the valid SRI hash algorithm
+ // token set.
for (auto supported_hash_function : supported_hash_functions) {
- if (algorithm == supported_hash_function)
+ if (string.equals_ignoring_ascii_case(supported_hash_function))
return true;
}
return false;
@@ -99,21 +102,23 @@ ErrorOr<Vector<Metadata>> parse_metadata(Utf16View metadata)
// 5. Let algorithm be hash-expr-token-list[0].
auto algorithm = hash_expr_token_list[0];
- // 6. If hash-expr-token-list[1] exists, set base64-value to hash-expr-token-list[1].
+ // 6. If algorithm is not a valid SRI hash algorithm token, then continue.
+ if (!is_valid_sri_hash_algorithm_token(algorithm))
+ continue;
+
+ // 7. If hash-expr-token-list[1] exists, set base64-value to hash-expr-token-list[1].
// INTEROP: A base64url-encoded value contains "-" itself, so like other engines, take everything after the
// first "-" as the value.
if (hash_expr_token_list.size() > 1)
base64_value = hash_expression.substring_view(algorithm.length_in_code_units() + 1);
- // 7. If algorithm is not a hash function recognized by the user agent, continue.
- if (!is_supported_hash_function(algorithm))
- continue;
-
// 8. Let metadata be the ordered map «["alg" → algorithm, "val" → base64-value]».
// Note: Since no options are defined (see the §3.1 Integrity metadata), a corresponding entry is not set in metadata.
// If options are defined in a future version, hash-with-opt-token-list[1] can be utilized as options.
+ // AD-HOC: Store algorithm in ASCII lowercase, since later steps look it up in the valid SRI hash algorithm token
+ // set, which only contains lowercase tokens.
auto metadata = Metadata {
- .algorithm = TRY(algorithm.to_utf8()),
+ .algorithm = algorithm.to_ascii_lowercase().to_utf8(),
.base64_value = TRY(base64_value.to_utf8()),
.options = {},
};
diff --git a/Tests/LibWeb/Text/expected/Fetch/integrity-metadata.txt b/Tests/LibWeb/Text/expected/Fetch/integrity-metadata.txt
index 0022ffc98ca..5a785fd0ac3 100644
--- a/Tests/LibWeb/Text/expected/Fetch/integrity-metadata.txt
+++ b/Tests/LibWeb/Text/expected/Fetch/integrity-metadata.txt
@@ -16,3 +16,10 @@ Invalid stronger digest: PASS
Invalid strongest digest: PASS
Valid strongest digest: PASS
Matching digest at same strength: PASS
+Uppercase SHA-256: PASS
+Mixed-case SHA-512: PASS
+Mismatching uppercase SHA-256: PASS
+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
diff --git a/Tests/LibWeb/Text/input/Fetch/integrity-metadata.html b/Tests/LibWeb/Text/input/Fetch/integrity-metadata.html
index 86e6d0290d4..6f51d00e161 100644
--- a/Tests/LibWeb/Text/input/Fetch/integrity-metadata.html
+++ b/Tests/LibWeb/Text/input/Fetch/integrity-metadata.html
@@ -16,6 +16,8 @@
"sha512-3a81oZNherrMQXNJriBBMRLm+k6JqX6iCp7u5ktV05ohkpkqJ0/BqDa6PCOj/uu9RU1EI2Q86A4qmslPpUyknw==";
const base64url = metadata => metadata.replaceAll("+", "-").replaceAll("/", "_");
const unpadded = metadata => metadata.replace(/=+$/, "");
+ const uppercase = metadata => metadata.replace(/^sha/, "SHA");
+ const mixedCase = metadata => metadata.replace(/^sha/, "ShA");
// NB: A different first base64 character keeps the encoding valid but changes the digest.
const wrong = metadata => metadata.replace(/-./, "-A");
const cases = [
@@ -37,6 +39,13 @@
["Invalid strongest digest", `${sha384} ${unpadded(base64url(wrong(sha512)))}`, false],
["Valid strongest digest", `${wrong(sha384)} ${unpadded(base64url(sha512))}`, true],
["Matching digest at same strength", `${wrong(sha512)} ${base64url(sha512)}`, true],
+ ["Uppercase SHA-256", uppercase(sha256), true],
+ ["Mixed-case SHA-512", mixedCase(sha512), true],
+ ["Mismatching uppercase SHA-256", uppercase(wrong(sha256)), false],
+ ["Mismatching mixed-case SHA-384", mixedCase(wrong(sha384)), false],
+ ["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],
];
for (const [label, integrity, shouldPass] of cases) {
let response;