@@ -43,9 +43,9 @@ mod shutdown;
4343mod utils;
4444
4545use std:: fs;
46+ use std:: io;
4647use std:: path:: Path ;
4748use std:: sync:: Mutex ;
48- use std:: { io, path:: PathBuf } ;
4949
5050use cache:: Cache ;
5151use hteapot:: { Hteapot , HttpRequest , HttpResponse , HttpStatus } ;
@@ -54,48 +54,9 @@ use utils::get_mime_tipe;
5454use logger:: { LogLevel , Logger } ;
5555use std:: time:: Instant ;
5656
57- use http_responders:: file:: serve_file;
57+ use http_responders:: file:: { safe_join_paths , serve_file} ;
5858use http_responders:: proxy:: is_proxy;
5959
60- /// Attempts to safely join a root directory and a requested relative path.
61- ///
62- /// Ensures that the resulting path:
63- /// - Resolves symbolic links and `..` segments via `canonicalize`
64- /// - Remains within the bounds of the specified root directory
65- /// - Actually exists on disk
66- ///
67- /// This protects against directory traversal vulnerabilities, such as accessing
68- /// files outside of the intended root (e.g., `/etc/passwd`).
69- ///
70- /// # Arguments
71- /// * `root` - The root directory from which serving is allowed.
72- /// * `requested_path` - The path requested by the client (usually from the URL).
73- ///
74- /// # Returns
75- /// `Some(PathBuf)` if the resolved path exists and is within the root. `None` otherwise.
76- ///
77- /// # Example
78- /// ```
79- /// let safe_path = safe_join_paths("/var/www", "/index.html");
80- /// assert!(safe_path.unwrap().ends_with("index.html"));
81- /// ```
82- fn safe_join_paths ( root : & str , requested_path : & str ) -> Option < PathBuf > {
83- let root_path = Path :: new ( root) . canonicalize ( ) . ok ( ) ?;
84- let requested_full_path = root_path. join ( requested_path. trim_start_matches ( "/" ) ) ;
85-
86- if !requested_full_path. exists ( ) {
87- return None ;
88- }
89-
90- let canonical_path = requested_full_path. canonicalize ( ) . ok ( ) ?;
91-
92- if canonical_path. starts_with ( & root_path) {
93- Some ( canonical_path)
94- } else {
95- None
96- }
97- }
98-
9960/// Main entry point of the Hteapot server.
10061///
10162/// Handles command-line interface, config file parsing, optional file-serving mode,
0 commit comments