How to Sanitize User Input Before Passing It to a Tool Call

Tested by TNT Review — record TNT-TEST-2026-10-05-033, 5 October 2026. We ran 5 real cases below; the output matches exactly what’s shown in Verification.

The problem

An agent that passes a user’s (or even a model’s own generated) text straight into a shell command or a file path is trusting that text not to contain anything dangerous — a semicolon that chains a second command, a ../ that escapes the intended folder. Checking for these specific shapes before acting is cheap; not checking can be expensive.

Expected outcome

By the end of this guide you will have two small checks: one for values headed into a shell command, one for values headed into a file path, each flagging the specific patterns that indicate injection.

Requirements and costs

  • Node.js (version 18 or newer).
  • Cost: free. No dependencies.

Step-by-step implementation

  1. Download the starter from the link below.
  2. Check any value before it reaches a shell command:
    const { checkShellArg } = require("./sanitize-tool-args");
    const result = checkShellArg(userProvidedValue);
    if (!result.ok) { /* refuse, log, or ask again -- don't proceed */ }
  3. Check any value before it reaches a file path:
    const { checkPathArg } = require("./sanitize-tool-args");
    const result = checkPathArg(userProvidedValue, "/var/data");

A real example

checkShellArg("report-2026-10.txt")              // { ok: true }
checkShellArg("report.txt; cat /etc/passwd")     // { ok: false, reason: "contains shell metacharacter \";\"..." }

checkPathArg("uploads/report.txt", "/var/data")  // { ok: true, resolved_hint: "/var/data/uploads/report.txt" }
checkPathArg("../../etc/passwd", "/var/data")    // { ok: false, reason: "contains '..'..." }
checkPathArg("/etc/passwd", "/var/data")         // { ok: false, reason: "absolute path not allowed" }

Verification and troubleshooting

What we tested, on 5 October 2026 (record TNT-TEST-2026-10-05-033): a genuinely safe filename (passed), a value containing a shell metacharacter used to chain a second command (correctly caught, with the exact character and position named), a genuinely safe relative path (passed, with a resolved-path hint), a path traversal attempt using ../ (correctly caught), and an absolute path (correctly caught as not allowed under a restricted base directory) — all 5 matched expected behaviour.

Troubleshooting:

  • A legitimate filename gets rejected. Check whether it genuinely contains one of the flagged characters (; & | \` $ ( ) < >) — if so, that’s a real constraint on what filenames you can safely pass through a shell, not a false positive to work around.
  • You need to allow absolute paths in some cases. checkPathArg is deliberately restrictive by default; if you genuinely need absolute paths, validate them against an explicit allowlist of directories instead of removing the check entirely.
  • You’re building the shell command yourself, not passing user text directly. Checking is still worth it if ANY part of the command is built from user or model input — the risk comes from untrusted input reaching the command, regardless of how much of the rest you wrote yourself.

Limitations and alternatives

These are pattern checks for known-dangerous shapes, not a complete guarantee of safety — the more robust fix where possible is avoiding shell string concatenation entirely (using an API that takes arguments as a real array, not a single string) and using path-joining functions that can’t produce traversal by construction. These checks are a cheap second layer, not a replacement for those.

Free resources

The download contains both checks.

Download the input sanitizer (ZIP, 1 KB)

Is there a paid version?

Not yet. This is the complete method. TNT Review has sold nothing to date and has no checkout, so any price we discuss in future should be read as a hypothesis, not an offer.

Leave a comment