Skip to content

Commit b46821b

Browse files
committed
Check that URLs begin with http:// or https://
This switches to a manual check of the string prefix, because `Url::parse` may sanitize URLs as they are parsed, making it difficult to ensure both slashes are present. Fixes: #169
1 parent 03206c0 commit b46821b

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/models/krate.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,19 @@ impl<'a> NewCrate<'a> {
134134
Some(s) => s,
135135
None => return Ok(()),
136136
};
137-
let url = Url::parse(url)
138-
.map_err(|_| human(&format_args!("`{}` is not a valid url: `{}`", field, url)))?;
139-
match &url.scheme()[..] {
140-
"http" | "https" => {}
141-
s => {
142-
return Err(human(&format_args!(
143-
"`{}` has an invalid url \
144-
scheme: `{}`",
145-
field, s
146-
)));
147-
}
148-
}
149-
if url.cannot_be_a_base() {
137+
138+
// Manually check the string, as `Url::parse` may normalize relative URLs
139+
// making it difficult to ensure that both slashes are present.
140+
if !url.starts_with("http://") && !url.starts_with("https://") {
150141
return Err(human(&format_args!(
151-
"`{}` must have relative scheme \
152-
data: {}",
142+
"URL for field `{}` must begin with http:// or https:// (url: {})",
153143
field, url
154144
)));
155145
}
146+
147+
// Ensure the entire URL parses as well
148+
Url::parse(url)
149+
.map_err(|_| human(&format_args!("`{}` is not a valid url: `{}`", field, url)))?;
156150
Ok(())
157151
}
158152

@@ -531,7 +525,7 @@ mod tests {
531525
let krate = NewCrate {
532526
name: "name",
533527
description: None,
534-
homepage: Some("http:/example.com/home"),
528+
homepage: Some("https:/example.com/home"),
535529
documentation: None,
536530
readme: None,
537531
repository: None,

0 commit comments

Comments
 (0)