Skip to content

Directory transversal and content sniffing fix#25

Merged
Az107 merged 5 commits intoAz107:masterfrom
miky-rola:requestpath/bug
Apr 10, 2025
Merged

Directory transversal and content sniffing fix#25
Az107 merged 5 commits intoAz107:masterfrom
miky-rola:requestpath/bug

Conversation

@miky-rola
Copy link
Copy Markdown
Contributor

  • Path Traversal Protection: Added a safe_join_paths function that:
  • Canonicalizes paths to resolve any ".." components
  • Ensures the requested path is within the configured root directory
  • Returns None for paths that attempt to escape the root
  • Uses PathBuf for path handling instead of string concatenation
  • Added proper directory path handling with index files
  • Added special handling for the root path
  • Added "X-Content-Type-Options: nosniff" to prevent content type sniffing attacks
  • Added support for many more file types
  • Uses a safe default "application/octet-stream" for unknown types instead of "text/plain"

src/main.rs Outdated
Some(c) => {
let mut headers = headers!("Content-Type" => mimetype);
if let Some(ref mut map) = headers { // Unwrap the Option safely
map.insert("X-Content-Type-Options".to_string(), "nosniff".to_string());
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add headers easily with the macro as

headers!("Content-Type" => mimetype, "X-Content-Type-Options"=>"nosniff")

Copy link
Copy Markdown
Contributor Author

@miky-rola miky-rola Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better, lemme update it

@Az107 Az107 merged commit ffbf23f into Az107:master Apr 10, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants