Use it when writing or reviewing an API that accepts data, or when an API accepted a request it should have rejected.
REST API Input Validation
Input validation constrains request types, lengths, ranges, media types and data models before acting on untrusted input.
1. Validate type, length, format and allowed values on the server side.
2. Reject unknown fields and oversized bodies.
3. Use parameterised queries — never build queries from input.
request arrives → size and content type checked → body parsed against the schema or YANG model → values range-checked → rejected with 400/422 before anything is done → accepted input passed on as parameters, never pasted into commands or queries
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Read-only Verification Verify boundary
Review the endpoint contract and validation behavior. Use safe test fixtures in an isolated environment for malformed/oversized or out-of-sequence input.
Step 2 Common mistakes Narrow the fault
- Validating only on the client side.
- Building commands or queries from input.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
A schema-valid payload is not proof of authorized intent or safe network behavior; rejected malformed input does not prove every parser path is safe.
In a lab, send a request with a wrong type, an out-of-range value and an unknown field → expect 400 or 422 with an error body, and no change made. With RESTCONF, the YANG model performs this validation for you.
ReferenceWhat it is1 section · depth on demand
Direct Answer
Input validation constrains request types, lengths, ranges, media types and data models before acting on untrusted input.
ReferenceHow it works1 section · depth on demand
Technical Mechanics
Validate both syntax/schema and the requested operation/resource against server-side policy. Harden XML parsers where XML is accepted; impose appropriate request-size limits. YANG validation does not replace authorization or application/service validation.
ReferenceImportant terms & boundaries4 sections · depth on demand
Official Sources
Proof Boundaries
A schema-valid payload is not proof of authorized intent or safe network behavior; rejected malformed input does not prove every parser path is safe.
In depth
Input validation: syntax, meaning and allowlists
Input validation checks that data meets an application's requirements before the data is used. The OWASP Input Validation Cheat Sheet describes the benefits as reducing malformed data, invalid business operations and excessive resource consumption. It separates validation from the defences needed when the data is used, such as parameterised queries and output encoding. The same thinking applies to network automation and APIs: a script that takes VLAN IDs, prefixes or hostnames from a form, a CSV file or a ticket should check them before building configuration.
Two levels of checks (cheat sheet)
| Level | Question | Network example |
|---|---|---|
| Syntactic | Right type and format? | Is vlan an integer? Is prefix a valid CIDR string? |
| Semantic | Does the value make sense for this operation? | Is the VLAN in 2–4094 and not reserved locally? Is the prefix inside the site's assigned block? |
Validation strategies (cheat sheet)
| Data | Check |
|---|---|
| Numbers and dates | Expected type, minimum and maximum |
| Fixed choices | Exact membership in the allowed set (for example access, trunk) |
| Strings | Length limits, required characters or structure |
| Objects and arrays | Allowed and required fields; a cap on item count; validate every item |
| Regular expressions | Match the whole value, bound the length first, avoid heavy backtracking |
Allowlist, not denylist
Define what is accepted and reject everything else. Trying to block every malicious string fails both ways: legitimate input such as names with apostrophes gets rejected, and queries are still not made safe. Validate on the server even when the browser checks the same fields, because client-side checks can be bypassed. Use them only for immediate feedback to the user.
What validation isn't
| Threat | Primary defence | Role of validation |
|---|---|---|
| SQL injection | Parameterised queries | Supporting only |
| XSS | Context-aware output encoding | Supporting only |
| User-authored HTML | A maintained HTML sanitisation library | Can't replace it |
| File uploads | Content checks, size limits, safe storage | The filename and content type are untrusted metadata |
import ipaddress, re
HOSTNAME = re.compile(r"[a-z0-9][a-z0-9-]{0,62}")
MODES = {"access", "trunk"}
SITE_BLOCK = ipaddress.ip_network("10.110.0.0/16")
def validate_port_request(req: dict) -> dict:
allowed = {"hostname", "interface", "mode", "vlan", "prefix"}
if set(req) - allowed:
raise ValueError("unexpected fields")
host = str(req["hostname"])
if len(host) > 63 or not HOSTNAME.fullmatch(host):
raise ValueError("invalid hostname")
if req["mode"] not in MODES:
raise ValueError("invalid mode")
vlan = int(req["vlan"])
if not 2 <= vlan <= 4094:
raise ValueError("vlan out of range")
net = ipaddress.ip_network(req["prefix"], strict=True)
if not net.subnet_of(SITE_BLOCK):
raise ValueError("prefix outside site block")
return {"hostname": host, "mode": req["mode"], "vlan": vlan, "prefix": str(net)}
Notes on the example
fullmatchchecks the whole string, as the cheat sheet recommends, and the length is bounded before matching.- The prefix is parsed by a library instead of a regex, then checked semantically against the site block.
- The function returns the normalised values. Later code uses those, not the raw input, so nothing is decoded or reinterpreted downstream.
Text, encodings and decoding
The cheat sheet recommends one agreed character encoding and rejecting malformed input. If normalisation is needed, apply one policy before validating, storing and comparing. Decode according to the protocol before checking field rules, and validate the representation that will actually be used. Decoding again later can invalidate the earlier checks.
Documented example, not captured output.
Quick reference
Input validation on APIs prevents injection and malformed data from reaching backends: validate types, lengths, ranges and formats against a schema (OpenAPI/JSON Schema), reject unexpected fields, and encode outputs.
For network automation that builds configuration from input (templates, YANG payloads), validate values (VLAN IDs 1–4094, valid IP prefixes, allowed interface names) before pushing — otherwise a typo or a malicious value can produce broken or dangerous device configs. Use YANG model validation and dry-run/candidate configuration where supported.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Security → REST API Security → Input validation. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.cisco.com/c/en/us/support/ios-nx-os-software/ios-xe-17/series.html · confidence 94%