@@ -116,6 +116,7 @@ public function delete_file(string $url_key): void
116116 public function get_file ($ filename ): void
117117 {
118118 // Security: Removed urldecode() - CodeIgniter already handles URL decoding
119+ // First sanitize to handle the url_key_filename format
119120 $ filename = $ this ->sanitize_file_name ($ filename );
120121
121122 $ underscorePos = mb_strpos ($ filename , '_ ' );
@@ -127,18 +128,29 @@ public function get_file($filename): void
127128
128129 $ url_key = mb_substr ($ filename , 0 , $ underscorePos );
129130 $ real_filename = mb_substr ($ filename , $ underscorePos + 1 );
130- $ fullPath = $ this ->get_target_file_path ($ url_key , $ real_filename );
131131
132- if ( ! file_exists ($ fullPath )) {
133- log_message ('debug ' , 'upload: File not found in uploads directory ' );
134- $ this ->respond_message (404 , 'upload_error_file_not_found ' , 'File not found ' );
135- }
132+ // Security: Use comprehensive validation on the real filename component
133+ $ validation = validate_file_access ($ real_filename , $ this ->targetPath );
136134
137- // Security: Validate file is within allowed directory
138- if (!validate_file_in_directory ($ fullPath , $ this ->targetPath )) {
139- $ filenameHash = hash_for_logging ($ filename );
140- log_message ('error ' , 'upload: Path traversal detected (hash: ' . $ filenameHash . ') ' );
141- $ this ->respond_message (403 , 'upload_error_unauthorized_access ' , 'Unauthorized access ' );
135+ // For uploads, we need to check the actual path with url_key prefix
136+ if (!$ validation ['valid ' ] || $ validation ['error ' ] === 'file_not_found ' ) {
137+ // Try with the url_key prefix (uploads use url_key_filename format)
138+ $ fullPath = $ this ->get_target_file_path ($ url_key , $ real_filename );
139+
140+ if (!file_exists ($ fullPath )) {
141+ log_message ('debug ' , 'upload: File not found in uploads directory ' );
142+ $ this ->respond_message (404 , 'upload_error_file_not_found ' , 'File not found ' );
143+ }
144+
145+ // Validate the actual path is within allowed directory
146+ if (!validate_file_in_directory ($ fullPath , $ this ->targetPath )) {
147+ $ filenameHash = hash_for_logging ($ filename );
148+ log_message ('error ' , 'upload: Path traversal detected (hash: ' . $ filenameHash . ') ' );
149+ $ this ->respond_message (403 , 'upload_error_unauthorized_access ' , 'Unauthorized access ' );
150+ }
151+ } else {
152+ // Use validated path from helper
153+ $ fullPath = $ this ->get_target_file_path ($ url_key , $ real_filename );
142154 }
143155
144156 $ path_parts = pathinfo ($ fullPath );
0 commit comments