Skip to main content

Module presets

Module presets 

Source
Expand description

Ready-made sanitizer configurations for common threat models.

Each preset returns a fully configured Sanitizer with rules matched to a specific use case. Choose based on how the validated value will be consumed:

PresetTarget contextRules
command_argCommand::new().arg()ControlChar
shell_commandsh -c, SSH, popenShellMeta + ControlChar + EnvExpansion + Glob
file_pathUpload dest, includePathTraversal + ControlChar
file_path_absoluteConfig file, absolute OKPathTraversal(allow_abs) + ControlChar
strictSSH remote path ops, max protectionAll 5 rules

§AI agent use case

When an LLM generates tool calls (e.g. Claude Code, Copilot, Devin), treat the data arguments as untrusted input — indirect prompt injection can manipulate what the AI produces.

§What this crate CAN validate

Path arguments from structured tool calls — this is the primary value for AI agents:

AI: { tool: "read_file", path: "../../etc/shadow" }
                                ^^^^^^^^^^^^^^^^
                                file_path() catches this

AI: { tool: "write_file", path: "/etc/crontab" }
                                ^^^^^^^^^^^^^
                                file_path() catches this

Individual arguments when the framework provides structured tool calls:

AI: { tool: "git_clone", url: "https://evil.com; rm -rf /" }
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                               shell_command() catches the `;`

Template slots when a trusted template is filled with AI data:

Template (hardcoded):  "rsync -avz {} {}"
Slot 1 (file_path):    validated_src
Slot 2 (shell_command): validated_dest

§What this crate CANNOT validate

Free-form bash command strings — the AI generates the entire command, not just arguments:

AI: Bash("git diff HEAD~3")           ← legitimate
AI: Bash("git diff HEAD~3; rm -rf /") ← injection

Sanitizing the full string would break the legitimate command.
This requires: sandbox, container isolation, command allowlist.

§Preset selection for AI tool calls

AI tool typePresetExample
File read/writefile_pathread("src/lib.rs")
Config filefile_path_absoluteread("/etc/app/config.toml")
Shell arg slotshell_commandssh("deploy {tag}")
Command::new().arg()command_arggit.arg(branch_name)
Unknown contextstrictany mixed-use value
Free-form bashout of scopeBash("cd repo && make")

§Known limitations

These presets do not defend against:

  • Free-form command strings — use sandbox/container isolation.
  • Argument injection (--upload-pack=evil) — a flag prefixed with -- is valid shell text. Use -- separators or command-specific validation.
  • URL-encoded bypasses (%2e%2e) — decode input before sanitizing.
  • Semantic attacks — a path like safe/but/wrong/file.txt passes all rules but may still be the wrong file.

Functions§

command_arg
Minimal validation for Command::new().arg() contexts.
file_path
Sanitizer for relative file paths.
file_path_absolute
Sanitizer for file paths where absolute paths are acceptable.
shell_command
Sanitizer for values interpolated into shell command strings.
strict
Maximum-protection sanitizer with all rules enabled.