[control] API fixes: atomic Idempotency-Key reservation, no echo of odd field names or path parameters, askpass answers only password prompts, file protocol not forced, token test split on the known prefix, serialisation test without second-resolution times
This commit is contained in:
@@ -51,10 +51,15 @@ pub struct Actor {
|
||||
}
|
||||
|
||||
pub enum Stored {
|
||||
/// The first request with this key is still being handled.
|
||||
Pending,
|
||||
Apply(String),
|
||||
Json(u16, serde_json::Value),
|
||||
}
|
||||
|
||||
/// Most Idempotency-Keys held at once (each for 24 h).
|
||||
const MAX_IDEM_KEYS: usize = 2000;
|
||||
|
||||
pub struct AppState {
|
||||
pub ctl: Arc<Control>,
|
||||
idem: Mutex<HashMap<String, (String, Stored, u64)>>,
|
||||
@@ -91,32 +96,65 @@ impl AppState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Look up an Idempotency-Key. `Ok(Some)` replays, `Err` when the key was used for
|
||||
/// another request.
|
||||
pub fn idem_get(&self, key: &str, fingerprint: &str) -> Result<Option<Stored>, ApiError> {
|
||||
let Ok(m) = self.idem.lock() else {
|
||||
return Ok(None);
|
||||
};
|
||||
/// Start a request with an Idempotency-Key, atomically: `Ok(Some)` replays an earlier
|
||||
/// answer, `Ok(None)` reserves the key (finish with [`AppState::idem_finish`]), `Err` when
|
||||
/// the key was used for another request, is still in progress, or no key can be held.
|
||||
pub fn idem_begin(&self, key: &str, fingerprint: &str) -> Result<Option<Stored>, ApiError> {
|
||||
let mut m = self
|
||||
.idem
|
||||
.lock()
|
||||
.map_err(|_| ApiError::internal("idempotency store"))?;
|
||||
let t = now();
|
||||
match m.get(key) {
|
||||
Some((fp, _, _)) if fp != fingerprint => Err(ApiError::new(
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"request.idempotency",
|
||||
Params::new(),
|
||||
)),
|
||||
Some((_, Stored::Apply(id), _)) => Ok(Some(Stored::Apply(id.clone()))),
|
||||
Some((_, Stored::Json(s, v), _)) => Ok(Some(Stored::Json(*s, v.clone()))),
|
||||
None => Ok(None),
|
||||
Some((fp, _, at)) if t.saturating_sub(*at) < 86_400 && fp != fingerprint => {
|
||||
return Err(ApiError::new(
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"request.idempotency",
|
||||
Params::new(),
|
||||
));
|
||||
}
|
||||
Some((_, Stored::Pending, at)) if t.saturating_sub(*at) < 86_400 => {
|
||||
return Err(ApiError::new(
|
||||
StatusCode::CONFLICT,
|
||||
"request.in_progress",
|
||||
Params::new(),
|
||||
)
|
||||
.retry(1));
|
||||
}
|
||||
Some((_, Stored::Apply(id), at)) if t.saturating_sub(*at) < 86_400 => {
|
||||
return Ok(Some(Stored::Apply(id.clone())));
|
||||
}
|
||||
Some((_, Stored::Json(s, v), at)) if t.saturating_sub(*at) < 86_400 => {
|
||||
return Ok(Some(Stored::Json(*s, v.clone())));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if m.len() >= MAX_IDEM_KEYS {
|
||||
m.retain(|_, (_, _, at)| t.saturating_sub(*at) < 86_400);
|
||||
}
|
||||
if m.len() >= MAX_IDEM_KEYS {
|
||||
return Err(ApiError::new(
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
"rate.limited",
|
||||
Params::new(),
|
||||
)
|
||||
.retry(60));
|
||||
}
|
||||
m.insert(key.to_owned(), (fingerprint.to_owned(), Stored::Pending, t));
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub fn idem_put(&self, key: &str, fingerprint: &str, s: Stored) {
|
||||
/// Finish a reserved key: remember the answer, or (on `None`, a refusal) free the key.
|
||||
pub fn idem_finish(&self, key: &str, fingerprint: &str, s: Option<Stored>) {
|
||||
if let Ok(mut m) = self.idem.lock() {
|
||||
let t = now();
|
||||
m.retain(|_, (_, _, at)| t.saturating_sub(*at) < 86_400);
|
||||
if m.len() >= 2000 {
|
||||
return;
|
||||
match s {
|
||||
Some(s) => {
|
||||
m.insert(key.to_owned(), (fingerprint.to_owned(), s, now()));
|
||||
}
|
||||
None => {
|
||||
m.remove(key);
|
||||
}
|
||||
}
|
||||
m.insert(key.to_owned(), (fingerprint.to_owned(), s, t));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -234,12 +272,22 @@ fn body_error(e: &serde_json::Error) -> ApiError {
|
||||
Some(rest.split('`').next()?.to_owned())
|
||||
};
|
||||
if let Some(f) = quoted("unknown field `") {
|
||||
// The name is the sender's: repeat it only if it is a plain field name.
|
||||
let plain = !f.is_empty()
|
||||
&& f.len() <= 64
|
||||
&& f.bytes()
|
||||
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'_');
|
||||
let shown = if plain {
|
||||
f
|
||||
} else {
|
||||
"(a field name)".to_owned()
|
||||
};
|
||||
let mut err = ApiError::new(
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"request.body_unknown_field",
|
||||
messages::params([("field", f.clone())]),
|
||||
messages::params([("field", shown.clone())]),
|
||||
);
|
||||
err.body.field = Some(f);
|
||||
err.body.field = plain.then_some(shown);
|
||||
return err;
|
||||
}
|
||||
if let Some(f) = quoted("missing field `") {
|
||||
|
||||
Reference in New Issue
Block a user