Skip to content

Commit e687196

Browse files
committed
More strict parsing rules for the setters.
1 parent 7be3ddb commit e687196

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/parser.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,18 @@ impl<'i> Input<'i> {
183183
Input::with_log(input, None)
184184
}
185185

186-
pub fn with_log(input: &'i str, vfn: Option<&dyn Fn(SyntaxViolation)>) -> Self {
186+
pub fn no_trim(input: &'i str) -> Self {
187+
Input {
188+
chars: input.chars(),
189+
}
190+
}
191+
192+
pub fn with_log(original_input: &'i str, vfn: Option<&dyn Fn(SyntaxViolation)>) -> Self {
193+
let input = original_input.trim_matches(c0_control_or_space);
187194
if let Some(vfn) = vfn {
195+
if input.len() != original_input.len() {
196+
vfn(SyntaxViolation::C0SpaceIgnored)
197+
}
188198
if input.chars().any(|c| matches!(c, '\t' | '\n' | '\r')) {
189199
vfn(SyntaxViolation::TabOrNewlineIgnored)
190200
}
@@ -1445,6 +1455,12 @@ fn c0_control_or_space(ch: char) -> bool {
14451455
ch <= ' ' // U+0000 to U+0020
14461456
}
14471457

1458+
/// https://infra.spec.whatwg.org/#ascii-tab-or-newline
1459+
#[inline]
1460+
fn ascii_tab_or_new_line(ch: char) -> bool {
1461+
matches!(ch, '\t' | '\r' | '\n')
1462+
}
1463+
14481464
/// https://url.spec.whatwg.org/#ascii-alpha
14491465
#[inline]
14501466
pub fn ascii_alpha(ch: char) -> bool {

src/quirks.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//! you probably want to use `Url` method instead.
1313
1414
use parser::{default_port, Context, Input, Parser, SchemeType};
15+
use std::cell::RefCell;
16+
use SyntaxViolation;
1517
use {idna, Host, ParseError, Position, Url};
1618

1719
/// https://url.spec.whatwg.org/#dom-url-domaintoascii
@@ -103,15 +105,18 @@ pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
103105
if url.cannot_be_a_base() {
104106
return Err(());
105107
}
108+
// Host parsing rules are strict,
109+
// We don't want to trim the input
110+
let input = Input::no_trim(new_host);
106111
let host;
107112
let opt_port;
108113
{
109114
let scheme = url.scheme();
110115
let scheme_type = SchemeType::from(scheme);
111116
let result = if scheme_type == SchemeType::File {
112-
Parser::get_file_host(Input::new(new_host))
117+
Parser::get_file_host(input)
113118
} else {
114-
Parser::parse_host(Input::new(new_host), scheme_type)
119+
Parser::parse_host(input, scheme_type)
115120
};
116121
match result {
117122
Ok((h, remaining)) => {
@@ -160,11 +165,14 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
160165
if url.cannot_be_a_base() {
161166
return Err(());
162167
}
168+
// Host parsing rules are strict,
169+
// We don't want to trim the input
170+
let input = Input::no_trim(new_hostname);
163171
let scheme_type = SchemeType::from(url.scheme());
164172
let result = if scheme_type == SchemeType::File {
165-
Parser::get_file_host(Input::new(new_hostname))
173+
Parser::get_file_host(input)
166174
} else {
167-
Parser::parse_host(Input::new(new_hostname), scheme_type)
175+
Parser::parse_host(input, scheme_type)
168176
};
169177
if let Ok((host, _remaining)) = result {
170178
if let Host::Domain(h) = &host {

0 commit comments

Comments
 (0)