Skip to content

Commit eafa84e

Browse files
committed
Fix hostname canonicalization of badhostnames
1 parent e2c6c2c commit eafa84e

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/canonicalize_and_process.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ pub fn canonicalize_hostname(value: &str) -> Result<String, Error> {
4646
return Ok(String::new());
4747
}
4848
let mut url = url::Url::parse("http://dummy.test").unwrap();
49-
url.set_host(Some(value)).map_err(Error::Url)?;
49+
url::quirks::set_host(&mut url, value)
50+
.map_err(|_| Error::Url(url::ParseError::InvalidDomainCharacter))?;
5051
Ok(url::quirks::hostname(&url).to_string())
5152
}
5253

src/testdata/urlpatterntestdata.json

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,17 @@
18831883
{
18841884
"pattern": [ "https://{sub.}?example{.com/}foo" ],
18851885
"inputs": [ "https://example.com/foo" ],
1886-
"expected_obj": "error"
1886+
"exactly_empty_components": [ "port" ],
1887+
"expected_obj": {
1888+
"protocol": "https",
1889+
"hostname": "{sub.}?example.com",
1890+
"pathname": "*"
1891+
},
1892+
"expected_match": {
1893+
"protocol": { "input": "https", "groups": {} },
1894+
"hostname": { "input": "example.com", "groups": {} },
1895+
"pathname": { "input": "/foo", "groups": { "0": "/foo" } }
1896+
}
18871897
},
18881898
{
18891899
"pattern": [ "{https://}example.com/foo" ],
@@ -2441,15 +2451,27 @@
24412451
},
24422452
{
24432453
"pattern": [{ "hostname": "bad#hostname" }],
2444-
"expected_obj": "error"
2454+
"inputs": [{ "hostname": "bad" }],
2455+
"expected_obj": {
2456+
"hostname": "bad"
2457+
},
2458+
"expected_match": {
2459+
"hostname": { "input": "bad", "groups": {} }
2460+
}
24452461
},
24462462
{
24472463
"pattern": [{ "hostname": "bad%hostname" }],
24482464
"expected_obj": "error"
24492465
},
24502466
{
24512467
"pattern": [{ "hostname": "bad/hostname" }],
2452-
"expected_obj": "error"
2468+
"inputs": [{ "hostname": "bad" }],
2469+
"expected_obj": {
2470+
"hostname": "bad"
2471+
},
2472+
"expected_match": {
2473+
"hostname": { "input": "bad", "groups": {} }
2474+
}
24532475
},
24542476
{
24552477
"skip": "likely a bug in rust-url",
@@ -2482,7 +2504,11 @@
24822504
},
24832505
{
24842506
"pattern": [{ "hostname": "bad\\\\hostname" }],
2485-
"expected_obj": "error"
2507+
"inputs": [{ "hostname": "badhostname" }],
2508+
"expected_obj": {
2509+
"hostname": "bad"
2510+
},
2511+
"expected_match": null
24862512
},
24872513
{
24882514
"pattern": [{ "hostname": "bad^hostname" }],
@@ -2494,15 +2520,33 @@
24942520
},
24952521
{
24962522
"pattern": [{ "hostname": "bad\nhostname" }],
2497-
"expected_obj": "error"
2523+
"inputs": [{ "hostname": "badhostname" }],
2524+
"expected_obj": {
2525+
"hostname": "badhostname"
2526+
},
2527+
"expected_match": {
2528+
"hostname": { "input": "badhostname", "groups": {} }
2529+
}
24982530
},
24992531
{
25002532
"pattern": [{ "hostname": "bad\rhostname" }],
2501-
"expected_obj": "error"
2533+
"inputs": [{ "hostname": "badhostname" }],
2534+
"expected_obj": {
2535+
"hostname": "badhostname"
2536+
},
2537+
"expected_match": {
2538+
"hostname": { "input": "badhostname", "groups": {} }
2539+
}
25022540
},
25032541
{
25042542
"pattern": [{ "hostname": "bad\thostname" }],
2505-
"expected_obj": "error"
2543+
"inputs": [{ "hostname": "badhostname" }],
2544+
"expected_obj": {
2545+
"hostname": "badhostname"
2546+
},
2547+
"expected_match": {
2548+
"hostname": { "input": "badhostname", "groups": {} }
2549+
}
25062550
},
25072551
{
25082552
"pattern": [{}],

0 commit comments

Comments
 (0)