Skip to content
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
20 changes: 19 additions & 1 deletion crates/bin/docs_rs_web/src/middleware/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ fn validate_path(initial_path: &str) -> Result<()> {
}

fn validate_decoded_path(path: &str) -> Result<()> {
if path.contains("/../") || path.ends_with("/..") {
if path.contains("/../")
Comment thread
syphar marked this conversation as resolved.
|| path.ends_with("/..")
Comment thread
syphar marked this conversation as resolved.
|| path.contains("//\\../")
|| path.contains("\\..\\")
|| path.ends_with("\\..")
{
bail!("path traversal attempt");
}

Expand All @@ -61,6 +66,7 @@ mod tests {
testing::{AxumResponseTestExt as _, AxumRouterTestExt as _},
};
use axum::{Router, middleware, routing::get};

use test_case::test_case;
use tower::ServiceBuilder;

Expand All @@ -78,6 +84,18 @@ mod tests {
#[test_case(
"/crate/mika-cli/latest/source/..%25c1%259c..%25c1%259c..%25c1%259c..%25c1%259c..%25c1%259c..%25c1%259c..%25c1%259c..%25c1%259c/etc/passwd"
)]
#[test_case(
"/crate/aether/latest/source/compiler/node_modules/@richardanaya//%5c../%5c../%5c../%5c../%5c../%5c../%5c../etc/passwd";
"with backslash"
)]
#[test_case(
"/casual_logger/0.6.4/%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5cwindows/win.ini";
"double backslash"
)]
#[test_case(
"/casual_logger/0.6.4/%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e%5c%2e%2e";
"ends with backslash dot dot"
)]
async fn test_invalid_path(path: &str) -> Result<()> {
let app = Router::new()
.route("/{*inner}", get(|| async { StatusCode::OK }))
Expand Down
Loading