Skip to main content

Module safe_parse

Module safe_parse 

Source
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§

CancellationFlag
A cancellation flag for aborting long-running parse operations.
SafeParser
Safe parser with resource limits and cancellation support.
SafeParserConfig
Configuration for SafeParser with 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.