Skip to content

Truly independent web browser

LibURL: Stop trimming U+007F DELETE from URL input

Tim Ledbetter4b0bbde2603b
diff --git a/Libraries/LibURL/Rust/src/url/input.rs b/Libraries/LibURL/Rust/src/url/input.rs
index a2f5a7525c4..8785c8dd7d9 100644
--- a/Libraries/LibURL/Rust/src/url/input.rs
+++ b/Libraries/LibURL/Rust/src/url/input.rs
@@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use super::parser::is_ascii_c0_control_or_space;
+
/// Borrowed URL input. Offsets refer to storage units, not Unicode scalar values.
#[derive(Clone, Copy)]
pub enum UrlInput<'a> {
@@ -217,23 +219,20 @@ impl<'a> UrlInput<'a> {
pub(crate) fn trim_ascii_c0(self) -> Self {
match self {
- Self::Utf8(text) => Self::Utf8(text.trim_matches(super::parser::is_ascii_c0_control_or_space)),
- Self::Latin1(text) => {
- let is_retained = |byte: &u8| *byte > 0x20 && *byte != 0x7f;
- let start = text.iter().position(is_retained).unwrap_or(text.len());
- let end = text.iter().rposition(is_retained).map_or(start, |index| index + 1);
- Self::Latin1(&text[start..end])
- }
- Self::Utf16(text) => {
- let is_retained = |unit: &u16| *unit > 0x20 && *unit != 0x7f;
- let start = text.iter().position(is_retained).unwrap_or(text.len());
- let end = text.iter().rposition(is_retained).map_or(start, |index| index + 1);
- Self::Utf16(&text[start..end])
- }
+ Self::Utf8(text) => Self::Utf8(text.trim_matches(is_ascii_c0_control_or_space)),
+ Self::Latin1(text) => Self::Latin1(trim_c0_control_or_space_code_units(text)),
+ Self::Utf16(text) => Self::Utf16(trim_c0_control_or_space_code_units(text)),
}
}
}
+fn trim_c0_control_or_space_code_units<T: Copy + Into<u32>>(units: &[T]) -> &[T] {
+ let is_retained = |unit: &T| !char::from_u32((*unit).into()).is_some_and(is_ascii_c0_control_or_space);
+ let start = units.iter().position(is_retained).unwrap_or(units.len());
+ let end = units.iter().rposition(is_retained).map_or(start, |index| index + 1);
+ &units[start..end]
+}
+
impl<'a> From<&'a str> for UrlInput<'a> {
fn from(text: &'a str) -> Self {
Self::Utf8(text)
diff --git a/Libraries/LibURL/Rust/src/url/parser.rs b/Libraries/LibURL/Rust/src/url/parser.rs
index 4704cc820aa..54b1b5b325c 100644
--- a/Libraries/LibURL/Rust/src/url/parser.rs
+++ b/Libraries/LibURL/Rust/src/url/parser.rs
@@ -61,7 +61,7 @@ pub(super) fn report_validation_error(state: State, pointer: usize, code_point:
// https://infra.spec.whatwg.org/#c0-control-or-space
pub(super) fn is_ascii_c0_control_or_space(code_point: char) -> bool {
- code_point.is_ascii_control() || code_point == ' '
+ code_point <= ' '
}
// https://url.spec.whatwg.org/#url-code-points
diff --git a/Tests/LibWeb/Text/expected/URL/delete-at-either-end-is-not-trimmed.txt b/Tests/LibWeb/Text/expected/URL/delete-at-either-end-is-not-trimmed.txt
new file mode 100644
index 00000000000..325cdc50b31
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/URL/delete-at-either-end-is-not-trimmed.txt
@@ -0,0 +1,6 @@
+ASCII input with a leading DELETE: http://example.com/dir/%7Fjavascript:x
+ASCII input with a trailing DELETE: http://example.com/x%7F
+Latin-1 input with a leading DELETE: http://example.com/dir/%7Fjavascript:%C3%A9
+Latin-1 input with a trailing DELETE: http://example.com/%C3%A9%7F
+UTF-16 input with a leading DELETE: http://example.com/dir/%7Fjavascript:%E4%B8%80
+UTF-16 input with a trailing DELETE: http://example.com/%E4%B8%80%7F
diff --git a/Tests/LibWeb/Text/input/URL/delete-at-either-end-is-not-trimmed.html b/Tests/LibWeb/Text/input/URL/delete-at-either-end-is-not-trimmed.html
new file mode 100644
index 00000000000..6531075aa89
--- /dev/null
+++ b/Tests/LibWeb/Text/input/URL/delete-at-either-end-is-not-trimmed.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // U+007F DELETE is not a C0 control, so URL parsing keeps it at either end of the input.
+ test(() => {
+ const DELETE = "\x7F";
+ const base = "http://example.com/dir/";
+ const inputs = [
+ { kind: "ASCII", text: "x" },
+ { kind: "Latin-1", text: "\xE9" },
+ { kind: "UTF-16", text: "一" },
+ ];
+
+ for (const { kind, text } of inputs) {
+ const leading = DELETE + "javascript:" + text;
+ println(`${kind} input with a leading DELETE: ${new URL(leading, base).href}`);
+
+ const trailing = "http://example.com/" + text + DELETE;
+ println(`${kind} input with a trailing DELETE: ${new URL(trailing).href}`);
+ }
+ });
+</script>