Expand description
Safe parsing utilities with resource limits.
This module provides a centralized, secure parser utility that enforces
input size limits, parse timeouts, and supports external cancellation.
All language plugins should use SafeParser to prevent OOM vulnerabilities
from pathological inputs.
§Security Background
Tree-sitter parsers can consume unbounded memory when encountering malformed input that triggers exponential backtracking in error recovery. A 103-byte input can amplify to 2GB+ memory consumption (~20 million× amplification).
§Usage
ⓘ
use sqry_core::plugin::safe_parse::{SafeParser, SafeParserConfig};
let config = SafeParserConfig::default();
let parser = SafeParser::new(config);
let result = parser.parse(&language, content, Some(file_path));
match result {
Ok(tree) => { /* use tree */ }
Err(ParseError::InputTooLarge { size, max, .. }) => {
log::warn!("File too large: {} bytes > {} limit", size, max);
}
Err(ParseError::ParseTimedOut { timeout_micros, .. }) => {
log::warn!("Parse timed out after {} ms", timeout_micros / 1000);
}
Err(e) => { /* handle other errors */ }
}Structs§
- Cancellation
Flag - A cancellation flag for aborting long-running parse operations.
- Safe
Parser - Safe parser with resource limits and cancellation support.
- Safe
Parser Config - Configuration for
SafeParserwith bounded limits.
Constants§
- DEFAULT_
MAX_ SIZE - Default maximum input size: 10 MiB.
- DEFAULT_
TIMEOUT_ MICROS - Default parse timeout: 2 seconds (2,000,000 microseconds).
- MAX_
MAX_ SIZE - Maximum allowed size limit: 32 MiB.
- MAX_
TIMEOUT_ MICROS - Maximum allowed timeout: 5 seconds (5,000,000 microseconds).
- MIN_
MAX_ SIZE - Minimum allowed size limit: 1 MiB.
- MIN_
TIMEOUT_ MICROS - Minimum allowed timeout: 100ms (100,000 microseconds).
Functions§
- parse_
safe - Parse content using the default safe parser configuration.