Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape routing regex in path segment literals #1719

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions rust-runtime/aws-smithy-http-server/src/routing/request_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

use std::borrow::Cow;

use http::Request;
use regex::Regex;

Expand Down Expand Up @@ -104,13 +106,13 @@ impl From<&PathSpec> for Regex {
.0
.iter()
.map(|segment_spec| match segment_spec {
PathSegment::Literal(literal) => literal,
PathSegment::Literal(literal) => Cow::Owned(regex::escape(literal)),
// TODO(https://github.com/awslabs/smithy/issues/975) URL spec says it should be ASCII but this regex accepts UTF-8:
// `*` instead of `+` because the empty string `""` can be bound to a label.
PathSegment::Label => "[^/]*",
PathSegment::Greedy => ".*",
PathSegment::Label => Cow::Borrowed("[^/]*"),
PathSegment::Greedy => Cow::Borrowed(".*"),
})
.fold(String::new(), |a, b| a + sep + b)
.fold(String::new(), |a, b| a + sep + &b)
};

Regex::new(&format!("^{}$", re)).expect("invalid `Regex` from `PathSpec`; please file a bug report under https://github.com/awslabs/smithy-rs/issues")
Expand Down Expand Up @@ -469,4 +471,22 @@ mod tests {
assert_eq!(Match::Yes, label_spec.matches(&req(method, uri, None)));
}
}

#[test]
fn unsanitary_path() {
let spec = RequestSpec::from_parts(
Method::GET,
vec![
PathSegment::Literal(String::from("ReDosLiteral")),
PathSegment::Label,
PathSegment::Literal(String::from("(a+)+")),
],
Vec::new(),
);

assert_eq!(
Match::Yes,
spec.matches(&req(&Method::GET, "/ReDosLiteral/abc/(a+)+", None))
);
}
}