Skip to content

Commit

Permalink
RequestFilter: fix path matching
Browse files Browse the repository at this point in the history
  • Loading branch information
picoHz committed Mar 27, 2024
1 parent 1db94e8 commit bd47db4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions taxy/src/proxy/http/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ impl RequestFilter {
if !host_matched && !self.vhosts.is_empty() {
return None;
}
let path = req.uri().path().split('/');
let path = req.uri().path().trim_start_matches('/').split('/');
let count = path
.clone()
.zip(self.path.iter())
.take_while(|(a, b)| a == b)
.count();
if count == self.path.len() {
let new_path = path.skip(count).collect::<Vec<_>>().join("/");
let new_path = "/".to_string() + &path.skip(count).collect::<Vec<_>>().join("/");
FilterResult::new(&new_path).ok()
} else {
None
Expand Down
43 changes: 37 additions & 6 deletions taxy/tests/http_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ async fn http_proxy() -> anyhow::Result<()> {
.create_async()
.await;

let mock_get_with_path = server
.mock("GET", "/Hello?world=1")
.match_header("via", "taxy")
.match_header("x-forwarded-for", mockito::Matcher::Missing)
.match_header("x-forwarded-host", mockito::Matcher::Missing)
.match_header("x-real-ip", mockito::Matcher::Missing)
.match_header("accept-encoding", "gzip, br")
.with_body("Hello")
.create_async()
.await;

let mock_get_trailing_slash = server
.mock("GET", "/hello/?world=1")
.match_header("via", "taxy")
Expand Down Expand Up @@ -78,12 +89,20 @@ async fn http_proxy() -> anyhow::Result<()> {
ports: vec!["test".parse().unwrap()],
kind: ProxyKind::Http(HttpProxy {
vhosts: vec!["localhost".parse().unwrap()],
routes: vec![Route {
path: "/".into(),
servers: vec![taxy_api::proxy::Server {
url: server.url().parse().unwrap(),
}],
}],
routes: vec![
Route {
path: "/was/ist/passiert".into(),
servers: vec![taxy_api::proxy::Server {
url: server.url().parse().unwrap(),
}],
},
Route {
path: "/".into(),
servers: vec![taxy_api::proxy::Server {
url: server.url().parse().unwrap(),
}],
},
],
}),
..Default::default()
},
Expand All @@ -103,6 +122,17 @@ async fn http_proxy() -> anyhow::Result<()> {
.await?;
assert_eq!(resp, "Hello");

let resp = client
.get(proxy_port.http_url("/was/ist/passiert/Hello?world=1"))
.header("x-forwarded-for", "0.0.0.0")
.header("x-forwarded-host", "untrusted.example.com")
.header("x-real-ip", "0.0.0.0")
.send()
.await?
.text()
.await?;
assert_eq!(resp, "Hello");

let resp = client
.get(proxy_port.http_url("/hello/?world=1"))
.header("x-forwarded-for", "0.0.0.0")
Expand Down Expand Up @@ -146,6 +176,7 @@ async fn http_proxy() -> anyhow::Result<()> {
.await?;

mock_get.assert_async().await;
mock_get_with_path.assert_async().await;
mock_get_trailing_slash.assert_async().await;
mock_post.assert_async().await;
mock_stream.assert_async().await;
Expand Down

0 comments on commit bd47db4

Please sign in to comment.