Skip to content

commandSecurity

Harden Input

Adds validation and sanitization to the inputs of one named handler and writes tests that throw bad data at it.

npx workspacetemplates add harden-input --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/harden-input.md15 lines · 1.2 KBFile
---
description: Adds validation and sanitization to the inputs of one named handler and writes tests that throw bad data at it.
argument-hint: "[handler, route or file name]"
---

Harden the inputs of this handler: $ARGUMENTS

1. Find the handler and list every external input it reads: body fields, query string, path params, headers, cookies, uploaded files.
2. Use the validation library the project already has (zod, joi, pydantic, class-validator, Bean Validation, go-playground/validator). If none exists, ask before adding a dependency.
3. Define a strict schema: exact types, min and max lengths, numeric ranges, enums for fixed choices, regex for ids and emails, a size cap for bodies and files. Reject unknown fields.
4. Escape or encode on output, and use parameterized queries. Never strip characters as a substitute for validation.
5. Return a 400 with field-level messages that do not echo raw input.
6. Add tests next to the existing ones for: missing fields, wrong types, empty strings, 10 MB strings, null bytes, SQL and HTML payloads, and negative or huge numbers. Run them.

Show the diff and test results, list any input you left unchecked and why, and ask before committing.

What it does

Handlers tend to trust whatever arrives. This command takes one handler, lists every value it reads from the outside world, and adds checks for type, length, range and format before the value is used.

It also adds tests for the ugly cases, such as oversized bodies and injection strings, so the protection stays in place after the next refactor.

Example

you › /harden-input createUser in src/api/users.ts

agent › Added a zod schema (email max 254, name 1-80, role enum). Unknown fields now rejected with 400.
9 new tests: all pass. Left unchecked: avatar upload (needs a decision on size limit).
Commit these changes?

Questions

Will it add a new library?
Only if the project has no validator, and it asks first.
Does sanitizing mean deleting bad characters?
No. It rejects invalid input outright and encodes safely on output, because stripping characters is easy to bypass.
  • Audit Dependencies

    CommandSecurity

    Free
  • Check Auth

    CommandSecurity

    Free
  • Find Secrets

    CommandSecurity

    Free
  • Review Permissions

    CommandSecurity

    Free