Skip to content

Lesson 1 of 9 · 22m

Input Validation at the Trust Boundary

Validation is not form decoration — it is type conversion, canonicalization, and rejection of anything your application cannot honestly represent.

The worst PHP vulnerabilities usually begin before the dangerous function call. They begin at the boundary, where a string from $_GET is treated as an integer, a filename, an email address, or a permission decision because nobody stopped to make the conversion explicit. I have reviewed systems where ?user_id=42 and ?user_id=42 OR 1=1 travelled ten layers deep before anyone asked whether the value was actually an integer.

The request is not your object model

$_GET, $_POST, $_COOKIE, headers, uploaded file metadata, and JSON bodies are attacker-controlled bytes. Even values generated by your own JavaScript are hostile because the attacker does not have to use your JavaScript.

declare(strict_types=1);

$id = filter_input(INPUT_GET, 'invoice_id', FILTER_VALIDATE_INT, [
    'options' => ['min_range' => 1],
]);

if ($id === null || $id === false) {
    http_response_code(400);
    exit('Invalid invoice id');
}

// From here down, $id is an int. Do not re-check it in every function.

The key move is to validate once at the edge and then carry a real type inward. If every function accepts mixed input, every function becomes responsible for security and none of them will do it consistently.

Allowlists beat reject lists

A reject list says "anything except what I remembered to block." An allowlist says "only the states this application understands."

$sort = $_GET['sort'] ?? 'created_at';
$allowedSorts = ['created_at', 'updated_at', 'total_cents'];

if (!in_array($sort, $allowedSorts, true)) {
    $sort = 'created_at';
}

That third true matters. PHP loose comparison is famous for turning "0" into 0, numeric strings into numbers, and careless checks into type-confusion bugs.

Validation is context-specific

There is no universal "clean string." A valid username is not a valid shell argument, filename, SQL identifier, or HTML fragment. Validate shape first, then escape or encode for the destination at the point of use. Length limits are part of security too: unbounded input becomes memory pressure, log spam, slow regexes, and database errors.

Legacy patterns that should make you suspicious

extract($_POST);              // creates variables controlled by the client
$user = $_GET['user'];        // no type, range, or ownership check
$mode = $_REQUEST['mode'];    // GET, POST, and cookie ambiguity
include $_GET['page'] . '.php'; // direct object reference to the filesystem

$_REQUEST deserves special hostility in old code. It merges sources, and a cookie can overwrite a form field depending on PHP configuration. When auditing it, do not assume you know where the value came from.

Field notes

  • filter_input() can return null, false, or the value depending on input presence and validation result; test both failure values deliberately.
  • JSON needs depth, size, and type validation too. json_decode with true still gives attacker-shaped arrays.
  • A hidden form field is hidden from the user interface, not from the request.
  • Casting with (int) is better than nothing, but validation tells you whether the request was malformed instead of silently inventing a new value.

Was this lesson helpful?

Sign in to rate