CVE-2025-57809
Executive Summary
CVE-2025-57809 is a denial-of-service (DoS) vulnerability in the XGrammar open-source library, which is used for efficient, flexible, and portable structured generation. The vulnerability stems from an infinite recursion issue within the grammar processing logic. An attacker can craft a malicious grammar input that triggers uncontrolled recursion, leading to a stack overflow and service crash. This issue has been patched in version 0.1.21. No public proof-of-concept (PoC) or exploit code is currently available, and the EPSS score is 0.0%, indicating low exploitation activity in the wild.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
XGrammar is distributed as a Python package via PyPI. Organizations using XGrammar in their software supply chain should immediately audit their dependencies and update to version 0.1.21 or later. The vulnerability can be introduced through transitive dependencies, so a full dependency tree review is recommended.
CVSS Breakdown & Threat Level
The CVSS score is currently 0.0, indicating that no official vector has been assigned. However, based on the nature of the vulnerability (infinite recursion leading to DoS), a reasonable assessment would be:
- Attack Vector (AV): Network (AV:N) – the grammar input can be provided remotely.
- Attack Complexity (AC): Low (AC:L) – no special conditions required.
- Privileges Required (PR): None (PR:N) – no authentication needed.
- User Interaction (UI): None (UI:N) – no user action required.
- Scope (S): Unchanged (S:U) – the vulnerable component is the only affected component.
- Confidentiality (C): None (C:N) – no data exposure.
- Integrity (I): None (I:N) – no data modification.
- Availability (A): High (A:H) – service crash leads to complete denial of service.
Estimated CVSS Vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H
Estimated Base Score: 7.5 (High)
Threat Level: Medium. While the impact is limited to availability, the low attack complexity and network vector make it a viable target for DoS attacks.
Deep Dive & Mechanics
The XGrammar library processes grammar definitions to generate structured outputs. The vulnerability exists in the grammar parsing or evaluation logic, where recursive rules are not properly bounded. When a grammar contains a self-referential or mutually recursive rule without a base case, the library enters an infinite recursion loop.
Root Cause: The recursive descent parser or grammar interpreter lacks a recursion depth limit or cycle detection mechanism. An attacker can supply a grammar like:
<root> ::= <root>
or a more complex mutually recursive set:
<a> ::= <b>
<b> ::= <a>
When the library attempts to process such a grammar, it recursively calls itself without termination, eventually exhausting the call stack and crashing the application.
Impact: Denial of service. The application becomes unresponsive or crashes, requiring a restart. No data is compromised, but service availability is affected.
Vulnerable vs. Patched Code
Vulnerable Code (pre-0.1.21):
# Simplified representation of vulnerable grammar processing
def process_rule(rule):
for element in rule.elements:
if element.is_nonterminal:
process_rule(element.rule) # No depth check
else:
# process terminal
pass
Patched Code (0.1.21):
# Patched version with recursion depth limit
MAX_RECURSION_DEPTH = 100
def process_rule(rule, depth=0):
if depth > MAX_RECURSION_DEPTH:
raise RecursionError("Grammar recursion limit exceeded")
for element in rule.elements:
if element.is_nonterminal:
process_rule(element.rule, depth + 1)
else:
# process terminal
pass
The patch introduces a recursion depth limit (MAX_RECURSION_DEPTH) and a depth counter that is incremented on each recursive call. If the limit is exceeded, a RecursionError is raised, preventing stack overflow.
Exploitation & Lab Setup
Red Team
Objective: Trigger a denial of service against a service using XGrammar.
Prerequisites:
- Access to an endpoint that accepts grammar definitions (e.g., an API that generates structured output based on user-provided grammar).
- Knowledge of the XGrammar library version (< 0.1.21).
Exploit Steps:
Craft a malicious grammar with infinite recursion:
<start> ::= <loop> <loop> ::= <loop>Send the grammar to the target service via its API (e.g., HTTP POST request):
curl -X POST https://target.com/api/generate \ -H "Content-Type: application/json" \ -d '{"grammar": "<start> ::= <loop>\n<loop> ::= <loop>"}'Observe the service crash or become unresponsive.
Expected Outcome: The service crashes due to stack overflow, requiring a restart.
Blue Team
Detection:
- Monitor for excessive recursion errors in application logs (e.g.,
RecursionError,StackOverflowError). - Set up alerts for high CPU or memory usage on servers running XGrammar.
- Use the detection rules provided in the Detection Engineering section.
Mitigation:
- Immediately upgrade XGrammar to version 0.1.21 or later.
- If immediate patching is not possible, implement a Web Application Firewall (WAF) rule to block grammar inputs containing self-referential patterns (e.g.,
<rule> ::= <rule>). - Restrict access to grammar submission endpoints to authenticated users only.
Indicators of Compromise (IOCs)
- Log Entries:
RecursionError: maximum recursion depth exceededStackOverflowErrorin Java-based wrappers- Repeated calls to
process_ruleor similar functions in stack traces
- Network Traffic:
- POST requests to grammar endpoints with unusually large or recursive grammar definitions
- System Impact:
- Application crashes or unresponsive services
- High CPU usage followed by process termination
Detection Engineering
YARA Rule
rule XGrammar_Infinite_Recursion_DoS {
meta:
description = "Detects XGrammar grammar files with infinite recursion patterns"
author = "Valters IT"
date = "2025-04-10"
reference = "CVE-2025-57809"
strings:
$recursive_pattern = /<[a-zA-Z_][a-zA-Z0-9_]*>\s*::=\s*<[a-zA-Z_][a-zA-Z0-9_]*>/
$self_reference = /<([a-zA-Z_][a-zA-Z0-9_]*)>\s*::=\s*<\1>/
condition:
any of ($recursive_pattern) and any of ($self_reference)
}
Sigma Rule
title: XGrammar Infinite Recursion DoS Attempt
id: 7a8b9c0d-1e2f-3a4b-5c6d-7e8f9a0b1c2d
status: experimental
description: Detects attempts to exploit CVE-2025-57809 by sending recursive grammar definitions
author: Valters IT
date: 2025-04-10
logsource:
category: application
product: xgrammar
detection:
selection:
EventID: 1001 # Example: Application error event
Message|contains: 'RecursionError'
condition: selection
falsepositives:
- Legitimate complex grammars with deep recursion (rare)
level: high
Suricata Rule
alert http any any -> any any (msg:"CVE-2025-57809 XGrammar Infinite Recursion DoS Attempt"; flow:to_server,established; content:"POST"; http_method; content:"grammar"; http_client_body; pcre:"/<[a-zA-Z_][a-zA-Z0-9_]*>\s*::=\s*<[a-zA-Z_][a-zA-Z0-9_]*>/"; classtype:attempted-dos; sid:1000001; rev:1;)
Remediation & Mitigation
Immediate Actions:
- Patch: Upgrade XGrammar to version 0.1.21 or later. This is the only complete fix.
- Workaround: If patching is delayed, implement input validation to reject grammar definitions that contain self-referential rules (e.g.,
<rule> ::= <rule>). - Rate Limiting: Apply rate limiting to grammar submission endpoints to reduce the impact of repeated DoS attempts.
Long-Term Measures:
- Integrate dependency scanning (e.g.,
pip-audit,safety) into CI/CD pipelines to detect vulnerable versions. - Implement a Web Application Firewall (WAF) with custom rules to block recursive grammar patterns.
- Conduct regular security reviews of open-source dependencies.
Verification:
- After patching, test with a recursive grammar to ensure the error is properly handled (e.g., returns a 400 Bad Request instead of crashing).
Valters IT
Securing Digital Frontiers