normogen/backend/src/middleware/client_ip.rs
goose cf6fcd4ef2 fix(backend): refresh-token jti + code cleanup (#15,#28,#29,#30)
Refresh-token rotation bug (found via Solaria smoke test):
* RefreshClaims now carries a unique random jti (UUID v4). Without it, two
  refresh tokens issued in the same second (e.g. on rotation) were byte-identical
  — breaking rotation, colliding on the tokenHash unique index, and making a
  stolen old token indistinguishable from the new one. Every refresh token is
  now unique regardless of issue time. Added a unit test asserting consecutive
  tokens differ.

Code cleanup (review items):
* #15: removed unused deps tower_governor, slog, thiserror (zero refs in src/).
* #30: deleted leftover src/main.rs.restore (a stale git-error-message backup).
* #28: removed the 9 single-line-comment stub files under src/db/ (appointment,
  family, health_data, lab_result, medication, profile, permission, share, user)
  and their mod declarations; nothing referenced them, real logic lives in
  mongodb_impl.rs/init.rs.
* #29: stripped 61 broad module-level #![allow(...)] suppressions across src/.
  Fixed the surfaced warnings instead: 4 unused 'claims' extractor bindings ->
  _claims; removed dead OpenFDAService.client/base_url fields + the never-called
  query_drug_events method + unused HashMap/reqwest imports; applied clippy's
  mechanical fixes (Ok(?) -> ?, Copy ObjectId clone, redundant closures,
  useless conversions); rewrote 'if let Ok(_) = x' -> 'if x.is_ok()'. Result:
  cargo build + clippy --all-targets are warning-free with no blanket allows.

Verified: fmt clean, build clean, clippy 0 warnings, 19 unit tests pass
(was 18; +1 jti-uniqueness test).
2026-06-27 20:57:24 -03:00

136 lines
4.4 KiB
Rust

//! Resolves the originating client IP for each request and exposes it to
//! handlers via the [`ClientIp`] request extension.
//!
//! Resolution order (most-trusted first):
//! 1. `X-Forwarded-For` — first hop (the original client). Normogen is deployed
//! behind a reverse proxy on Solaria, so this is the primary source.
//! 2. `X-Real-IP` — common single-IP proxy header.
//! 3. The peer address from Axum's `ConnectInfo<SocketAddr>` (direct connection).
//! 4. `"0.0.0.0"` only if none of the above are present.
use axum::{
extract::ConnectInfo, extract::Request, http::HeaderMap, middleware::Next, response::Response,
};
use std::net::SocketAddr;
/// Header carrying the originating client chain (proxy-populated).
const X_FORWARDED_FOR: &str = "x-forwarded-for";
/// Header some proxies set to the single originating IP.
const X_REAL_IP: &str = "x-real-ip";
/// Fallback when no IP can be determined.
const UNKNOWN_IP: &str = "0.0.0.0";
/// Extractor carrying the resolved client IP, inserted by [`client_ip_middleware`].
#[derive(Debug, Clone)]
pub struct ClientIp(pub String);
impl ClientIp {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::ops::Deref for ClientIp {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
/// Resolve the client IP from request headers, falling back to the peer socket.
///
/// Pure and unit-testable: pass the headers and an optional `ConnectInfo`.
pub fn extract_client_ip(headers: &HeaderMap, conn: Option<&ConnectInfo<SocketAddr>>) -> String {
// X-Forwarded-For: "client, proxy1, proxy2" — the leftmost entry is the
// original client. Only validate it looks like an IP; take the first token.
if let Some(xff) = headers.get(X_FORWARDED_FOR).and_then(|h| h.to_str().ok()) {
if let Some(first) = xff.split(',').map(str::trim).find(|s| !s.is_empty()) {
return first.to_string();
}
}
// X-Real-IP: single IP.
if let Some(ip) = headers.get(X_REAL_IP).and_then(|h| h.to_str().ok()) {
let ip = ip.trim();
if !ip.is_empty() {
return ip.to_string();
}
}
// Direct peer address, when ConnectInfo is wired (see main.rs).
if let Some(info) = conn {
return info.0.ip().to_string();
}
UNKNOWN_IP.to_string()
}
/// Middleware that computes the client IP once and stashes it in request
/// extensions so handlers can pull it out with `Extension(ClientIp(..))`.
pub async fn client_ip_middleware(mut req: Request, next: Next) -> Response {
let conn = req.extensions().get::<ConnectInfo<SocketAddr>>();
let ip = extract_client_ip(req.headers(), conn);
req.extensions_mut().insert(ClientIp(ip));
next.run(req).await
}
#[cfg(test)]
mod tests {
use super::*;
use axum::extract::ConnectInfo;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
fn headers() -> HeaderMap {
HeaderMap::new()
}
fn conn() -> ConnectInfo<SocketAddr> {
ConnectInfo(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(203, 0, 113, 7)),
12345,
))
}
#[test]
fn prefers_x_forwarded_for_first_hop() {
let mut h = headers();
h.insert(
X_FORWARDED_FOR,
"198.51.100.2, 10.0.0.1, 10.0.0.2".parse().unwrap(),
);
assert_eq!(extract_client_ip(&h, Some(&conn())), "198.51.100.2");
}
#[test]
fn falls_back_to_x_real_ip_when_no_xff() {
let mut h = headers();
h.insert(X_REAL_IP, "198.51.100.9".parse().unwrap());
assert_eq!(extract_client_ip(&h, Some(&conn())), "198.51.100.9");
}
#[test]
fn xff_takes_precedence_over_x_real_ip() {
let mut h = headers();
h.insert(X_FORWARDED_FOR, "198.51.100.2".parse().unwrap());
h.insert(X_REAL_IP, "10.0.0.99".parse().unwrap());
assert_eq!(extract_client_ip(&h, Some(&conn())), "198.51.100.2");
}
#[test]
fn uses_socket_when_no_proxy_headers() {
assert_eq!(extract_client_ip(&headers(), Some(&conn())), "203.0.113.7");
}
#[test]
fn unknown_when_nothing_available() {
assert_eq!(extract_client_ip(&headers(), None), UNKNOWN_IP);
}
#[test]
fn ignores_blank_xff_entries() {
let mut h = headers();
// Leading whitespace/empty entry should be skipped, not returned blank.
h.insert(X_FORWARDED_FOR, " , 10.0.0.5".parse().unwrap());
assert_eq!(extract_client_ip(&h, None), "10.0.0.5");
}
}