Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not parse base62 strings of unexpected length #8

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub const KSUID_EPOCH: i64 = 1_400_000_000;
const BASE_62_CHARS: &[u8; 62] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

const TOTAL_BYTES: usize = 20;
const TOTAL_BYTES_BASE62: usize = 27;

#[derive(Debug)]
pub struct Error(String);
Expand Down Expand Up @@ -237,6 +238,12 @@ pub trait KsuidLike {
///
/// ```
fn from_base62(s: &str) -> Result<Self::Type, Error> {
if s.len() != TOTAL_BYTES_BASE62 {
return Err(Error(format!(
"Got base62 ksuid of unexpected length {}",
s.len()
)));
}
if let Some(loaded) = base_encode::from_str(s, 62, BASE_62_CHARS) {
// Get the last TOTAL_BYTES
let loaded = if loaded.len() > TOTAL_BYTES {
Expand Down
12 changes: 12 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
let ksuid1 = Ksuid::new(None, None);
let ksuid2 = Ksuid::new(None, None);
// when
set.insert(ksuid1.clone());

Check warning on line 115 in tests/integration.rs

View workflow job for this annotation

GitHub Actions / check

using `clone` on type `Ksuid` which implements the `Copy` trait
set.insert(ksuid2.clone());

Check warning on line 116 in tests/integration.rs

View workflow job for this annotation

GitHub Actions / check

using `clone` on type `Ksuid` which implements the `Copy` trait
// then
assert_eq!(set.len(), 2);
assert!(set.contains(&ksuid1));
Expand All @@ -127,8 +127,8 @@
let ksuidms1 = KsuidMs::new(None, None);
let ksuidms2 = KsuidMs::new(None, None);
// when
set.insert(ksuidms1.clone());

Check warning on line 130 in tests/integration.rs

View workflow job for this annotation

GitHub Actions / check

using `clone` on type `KsuidMs` which implements the `Copy` trait
set.insert(ksuidms2.clone());

Check warning on line 131 in tests/integration.rs

View workflow job for this annotation

GitHub Actions / check

using `clone` on type `KsuidMs` which implements the `Copy` trait
// then
assert_eq!(set.len(), 2);
assert!(set.contains(&ksuidms1));
Expand Down Expand Up @@ -203,3 +203,15 @@
assert_eq!(ksuid_obj.id.to_string(), b62);
assert_eq!(ksuidms_obj.id.to_string(), b62);
}

#[test]
fn test_deserialize_bad_base62_length() {
let short_b62 = "ZBpBUvZwXKQmoEYga2";
let long_b62 = "1srOrx2ZWZBpBUvZwXKQmoEYga21srOrx2ZWZBpBUvZwXKQmoEYga2";

let result = Ksuid::from_base62(short_b62);
assert!(result.is_err(), "Short base62 strings should fail to parse");

let result = Ksuid::from_base62(long_b62);
assert!(result.is_err(), "Long base62 strings should fail to parse");
}
Loading