Skip to content

Truly independent web browser

LibWeb: Parse Document-Policy as a structured field

Shannon Boothd32d3f83d3d2
diff --git a/Libraries/LibWeb/HTML/PolicyContainers.cpp b/Libraries/LibWeb/HTML/PolicyContainers.cpp
index 87c0d65ba87..9104cb69339 100644
--- a/Libraries/LibWeb/HTML/PolicyContainers.cpp
+++ b/Libraries/LibWeb/HTML/PolicyContainers.cpp
@@ -5,7 +5,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/GenericLexer.h>
#include <LibGC/Heap.h>
#include <LibURL/URL.h>
#include <LibWeb/ContentSecurityPolicy/Policy.h>
@@ -63,66 +62,12 @@ GC::Ref<PolicyContainer> create_a_policy_container_from_a_fetch_response(GC::Ref
// https://wicg.github.io/scroll-to-text-fragment/#document-policy-integration
// This specification defines a configuration point in Document Policy with name
// "force-load-at-top". Its type is boolean with default value false.
- if (auto document_policy = response->header_list()->get("Document-Policy"sv); document_policy.has_value()) {
- // https://www.rfc-editor.org/rfc/rfc8941.html#name-parsing-a-dictionary
- // Parse this as a Structured Fields dictionary so commas inside strings or inner lists do
- // not accidentally create a force-load-at-top member. LibHTTP does not yet have a general
- // Structured Fields parser, so keep the member splitting local to this policy lookup.
- GenericLexer lexer { document_policy->view() };
- while (!lexer.is_eof()) {
- lexer.consume_while([](char code_unit) { return code_unit == ' ' || code_unit == '\t'; });
- auto const member_start = lexer.tell();
- auto member_end = member_start;
- bool inside_string = false;
- bool escaped = false;
- size_t inner_list_depth = 0;
- while (!lexer.is_eof()) {
- auto const code_unit = lexer.consume();
- if (inside_string) {
- if (escaped) {
- escaped = false;
- } else if (code_unit == '\\') {
- escaped = true;
- } else if (code_unit == '"') {
- inside_string = false;
- }
- } else if (code_unit == '"') {
- inside_string = true;
- } else if (code_unit == '(') {
- ++inner_list_depth;
- } else if (code_unit == ')' && inner_list_depth > 0) {
- --inner_list_depth;
- } else if (code_unit == ',' && inner_list_depth == 0) {
- member_end = lexer.tell() - 1;
- break;
- }
- member_end = lexer.tell();
+ if (auto document_policy = response->header_list()->get_structured_field_value<HTTP::StructuredFields::Dictionary>("Document-Policy"sv); document_policy.has_value()) {
+ if (auto force_load_at_top = document_policy->get("force-load-at-top"sv); force_load_at_top.has_value()) {
+ if (auto const* item = force_load_at_top->get_pointer<HTTP::StructuredFields::Item>()) {
+ if (auto const* value = item->bare_item.get_pointer<bool>())
+ result->force_load_at_top = *value;
}
-
- auto member = document_policy->view().substring_view(member_start, member_end - member_start).trim_whitespace();
- GenericLexer member_lexer { member };
- auto const key = member_lexer.consume_while([](char code_unit) {
- return is_ascii_lower_alpha(code_unit) || is_ascii_digit(code_unit) || first_is_one_of(code_unit, '_', '-', '.', '*');
- });
- if (key != "force-load-at-top"sv)
- continue;
-
- // https://www.rfc-editor.org/rfc/rfc8941.html#name-dictionaries
- // A dictionary member with no value has the value Boolean true.
- if (member_lexer.is_eof() || member_lexer.next_is(';')) {
- result->force_load_at_top = true;
- continue;
- }
-
- if (!member_lexer.consume_specific("=?"sv) || member_lexer.is_eof())
- continue;
- auto const value = member_lexer.consume();
- if (!member_lexer.is_eof() && !member_lexer.next_is(';'))
- continue;
- if (value == '1')
- result->force_load_at_top = true;
- else if (value == '0')
- result->force_load_at_top = false;
}
}