KGCS Documentation

05 — Defense Playbook: Input Validation & D3FEND

Primary Defense: Canonicalization

Resolve paths to their canonical form and validate:

from pathlib import Path

def safe_file_access(filename):
    base = Path('/var/www/uploads').resolve()
    target = (base / filename).resolve()

    if not str(target).startswith(str(base)):
        raise ValueError("Path traversal blocked")

    return open(target)

D3FEND Mitigations


Next: 06 Reasoning Walkthrough