Skip to content

Commit 40c702e

Browse files
committed
fix handling of special characters in file names
1 parent f3f79e9 commit 40c702e

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/webserver/routing.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::webserver::database::ParsedSqlFile;
33
use crate::{file_cache::FileCache, AppState};
44
use awc::http::uri::PathAndQuery;
55
use log::debug;
6+
use percent_encoding;
7+
use std::ffi::OsString;
68
use std::path::{Path, PathBuf};
79
use RoutingAction::{CustomNotFound, Execute, NotFound, Redirect, Serve};
810

@@ -88,7 +90,19 @@ where
8890
{
8991
match path_and_query.path().strip_prefix(config.prefix()) {
9092
None => Err(Redirect(config.prefix().to_string())),
91-
Some(path) => Ok(PathBuf::from(path)),
93+
Some(path) => {
94+
let decoded = percent_encoding::percent_decode_str(path);
95+
#[cfg(unix)]
96+
{
97+
use std::os::unix::ffi::OsStringExt;
98+
let decoded = decoded.collect::<Vec<u8>>();
99+
Ok(PathBuf::from(OsString::from_vec(decoded)))
100+
}
101+
#[cfg(not(unix))]
102+
{
103+
Ok(PathBuf::from(decoded.decode_utf8_lossy().as_ref()))
104+
}
105+
}
92106
}
93107
}
94108

tests/index.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,17 @@ async fn test_static_files() {
569569
assert_eq!(&body, &b"It works !"[..]);
570570
}
571571

572+
#[actix_web::test]
573+
async fn test_spaces_in_file_names() {
574+
let resp = req_path("/tests/spaces%20in%20file%20name.sql")
575+
.await
576+
.unwrap();
577+
assert_eq!(resp.status(), http::StatusCode::OK);
578+
let body = test::read_body(resp).await;
579+
let body_str = String::from_utf8(body.to_vec()).unwrap();
580+
assert!(body_str.contains("It works !"), "{body_str}");
581+
}
582+
572583
#[actix_web::test]
573584
async fn test_with_site_prefix() {
574585
let mut config = test_config();

tests/spaces in file name.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select 'text' as component, 'It works !' AS contents;

0 commit comments

Comments
 (0)