[control] Domi CA and UI sign-in: intermediate renewal by CSR with manual offline signing, domi host certificates, device requests approved by code, one-time PKCS#12 installs, revocation; Remote-User trusted only with edge's per-boot proof
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! In front of the API guard: the built web UI (`[ui] static_dir`) and the one-time device
|
||||
//! downloads. The UI's files hold no data (it reads everything from the API, which the guard
|
||||
//! protects), so they need no token; the Host check still applies against DNS rebinding.
|
||||
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::body::Body as AxumBody;
|
||||
use axum::extract::{Path as AxPath, Request, State};
|
||||
use axum::http::{HeaderValue, StatusCode, header};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
|
||||
use super::{ApiError, AppState, host_allowed};
|
||||
use crate::messages::Params;
|
||||
|
||||
const CSP: &str = "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; \
|
||||
script-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'";
|
||||
|
||||
pub async fn front(State(st): State<Arc<AppState>>, req: Request, next: Next) -> Response {
|
||||
let path = req.uri().path().to_owned();
|
||||
if let Some(token) = path.strip_prefix("/api/v1/device-bundles/") {
|
||||
if !host_allowed(&st.ctl, req.headers()) {
|
||||
return ApiError::new(StatusCode::FORBIDDEN, "request.host", Params::new())
|
||||
.into_response();
|
||||
}
|
||||
if req.method() != axum::http::Method::GET {
|
||||
return ApiError::new(
|
||||
StatusCode::METHOD_NOT_ALLOWED,
|
||||
"request.method",
|
||||
Params::new(),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
let token = token.to_owned();
|
||||
return super::pki::download_bundle(State(st), AxPath(token))
|
||||
.await
|
||||
.unwrap_or_else(IntoResponse::into_response);
|
||||
}
|
||||
if path.starts_with("/api/") || path == "/api" {
|
||||
return next.run(req).await;
|
||||
}
|
||||
if !host_allowed(&st.ctl, req.headers()) {
|
||||
return ApiError::new(StatusCode::FORBIDDEN, "request.host", Params::new()).into_response();
|
||||
}
|
||||
let Some(dir) = st.ctl.cfg.ui.as_ref().and_then(|u| u.static_dir.clone()) else {
|
||||
return ApiError::new(StatusCode::NOT_FOUND, "request.not_found", Params::new())
|
||||
.into_response();
|
||||
};
|
||||
if !matches!(req.method().as_str(), "GET" | "HEAD") {
|
||||
return ApiError::new(
|
||||
StatusCode::METHOD_NOT_ALLOWED,
|
||||
"request.method",
|
||||
Params::new(),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
serve_static(&dir, &path).await
|
||||
}
|
||||
|
||||
/// A file from the UI build. Paths with `..`, odd characters or hidden names are refused;
|
||||
/// a path without a file extension is the single-page app (`index.html`).
|
||||
async fn serve_static(dir: &Path, url_path: &str) -> Response {
|
||||
let rel = url_path.trim_start_matches('/');
|
||||
let clean = !rel.contains('\\')
|
||||
&& rel
|
||||
.bytes()
|
||||
.all(|c| c.is_ascii_alphanumeric() || b"-_./".contains(&c))
|
||||
&& Path::new(rel)
|
||||
.components()
|
||||
.all(|c| matches!(c, Component::Normal(n) if !n.to_string_lossy().starts_with('.')));
|
||||
if !clean {
|
||||
return ApiError::new(StatusCode::NOT_FOUND, "request.not_found", Params::new())
|
||||
.into_response();
|
||||
}
|
||||
let has_ext = Path::new(rel).extension().is_some();
|
||||
let file: PathBuf = if rel.is_empty() || !has_ext {
|
||||
dir.join("index.html")
|
||||
} else {
|
||||
dir.join(rel)
|
||||
};
|
||||
let Ok(bytes) = tokio::fs::read(&file).await else {
|
||||
return ApiError::new(StatusCode::NOT_FOUND, "request.not_found", Params::new())
|
||||
.into_response();
|
||||
};
|
||||
let ctype = match file.extension().and_then(|e| e.to_str()).unwrap_or("") {
|
||||
"html" => "text/html; charset=utf-8",
|
||||
"js" | "mjs" => "text/javascript; charset=utf-8",
|
||||
"css" => "text/css; charset=utf-8",
|
||||
"svg" => "image/svg+xml",
|
||||
"png" => "image/png",
|
||||
"ico" => "image/x-icon",
|
||||
"woff2" => "font/woff2",
|
||||
"json" | "webmanifest" => "application/json",
|
||||
"txt" => "text/plain; charset=utf-8",
|
||||
_ => "application/octet-stream",
|
||||
};
|
||||
let immutable = rel.starts_with("assets/") || rel.starts_with("_app/immutable/");
|
||||
let mut r = Response::new(AxumBody::from(bytes));
|
||||
let h = r.headers_mut();
|
||||
h.insert(header::CONTENT_TYPE, HeaderValue::from_static(ctype));
|
||||
h.insert(
|
||||
header::CACHE_CONTROL,
|
||||
HeaderValue::from_static(if immutable {
|
||||
"public, max-age=31536000, immutable"
|
||||
} else {
|
||||
"no-cache"
|
||||
}),
|
||||
);
|
||||
h.insert("content-security-policy", HeaderValue::from_static(CSP));
|
||||
h.insert(
|
||||
"x-content-type-options",
|
||||
HeaderValue::from_static("nosniff"),
|
||||
);
|
||||
h.insert("referrer-policy", HeaderValue::from_static("no-referrer"));
|
||||
h.insert("x-frame-options", HeaderValue::from_static("DENY"));
|
||||
r
|
||||
}
|
||||
Reference in New Issue
Block a user