Skip to main content

Module validation_cache

Module validation_cache 

Source
Expand description

The disk-backed cache for the pattern-safety validator’s expensive layer: the reference _validation_cache.py (the PHP lane’s Detection/Redos/ValidationCache.php and the TS PatternValidationCache port the same module).

The cheap deterministic layers (dangerous constructs, compile check, structural detectors) always re-run; only the empirical cost-verdict outcome is cached, keyed by pattern and flags, so a process boot reuses prior certifications instead of re-timing every custom pattern.

File shape: a JSON object mapping sha256(pattern 0x00 flags) hex keys to {version, safe, reason} entries; entries from a different engine version are dropped on load, a corrupt file starts empty (the outcome surfaces as LoadOutcome data - the engine keeps its no-logger idiom), and a write is atomic (tmp file plus rename).

§Example

use guard_core_engine::redos::validation_cache::{LoadOutcome, ValidationCache};

let dir = std::env::temp_dir().join("guard-validation-cache-doctest");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir)?;
let path = dir.join("validation-cache.json");
let cache = ValidationCache::load(&path);
assert!(matches!(cache.outcome(), LoadOutcome::Absent));

// First sight: the empirical chain runs, the verdict persists.
let (safe, _) = cache.validate_compat(r"^safe-\d+$");
assert!(safe);
assert!(path.is_file(), "the verdict landed on disk");

// Second boot: the same pattern answers from the loaded file.
let reloaded = ValidationCache::load(&path);
assert!(reloaded.get(r"^safe-\d+$", true).is_some());
std::fs::remove_dir_all(&dir)?;

Structs§

ValidationCache
The disk-backed pattern-validation cache.

Enums§

LoadOutcome
How the cache file loaded (the reference’s log-once outcomes, surfaced as data).

Functions§

engine_version
The cache-invalidating engine identity (the crate version): bumped with every release so verdicts from an older engine never load.