diff --git a/crates/toml_edit/src/parser/key.rs b/crates/toml_edit/src/parser/key.rs
index 52c61569..282a824c 100644
--- a/crates/toml_edit/src/parser/key.rs
+++ b/crates/toml_edit/src/parser/key.rs
@@ -6,6 +6,7 @@ use nom8::combinator::peek;
use nom8::multi::separated_list1;
use crate::key::Key;
+use crate::parser::errors::CustomError;
use crate::parser::prelude::*;
use crate::parser::strings::{basic_string, literal_string};
use crate::parser::trivia::{from_utf8_unchecked, ws};
@@ -24,6 +25,11 @@ pub(crate) fn key(input: Input<'_>) -> IResult, Vec, ParserError<
}),
)
.context(Context::Expression("key"))
+ .map_res(|k| {
+ // Inserting the key will require recursion down the line
+ RecursionCheck::check_depth(k.len())?;
+ Ok::<_, CustomError>(k)
+ })
.parse(input)
}
diff --git a/crates/toml_edit/src/parser/mod.rs b/crates/toml_edit/src/parser/mod.rs
index d04a5b90..9bdf1635 100644
--- a/crates/toml_edit/src/parser/mod.rs
+++ b/crates/toml_edit/src/parser/mod.rs
@@ -70,6 +70,14 @@ pub(crate) mod prelude {
#[cfg(not(feature = "unbounded"))]
impl RecursionCheck {
+ pub(crate) fn check_depth(depth: usize) -> Result<(), super::errors::CustomError> {
+ if depth < 128 {
+ Ok(())
+ } else {
+ Err(super::errors::CustomError::RecursionLimitExceeded)
+ }
+ }
+
pub(crate) fn recursing(
mut self,
input: Input<'_>,
@@ -95,6 +103,10 @@ pub(crate) mod prelude {
#[cfg(feature = "unbounded")]
impl RecursionCheck {
+ pub(crate) fn check_depth(_depth: usize) -> Result<(), super::errors::CustomError> {
+ Ok(())
+ }
+
pub(crate) fn recursing(
self,
_input: Input<'_>,