Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ sysinfo = { version = "0.39.1", default-features = false, features = ["system"]
tempfile = "3.24.0"
tokio = { version = "1.52.3", features = ["fs", "io-util", "macros", "rt-multi-thread"] }
tokio-util = { version = "0.7.18", features = ["io"] }
tower-http = { version = "0.6.11", features = ["set-header"] }
url = "2.5.8"
usized = "0.0.2"
walkdir = "2.5.0"
Expand Down
39 changes: 27 additions & 12 deletions src/subcommand/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
axum::{
Router,
extract::{Extension, Path},
http::{Uri, header},
http::{HeaderValue, Uri, header},
response::Redirect,
routing::{get, put},
},
Expand All @@ -15,6 +15,7 @@ use {
sysinfo::System,
tokio::{net::TcpListener, runtime},
tokio_util::io::ReaderStream,
tower_http::set_header::SetResponseHeaderLayer,
};

static THREAD_COUNTER: AtomicU64 = AtomicU64::new(0);
Expand Down Expand Up @@ -210,6 +211,16 @@ impl Serve {
Redirect::to(&destination)
}

fn redirect_router(destination: String) -> Router {
Router::new()
.fallback(Self::redirect_http_to_https)
.layer(Extension(destination))
.layer(SetResponseHeaderLayer::overriding(
header::X_CONTENT_TYPE_OPTIONS,
HeaderValue::from_static("nosniff"),
))
}

pub(crate) fn router(server: Arc<Server>, auth_config: Option<Arc<AuthConfig>>) -> Router {
let router = Router::new()
.route("/", get(Self::home))
Expand All @@ -219,7 +230,11 @@ impl Serve {
.route("/install.sh", get(Self::install_script))
.route("/static/{*path}", get(Self::static_asset))
.fallback(Self::fallback)
.layer(Extension(server));
.layer(Extension(server))
.layer(SetResponseHeaderLayer::overriding(
header::X_CONTENT_TYPE_OPTIONS,
HeaderValue::from_static("nosniff"),
));

if let Some(auth_config) = auth_config {
router.layer(Extension(auth_config))
Expand Down Expand Up @@ -392,12 +407,7 @@ impl Serve {
axum_server::from_tcp(listener)
.context(error::Serve)?
.handle(handle)
.serve(
Router::new()
.fallback(Self::redirect_http_to_https)
.layer(Extension(destination))
.into_make_service(),
)
.serve(Self::redirect_router(destination).into_make_service())
.await
.context(error::Serve)?;
}
Expand Down Expand Up @@ -488,7 +498,10 @@ mod tests {
method,
path: path.into(),
response_body: Vec::new(),
response_headers: BTreeMap::new(),
response_headers: BTreeMap::from([(
header::X_CONTENT_TYPE_OPTIONS.to_string(),
"nosniff".into(),
)]),
router,
status: StatusCode::OK,
token: None,
Expand Down Expand Up @@ -773,15 +786,17 @@ mod tests {
#[tokio::test]
async fn redirect_http_to_https() {
async fn case(path: &str, location: &str) {
let response = Router::new()
.fallback(Serve::redirect_http_to_https)
.layer(Extension("https://foo".to_string()))
let response = Serve::redirect_router("https://foo".into())
.oneshot(Request::builder().uri(path).body(Body::empty()).unwrap())
.await
.unwrap();

assert_eq!(response.status(), StatusCode::SEE_OTHER);
assert_eq!(response.headers()[header::LOCATION], location);
assert_eq!(
response.headers()[header::X_CONTENT_TYPE_OPTIONS],
"nosniff"
);
}

case("/", "https://foo/").await;
Expand Down
Loading