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
10 changes: 8 additions & 2 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,19 @@ async fn authenticate(parts: &Parts, conn: &mut AsyncPgConnection) -> AppResult<

match authenticate_via_cookie(parts, conn).await {
Ok(None) => {}
Ok(Some(auth)) => return Ok(Authentication::Cookie(auth)),
Ok(Some(auth)) => {
parts.request_log().add("auth_type", "cookie");
return Ok(Authentication::Cookie(auth));
}
Err(err) => return Err(err),
}

match authenticate_via_token(parts, conn).await {
Ok(None) => {}
Ok(Some(auth)) => return Ok(Authentication::Token(auth)),
Ok(Some(auth)) => {
parts.request_log().add("auth_type", "token");
return Ok(Authentication::Token(auth));
}
Err(err) => return Err(err),
}

Expand Down
2 changes: 2 additions & 0 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
.transpose()?;

let auth = if let Some(trustpub_token) = trustpub_token {
request_log.add("auth_type", "trustpub");

let Some(existing_crate) = &existing_crate else {
let error = forbidden(
"Trusted Publishing tokens do not support creating new crates. Publish the crate manually, first",
Expand Down
15 changes: 14 additions & 1 deletion src/middleware/log_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use axum::response::IntoResponse;
use axum_extra::TypedHeader;
use axum_extra::headers::UserAgent;
use derive_more::Deref;
use http::{Method, Uri};
use http::{Method, Uri, header};
use parking_lot::Mutex;
use sha2::{Digest, Sha256};
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Display;
Expand Down Expand Up @@ -48,6 +49,16 @@ pub async fn log_requests(
let custom_metadata = RequestLog::default();
req.extensions_mut().insert(custom_metadata.clone());

// Log all authorization headers via hashed mask
let hashed_auth_header = req
.headers()
.get(header::AUTHORIZATION)
.map(|header| hex::encode(Sha256::digest(header.as_bytes())));
let hashed_cookie_header = req
.headers()
.get(header::COOKIE)
.map(|header| hex::encode(Sha256::digest(header.as_bytes())));

let response = next.run(req).await;

let duration = start_instant.elapsed();
Expand Down Expand Up @@ -83,6 +94,8 @@ pub async fn log_requests(
http.matched_path = %matched_path,
http.request_id = %request_metadata.request_id.as_ref().map(|h| h.as_str()).unwrap_or_default(),
http.useragent = %request_metadata.user_agent.as_ref().map(|h| h.as_str()).unwrap_or_default(),
http.request.headers.hashed_authorization = hashed_auth_header.unwrap_or_default(),
http.request.headers.hashed_cookie = hashed_cookie_header.unwrap_or_default(),
http.status_code = status.as_u16(),
cause = response.extensions().get::<CauseField>().map(|e| e.0.as_str()).unwrap_or_default(),
error.message = response.extensions().get::<ErrorField>().map(|e| e.0.as_str()).unwrap_or_default(),
Expand Down