Modern PHP is a different language from the one that earned its security reputation. PHP 8 removed some of the worst comparison surprises, frameworks parameterize queries by default, and register_globals is long gone. Yet the bug classes that defined PHP security two decades ago still appear in code written last month, usually in the corners where a developer stepped outside the framework: a legacy controller, a quick admin script, an upload handler.
This post covers five of them with vulnerable and fixed code. It is about your application code. If you are here because of a vulnerability in the PHP interpreter itself, read our post on PHP2Shell and interpreter bugs instead: that is a patching problem, not a coding one, and a code scanner cannot see it.
1. unserialize() on anything a user can touch
unserialize() rebuilds objects, and rebuilding an object can run code: magic methods such as __wakeup() and __destruct() fire, and autoloading pulls in classes. An attacker who controls the serialized string chooses which classes are created and with which properties, and a chain of ordinary classes in your dependencies can turn that into file writes or code execution.
// Vulnerable: a cookie is attacker-controlled
$prefs = unserialize(base64_decode($_COOKIE['prefs']));// Fixed: use a data format that cannot create objects
$prefs = json_decode($_COOKIE['prefs'] ?? '{}', true, 16, JSON_THROW_ON_ERROR);The allowed_classes option narrows what can be instantiated, but the PHP manual is blunt: do not pass untrusted input to unserialize() "regardless of the options value of allowed_classes". Use JSON for anything that crosses a trust boundary, and sign the value with hash_hmac if you need to know it has not been altered.
A related path is the phar:// stream wrapper. Before PHP 8.0, filesystem functions such as file_exists() on a phar:// path would unserialize the archive's metadata. PHP 8.0 stopped doing that automatically, but code that runs on older versions, or that passes user paths to filesystem calls, still deserves a look.
2. include and require with a user-chosen path
// Vulnerable: ?page=../../../../etc/passwd, or a wrapper path
$page = $_GET['page'];
include __DIR__ . "/pages/{$page}.php";Remote file inclusion needs allow_url_include, which is off by default and deprecated since PHP 7.4 (filesystem configuration). Local file inclusion needs nothing: path traversal reads files, and an attacker who can get content into any file on the server (an upload, a log line, a session file) can often get it executed by including it.
// Fixed: the request picks a key, never a path
$pages = ['home' => 'home.php', 'about' => 'about.php', 'contact' => 'contact.php'];
$file = $pages[$_GET['page'] ?? 'home'] ?? 'home.php';
include __DIR__ . '/pages/' . $file;Front controllers and router tables in every major framework already work this way. The bug lives in the older files nobody migrated.
3. PDO used like string concatenation
PDO gives you prepared statements, but prepare() only protects the values you bind. A query string with a variable already in it is injectable no matter which method runs it.
// Vulnerable: prepare() on an already-built string protects nothing
$stmt = $pdo->prepare("SELECT * FROM orders WHERE customer = '" . $_GET['c'] . "'");
$stmt->execute();// Fixed: placeholders for values
$stmt = $pdo->prepare('SELECT * FROM orders WHERE customer = :c');
$stmt->execute(['c' => $_GET['c']]);Two places still trip careful developers:
- Identifiers. Column names in
ORDER BYand table names cannot be bound. Map the input to a fixed list of known columns. - IN lists. Build one placeholder per element (
implode(',', array_fill(0, count($ids), '?'))) and bind the array. Do notimplodethe values themselves.
Set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION and the charset in the DSN, so errors are not silently swallowed and the connection encoding is what you expect.
4. File uploads stored as the user named them
Everything in $_FILES except the temporary path is supplied by the client. The PHP upload documentation says the MIME type "is however not checked on the PHP side", and name is the original name on the client machine.
// Vulnerable: client name, client type, inside the web root
if ($_FILES['avatar']['type'] === 'image/png') {
move_uploaded_file($_FILES['avatar']['tmp_name'], 'uploads/' . $_FILES['avatar']['name']);
}Upload shell.php with a Content-Type: image/png header and, if uploads/ is served by PHP, you have remote code execution.
// Fixed: server-chosen name, content-checked type, outside the web root
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($_FILES['avatar']['tmp_name']);
$ext = ['image/png' => 'png', 'image/jpeg' => 'jpg'][$mime] ?? null;
if ($ext === null) { http_response_code(415); exit; }
$name = bin2hex(random_bytes(16)) . '.' . $ext;
move_uploaded_file($_FILES['avatar']['tmp_name'], '/srv/app-data/avatars/' . $name);Serve uploads through a script that sets the content type, or from a location where the web server never executes PHP. Limit size with upload_max_filesize and in your own code.
5. Type juggling in comparisons
PHP 8 fixed the most notorious case: 0 == "foo" is now false (PHP 8.0 incompatible changes). Numeric strings are still compared as numbers with ==, so two different strings that both look like numbers in scientific notation can compare equal. A hash comparison such as md5($input) == $stored is true whenever both values are strings like "0e" followed only by digits, because both read as zero.
// Vulnerable: loose comparison on secrets
if ($_POST['token'] == $user->reset_token) { ... }
if (md5($_POST['password']) == $user->password_hash) { ... }// Fixed: constant-time, strict, and a real password hash
if (hash_equals($user->reset_token, (string) ($_POST['token'] ?? ''))) { ... }
if (password_verify($_POST['password'] ?? '', $user->password_hash)) { ... }Use === everywhere a type matters, hash_equals() for tokens and MACs, and password_hash() / password_verify() for passwords. Unsalted md5 for passwords is a bug on its own, whatever the comparison operator.
What Vulkro checks in PHP
Vulkro Core reads PHP source with dedicated detectors plus a short-range taint pass, and checks composer.lock against the local CVE bundle. The detectors for this post are PHP-DESER-001 (unserialize of request input), PHP-PHAR-001, PHP-LFI-001 (request input in an include / require path), PHP-SQLI-001, PHP-UPLOAD-001 (an upload stored under the client's file name) and PHP-TYPEJUGGLE-001 (a loose == on a password, hash or token). Request sources include the superglobals and common framework request objects.
The limits are documented, and we would rather you know them: taint is followed within one function plus one call into a project function, and PHP has no endpoint map or auth model yet, so the API-level authorization checks that need one do not run for PHP. The PHP docs page has the full list.
FAQ
Is unserialize() safe with allowed_classes set to false?
It removes object creation, which blocks the usual gadget chains, but the PHP manual still says not to pass untrusted input to unserialize() regardless of that option. Use json_decode() for data that comes from users.
Does PDO prevent SQL injection automatically?
Only for values bound through placeholders. If you concatenate user input into the string you pass to prepare() or query(), the query is injectable. Identifiers such as column names must come from an allowlist.
Is PHP type juggling still a problem in PHP 8?
Less than before, since 0 == "non-numeric" is now false, but numeric strings still compare numerically with ==. Use ===, hash_equals() and password_verify() for anything security relevant.
See the checks on your own PHP code: Start a 14-day trial. Related: taint analysis: sources, sinks and sanitizers.