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(parser): Avoid stackoverflow with long dotted keys #408

Merged
merged 1 commit into from Dec 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/toml_edit/src/parser/key.rs
Expand Up @@ -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};
Expand All @@ -24,6 +25,11 @@ pub(crate) fn key(input: Input<'_>) -> IResult<Input<'_>, Vec<Key>, 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)
}

Expand Down
12 changes: 12 additions & 0 deletions crates/toml_edit/src/parser/mod.rs
Expand Up @@ -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<'_>,
Expand All @@ -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<'_>,
Expand Down