Skip to content

Commit

Permalink
Escape backslash in special URL path components
Browse files Browse the repository at this point in the history
Closes servo#468
  • Loading branch information
sfackler committed Nov 6, 2018
1 parent a07eac0 commit 20bebc2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions percent_encoding/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ define_encode_set! {
/// space, double quote ("), hash (#), inequality qualifiers (<), (>), backtick (`),
/// question mark (?), and curly brackets ({), (}), percent sign (%), forward slash (/) are
/// encoded.
///
/// # Note
///
/// For [special URLs](https://url.spec.whatwg.org/#is-special), the backslash (\) character should
/// additionally be escaped, but that is *not* included in this encode set.
pub PATH_SEGMENT_ENCODE_SET = [DEFAULT_ENCODE_SET] | {'%', '/'}
}

Expand Down
26 changes: 23 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,24 @@ use host::{Host, HostInternal};
use percent_encoding::{
utf8_percent_encode, percent_encode,
SIMPLE_ENCODE_SET, DEFAULT_ENCODE_SET, USERINFO_ENCODE_SET, QUERY_ENCODE_SET,
PATH_SEGMENT_ENCODE_SET
PATH_SEGMENT_ENCODE_SET, EncodeSet
};

// The backslash (\) character is treated as a path separator in special URLs
// so it needs to be additionally escaped in that case.
#[derive(Clone)]
struct SPECIAL_PATH_SEGMENT_ENCODE_SET;

impl EncodeSet for SPECIAL_PATH_SEGMENT_ENCODE_SET {
#[inline]
fn contains(&self, byte: u8) -> bool {
match byte {
b'\\' => true,
_ => PATH_SEGMENT_ENCODE_SET.contains(byte)
}
}
}

pub type ParseResult<T> = Result<T, ParseError>;

macro_rules! simple_enum_error {
Expand Down Expand Up @@ -1011,8 +1026,13 @@ impl<'a> Parser<'a> {
_ => {
self.check_url_code_point(c, &input);
if self.context == Context::PathSegmentSetter {
self.serialization.extend(utf8_percent_encode(
utf8_c, PATH_SEGMENT_ENCODE_SET));
if scheme_type.is_special() {
self.serialization.extend(utf8_percent_encode(
utf8_c, SPECIAL_PATH_SEGMENT_ENCODE_SET));
} else {
self.serialization.extend(utf8_percent_encode(
utf8_c, PATH_SEGMENT_ENCODE_SET));
}
} else {
self.serialization.extend(utf8_percent_encode(
utf8_c, DEFAULT_ENCODE_SET));
Expand Down
11 changes: 11 additions & 0 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ fn new_directory_paths() {
}
}

#[test]
fn path_backslash_fun() {
let mut special_url = "http://foobar.com".parse::<Url>().unwrap();
special_url.path_segments_mut().unwrap().push("foo\\bar");
assert_eq!(special_url.as_str(), "http://foobar.com/foo%5Cbar");

let mut nonspecial_url = "thing://foobar.com".parse::<Url>().unwrap();
nonspecial_url.path_segments_mut().unwrap().push("foo\\bar");
assert_eq!(nonspecial_url.as_str(), "thing://foobar.com/foo\\bar");
}

#[test]
fn from_str() {
assert!("http://testing.com/this".parse::<Url>().is_ok());
Expand Down

0 comments on commit 20bebc2

Please sign in to comment.