Table of Contents

2. Input Validation & Data Shape — The First Security Boundary

This page builds directly on the previous idea that security begins with predictable shape.
Security begins at the boundary.
Before escaping, before hashing, before permissions, before anything else
— the system must decide what it will accept and what it will reject.

Modern PHP makes this easier,
but only if we treat input shape as a first‑class concept.

A secure system does not accept “almost valid” data.
It accepts only what it understands.

This page introduces the mindset of shaping, validating,
and rejecting input with clarity.


1. Validation Is Not About Checking — It’s About Shaping

Validation is not a series of checks.
It is the act of shaping untrusted data
into a predictable form.

A secure system:

If the input doesn’t match the shape,
the system says no
.


2. Reject Unknown Fields

One of the most overlooked security principles is
Unknown fields are not harmless
— they are untrusted
.

A secure boundary

Silent acceptance is how vulnerabilities slip in.


3. Validate at the Boundary, Not Deep Inside

Validation belongs at the edge of the system:

Once data crosses the boundary,
it should already be

Internal code should never wonder whether
the input is valid
.


4. Prefer DTOs Over Arrays

Arrays are flexible.
Flexibility is the opposite of security
.

DTOs give you:

A DTO is a contract.
An array is a suggestion
.


5. Avoid Implicit Coercion

PHP will happily coerce

This is convenient
— and dangerous
.

Safe defaults:

Predictability
is security
.


6. Validate Types, Not Just Values

A value can be “valid”
but still unsafe
.

Examples:

"123" is not an integer
"true" is not a boolean
"[]" is not an array
"null" is not null
"5" is not a float

Safe validation → checks for

Security is not about correctness
— it's about clarity
.


7. Validate Structure Before Content

Before checking meaning,
check shape
.

Example for a user registration payload:

  1. Is it an object?
  2. Does it contain exactly the expected fields?
  3. Are the fields the correct types?

Only then: check length, format, domain rules.

Structure first.
Meaning second
.


8. Fail Fast, Fail Loudly

A secure system:

So, do not:

A system that fails loudly
is harder to exploit
.


9. Validation Is a Security Boundary, Not a UX Feature

User‑friendly error messages belong in the UI.
Strict validation belongs in the backend
.

Security validation should be

Friendly messages can wrap strict rules
— not replace them
.


Summary

Input validation
is the first and most important security boundary.

A secure system

Security begins with predictable input.
Everything else builds on that foundation
.


This page describes secure defaults and common practices in modern PHP. It is not a complete security guide, and it does not replace formal audits or framework‑specific documentation. These principles are consistent enough to be useful, but security always requires context, judgment, and ongoing review.