File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,14 +2,25 @@ use clap::Args;
22
33use 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 ) ]
613pub enum Error {
14+ InputTooLarge { len : usize , max : usize } ,
715 Json ( serde_json:: Error ) ,
816}
917
1018impl 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
2839impl 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}" ) ;
You can’t perform that action at this time.
0 commit comments