Skip to content

fix(fluent-syntax): limit parser nesting depth to prevent stack overflow - #411

Open
Goldloli wants to merge 1 commit into
projectfluent:mainfrom
Goldloli:fix/parser-depth-limit-410
Open

Goldloli wants to merge 1 commit into
projectfluent:mainfrom
Goldloli:fix/parser-depth-limit-410

Conversation

@Goldloli

Copy link
Copy Markdown

Fixes #410

Problem

The parser recurses once per nesting level of placeables, select expressions and call arguments, with no depth limit. Deeply nested input overflows the stack, and a stack overflow aborts the whole process, so any app parsing untrusted .ftl can be crashed.

Reproduced on current main (0f87afb) with the issue's repro (3000 nested {, 2 MiB thread): thread has overflowed its stack / fatal runtime error: stack overflow, aborting.

Fix

Count the active nesting depth in the parser and return a regular ParserError past a fixed limit, as suggested in the issue. Every recursion cycle passes through get_placeable (placeable and select nesting) or get_call_arguments (call argument nesting), so both increment a depth counter and report a new ErrorKind::TooDeepNesting once it exceeds 64. The entry then becomes Junk like any other syntax error, with no behavior change for callers that stay under the limit — real FTL rarely nests more than a few levels.

Note the counter deliberately sits in get_placeable/get_call_arguments rather than get_inline_expression: a select's variants are parsed after its selector's inline expression has returned, so the select recursion never nests inline expressions.

The limit of 64 matches the value cited in the issue (bevy_markup's pre-parse check) and keeps a wide margin below the observed crash depths on a 2 MiB stack (213 placeables in debug builds, 2535 in release).

Verification

  • New regression tests fluent-syntax/tests/parser_depth.rs: 3000-deep placeable, select and call-argument nesting each parse to Junk with TooDeepNesting on a 2 MiB thread (previously: process abort); a normally nested message still parses cleanly.
  • cargo test -p fluent-syntax — all pass (12 unit + 4 new + 39 fixture/doc tests)
  • cargo test --workspace — all pass
  • cargo clippy --workspace --all-targets — no new warnings (the remaining ones in fluent-bundle/fluent-fallback predate this change)
  • The issue's repro program now prints junk as expected, first error: Nesting of placeables, select expressions and call arguments exceeds the parser's depth limit for all three nesting kinds instead of aborting.

Deeply nested placeables, select expressions or call arguments recurse
once per nesting level with no limit, overflowing the stack and aborting
the whole process on adversarial input.

Count the active nesting depth at the two constructs every recursion
cycle passes through (placeables and call arguments) and report a
regular ErrorKind::TooDeepNesting past a limit of 64, so the entry
becomes Junk like any other syntax error. Real FTL rarely nests more
than a few levels.

Refs projectfluent#410
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fluent-syntax: deeply nested placeables/selects overflow the stack (process abort)

1 participant