mirror of
https://github.com/dani-garcia/vaultwarden.wiki.git
synced 2026-11-04 04:06:47 +02:00
Refuse key ids that aren't 32 lowercase hex characters, like upstream
This commit is contained in:
1 file changed
+34
-14
+34
-14
@@ -660,25 +660,26 @@ impl Invitation {
|
|||||||
pub struct UserId(String);
|
pub struct UserId(String);
|
||||||
|
|
||||||
#[derive(
|
#[derive(
|
||||||
Clone,
|
Clone, Debug, DieselNewType, FromForm, PartialEq, Eq, Hash, Serialize, AsRef, Deref, Display, From, UuidFromParam,
|
||||||
Debug,
|
|
||||||
DieselNewType,
|
|
||||||
FromForm,
|
|
||||||
PartialEq,
|
|
||||||
Eq,
|
|
||||||
Hash,
|
|
||||||
Serialize,
|
|
||||||
Deserialize,
|
|
||||||
AsRef,
|
|
||||||
Deref,
|
|
||||||
Display,
|
|
||||||
From,
|
|
||||||
UuidFromParam,
|
|
||||||
)]
|
)]
|
||||||
#[deref(forward)]
|
#[deref(forward)]
|
||||||
#[from(forward)]
|
#[from(forward)]
|
||||||
pub struct KeyId(String);
|
pub struct KeyId(String);
|
||||||
|
|
||||||
|
/// Like upstream, a key id is 32 lowercase hex characters. Key ids are compared as strings, so one in any other
|
||||||
|
/// format would never match the id the clients send.
|
||||||
|
///
|
||||||
|
/// Ref: <https://github.com/bitwarden/server/blob/8da186ad73f80d37157fe6fd42323e4284e83ec1/src/Core/KeyManagement/Models/Data/KeyId.cs#L20-L42>
|
||||||
|
impl<'de> serde::Deserialize<'de> for KeyId {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||||
|
let key_id = String::deserialize(deserializer)?;
|
||||||
|
if key_id.len() != 32 || !key_id.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) {
|
||||||
|
return Err(serde::de::Error::custom("Key id must be 32 lowercase hex characters"));
|
||||||
|
}
|
||||||
|
Ok(Self(key_id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl SsoUser {
|
impl SsoUser {
|
||||||
pub async fn save(&self, conn: &DbConn) -> EmptyResult {
|
pub async fn save(&self, conn: &DbConn) -> EmptyResult {
|
||||||
db_run! { conn:
|
db_run! { conn:
|
||||||
@@ -732,3 +733,22 @@ impl SsoUser {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_key_id_format() {
|
||||||
|
let key_id: KeyId = serde_json::from_str(r#""0123456789abcdef0123456789abcdef""#).unwrap();
|
||||||
|
assert_eq!(key_id.as_ref(), "0123456789abcdef0123456789abcdef");
|
||||||
|
|
||||||
|
for invalid in ["", "0123456789ABCDEF0123456789ABCDEF", "0123456789abcdef", "0123456789abcdef0123456789abcdeg"]
|
||||||
|
{
|
||||||
|
assert!(serde_json::from_str::<KeyId>(&format!("\"{invalid}\"")).is_err(), "{invalid:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// An absent key id stays optional
|
||||||
|
assert_eq!(serde_json::from_str::<Option<KeyId>>("null").unwrap(), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user