fix(auth): validate Minecraft profile UUIDs
This commit is contained in:
1 parent
64bb5e2d16
commit
45ce7ef84b
1 file changed
+32
-11
+32
-11
@@ -34,6 +34,19 @@ struct McProfile {
|
|||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn normalize_profile_uuid(id: &str) -> Option<String> {
|
||||||
|
(id.len() == 32 && id.bytes().all(|byte| byte.is_ascii_hexdigit())).then(|| {
|
||||||
|
format!(
|
||||||
|
"{}-{}-{}-{}-{}",
|
||||||
|
&id[..8],
|
||||||
|
&id[8..12],
|
||||||
|
&id[12..16],
|
||||||
|
&id[16..20],
|
||||||
|
&id[20..32],
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// shared slot so the TUI can poll for the device code to show the user
|
// shared slot so the TUI can poll for the device code to show the user
|
||||||
pub static DEVICE_CODE_DISPLAY: LazyLock<Arc<Mutex<Option<DeviceCodeInfo>>>> =
|
pub static DEVICE_CODE_DISPLAY: LazyLock<Arc<Mutex<Option<DeviceCodeInfo>>>> =
|
||||||
LazyLock::new(|| Arc::new(Mutex::new(None)));
|
LazyLock::new(|| Arc::new(Mutex::new(None)));
|
||||||
@@ -136,17 +149,9 @@ async fn exchange_and_build_account(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// mojang returns uuids without dashes
|
// mojang returns uuids without dashes
|
||||||
let uuid = if profile.id.len() == 32 {
|
let uuid = match normalize_profile_uuid(&profile.id) {
|
||||||
format!(
|
Some(uuid) => uuid,
|
||||||
"{}-{}-{}-{}-{}",
|
None => return AuthResult::Error("Profile returned an invalid UUID".to_owned()),
|
||||||
&profile.id[..8],
|
|
||||||
&profile.id[8..12],
|
|
||||||
&profile.id[12..16],
|
|
||||||
&profile.id[16..20],
|
|
||||||
&profile.id[20..32],
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
profile.id.clone()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
AuthResult::Success(Account {
|
AuthResult::Success(Account {
|
||||||
@@ -274,4 +279,20 @@ mod tests {
|
|||||||
|
|
||||||
assert!(valid_cached_mc_token(&account, 1_000).is_none());
|
assert!(valid_cached_mc_token(&account, 1_000).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn profile_uuid_is_normalized_without_slicing_unicode() {
|
||||||
|
assert_eq!(
|
||||||
|
normalize_profile_uuid("0123456789abcdef0123456789abcdef"),
|
||||||
|
Some("01234567-89ab-cdef-0123-456789abcdef".to_owned())
|
||||||
|
);
|
||||||
|
|
||||||
|
let unicode_id = format!("{}é{}", "a".repeat(7), "b".repeat(23));
|
||||||
|
assert_eq!(unicode_id.len(), 32);
|
||||||
|
assert_eq!(normalize_profile_uuid(&unicode_id), None);
|
||||||
|
assert_eq!(
|
||||||
|
normalize_profile_uuid("not-a-valid-minecraft-profile-id"),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user