use std::time::Duration;
use axum::body::{Body, to_bytes};
use axum::extract::{MatchedPath, Request};
use axum::http::header::{CONTENT_TYPE, DATE, SET_COOKIE};
use axum::http::{HeaderName, HeaderValue, Method, StatusCode};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::db::{DateTime, Db, Migration, now, sql};
use crate::{Error, Result};
pub(crate) const MIGRATION: Migration = crate::db::framework_migration!(
"idempotency",
"00010101000440_create_idempotency_keys_table"
);
pub const DEFAULT_RETENTION: std::time::Duration = std::time::Duration::from_secs(7 * 24 * 60 * 60);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Stored {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
pub created_at: DateTime,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Claim {
Claimed,
Replay(Stored),
Mismatch,
InProgress,
}
pub fn body_hash(body: &[u8]) -> String {
use std::fmt::Write;
Sha256::digest(body)
.iter()
.fold(String::with_capacity(64), |mut hex, byte| {
let _ = write!(hex, "{byte:02x}");
hex
})
}
pub async fn claim(db: &Db, scope: &str, route: &str, key: &str, body_hash: &str) -> Result<Claim> {
let inserted = sql(
"INSERT INTO idempotency_keys (scope, route, key, body_hash, created_at) \
VALUES (?, ?, ?, ?, ?) ON CONFLICT (scope, route, key) DO NOTHING",
)
.bind(scope)
.bind(route)
.bind(key)
.bind(body_hash)
.bind(now())
.execute(db)
.await?;
if inserted == 1 {
return Ok(Claim::Claimed);
}
let row = sql("SELECT body_hash, status, headers, response, created_at \
FROM idempotency_keys WHERE scope = ? AND route = ? AND key = ?")
.bind(scope)
.bind(route)
.bind(key)
.fetch_optional(db)
.await?;
let Some(row) = row else {
return Box::pin(claim(db, scope, route, key, body_hash)).await;
};
let stored_hash: String = row.try_get("body_hash")?;
if stored_hash != body_hash {
return Ok(Claim::Mismatch);
}
let status: Option<i64> = row.try_get("status")?;
let Some(status) = status else {
return Ok(Claim::InProgress);
};
let headers: Option<String> = row.try_get("headers")?;
let headers = match headers {
Some(text) => serde_json::from_str(&text).map_err(anyhow::Error::from)?,
None => Vec::new(),
};
Ok(Claim::Replay(Stored {
status: u16::try_from(status).map_err(anyhow::Error::from)?,
headers,
body: row
.try_get::<Option<Vec<u8>>>("response")?
.unwrap_or_default(),
created_at: row.try_get("created_at")?,
}))
}
pub async fn complete(
db: &Db,
scope: &str,
route: &str,
key: &str,
status: u16,
headers: &[(String, String)],
body: &[u8],
) -> Result<()> {
let headers = serde_json::to_string(headers).map_err(anyhow::Error::from)?;
let changed = sql(
"UPDATE idempotency_keys SET status = ?, headers = ?, response = ? \
WHERE scope = ? AND route = ? AND key = ?",
)
.bind(i64::from(status))
.bind(headers)
.bind(body.to_vec())
.bind(scope)
.bind(route)
.bind(key)
.execute(db)
.await?;
if changed == 0 {
return Err(Error::NotFound);
}
Ok(())
}
pub async fn release(db: &Db, scope: &str, route: &str, key: &str) -> Result<()> {
sql("DELETE FROM idempotency_keys \
WHERE scope = ? AND route = ? AND key = ? AND status IS NULL")
.bind(scope)
.bind(route)
.bind(key)
.execute(db)
.await?;
Ok(())
}
pub async fn prune(db: &Db, before: DateTime) -> Result<u64> {
Ok(sql("DELETE FROM idempotency_keys WHERE created_at < ?")
.bind(before)
.execute(db)
.await?)
}
pub(crate) async fn prune_retained(state: &crate::AppState, days: Option<u32>) -> Result<u64> {
let days = days.unwrap_or(state.config.idempotency_retention_days);
prune(&state.db, now() - chrono::TimeDelta::days(i64::from(days))).await
}
pub const KEY_HEADER: &str = "idempotency-key";
pub const REPLAY_HEADER: &str = "idempotent-replayed";
const KEY_LIMIT: usize = 255;
const RESPONSE_LIMIT: usize = 16 * 1024 * 1024;
const WAIT: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Skew {
past: Duration,
future: Duration,
}
impl Default for Skew {
fn default() -> Self {
Self {
past: Duration::from_secs(24 * 60 * 60),
future: Duration::from_secs(5 * 60),
}
}
}
impl Skew {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn past(mut self, limit: Duration) -> Self {
self.past = limit;
self
}
#[must_use]
pub fn future(mut self, limit: Duration) -> Self {
self.future = limit;
self
}
pub fn max_past(&self) -> Duration {
self.past
}
pub fn max_future(&self) -> Duration {
self.future
}
}
pub const RECORDED_AT_HEADER: &str = "x-recorded-at";
pub const CLIENT_ID_HEADER: &str = "x-client-id";
const CLIENT_ID_LIMIT: usize = 128;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Offline {
pub recorded_at: DateTime,
pub client_id: Option<String>,
pub received_at: DateTime,
pub skew_exceeded: bool,
}
impl Offline {
pub(crate) fn read(headers: &axum::http::HeaderMap, skew: Skew, received_at: DateTime) -> Self {
let text = |name: &str| {
headers
.get(name)
.and_then(|v| v.to_str().ok())
.map(str::trim)
.filter(|v| !v.is_empty())
};
let device = text(RECORDED_AT_HEADER)
.and_then(|v| chrono::DateTime::parse_from_rfc3339(v).ok())
.map(|t| t.with_timezone(&chrono::Utc));
let recorded_at = device.unwrap_or(received_at);
let skew_exceeded = device.is_some_and(|t| {
let age = received_at - t;
let past =
chrono::TimeDelta::from_std(skew.max_past()).unwrap_or(chrono::TimeDelta::MAX);
let future =
chrono::TimeDelta::from_std(skew.max_future()).unwrap_or(chrono::TimeDelta::MAX);
age > past || -age > future
});
let client_id = text(CLIENT_ID_HEADER)
.filter(|v| v.len() <= CLIENT_ID_LIMIT)
.map(str::to_owned);
Self {
recorded_at,
client_id,
received_at,
skew_exceeded,
}
}
}
impl<S: Send + Sync> axum::extract::FromRequestParts<S> for Offline {
type Rejection = std::convert::Infallible;
async fn from_request_parts(
parts: &mut axum::http::request::Parts,
_: &S,
) -> std::result::Result<Self, Self::Rejection> {
let skew = parts.extensions.get::<Skew>().copied().unwrap_or_default();
Ok(Self::read(&parts.headers, skew, now()))
}
}
fn scope_of(req: &Request) -> String {
let current = req.extensions().get::<crate::auth::CurrentUser>();
if let Some(user) = current.and_then(|c| c.user.as_ref()) {
return format!("user:{}", user.id);
}
if let Some(device) = current.and_then(|c| c.device.as_ref()) {
return format!("device:{}", device.key());
}
req.extensions()
.get::<crate::session::Session>()
.map_or_else(String::new, |s| format!("session:{}", s.token()))
}
fn comparable(headers: &axum::http::HeaderMap, body: &[u8]) -> Vec<u8> {
let content_type = headers
.get(CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or_default();
if content_type.starts_with("application/x-www-form-urlencoded") {
let mut out = form_urlencoded::Serializer::new(String::new());
for (name, value) in
form_urlencoded::parse(body).filter(|(name, _)| name != crate::csrf::CSRF_FIELD)
{
out.append_pair(&name, &value);
}
return out.finish().into_bytes();
}
let boundary = content_type
.split(';')
.filter_map(|part| part.trim().strip_prefix("boundary="))
.next()
.map(|b| b.trim_matches('"'))
.filter(|b| !b.is_empty());
match (content_type.starts_with("multipart/form-data"), boundary) {
(true, Some(boundary)) => String::from_utf8_lossy(body)
.replace(boundary, "-")
.into_bytes(),
_ => body.to_vec(),
}
}
fn replay_of(stored: Stored) -> Response {
let mut res = Response::new(Body::from(stored.body));
*res.status_mut() = StatusCode::from_u16(stored.status).unwrap_or(StatusCode::OK);
for (name, value) in stored.headers {
if let (Ok(name), Ok(value)) = (
HeaderName::from_bytes(name.as_bytes()),
HeaderValue::from_str(&value),
) {
res.headers_mut().append(name, value);
}
}
res.headers_mut().insert(
HeaderName::from_static(REPLAY_HEADER),
HeaderValue::from_static("true"),
);
res
}
struct Guard {
db: Db,
scope: String,
route: String,
key: String,
armed: bool,
}
impl Drop for Guard {
fn drop(&mut self) {
if !self.armed {
return;
}
let (db, scope, route, key) = (
self.db.clone(),
std::mem::take(&mut self.scope),
std::mem::take(&mut self.route),
std::mem::take(&mut self.key),
);
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
runtime.spawn(async move {
let _ = release(&db, &scope, &route, &key).await;
});
}
}
}
pub(crate) async fn middleware(skew: Skew, mut req: Request, next: Next) -> Response {
if matches!(
*req.method(),
Method::GET | Method::HEAD | Method::OPTIONS | Method::TRACE
) {
return next.run(req).await;
}
let Some(key) = req
.headers()
.get(KEY_HEADER)
.and_then(|v| v.to_str().ok())
.map(|v| v.trim().to_owned())
.filter(|v| !v.is_empty() && v.len() <= KEY_LIMIT)
else {
return Error::BadRequest(
"This request needs an Idempotency-Key header (at most 255 characters).".into(),
)
.into_response();
};
let Some(state) = req.extensions().get::<crate::AppState>().cloned() else {
return Error::from(anyhow::anyhow!(
"`.offline()` needs the app state; it runs inside an App"
))
.into_response();
};
let db = state.db.clone();
let scope = scope_of(&req);
let route = format!(
"{} {}",
req.method(),
req.extensions()
.get::<MatchedPath>()
.map_or_else(|| req.uri().path(), MatchedPath::as_str)
);
let limit = state.config.upload_max_size.max(crate::csrf::FORM_LIMIT);
let target = req
.uri()
.path_and_query()
.map_or_else(String::new, ToString::to_string);
let (parts, body) = req.into_parts();
let Ok(bytes) = to_bytes(body, limit).await else {
return StatusCode::PAYLOAD_TOO_LARGE.into_response();
};
let mut compared = target.into_bytes();
compared.push(b'\n');
compared.extend(comparable(&parts.headers, &bytes));
let hash = body_hash(&compared);
req = Request::from_parts(parts, Body::from(bytes));
let waited = std::time::Instant::now();
loop {
match claim(&db, &scope, &route, &key, &hash).await {
Err(err) => return err.into_response(),
Ok(Claim::Replay(stored)) => return replay_of(stored),
Ok(Claim::Mismatch) => {
return Error::Status(
StatusCode::UNPROCESSABLE_ENTITY,
"This Idempotency-Key was already used for a different request.".into(),
)
.into_response();
}
Ok(Claim::InProgress) => {
if waited.elapsed() >= WAIT {
let mut res = Error::Status(
StatusCode::CONFLICT,
"The first request with this Idempotency-Key is still running.".into(),
)
.into_response();
res.headers_mut()
.insert("retry-after", HeaderValue::from_static("1"));
return res;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
Ok(Claim::Claimed) => break,
}
}
let mut guard = Guard {
db: db.clone(),
scope: scope.clone(),
route: route.clone(),
key: key.clone(),
armed: true,
};
req.extensions_mut().insert(skew);
let res = next.run(req).await;
if res.status().is_server_error() {
return res;
}
let (parts, body) = res.into_parts();
let Ok(bytes) = to_bytes(body, RESPONSE_LIMIT).await else {
return Error::from(anyhow::anyhow!("the response is too large to keep")).into_response();
};
let headers: Vec<(String, String)> = parts
.headers
.iter()
.filter(|(name, _)| **name != SET_COOKIE && **name != DATE)
.filter_map(|(name, value)| {
Some((name.as_str().to_owned(), value.to_str().ok()?.to_owned()))
})
.collect();
match complete(
&db,
&scope,
&route,
&key,
parts.status.as_u16(),
&headers,
&bytes,
)
.await
{
Ok(()) => guard.armed = false,
Err(err) => tracing::warn!(error = ?err, "could not store an idempotent response"),
}
Response::from_parts(parts, Body::from(bytes))
}