1use std::{collections::BTreeMap, fmt, str::FromStr};
7
8use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
9use serde::{
10 Deserialize, Deserializer, Serialize, Serializer,
11 de::{self, DeserializeSeed, MapAccess, SeqAccess, Visitor},
12};
13use serde_json::{Map, Number, Value};
14use thiserror::Error;
15
16const KIBIBYTE: usize = 1024;
17const MEBIBYTE: usize = KIBIBYTE * KIBIBYTE;
18
19#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
21#[non_exhaustive]
22pub enum JsonLimit {
23 Bytes,
25 Depth,
27 ContainerEntries,
29 Nodes,
31 StringBytes,
33 ObjectKeyBytes,
35}
36
37impl fmt::Display for JsonLimit {
38 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
39 formatter.write_str(match self {
40 Self::Bytes => "JSON bytes",
41 Self::Depth => "JSON nesting depth",
42 Self::ContainerEntries => "JSON container entries",
43 Self::Nodes => "JSON value nodes",
44 Self::StringBytes => "JSON string bytes",
45 Self::ObjectKeyBytes => "JSON object-key bytes",
46 })
47 }
48}
49
50#[allow(clippy::struct_field_names)]
57#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
58pub struct JsonLimits {
59 max_bytes: usize,
60 max_depth: usize,
61 max_container_entries: usize,
62 max_nodes: usize,
63 max_string_bytes: usize,
64 max_object_key_bytes: usize,
65}
66
67impl JsonLimits {
68 pub const DEFAULT: Self = Self {
70 max_bytes: 256 * KIBIBYTE,
71 max_depth: 32,
72 max_container_entries: 1024,
73 max_nodes: 16_384,
74 max_string_bytes: 64 * KIBIBYTE,
75 max_object_key_bytes: 256,
76 };
77
78 pub const MAXIMUM: Self = Self {
80 max_bytes: 2 * MEBIBYTE,
81 max_depth: 64,
82 max_container_entries: 8192,
83 max_nodes: 131_072,
84 max_string_bytes: MEBIBYTE,
85 max_object_key_bytes: 1024,
86 };
87
88 pub fn try_new(
95 max_bytes: usize,
96 max_depth: usize,
97 max_container_entries: usize,
98 max_nodes: usize,
99 max_string_bytes: usize,
100 max_object_key_bytes: usize,
101 ) -> Result<Self, JsonLimitsError> {
102 validate_configured_limit(JsonLimit::Bytes, max_bytes, Self::MAXIMUM.max_bytes)?;
103 validate_configured_limit(JsonLimit::Depth, max_depth, Self::MAXIMUM.max_depth)?;
104 validate_configured_limit(
105 JsonLimit::ContainerEntries,
106 max_container_entries,
107 Self::MAXIMUM.max_container_entries,
108 )?;
109 validate_configured_limit(JsonLimit::Nodes, max_nodes, Self::MAXIMUM.max_nodes)?;
110 validate_configured_limit(
111 JsonLimit::StringBytes,
112 max_string_bytes,
113 Self::MAXIMUM.max_string_bytes,
114 )?;
115 validate_configured_limit(
116 JsonLimit::ObjectKeyBytes,
117 max_object_key_bytes,
118 Self::MAXIMUM.max_object_key_bytes,
119 )?;
120
121 Ok(Self {
122 max_bytes,
123 max_depth,
124 max_container_entries,
125 max_nodes,
126 max_string_bytes,
127 max_object_key_bytes,
128 })
129 }
130
131 #[must_use]
133 pub const fn max_bytes(self) -> usize {
134 self.max_bytes
135 }
136
137 #[must_use]
139 pub const fn max_depth(self) -> usize {
140 self.max_depth
141 }
142
143 #[must_use]
145 pub const fn max_container_entries(self) -> usize {
146 self.max_container_entries
147 }
148
149 #[must_use]
151 pub const fn max_nodes(self) -> usize {
152 self.max_nodes
153 }
154
155 #[must_use]
157 pub const fn max_string_bytes(self) -> usize {
158 self.max_string_bytes
159 }
160
161 #[must_use]
163 pub const fn max_object_key_bytes(self) -> usize {
164 self.max_object_key_bytes
165 }
166}
167
168impl Default for JsonLimits {
169 fn default() -> Self {
170 Self::DEFAULT
171 }
172}
173
174fn validate_configured_limit(
175 limit: JsonLimit,
176 actual: usize,
177 maximum: usize,
178) -> Result<(), JsonLimitsError> {
179 if actual == 0 {
180 return Err(JsonLimitsError::Zero { limit });
181 }
182 if actual > maximum {
183 return Err(JsonLimitsError::AboveHardMaximum {
184 limit,
185 maximum,
186 actual,
187 });
188 }
189 Ok(())
190}
191
192#[derive(Clone, Debug, Eq, Error, PartialEq)]
194#[non_exhaustive]
195pub enum JsonLimitsError {
196 #[error("{limit} limit must be greater than zero")]
198 Zero {
199 limit: JsonLimit,
201 },
202
203 #[error("configured {limit} limit is {actual}; hard maximum is {maximum}")]
205 AboveHardMaximum {
206 limit: JsonLimit,
208 maximum: usize,
210 actual: usize,
212 },
213}
214
215#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
217pub struct JsonStats {
218 compact_bytes: usize,
219 max_depth: usize,
220 nodes: usize,
221}
222
223impl JsonStats {
224 #[must_use]
227 pub const fn compact_bytes(self) -> usize {
228 self.compact_bytes
229 }
230
231 #[must_use]
235 pub const fn max_depth(self) -> usize {
236 self.max_depth
237 }
238
239 #[must_use]
241 pub const fn nodes(self) -> usize {
242 self.nodes
243 }
244}
245
246#[derive(Clone, Debug, Eq, Error, PartialEq)]
248#[non_exhaustive]
249pub enum BoundedJsonError {
250 #[error("JSON input is {actual} bytes; maximum is {maximum}")]
252 InputTooLarge {
253 maximum: usize,
255 actual: usize,
257 },
258
259 #[error("compact JSON reached {actual} bytes; maximum is {maximum}")]
261 CompactRepresentationTooLarge {
262 maximum: usize,
264 actual: usize,
266 },
267
268 #[error("JSON nesting depth reached {actual}; maximum is {maximum}")]
270 NestingTooDeep {
271 maximum: usize,
273 actual: usize,
275 },
276
277 #[error("JSON container reached {actual} entries; maximum is {maximum}")]
279 TooManyContainerEntries {
280 maximum: usize,
282 actual: usize,
284 },
285
286 #[error("JSON document reached {actual} value nodes; maximum is {maximum}")]
288 TooManyNodes {
289 maximum: usize,
291 actual: usize,
293 },
294
295 #[error("JSON string is {actual} decoded bytes; maximum is {maximum}")]
297 StringTooLong {
298 maximum: usize,
300 actual: usize,
302 },
303
304 #[error("JSON object key is {actual} decoded bytes; maximum is {maximum}")]
306 ObjectKeyTooLong {
307 maximum: usize,
309 actual: usize,
311 },
312
313 #[error("JSON object contains a duplicate member name")]
315 DuplicateObjectKey,
316
317 #[error("JSON numbers must be finite")]
319 NonFiniteNumber,
320
321 #[error("JSON integer is outside serde_json's supported range")]
323 NumberOutOfRange,
324
325 #[error("invalid JSON at line {line}, column {column}")]
327 InvalidJson {
328 line: usize,
330 column: usize,
332 },
333}
334
335#[derive(Clone, Eq, PartialEq)]
350pub struct BoundedJson {
351 value: Value,
352 stats: JsonStats,
353}
354
355impl BoundedJson {
356 pub(crate) fn deserialize_with_limits<'de, D>(
364 deserializer: D,
365 limits: JsonLimits,
366 ) -> Result<Self, D::Error>
367 where
368 D: Deserializer<'de>,
369 {
370 let mut tracker = Tracker::new(limits);
371 let value = ValueSeed {
372 tracker: &mut tracker,
373 parent_depth: 0,
374 }
375 .deserialize(deserializer)?;
376
377 Ok(Self {
378 value,
379 stats: tracker.stats(),
380 })
381 }
382
383 pub fn from_slice(input: &[u8]) -> Result<Self, BoundedJsonError> {
390 Self::from_slice_with_limits(input, JsonLimits::DEFAULT)
391 }
392
393 pub fn from_slice_with_limits(
403 input: &[u8],
404 limits: JsonLimits,
405 ) -> Result<Self, BoundedJsonError> {
406 if input.len() > limits.max_bytes {
407 return Err(BoundedJsonError::InputTooLarge {
408 maximum: limits.max_bytes,
409 actual: input.len(),
410 });
411 }
412
413 let mut tracker = Tracker::new(limits);
414 let mut deserializer = serde_json::Deserializer::from_slice(input);
415 let parsed = ValueSeed {
416 tracker: &mut tracker,
417 parent_depth: 0,
418 }
419 .deserialize(&mut deserializer);
420
421 let value = match parsed {
422 Ok(value) => value,
423 Err(error) => return Err(map_deserialize_error(&mut tracker, &error)),
424 };
425
426 if let Err(error) = deserializer.end() {
427 return Err(map_deserialize_error(&mut tracker, &error));
428 }
429
430 Ok(Self {
431 value,
432 stats: tracker.stats(),
433 })
434 }
435
436 #[allow(clippy::should_implement_trait)]
443 pub fn from_str(input: &str) -> Result<Self, BoundedJsonError> {
444 Self::from_slice(input.as_bytes())
445 }
446
447 pub fn from_str_with_limits(input: &str, limits: JsonLimits) -> Result<Self, BoundedJsonError> {
454 Self::from_slice_with_limits(input.as_bytes(), limits)
455 }
456
457 pub fn try_from_value(value: Value) -> Result<Self, BoundedJsonError> {
468 Self::try_from_value_with_limits(value, JsonLimits::DEFAULT)
469 }
470
471 pub fn try_from_value_with_limits(
482 value: Value,
483 limits: JsonLimits,
484 ) -> Result<Self, BoundedJsonError> {
485 let mut tracker = Tracker::new(limits);
486 let parsed = ValueSeed {
487 tracker: &mut tracker,
488 parent_depth: 0,
489 }
490 .deserialize(value);
491
492 match parsed {
493 Ok(value) => Ok(Self {
494 value,
495 stats: tracker.stats(),
496 }),
497 Err(error) => Err(map_deserialize_error(&mut tracker, &error)),
498 }
499 }
500
501 #[must_use]
503 pub const fn as_value(&self) -> &Value {
504 &self.value
505 }
506
507 #[must_use]
509 pub fn into_value(self) -> Value {
510 self.value
511 }
512
513 #[must_use]
515 pub const fn stats(&self) -> JsonStats {
516 self.stats
517 }
518}
519
520impl AsRef<Value> for BoundedJson {
521 fn as_ref(&self) -> &Value {
522 self.as_value()
523 }
524}
525
526impl fmt::Debug for BoundedJson {
527 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
528 formatter
529 .debug_struct("BoundedJson")
530 .field("compact_bytes", &self.stats.compact_bytes)
531 .field("max_depth", &self.stats.max_depth)
532 .field("nodes", &self.stats.nodes)
533 .finish_non_exhaustive()
534 }
535}
536
537impl FromStr for BoundedJson {
538 type Err = BoundedJsonError;
539
540 fn from_str(input: &str) -> Result<Self, Self::Err> {
541 Self::from_str(input)
542 }
543}
544
545impl TryFrom<Value> for BoundedJson {
546 type Error = BoundedJsonError;
547
548 fn try_from(value: Value) -> Result<Self, Self::Error> {
549 Self::try_from_value(value)
550 }
551}
552
553impl Serialize for BoundedJson {
554 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
555 where
556 S: Serializer,
557 {
558 self.value.serialize(serializer)
559 }
560}
561
562impl<'de> Deserialize<'de> for BoundedJson {
563 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
564 where
565 D: Deserializer<'de>,
566 {
567 Self::deserialize_with_limits(deserializer, JsonLimits::DEFAULT)
568 }
569}
570
571impl JsonSchema for BoundedJson {
572 fn schema_name() -> std::borrow::Cow<'static, str> {
573 "BoundedJson".into()
574 }
575
576 fn schema_id() -> std::borrow::Cow<'static, str> {
577 concat!(module_path!(), "::BoundedJson").into()
578 }
579
580 fn json_schema(_: &mut SchemaGenerator) -> Schema {
581 json_schema!({
582 "description": "Any JSON value. StateKnot enforces byte, depth, container-entry, node, string, object-key, duplicate-key, and finite-number constraints at runtime; JSON Schema alone cannot express those aggregate limits."
583 })
584 }
585
586 fn inline_schema() -> bool {
587 true
588 }
589}
590
591fn map_deserialize_error(tracker: &mut Tracker, error: &serde_json::Error) -> BoundedJsonError {
592 tracker
593 .violation
594 .take()
595 .unwrap_or(BoundedJsonError::InvalidJson {
596 line: error.line(),
597 column: error.column(),
598 })
599}
600
601struct Tracker {
602 limits: JsonLimits,
603 compact_bytes: usize,
604 max_depth: usize,
605 nodes: usize,
606 violation: Option<BoundedJsonError>,
607}
608
609impl Tracker {
610 const fn new(limits: JsonLimits) -> Self {
611 Self {
612 limits,
613 compact_bytes: 0,
614 max_depth: 0,
615 nodes: 0,
616 violation: None,
617 }
618 }
619
620 const fn stats(&self) -> JsonStats {
621 JsonStats {
622 compact_bytes: self.compact_bytes,
623 max_depth: self.max_depth,
624 nodes: self.nodes,
625 }
626 }
627
628 fn reject<T, E>(&mut self, error: BoundedJsonError) -> Result<T, E>
629 where
630 E: de::Error,
631 {
632 let message = error.to_string();
633 if self.violation.is_none() {
634 self.violation = Some(error);
635 }
636 Err(E::custom(message))
637 }
638
639 fn add_node<E>(&mut self) -> Result<(), E>
640 where
641 E: de::Error,
642 {
643 let actual = self.nodes.saturating_add(1);
644 if actual > self.limits.max_nodes {
645 return self.reject(BoundedJsonError::TooManyNodes {
646 maximum: self.limits.max_nodes,
647 actual,
648 });
649 }
650 self.nodes = actual;
651 Ok(())
652 }
653
654 fn enter_container<E>(&mut self, parent_depth: usize) -> Result<usize, E>
655 where
656 E: de::Error,
657 {
658 let actual = parent_depth.saturating_add(1);
659 if actual > self.limits.max_depth {
660 return self.reject(BoundedJsonError::NestingTooDeep {
661 maximum: self.limits.max_depth,
662 actual,
663 });
664 }
665 self.max_depth = self.max_depth.max(actual);
666 Ok(actual)
667 }
668
669 fn add_compact_bytes<E>(&mut self, additional: usize) -> Result<(), E>
670 where
671 E: de::Error,
672 {
673 let actual = self.compact_bytes.saturating_add(additional);
674 if actual > self.limits.max_bytes {
675 return self.reject(BoundedJsonError::CompactRepresentationTooLarge {
676 maximum: self.limits.max_bytes,
677 actual,
678 });
679 }
680 self.compact_bytes = actual;
681 Ok(())
682 }
683
684 fn check_string<E>(&mut self, actual: usize) -> Result<(), E>
685 where
686 E: de::Error,
687 {
688 if actual > self.limits.max_string_bytes {
689 return self.reject(BoundedJsonError::StringTooLong {
690 maximum: self.limits.max_string_bytes,
691 actual,
692 });
693 }
694 Ok(())
695 }
696
697 fn check_object_key<E>(&mut self, actual: usize) -> Result<(), E>
698 where
699 E: de::Error,
700 {
701 if actual > self.limits.max_object_key_bytes {
702 return self.reject(BoundedJsonError::ObjectKeyTooLong {
703 maximum: self.limits.max_object_key_bytes,
704 actual,
705 });
706 }
707 Ok(())
708 }
709}
710
711struct ValueSeed<'tracker> {
712 tracker: &'tracker mut Tracker,
713 parent_depth: usize,
714}
715
716impl<'de> DeserializeSeed<'de> for ValueSeed<'_> {
717 type Value = Value;
718
719 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
720 where
721 D: Deserializer<'de>,
722 {
723 self.tracker.add_node::<D::Error>()?;
724 deserializer.deserialize_any(ValueVisitor {
725 tracker: self.tracker,
726 parent_depth: self.parent_depth,
727 })
728 }
729}
730
731struct ValueVisitor<'tracker> {
732 tracker: &'tracker mut Tracker,
733 parent_depth: usize,
734}
735
736impl ValueVisitor<'_> {
737 fn visit_number<E>(self, number: Number) -> Result<Value, E>
738 where
739 E: de::Error,
740 {
741 self.tracker
742 .add_compact_bytes::<E>(number.to_string().len())?;
743 Ok(Value::Number(number))
744 }
745
746 fn visit_string_value<E>(self, value: String) -> Result<Value, E>
747 where
748 E: de::Error,
749 {
750 self.tracker.check_string::<E>(value.len())?;
751 self.tracker
752 .add_compact_bytes::<E>(encoded_json_string_len(&value))?;
753 Ok(Value::String(value))
754 }
755}
756
757impl<'de> Visitor<'de> for ValueVisitor<'_> {
758 type Value = Value;
759
760 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
761 formatter.write_str("a resource-bounded JSON value")
762 }
763
764 fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
765 where
766 E: de::Error,
767 {
768 self.tracker
769 .add_compact_bytes::<E>(if value { 4 } else { 5 })?;
770 Ok(Value::Bool(value))
771 }
772
773 fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
774 where
775 E: de::Error,
776 {
777 self.visit_i64(i64::from(value))
778 }
779
780 fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
781 where
782 E: de::Error,
783 {
784 self.visit_i64(i64::from(value))
785 }
786
787 fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
788 where
789 E: de::Error,
790 {
791 self.visit_i64(i64::from(value))
792 }
793
794 fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
795 where
796 E: de::Error,
797 {
798 self.visit_number(value.into())
799 }
800
801 fn visit_i128<E>(self, value: i128) -> Result<Self::Value, E>
802 where
803 E: de::Error,
804 {
805 let Some(number) = Number::from_i128(value) else {
806 return self.tracker.reject(BoundedJsonError::NumberOutOfRange);
807 };
808 self.visit_number(number)
809 }
810
811 fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
812 where
813 E: de::Error,
814 {
815 self.visit_u64(u64::from(value))
816 }
817
818 fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
819 where
820 E: de::Error,
821 {
822 self.visit_u64(u64::from(value))
823 }
824
825 fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
826 where
827 E: de::Error,
828 {
829 self.visit_u64(u64::from(value))
830 }
831
832 fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
833 where
834 E: de::Error,
835 {
836 self.visit_number(value.into())
837 }
838
839 fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
840 where
841 E: de::Error,
842 {
843 let Some(number) = Number::from_u128(value) else {
844 return self.tracker.reject(BoundedJsonError::NumberOutOfRange);
845 };
846 self.visit_number(number)
847 }
848
849 fn visit_f32<E>(self, value: f32) -> Result<Self::Value, E>
850 where
851 E: de::Error,
852 {
853 self.visit_f64(f64::from(value))
854 }
855
856 fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
857 where
858 E: de::Error,
859 {
860 let Some(number) = Number::from_f64(value) else {
861 return self.tracker.reject(BoundedJsonError::NonFiniteNumber);
862 };
863 self.visit_number(number)
864 }
865
866 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
867 where
868 E: de::Error,
869 {
870 self.tracker.check_string::<E>(value.len())?;
871 self.tracker
872 .add_compact_bytes::<E>(encoded_json_string_len(value))?;
873 Ok(Value::String(value.to_owned()))
874 }
875
876 fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
877 where
878 E: de::Error,
879 {
880 self.visit_str(value)
881 }
882
883 fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
884 where
885 E: de::Error,
886 {
887 self.visit_string_value(value)
888 }
889
890 fn visit_none<E>(self) -> Result<Self::Value, E>
891 where
892 E: de::Error,
893 {
894 self.visit_unit()
895 }
896
897 fn visit_unit<E>(self) -> Result<Self::Value, E>
898 where
899 E: de::Error,
900 {
901 self.tracker.add_compact_bytes::<E>(4)?;
902 Ok(Value::Null)
903 }
904
905 fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
906 where
907 A: SeqAccess<'de>,
908 {
909 let depth = self
910 .tracker
911 .enter_container::<A::Error>(self.parent_depth)?;
912 self.tracker.add_compact_bytes::<A::Error>(2)?;
913
914 let maximum = self.tracker.limits.max_container_entries;
915 let remaining_nodes = self
916 .tracker
917 .limits
918 .max_nodes
919 .saturating_sub(self.tracker.nodes);
920 let capacity = sequence
921 .size_hint()
922 .unwrap_or(0)
923 .min(maximum)
924 .min(remaining_nodes);
925 let mut values = Vec::with_capacity(capacity);
926
927 while values.len() < maximum {
928 let next = sequence.next_element_seed(ArrayElementSeed {
929 tracker: self.tracker,
930 parent_depth: depth,
931 separator: !values.is_empty(),
932 })?;
933 match next {
934 Some(value) => values.push(value),
935 None => return Ok(Value::Array(values)),
936 }
937 }
938
939 let _: Option<()> = sequence.next_element_seed(RejectExtraEntrySeed {
940 tracker: self.tracker,
941 maximum,
942 })?;
943 Ok(Value::Array(values))
944 }
945
946 fn visit_map<A>(self, mut object: A) -> Result<Self::Value, A::Error>
947 where
948 A: MapAccess<'de>,
949 {
950 let depth = self
951 .tracker
952 .enter_container::<A::Error>(self.parent_depth)?;
953 self.tracker.add_compact_bytes::<A::Error>(2)?;
954
955 let maximum = self.tracker.limits.max_container_entries;
956 let mut sorted = BTreeMap::new();
957
958 while sorted.len() < maximum {
959 let next_key = object.next_key_seed(ObjectKeySeed {
960 tracker: self.tracker,
961 separator: !sorted.is_empty(),
962 })?;
963 let Some(key) = next_key else {
964 return Ok(Value::Object(sorted_map(sorted)));
965 };
966
967 if sorted.contains_key(&key) {
968 return self.tracker.reject(BoundedJsonError::DuplicateObjectKey);
969 }
970
971 let value = object.next_value_seed(ValueSeed {
972 tracker: self.tracker,
973 parent_depth: depth,
974 })?;
975 sorted.insert(key, value);
976 }
977
978 let _: Option<()> = object.next_key_seed(RejectExtraEntrySeed {
979 tracker: self.tracker,
980 maximum,
981 })?;
982 Ok(Value::Object(sorted_map(sorted)))
983 }
984}
985
986struct ArrayElementSeed<'tracker> {
987 tracker: &'tracker mut Tracker,
988 parent_depth: usize,
989 separator: bool,
990}
991
992impl<'de> DeserializeSeed<'de> for ArrayElementSeed<'_> {
993 type Value = Value;
994
995 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
996 where
997 D: Deserializer<'de>,
998 {
999 if self.separator {
1000 self.tracker.add_compact_bytes::<D::Error>(1)?;
1001 }
1002 ValueSeed {
1003 tracker: self.tracker,
1004 parent_depth: self.parent_depth,
1005 }
1006 .deserialize(deserializer)
1007 }
1008}
1009
1010struct ObjectKeySeed<'tracker> {
1011 tracker: &'tracker mut Tracker,
1012 separator: bool,
1013}
1014
1015impl<'de> DeserializeSeed<'de> for ObjectKeySeed<'_> {
1016 type Value = String;
1017
1018 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
1019 where
1020 D: Deserializer<'de>,
1021 {
1022 deserializer.deserialize_string(ObjectKeyVisitor {
1023 tracker: self.tracker,
1024 separator: self.separator,
1025 })
1026 }
1027}
1028
1029struct ObjectKeyVisitor<'tracker> {
1030 tracker: &'tracker mut Tracker,
1031 separator: bool,
1032}
1033
1034impl ObjectKeyVisitor<'_> {
1035 fn visit_key<E>(self, value: &str) -> Result<String, E>
1036 where
1037 E: de::Error,
1038 {
1039 self.tracker.check_object_key::<E>(value.len())?;
1040 if self.separator {
1041 self.tracker.add_compact_bytes::<E>(1)?;
1042 }
1043 self.tracker
1044 .add_compact_bytes::<E>(encoded_json_string_len(value).saturating_add(1))?;
1045 Ok(value.to_owned())
1046 }
1047}
1048
1049impl<'de> Visitor<'de> for ObjectKeyVisitor<'_> {
1050 type Value = String;
1051
1052 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1053 formatter.write_str("a bounded JSON object member name")
1054 }
1055
1056 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
1057 where
1058 E: de::Error,
1059 {
1060 self.visit_key(value)
1061 }
1062
1063 fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
1064 where
1065 E: de::Error,
1066 {
1067 self.visit_key(value)
1068 }
1069
1070 fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
1071 where
1072 E: de::Error,
1073 {
1074 self.tracker.check_object_key::<E>(value.len())?;
1075 if self.separator {
1076 self.tracker.add_compact_bytes::<E>(1)?;
1077 }
1078 self.tracker
1079 .add_compact_bytes::<E>(encoded_json_string_len(&value).saturating_add(1))?;
1080 Ok(value)
1081 }
1082}
1083
1084struct RejectExtraEntrySeed<'tracker> {
1085 tracker: &'tracker mut Tracker,
1086 maximum: usize,
1087}
1088
1089impl<'de> DeserializeSeed<'de> for RejectExtraEntrySeed<'_> {
1090 type Value = ();
1091
1092 fn deserialize<D>(self, _deserializer: D) -> Result<Self::Value, D::Error>
1093 where
1094 D: Deserializer<'de>,
1095 {
1096 self.tracker
1097 .reject(BoundedJsonError::TooManyContainerEntries {
1098 maximum: self.maximum,
1099 actual: self.maximum.saturating_add(1),
1100 })
1101 }
1102}
1103
1104fn sorted_map(values: BTreeMap<String, Value>) -> Map<String, Value> {
1105 values.into_iter().collect()
1106}
1107
1108fn encoded_json_string_len(value: &str) -> usize {
1109 value.bytes().fold(2_usize, |length, byte| {
1110 length.saturating_add(match byte {
1111 b'\x08' | b'\t' | b'\n' | b'\x0c' | b'\r' | b'"' | b'\\' => 2,
1112 0x00..=0x1f => 6,
1113 _ => 1,
1114 })
1115 })
1116}
1117
1118#[cfg(test)]
1119mod tests {
1120 use super::*;
1121 use proptest::{collection, prelude::*};
1122 use serde::de::value::{F64Deserializer, U128Deserializer};
1123 use serde_json::json;
1124
1125 fn limits(
1126 max_bytes: usize,
1127 max_depth: usize,
1128 max_container_entries: usize,
1129 max_nodes: usize,
1130 max_string_bytes: usize,
1131 max_object_key_bytes: usize,
1132 ) -> JsonLimits {
1133 JsonLimits::try_new(
1134 max_bytes,
1135 max_depth,
1136 max_container_entries,
1137 max_nodes,
1138 max_string_bytes,
1139 max_object_key_bytes,
1140 )
1141 .unwrap()
1142 }
1143
1144 #[test]
1145 fn default_and_hard_limits_are_stable_and_validated() {
1146 assert_eq!(JsonLimits::default(), JsonLimits::DEFAULT);
1147 assert_eq!(JsonLimits::DEFAULT.max_bytes(), 256 * KIBIBYTE);
1148 assert_eq!(JsonLimits::DEFAULT.max_depth(), 32);
1149 assert_eq!(JsonLimits::DEFAULT.max_container_entries(), 1024);
1150 assert_eq!(JsonLimits::DEFAULT.max_nodes(), 16_384);
1151 assert_eq!(JsonLimits::DEFAULT.max_string_bytes(), 64 * KIBIBYTE);
1152 assert_eq!(JsonLimits::DEFAULT.max_object_key_bytes(), 256);
1153
1154 for limit in [
1155 JsonLimit::Bytes,
1156 JsonLimit::Depth,
1157 JsonLimit::ContainerEntries,
1158 JsonLimit::Nodes,
1159 JsonLimit::StringBytes,
1160 JsonLimit::ObjectKeyBytes,
1161 ] {
1162 let result = match limit {
1163 JsonLimit::Bytes => JsonLimits::try_new(0, 1, 1, 1, 1, 1),
1164 JsonLimit::Depth => JsonLimits::try_new(1, 0, 1, 1, 1, 1),
1165 JsonLimit::ContainerEntries => JsonLimits::try_new(1, 1, 0, 1, 1, 1),
1166 JsonLimit::Nodes => JsonLimits::try_new(1, 1, 1, 0, 1, 1),
1167 JsonLimit::StringBytes => JsonLimits::try_new(1, 1, 1, 1, 0, 1),
1168 JsonLimit::ObjectKeyBytes => JsonLimits::try_new(1, 1, 1, 1, 1, 0),
1169 };
1170 assert_eq!(result, Err(JsonLimitsError::Zero { limit }));
1171 }
1172
1173 assert_eq!(
1174 JsonLimits::try_new(JsonLimits::MAXIMUM.max_bytes() + 1, 1, 1, 1, 1, 1),
1175 Err(JsonLimitsError::AboveHardMaximum {
1176 limit: JsonLimit::Bytes,
1177 maximum: JsonLimits::MAXIMUM.max_bytes(),
1178 actual: JsonLimits::MAXIMUM.max_bytes() + 1,
1179 })
1180 );
1181
1182 for (limit, result, maximum) in [
1183 (
1184 JsonLimit::Depth,
1185 JsonLimits::try_new(1, JsonLimits::MAXIMUM.max_depth() + 1, 1, 1, 1, 1),
1186 JsonLimits::MAXIMUM.max_depth(),
1187 ),
1188 (
1189 JsonLimit::ContainerEntries,
1190 JsonLimits::try_new(
1191 1,
1192 1,
1193 JsonLimits::MAXIMUM.max_container_entries() + 1,
1194 1,
1195 1,
1196 1,
1197 ),
1198 JsonLimits::MAXIMUM.max_container_entries(),
1199 ),
1200 (
1201 JsonLimit::Nodes,
1202 JsonLimits::try_new(1, 1, 1, JsonLimits::MAXIMUM.max_nodes() + 1, 1, 1),
1203 JsonLimits::MAXIMUM.max_nodes(),
1204 ),
1205 (
1206 JsonLimit::StringBytes,
1207 JsonLimits::try_new(1, 1, 1, 1, JsonLimits::MAXIMUM.max_string_bytes() + 1, 1),
1208 JsonLimits::MAXIMUM.max_string_bytes(),
1209 ),
1210 (
1211 JsonLimit::ObjectKeyBytes,
1212 JsonLimits::try_new(
1213 1,
1214 1,
1215 1,
1216 1,
1217 1,
1218 JsonLimits::MAXIMUM.max_object_key_bytes() + 1,
1219 ),
1220 JsonLimits::MAXIMUM.max_object_key_bytes(),
1221 ),
1222 ] {
1223 assert_eq!(
1224 result,
1225 Err(JsonLimitsError::AboveHardMaximum {
1226 limit,
1227 maximum,
1228 actual: maximum + 1,
1229 })
1230 );
1231 }
1232 }
1233
1234 #[test]
1235 fn parses_every_json_kind_and_tracks_exact_compact_statistics() {
1236 let input = r#" { "z": [null, true, false, -2, 3, 1.5, "a\né"], "a": {} } "#;
1237 let bounded = BoundedJson::from_slice(input.as_bytes()).unwrap();
1238 let compact = serde_json::to_vec(bounded.as_value()).unwrap();
1239
1240 assert_eq!(bounded.stats().compact_bytes(), compact.len());
1241 assert_eq!(bounded.stats().max_depth(), 2);
1242 assert_eq!(bounded.stats().nodes(), 10);
1243 assert_eq!(bounded.as_value()["z"][6], "a\né");
1244
1245 let keys = bounded
1246 .as_value()
1247 .as_object()
1248 .unwrap()
1249 .keys()
1250 .map(String::as_str)
1251 .collect::<Vec<_>>();
1252 assert_eq!(keys, ["a", "z"]);
1253 }
1254
1255 #[test]
1256 fn rejects_duplicate_keys_after_escape_processing_before_reading_value() {
1257 for input in [
1258 r#"{"a":1,"a":2}"#,
1259 r#"{"a":1,"\u0061":2}"#,
1260 r#"{"nested":{"x":1,"\u0078":2}}"#,
1261 ] {
1262 assert_eq!(
1263 BoundedJson::from_str(input),
1264 Err(BoundedJsonError::DuplicateObjectKey),
1265 "accepted {input}"
1266 );
1267 }
1268
1269 let deep_value = format!("{}0{}", "[".repeat(200), "]".repeat(200));
1270 let input = format!(r#"{{"a":1,"\u0061":{deep_value}}}"#);
1271 assert_eq!(
1272 BoundedJson::from_str(&input),
1273 Err(BoundedJsonError::DuplicateObjectKey)
1274 );
1275 }
1276
1277 #[test]
1278 fn rejects_invalid_syntax_and_trailing_values_without_echoing_input() {
1279 for input in ["", "[1,]", r#"{"a":}"#, "true false", "\u{feff}null"] {
1280 let error = BoundedJson::from_str(input).unwrap_err();
1281 assert!(matches!(error, BoundedJsonError::InvalidJson { .. }));
1282 if !input.is_empty() {
1283 assert!(!error.to_string().contains(input));
1284 }
1285 }
1286
1287 assert!(matches!(
1288 BoundedJson::from_slice(&[0xff]),
1289 Err(BoundedJsonError::InvalidJson { .. })
1290 ));
1291 }
1292
1293 #[test]
1294 fn raw_input_is_bounded_before_whitespace_or_syntax_processing() {
1295 let configured = limits(4, 4, 4, 4, 4, 4);
1296 assert!(BoundedJson::from_str_with_limits("null", configured).is_ok());
1297 assert_eq!(
1298 BoundedJson::from_str_with_limits(" null", configured),
1299 Err(BoundedJsonError::InputTooLarge {
1300 maximum: 4,
1301 actual: 5,
1302 })
1303 );
1304 }
1305
1306 #[test]
1307 fn compact_encoding_is_bounded_for_trusted_values() {
1308 let configured = limits(3, 4, 4, 4, 4, 4);
1309 assert!(
1310 BoundedJson::try_from_value_with_limits(Value::String("a".into()), configured).is_ok()
1311 );
1312 assert_eq!(
1313 BoundedJson::try_from_value_with_limits(Value::String("aa".into()), configured),
1314 Err(BoundedJsonError::CompactRepresentationTooLarge {
1315 maximum: 3,
1316 actual: 4,
1317 })
1318 );
1319 }
1320
1321 #[test]
1322 fn nesting_depth_uses_zero_for_scalars_and_one_for_root_containers() {
1323 let configured = limits(128, 2, 8, 16, 32, 32);
1324 assert_eq!(
1325 BoundedJson::from_str_with_limits("0", configured)
1326 .unwrap()
1327 .stats()
1328 .max_depth(),
1329 0
1330 );
1331 assert_eq!(
1332 BoundedJson::from_str_with_limits("[[0]]", configured)
1333 .unwrap()
1334 .stats()
1335 .max_depth(),
1336 2
1337 );
1338 assert_eq!(
1339 BoundedJson::from_str_with_limits("[[[0]]]", configured),
1340 Err(BoundedJsonError::NestingTooDeep {
1341 maximum: 2,
1342 actual: 3,
1343 })
1344 );
1345 }
1346
1347 #[test]
1348 fn container_limit_stops_before_traversing_an_extra_value() {
1349 let configured = limits(2048, 4, 2, 16, 32, 32);
1350 assert!(BoundedJson::from_str_with_limits("[0,1]", configured).is_ok());
1351 assert!(BoundedJson::from_str_with_limits(r#"{"a":0,"b":1}"#, configured).is_ok());
1352
1353 let deep_value = format!("{}0{}", "[".repeat(200), "]".repeat(200));
1354 for input in [
1355 format!("[0,1,{deep_value}]"),
1356 format!(r#"{{"a":0,"b":1,"c":{deep_value}}}"#),
1357 ] {
1358 assert_eq!(
1359 BoundedJson::from_str_with_limits(&input, configured),
1360 Err(BoundedJsonError::TooManyContainerEntries {
1361 maximum: 2,
1362 actual: 3,
1363 })
1364 );
1365 }
1366 }
1367
1368 #[test]
1369 fn node_limit_stops_before_traversing_an_extra_value() {
1370 let configured = limits(1024, 4, 8, 2, 32, 32);
1371 assert!(BoundedJson::from_str_with_limits("[0]", configured).is_ok());
1372
1373 let deep_value = format!("{}0{}", "[".repeat(200), "]".repeat(200));
1374 let input = format!("[0,{deep_value}]");
1375 assert_eq!(
1376 BoundedJson::from_str_with_limits(&input, configured),
1377 Err(BoundedJsonError::TooManyNodes {
1378 maximum: 2,
1379 actual: 3,
1380 })
1381 );
1382 }
1383
1384 #[test]
1385 fn string_and_key_limits_count_decoded_utf8_bytes() {
1386 let configured = limits(128, 4, 8, 8, 2, 2);
1387 assert!(BoundedJson::from_str_with_limits(r#""\u00e9""#, configured).is_ok());
1388 assert_eq!(
1389 BoundedJson::from_str_with_limits(r#""\u00e9a""#, configured),
1390 Err(BoundedJsonError::StringTooLong {
1391 maximum: 2,
1392 actual: 3,
1393 })
1394 );
1395 assert!(BoundedJson::from_str_with_limits(r#"{"\u00e9":0}"#, configured).is_ok());
1396 assert_eq!(
1397 BoundedJson::from_str_with_limits(r#"{"\u00e9a":0}"#, configured),
1398 Err(BoundedJsonError::ObjectKeyTooLong {
1399 maximum: 2,
1400 actual: 3,
1401 })
1402 );
1403 }
1404
1405 #[test]
1406 fn generic_serde_rejects_non_json_numeric_values() {
1407 let nan_error =
1408 BoundedJson::deserialize(F64Deserializer::<de::value::Error>::new(f64::NAN))
1409 .unwrap_err();
1410 assert!(nan_error.to_string().contains("finite"));
1411
1412 let integer_error =
1413 BoundedJson::deserialize(U128Deserializer::<de::value::Error>::new(u128::MAX))
1414 .unwrap_err();
1415 assert!(integer_error.to_string().contains("supported range"));
1416 }
1417
1418 #[test]
1419 fn serde_round_trip_revalidates_and_debug_is_redacted() {
1420 let secret = "credential-like-secret";
1421 let bounded = BoundedJson::from_str(&format!(r#"{{"secret":"{secret}"}}"#)).unwrap();
1422 let debug = format!("{bounded:?}");
1423 assert!(!debug.contains(secret));
1424 assert!(debug.contains("compact_bytes"));
1425
1426 let encoded = serde_json::to_string(&bounded).unwrap();
1427 let decoded: BoundedJson = serde_json::from_str(&encoded).unwrap();
1428 let parsed: BoundedJson = encoded.parse().unwrap();
1429 assert_eq!(decoded, bounded);
1430 assert_eq!(parsed, bounded);
1431 assert_eq!(decoded.stats(), bounded.stats());
1432 }
1433
1434 #[test]
1435 fn trusted_value_construction_sorts_objects_without_claiming_canonicalization() {
1436 let mut object = Map::new();
1437 object.insert("z".into(), json!(1));
1438 object.insert("a".into(), json!(2));
1439 let bounded = BoundedJson::try_from_value(Value::Object(object)).unwrap();
1440 assert_eq!(serde_json::to_string(&bounded).unwrap(), r#"{"a":2,"z":1}"#);
1441 assert_eq!(bounded.stats().compact_bytes(), 13);
1442
1443 let schema = serde_json::to_value(schemars::schema_for!(BoundedJson)).unwrap();
1444 assert!(schema.get("type").is_none());
1445 assert!(schema["description"].as_str().unwrap().contains("runtime"));
1446 }
1447
1448 fn arbitrary_json() -> impl Strategy<Value = Value> {
1449 let leaf = prop_oneof![
1450 Just(Value::Null),
1451 any::<bool>().prop_map(Value::Bool),
1452 any::<i64>().prop_map(|value| Value::Number(value.into())),
1453 "[a-zA-Z0-9\\n\\t]{0,24}".prop_map(Value::String),
1454 ];
1455
1456 leaf.prop_recursive(4, 64, 8, |inner| {
1457 prop_oneof![
1458 collection::vec(inner.clone(), 0..5).prop_map(Value::Array),
1459 collection::btree_map("[a-z]{1,8}", inner, 0..5)
1460 .prop_map(|values| { Value::Object(values.into_iter().collect()) }),
1461 ]
1462 })
1463 }
1464
1465 proptest! {
1466 #[test]
1467 fn bounded_values_round_trip_with_exact_compact_size(value in arbitrary_json()) {
1468 let encoded = serde_json::to_vec(&value).unwrap();
1469 let bounded = BoundedJson::from_slice(&encoded).unwrap();
1470 prop_assert_eq!(bounded.as_value(), &value);
1471 prop_assert_eq!(bounded.stats().compact_bytes(), encoded.len());
1472
1473 let reparsed = BoundedJson::try_from_value(value).unwrap();
1474 prop_assert_eq!(reparsed.stats(), bounded.stats());
1475 }
1476 }
1477}