Skip to content

Truly independent web browser

LibWeb: Honor the selected form submission encoding

Andreas Kling48734d246206
diff --git a/Libraries/LibWeb/HTML/FormControlInfrastructure.cpp b/Libraries/LibWeb/HTML/FormControlInfrastructure.cpp
index baf79cd6a60..f847d5ede8d 100644
--- a/Libraries/LibWeb/HTML/FormControlInfrastructure.cpp
+++ b/Libraries/LibWeb/HTML/FormControlInfrastructure.cpp
@@ -5,7 +5,10 @@
*/
#include <AK/Random.h>
+#include <AK/StringBuilder.h>
#include <AK/Utf16StringBuilder.h>
+#include <AK/Utf8View.h>
+#include <LibTextCodec/Encoder.h>
#include <LibWeb/FileAPI/File.h>
#include <LibWeb/HTML/FormControlInfrastructure.h>
#include <LibWeb/HTML/FormDataEvent.h>
@@ -280,35 +283,54 @@ ErrorOr<Utf16String> normalize_line_breaks(Utf16View value)
return builder.to_string();
}
+// https://encoding.spec.whatwg.org/#encode
+ErrorOr<ByteBuffer> encode(Utf16View input, StringView encoding)
+{
+ // 1. Let encoder be the result of getting an encoder from encoding.
+ auto encoder = TextCodec::encoder_for(encoding);
+ VERIFY(encoder.has_value());
+
+ // 2. Process a queue with encoder, ioQueue, output, and "html".
+ auto input_utf8 = TRY(input.to_utf8());
+ StringBuilder output;
+ TRY(encoder->process(Utf8View { input_utf8 }, [&](ReadonlyBytes bytes) { return output.try_append(bytes); }, [&](u32 code_point) { return output.try_appendff("&#{};", code_point); }));
+
+ // 3. Return output.
+ return output.to_byte_buffer();
+}
+
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart/form-data-encoding-algorithm
-ErrorOr<SerializedFormData> serialize_to_multipart_form_data(GC::ConservativeVector<XHR::FormDataEntry> const& entry_list)
+ErrorOr<SerializedFormData> serialize_to_multipart_form_data(GC::ConservativeVector<XHR::FormDataEntry> const& entry_list, StringView encoding)
{
- auto escape_line_feed_carriage_return_double_quote = [](Utf16View value) -> ErrorOr<Utf16String> {
- Utf16StringBuilder builder { value.length_in_code_units() };
+ // For field names and filenames for file fields, the result of the encoding in the previous bullet point must be
+ // escaped by replacing any 0x0A (LF) bytes with the byte sequence `%0A`, 0x0D (CR) with `%0D` and 0x22 (") with `%22`.
+ // The user agent must not perform any other escapes.
+ auto escape_line_feed_carriage_return_double_quote = [](ReadonlyBytes value) -> ErrorOr<ByteBuffer> {
+ StringBuilder builder { value.size() };
size_t chunk_start = 0;
- for (size_t i = 0; i < value.length_in_code_units(); ++i) {
- auto code_unit = value.code_unit_at(i);
- if (code_unit != '\r' && code_unit != '\n' && code_unit != '"')
+ for (size_t i = 0; i < value.size(); ++i) {
+ auto byte = value[i];
+ if (byte != '\r' && byte != '\n' && byte != '"')
continue;
- builder.append(value.substring_view(chunk_start, i - chunk_start));
- switch (code_unit) {
+ TRY(builder.try_append(value.slice(chunk_start, i - chunk_start)));
+ switch (byte) {
case '\r':
- builder.append("%0D"_utf16);
+ TRY(builder.try_append("%0D"sv));
break;
case '\n':
- builder.append("%0A"_utf16);
+ TRY(builder.try_append("%0A"sv));
break;
case '"':
- builder.append("%22"_utf16);
+ TRY(builder.try_append("%22"sv));
break;
default:
VERIFY_NOT_REACHED();
}
chunk_start = i + 1;
}
- builder.append(value.substring_view(chunk_start));
- return builder.to_string();
+ TRY(builder.try_append(value.slice(chunk_start)));
+ return builder.to_byte_buffer();
};
// The boundary used by the user agent in generating the return value of this algorithm is the multipart/form-data boundary string.
@@ -321,18 +343,22 @@ ErrorOr<SerializedFormData> serialize_to_multipart_form_data(GC::ConservativeVec
// Replace every occurrence of U+000D (CR) not followed by U+000A (LF), and every occurrence of U+000A (LF) not preceded by U+000D (CR) by a string consisting of a U+000D (CR) and U+000A (LF).
auto normalized_name = TRY(normalize_line_breaks(entry.name.utf16_view()));
- // For field names replace any 0x0A (LF) bytes with the byte sequence `%0A`, 0x0D (CR) with `%0D` and 0x22 (") with `%22`
- auto escaped_name = TRY(escape_line_feed_carriage_return_double_quote(normalized_name.utf16_view()));
- auto escaped_name_utf8 = TRY(escaped_name.utf16_view().to_utf8());
+ // Field names, field values for non-file fields, and filenames for file fields, in the generated multipart/form-data
+ // resource must be set to the result of encoding the corresponding entry's name or value with encoding, converted to a byte sequence.
+ auto encoded_name = TRY(encode(normalized_name.utf16_view(), encoding));
+ auto escaped_name = TRY(escape_line_feed_carriage_return_double_quote(encoded_name));
+ TRY(builder.try_append("Content-Disposition: form-data; name=\""sv));
+ TRY(builder.try_append(escaped_name));
+ TRY(builder.try_append('"'));
TRY(entry.value.visit(
[&](GC::Ref<FileAPI::File> file) -> ErrorOr<void> {
- // For filenames replace any 0x0A (LF) bytes with the byte sequence `%0A`, 0x0D (CR) with `%0D` and 0x22 (") with `%22`
auto file_name = file->name();
- auto escaped_filename = TRY(escape_line_feed_carriage_return_double_quote(file_name.utf16_view()));
- auto escaped_filename_utf8 = TRY(escaped_filename.utf16_view().to_utf8());
- // Add a `Content-Disposition` header with a `name` set to entry's name and `filename` set to entry's filename.
- TRY(builder.try_append(TRY(String::formatted("Content-Disposition: form-data; name=\"{}\"; filename=\"{}\"\r\n", escaped_name_utf8, escaped_filename_utf8))));
+ auto encoded_filename = TRY(encode(file_name.utf16_view(), encoding));
+ auto escaped_filename = TRY(escape_line_feed_carriage_return_double_quote(encoded_filename));
+ TRY(builder.try_append("; filename=\""sv));
+ TRY(builder.try_append(escaped_filename));
+ TRY(builder.try_append("\"\r\n"sv));
// The parts of the generated multipart/form-data resource that correspond to file fields must have a `Content-Type` header specified.
// RFC7578: If the contents of a file are to be sent, the file data SHOULD be labeled with an appropriate media type, if known, or
// "application/octet-stream".
@@ -349,9 +375,9 @@ ErrorOr<SerializedFormData> serialize_to_multipart_form_data(GC::ConservativeVec
[&](Utf16String const& string) -> ErrorOr<void> {
// Replace every occurrence of U+000D (CR) not followed by U+000A (LF), and every occurrence of U+000A (LF) not preceded by U+000D (CR) by a string consisting of a U+000D (CR) and U+000A (LF).
auto normalized_value = TRY(normalize_line_breaks(string.utf16_view()));
- // Add a `Content-Disposition` header with a `name` set to entry's name.
- TRY(builder.try_append(TRY(String::formatted("Content-Disposition: form-data; name=\"{}\"\r\n\r\n", escaped_name_utf8))));
- TRY(builder.try_append(TRY(String::formatted("{}\r\n", TRY(normalized_value.utf16_view().to_utf8())))));
+ TRY(builder.try_append("\r\n\r\n"sv));
+ TRY(builder.try_append(TRY(encode(normalized_value.utf16_view(), encoding))));
+ TRY(builder.try_append("\r\n"sv));
return {};
}));
}
diff --git a/Libraries/LibWeb/HTML/FormControlInfrastructure.h b/Libraries/LibWeb/HTML/FormControlInfrastructure.h
index a37a0dda74e..1c0d4594399 100644
--- a/Libraries/LibWeb/HTML/FormControlInfrastructure.h
+++ b/Libraries/LibWeb/HTML/FormControlInfrastructure.h
@@ -20,6 +20,7 @@ struct SerializedFormData {
WebIDL::ExceptionOr<XHR::FormDataEntry> create_entry(JS::Realm& realm, Utf16View name, Variant<GC::Ref<FileAPI::Blob>, Utf16String> const& value, Optional<Utf16String> const& filename = {});
WebIDL::ExceptionOr<Optional<GC::ConservativeVector<XHR::FormDataEntry>>> construct_entry_list(JS::Realm&, HTMLFormElement&, GC::Ptr<HTMLElement> submitter = nullptr, Optional<Utf16String> encoding = {});
ErrorOr<Utf16String> normalize_line_breaks(Utf16View value);
-ErrorOr<SerializedFormData> serialize_to_multipart_form_data(GC::ConservativeVector<XHR::FormDataEntry> const& entry_list);
+ErrorOr<ByteBuffer> encode(Utf16View input, StringView encoding);
+ErrorOr<SerializedFormData> serialize_to_multipart_form_data(GC::ConservativeVector<XHR::FormDataEntry> const& entry_list, StringView encoding = "UTF-8"sv);
}
diff --git a/Libraries/LibWeb/HTML/HTMLFormElement.cpp b/Libraries/LibWeb/HTML/HTMLFormElement.cpp
index d682709a162..39d695b80bf 100644
--- a/Libraries/LibWeb/HTML/HTMLFormElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLFormElement.cpp
@@ -923,7 +923,7 @@ ErrorOr<void> HTMLFormElement::mutate_action_url(URL::URL parsed_action, GC::Con
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#submit-body
-ErrorOr<void> HTMLFormElement::submit_as_entity_body(URL::URL parsed_action, GC::ConservativeVector<XHR::FormDataEntry> entry_list, EncodingTypeAttributeState encoding_type, [[maybe_unused]] Utf16String encoding, GC::Ref<Navigable> target_navigable, NavigationHistoryBehavior history_handling, UserNavigationInvolvement user_involvement, GC::Ref<HTMLElement> submitter)
+ErrorOr<void> HTMLFormElement::submit_as_entity_body(URL::URL parsed_action, GC::ConservativeVector<XHR::FormDataEntry> entry_list, EncodingTypeAttributeState encoding_type, Utf16String encoding, GC::Ref<Navigable> target_navigable, NavigationHistoryBehavior history_handling, UserNavigationInvolvement user_involvement, GC::Ref<HTMLElement> submitter)
{
// 1. Assert: method is POST.
@@ -952,7 +952,7 @@ ErrorOr<void> HTMLFormElement::submit_as_entity_body(URL::URL parsed_action, GC:
case EncodingTypeAttributeState::FormData: {
// -> multipart/form-data
// 1. Let body be the result of running the multipart/form-data encoding algorithm with entry list and encoding.
- auto body_and_mime_type = TRY(serialize_to_multipart_form_data(entry_list));
+ auto body_and_mime_type = TRY(serialize_to_multipart_form_data(entry_list, form_encoding_label_for_byte_serialization(encoding)));
body = move(body_and_mime_type.serialized_data);
// 2. Let mimeType be the isomorphic encoding of the concatenation of "multipart/form-data; boundary=" and the multipart/form-data
@@ -968,10 +968,9 @@ ErrorOr<void> HTMLFormElement::submit_as_entity_body(URL::URL parsed_action, GC:
// 2. Let body be the result of running the text/plain encoding algorithm with pairs.
auto serialized_body = TRY(plain_text_encode(pairs));
- auto serialized_body_utf8 = serialized_body.to_utf8();
- body = TRY(ByteBuffer::copy(serialized_body_utf8.bytes()));
- // FIXME: 3. Set body to the result of encoding body using encoding.
+ // 3. Set body to the result of encoding body using encoding.
+ body = TRY(encode(serialized_body.utf16_view(), form_encoding_label_for_byte_serialization(encoding)));
// 4. Let mimeType be `text/plain`.
mime_type = POSTResource::RequestContentType::TextPlain;
diff --git a/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-iso-2022-jp.txt b/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-iso-2022-jp.txt
index 7bf4c9ec38e..e936009aaa2 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-iso-2022-jp.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-iso-2022-jp.txt
@@ -2,11 +2,10 @@ Harness status: OK
Found 6 tests
-1 Pass
-5 Fail
+6 Pass
Pass Upload file-for-upload-in-form.txt (ASCII) in ISO-2022-JP form
-Fail Upload file-for-upload-in-form-.txt (x-user-defined) in ISO-2022-JP form
-Fail Upload file-for-upload-in-form-☺😂.txt (windows-1252) in ISO-2022-JP form
-Fail Upload file-for-upload-in-form-★星★.txt (JIS X 0201 and JIS X 0208) in ISO-2022-JP form
-Fail Upload file-for-upload-in-form-☺😂.txt (Unicode) in ISO-2022-JP form
-Fail Upload file-for-upload-in-form-ABC~‾¥≈¤・・•∙·☼★星🌟星★☼·∙•・・¤≈¥‾~XYZ.txt (Unicode) in ISO-2022-JP form
\ No newline at end of file
+Pass Upload file-for-upload-in-form-.txt (x-user-defined) in ISO-2022-JP form
+Pass Upload file-for-upload-in-form-☺😂.txt (windows-1252) in ISO-2022-JP form
+Pass Upload file-for-upload-in-form-★星★.txt (JIS X 0201 and JIS X 0208) in ISO-2022-JP form
+Pass Upload file-for-upload-in-form-☺😂.txt (Unicode) in ISO-2022-JP form
+Pass Upload file-for-upload-in-form-ABC~‾¥≈¤・・•∙·☼★星🌟星★☼·∙•・・¤≈¥‾~XYZ.txt (Unicode) in ISO-2022-JP form
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-windows-1252.txt b/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-windows-1252.txt
index 3e74eb8fe8f..6c846592ca0 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-windows-1252.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-windows-1252.txt
@@ -2,11 +2,10 @@ Harness status: OK
Found 6 tests
-1 Pass
-5 Fail
+6 Pass
Pass Upload file-for-upload-in-form.txt (ASCII) in windows-1252 form
-Fail Upload file-for-upload-in-form-.txt (x-user-defined) in windows-1252 form
-Fail Upload file-for-upload-in-form-☺😂.txt (windows-1252) in windows-1252 form
-Fail Upload file-for-upload-in-form-★星★.txt (JIS X 0201 and JIS X 0208) in windows-1252 form
-Fail Upload file-for-upload-in-form-☺😂.txt (Unicode) in windows-1252 form
-Fail Upload file-for-upload-in-form-ABC~‾¥≈¤・・•∙·☼★星🌟星★☼·∙•・・¤≈¥‾~XYZ.txt (Unicode) in windows-1252 form
\ No newline at end of file
+Pass Upload file-for-upload-in-form-.txt (x-user-defined) in windows-1252 form
+Pass Upload file-for-upload-in-form-☺😂.txt (windows-1252) in windows-1252 form
+Pass Upload file-for-upload-in-form-★星★.txt (JIS X 0201 and JIS X 0208) in windows-1252 form
+Pass Upload file-for-upload-in-form-☺😂.txt (Unicode) in windows-1252 form
+Pass Upload file-for-upload-in-form-ABC~‾¥≈¤・・•∙·☼★星🌟星★☼·∙•・・¤≈¥‾~XYZ.txt (Unicode) in windows-1252 form
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-x-user-defined.txt b/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-x-user-defined.txt
index 9e83a7dd087..30ea14c1d0f 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-x-user-defined.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/FileAPI/file/send-file-form-x-user-defined.txt
@@ -2,11 +2,10 @@ Harness status: OK
Found 6 tests
-1 Pass
-5 Fail
+6 Pass
Pass Upload file-for-upload-in-form.txt (ASCII) in x-user-defined form
-Fail Upload file-for-upload-in-form-.txt (x-user-defined) in x-user-defined form
-Fail Upload file-for-upload-in-form-☺😂.txt (windows-1252) in x-user-defined form
-Fail Upload file-for-upload-in-form-★星★.txt (JIS X 0201 and JIS X 0208) in x-user-defined form
-Fail Upload file-for-upload-in-form-☺😂.txt (Unicode) in x-user-defined form
-Fail Upload file-for-upload-in-form-ABC~‾¥≈¤・・•∙·☼★星🌟星★☼·∙•・・¤≈¥‾~XYZ.txt (Unicode) in x-user-defined form
\ No newline at end of file
+Pass Upload file-for-upload-in-form-.txt (x-user-defined) in x-user-defined form
+Pass Upload file-for-upload-in-form-☺😂.txt (windows-1252) in x-user-defined form
+Pass Upload file-for-upload-in-form-★星★.txt (JIS X 0201 and JIS X 0208) in x-user-defined form
+Pass Upload file-for-upload-in-form-☺😂.txt (Unicode) in x-user-defined form
+Pass Upload file-for-upload-in-form-ABC~‾¥≈¤・・•∙·☼★星🌟星★☼·∙•・・¤≈¥‾~XYZ.txt (Unicode) in x-user-defined form
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/form-submission-0/multipart-formdata.window.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/form-submission-0/multipart-formdata.window.txt
index fd7fd78c637..2af0cb7d1da 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/form-submission-0/multipart-formdata.window.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/form-submission-0/multipart-formdata.window.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 62 tests
-56 Pass
-6 Fail
+62 Pass
Pass multipart/form-data: Basic test (normal form)
Pass multipart/form-data: Basic test (formdata event)
Pass multipart/form-data: Basic File test (normal form)
@@ -60,9 +59,9 @@ Pass multipart/form-data: non-ASCII in name and value (normal form)
Pass multipart/form-data: non-ASCII in name and value (formdata event)
Pass multipart/form-data: non-ASCII in filename (normal form)
Pass multipart/form-data: non-ASCII in filename (formdata event)
-Fail multipart/form-data: characters not in encoding in name and value (normal form)
-Fail multipart/form-data: characters not in encoding in name and value (formdata event)
-Fail multipart/form-data: character not in encoding in filename (normal form)
-Fail multipart/form-data: character not in encoding in filename (formdata event)
-Fail multipart/form-data: lone surrogate in name and value (normal form)
-Fail multipart/form-data: lone surrogate in name and value (formdata event)
\ No newline at end of file
+Pass multipart/form-data: characters not in encoding in name and value (normal form)
+Pass multipart/form-data: characters not in encoding in name and value (formdata event)
+Pass multipart/form-data: character not in encoding in filename (normal form)
+Pass multipart/form-data: character not in encoding in filename (formdata event)
+Pass multipart/form-data: lone surrogate in name and value (normal form)
+Pass multipart/form-data: lone surrogate in name and value (formdata event)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/form-submission-0/text-plain.window.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/form-submission-0/text-plain.window.txt
index 58a2a6c55e4..e130cec2874 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/form-submission-0/text-plain.window.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/form-submission-0/text-plain.window.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 62 tests
-56 Pass
-6 Fail
+62 Pass
Pass text/plain: Basic test (normal form)
Pass text/plain: Basic test (formdata event)
Pass text/plain: Basic File test (normal form)
@@ -60,9 +59,9 @@ Pass text/plain: non-ASCII in name and value (normal form)
Pass text/plain: non-ASCII in name and value (formdata event)
Pass text/plain: non-ASCII in filename (normal form)
Pass text/plain: non-ASCII in filename (formdata event)
-Fail text/plain: characters not in encoding in name and value (normal form)
-Fail text/plain: characters not in encoding in name and value (formdata event)
-Fail text/plain: character not in encoding in filename (normal form)
-Fail text/plain: character not in encoding in filename (formdata event)
-Fail text/plain: lone surrogate in name and value (normal form)
-Fail text/plain: lone surrogate in name and value (formdata event)
\ No newline at end of file
+Pass text/plain: characters not in encoding in name and value (normal form)
+Pass text/plain: characters not in encoding in name and value (formdata event)
+Pass text/plain: character not in encoding in filename (normal form)
+Pass text/plain: character not in encoding in filename (formdata event)
+Pass text/plain: lone surrogate in name and value (normal form)
+Pass text/plain: lone surrogate in name and value (formdata event)
\ No newline at end of file