Skip to content

Commit 03455b8

Browse files
Merge branch 'main' into typo
2 parents af2344d + 4cc3199 commit 03455b8

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/cli/encode.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ use clap::Args;
22

33
use crate::{Decoded, Strkey};
44

5+
// Bound on the JSON input size. The largest legitimate Decoded<Strkey> JSON
6+
// (a pretty-printed signed_payload_ed25519 with a max 64-byte payload) is
7+
// under 300 bytes; 10 KiB allows generous formatting headroom while
8+
// preventing pathologically large hex fields from forcing intermediate
9+
// allocations during deserialization.
10+
const MAX_JSON_LEN: usize = 10 * 1024;
11+
512
#[derive(Debug)]
613
pub enum Error {
14+
InputTooLarge { len: usize, max: usize },
715
Json(serde_json::Error),
816
}
917

1018
impl core::fmt::Display for Error {
1119
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1220
match self {
21+
Error::InputTooLarge { len, max } => f.write_fmt(format_args!(
22+
"json input too large: {len} bytes (max {max})"
23+
)),
1324
Error::Json(e) => f.write_fmt(format_args!("{e}")),
1425
}
1526
}
@@ -27,6 +38,12 @@ pub struct Cmd {
2738

2839
impl Cmd {
2940
pub fn run(&self) -> Result<(), Error> {
41+
if self.json.len() > MAX_JSON_LEN {
42+
return Err(Error::InputTooLarge {
43+
len: self.json.len(),
44+
max: MAX_JSON_LEN,
45+
});
46+
}
3047
let Decoded(strkey): Decoded<Strkey> =
3148
serde_json::from_str(&self.json).map_err(Error::Json)?;
3249
println!("{strkey}");

0 commit comments

Comments
 (0)