use std::fmt;
use serde::de::DeserializeOwned;
use toml_parser::lexer::TokenKind;
pub const MAX_NESTING: usize = 128;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NestingTooDeep;
impl fmt::Display for NestingTooDeep {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"TOML arrays and inline tables nest deeper than {MAX_NESTING} levels"
)
}
}
impl std::error::Error for NestingTooDeep {}
#[derive(Debug)]
pub enum TomlInputError {
TooDeep(NestingTooDeep),
Parse(toml::de::Error),
}
impl fmt::Display for TomlInputError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooDeep(err) => err.fmt(f),
Self::Parse(err) => err.fmt(f),
}
}
}
impl std::error::Error for TomlInputError {}
pub fn check_nesting(source: &str) -> Result<(), NestingTooDeep> {
let mut open: Vec<TokenKind> = Vec::new();
for token in toml_parser::Source::new(source).lex() {
match token.kind() {
kind @ (TokenKind::LeftSquareBracket | TokenKind::LeftCurlyBracket) => {
if open.len() == MAX_NESTING {
return Err(NestingTooDeep);
}
open.push(kind);
}
TokenKind::RightSquareBracket if open.last() == Some(&TokenKind::LeftSquareBracket) => {
open.pop();
}
TokenKind::RightCurlyBracket if open.last() == Some(&TokenKind::LeftCurlyBracket) => {
open.pop();
}
_ => {}
}
}
Ok(())
}
pub fn from_str<T: DeserializeOwned>(source: &str) -> Result<T, TomlInputError> {
check_nesting(source).map_err(TomlInputError::TooDeep)?;
toml::from_str(source).map_err(TomlInputError::Parse)
}
#[cfg(test)]
mod tests {
use super::{MAX_NESTING, NestingTooDeep, TomlInputError, check_nesting, from_str};
#[test]
fn accepts_nesting_up_to_the_limit() {
let source = format!("a = {}{}", "[".repeat(MAX_NESTING), "]".repeat(MAX_NESTING));
assert_eq!(check_nesting(&source), Ok(()));
}
#[test]
fn rejects_nesting_above_the_limit() {
let depth = MAX_NESTING + 1;
let source = format!("a = {}{}", "[".repeat(depth), "]".repeat(depth));
assert_eq!(check_nesting(&source), Err(NestingTooDeep));
}
#[test]
fn mismatched_closing_brackets_do_not_lower_the_depth() {
let source = format!("a = [{}{}", "}".repeat(1_000), "[".repeat(MAX_NESTING));
assert_eq!(check_nesting(&source), Err(NestingTooDeep));
}
#[test]
fn sibling_tables_and_arrays_do_not_add_up() {
let source = "[a]\nx = [1, [2, 3], {y = [4]}]\n[[b]]\nz = {w = {v = 1}}\n".repeat(1_000);
assert_eq!(check_nesting(&source), Ok(()));
let value: toml::Value = toml::from_str("x = [1, [2, 3], {y = [4]}]").unwrap();
assert_eq!(
from_str::<toml::Value>("x = [1, [2, 3], {y = [4]}]").unwrap(),
value
);
}
#[test]
fn rejects_input_that_evades_the_toml_depth_limit() {
const REPEATS: usize = 200_000;
let source = format!(
"a = {{{}}}\nb = {}{}\n",
"[]".repeat(REPEATS),
"[".repeat(REPEATS),
"]".repeat(REPEATS)
);
let err = from_str::<toml::Value>(&source).unwrap_err();
assert!(matches!(err, TomlInputError::TooDeep(_)), "{err}");
}
#[test]
fn closing_brackets_in_strings_and_comments_do_not_lower_the_depth() {
let depth = MAX_NESTING + 1;
for element in [
"\"]\", ",
"'}]', ",
"\"\"\"]\n]\"\"\", ",
"'''\n]]\n''', ",
"\"\\\"]\", ",
"# ]\n",
] {
let source = format!(
"a = {}{}",
format!("[{element}").repeat(depth),
"]".repeat(depth)
);
assert_eq!(check_nesting(&source), Err(NestingTooDeep), "{element:?}");
}
}
#[test]
fn brackets_in_strings_and_comments_are_not_counted() {
let deep = "[".repeat(MAX_NESTING * 2);
let source = format!(
"# {deep}\na = \"{deep}\"\nb = '{deep}'\nc = \"\"\"\n{deep}\n\"\"\"\nd = '''{deep}'''\n"
);
assert_eq!(check_nesting(&source), Ok(()));
assert!(from_str::<toml::Value>(&source).is_ok());
}
#[test]
fn reports_toml_syntax_errors() {
let err = from_str::<toml::Value>("a = [unquoted").unwrap_err();
assert!(matches!(err, TomlInputError::Parse(_)), "{err}");
}
}