# general-injection-php-v1

> general-injection-php-v1: 10 Keryn rules for php (7 error, 3 warning).

Source: https://keryn.dev/rules/general-injection-php-v1/

10 rules · Keryn Software Licence · rules 0.8.0 · [all packs](https://keryn.dev/rules/)

**How to read a rule**

- **Severity**: How bad the finding is if it is real: `error` fails a default gate, `warning` and `info` do not.

- **Confidence**: **P0** exploitable as written; **P1** dangerous in context; **P2** a heuristic or a posture check: confirm by reading the code.

- **False-positive risk**: **low**: an exact API or flag, so hits are almost always real; **medium**: the pattern is legitimate in many programs, so check each hit; **high**: it finds a lead to confirm, so expect benign hits.

- **Edge**: The relationship the finding sits on. **reaches**: content or traffic gets to a thing; **calls**: one thing invokes another; **reads**: one thing reads from another; **writes**: one thing writes to another; **sends**: data leaves for another party; **grants**: authority is given or left unchecked; **depends_on**: something relies on a component it does not verify; **trusts**: one party believes another without evidence. The boundary it crosses, when there is one: **user->prompt** (untrusted input reaching a prompt); **model->code** (model output reaching code, a shell, a query or a page); **secret->outside** (a secret leaving the trusted side); **supply-chain** (code, models or prompts pulled in from outside).

- **Taxonomy chips**: References for readers who work in OWASP LLM 2025, OWASP Agentic, CWE or NIST AI RMF. They say where a rule belongs, not that a framework is satisfied.

## keryn.general.php.xss-request-to-output

`error` php data-flow rule php, symfony, laravel, wordpress

A request value is written to the response (echo, print, printf, die/exit, &lt;?= ?&gt;, or a response body) without output escaping. A script or HTML payload in the value runs in the visitor's browser. Escape for the context: htmlspecialchars($v, ENT\_QUOTES, 'UTF-8') for HTML, esc\_html / esc\_attr in WordPress, or render through Twig / Blade, which escape by default.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-79](https://keryn.dev/taxonomies/cwe/#cwe-79-cross-site-scripting) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Escape at the point of output: echo htmlspecialchars($value, ENT\_QUOTES | ENT\_SUBSTITUTE, 'UTF-8'). In templates use Twig or Blade (&#123;&#123; &#125;&#125;) which escape automatically. Cast numeric values with (int). Return JSON with json\_encode and a JSON content type instead of printing it into HTML; if it must sit inside a script tag use JSON\_HEX\_TAG.

**References:** [https://www.php.net/manual/en/function.htmlspecialchars.php](https://www.php.net/manual/en/function.htmlspecialchars.php) · [https://symfony.com/doc/current/templates.html#output-escaping](https://symfony.com/doc/current/templates.html#output-escaping) · [https://laravel.com/framework/docs/blade#displaying-data](https://laravel.com/framework/docs/blade#displaying-data)

## keryn.general.php.sql-injection-request-to-query

`error` php data-flow rule php, mysqli, pdo, doctrine-dbal, laravel, wordpress

A request value is built into SQL text passed to a query function (mysqli, PDO, pg\_\*, Doctrine DBAL, Laravel DB and raw query-builder methods). The value can change the query. Keep the SQL constant with ? or :name placeholders and pass values as bound parameters.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Use prepared statements: $stmt = $pdo-&gt;prepare('SELECT \* FROM t WHERE id = ?'); $stmt-&gt;execute([$id]). For Laravel use DB::select('... where id = ?', [$id]) and whereRaw('x = ?', [$v]). Identifiers (column, table, ORDER BY direction) cannot be bound: map them from a fixed allowlist with in\_array(..., true). Cast numbers with (int).

**References:** [https://www.php.net/manual/en/security.database.sql-injection.php](https://www.php.net/manual/en/security.database.sql-injection.php) · [https://www.php.net/manual/en/pdo.prepared-statements.php](https://www.php.net/manual/en/pdo.prepared-statements.php) · [https://laravel.com/framework/docs/queries#raw-expressions](https://laravel.com/framework/docs/queries#raw-expressions) · [https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/security.html](https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/security.html)

## keryn.general.php.sql-built-from-variable-audit

`warning` php php, mysqli, pdo, doctrine-dbal, laravel, wordpress

The SQL passed to a query function is a concatenation, an interpolated string or sprintf() that contains a non-constant part. Whatever the source of the variable, this style invites injection when the value is ever user-influenced. Switch to placeholders and bound parameters, or cast / quote every variable part.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Replace string building with placeholders (? or :name) and pass values through execute() / bind parameters. Where a fragment cannot be bound (column or table name), choose it from a fixed allowlist. If the part is numeric, cast it with (int) at the point of use.

**References:** [https://www.php.net/manual/en/security.database.sql-injection.php](https://www.php.net/manual/en/security.database.sql-injection.php) · [https://www.php.net/manual/en/pdo.prepared-statements.php](https://www.php.net/manual/en/pdo.prepared-statements.php) · [https://laravel.com/framework/docs/queries#raw-expressions](https://laravel.com/framework/docs/queries#raw-expressions) · [https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/security.html](https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/security.html)

## keryn.general.php.path-traversal-request-to-file

`error` php data-flow rule php, symfony, laravel, wordpress

A request value reaches a file include, read, write, delete, download or Storage call without being reduced to a base name or confined to a directory. ../ sequences (or an absolute path) escape the intended folder, and include/require can run attacker-chosen code. Use basename(), or realpath() the result and check it starts with the base directory before use.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [reaches](https://keryn.dev/docs/edges/edge-kinds/#reaches) content or traffic gets to a thing; it crosses the [`user->prompt`](https://keryn.dev/docs/edges/trust-boundaries/#user-to-prompt) boundary (untrusted input reaching a prompt).

[CWE-22](https://keryn.dev/taxonomies/cwe/#cwe-22-path-traversal) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Take only basename($name), or map an id to a path on the server. Otherwise resolve with realpath($base . '/' . $name) and refuse unless it is not false and str\_starts\_with($real, realpath($base) . DIRECTORY\_SEPARATOR). Never pass a request value to include/require; select from a fixed list of files.

**References:** [https://www.php.net/manual/en/function.basename.php](https://www.php.net/manual/en/function.basename.php) · [https://www.php.net/manual/en/function.realpath.php](https://www.php.net/manual/en/function.realpath.php) · [https://www.php.net/manual/en/security.filesystem.php](https://www.php.net/manual/en/security.filesystem.php) · [https://laravel.com/framework/docs/filesystem](https://laravel.com/framework/docs/filesystem) · [https://symfony.com/doc/current/components/http_foundation.html#serving-files](https://symfony.com/doc/current/components/http_foundation.html#serving-files)

## keryn.general.php.command-injection-request-to-shell

`error` php data-flow rule php, symfony, laravel

A request value reaches a shell execution function (exec, system, shell\_exec, passthru, popen, proc\_open, pcntl\_exec or backticks) without shell escaping. Shell metacharacters in the value run extra commands. Pass the value through escapeshellarg(), or avoid the shell and call proc\_open with an argument array.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Wrap each user-supplied argument in escapeshellarg(). Better, skip the shell: proc\_open(['convert', $file, $out], ...) or Symfony Process with an argument array. Validate the value (cast to int, match an allowlist) before it gets near a command.

**References:** [https://www.php.net/manual/en/function.escapeshellarg.php](https://www.php.net/manual/en/function.escapeshellarg.php) · [https://www.php.net/manual/en/function.escapeshellcmd.php](https://www.php.net/manual/en/function.escapeshellcmd.php) · [https://www.php.net/manual/en/function.proc-open.php](https://www.php.net/manual/en/function.proc-open.php) · [https://symfony.com/doc/current/components/process.html](https://symfony.com/doc/current/components/process.html)

## keryn.general.php.ssrf-request-to-http-client

`error` php data-flow rule php, curl, guzzle, laravel, symfony-http-client

A request value is used as the URL of a server-side HTTP request (curl, fopen/file\_get\_contents, Guzzle, Laravel Http, Symfony HttpClient). An attacker can make the server call internal addresses, cloud metadata endpoints or other hosts. filter\_var(FILTER\_VALIDATE\_URL) only checks syntax and does not stop this; compare the parsed host against an allowlist.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [reaches](https://keryn.dev/docs/edges/edge-kinds/#reaches) content or traffic gets to a thing; it crosses the [`user->prompt`](https://keryn.dev/docs/edges/trust-boundaries/#user-to-prompt) boundary (untrusted input reaching a prompt).

[CWE-918](https://keryn.dev/taxonomies/cwe/#cwe-918-server-side-request-forgery) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Parse the URL and require an https scheme and a host that is in a fixed allowlist: in\_array(parse\_url($url, PHP\_URL\_HOST), $allowed, true). Better, accept an id or a path and build the URL from a fixed base. Block redirects (CURLOPT\_FOLLOWLOCATION false, Guzzle allow\_redirects false) and resolve and reject private or link-local addresses if arbitrary hosts are required.

**References:** [https://www.php.net/manual/en/function.parse-url.php](https://www.php.net/manual/en/function.parse-url.php) · [https://www.php.net/manual/en/function.curl-setopt.php](https://www.php.net/manual/en/function.curl-setopt.php) · [https://docs.guzzlephp.org/en/stable/request-options.html](https://docs.guzzlephp.org/en/stable/request-options.html) · [https://laravel.com/framework/docs/http-client](https://laravel.com/framework/docs/http-client) · [https://symfony.com/doc/current/http_client.html](https://symfony.com/doc/current/http_client.html)

## keryn.general.php.open-redirect-request-to-location

`warning` php data-flow rule php, symfony, laravel, wordpress

A request value decides where the browser is redirected (Location header, redirect(), RedirectResponse, wp\_redirect). An attacker can send victims from your domain to a phishing page. Redirect only to a fixed route name, an allowlisted host, or a local path that starts with a single slash.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [reaches](https://keryn.dev/docs/edges/edge-kinds/#reaches) content or traffic gets to a thing; it crosses the [`user->prompt`](https://keryn.dev/docs/edges/trust-boundaries/#user-to-prompt) boundary (untrusted input reaching a prompt).

[CWE-601](https://keryn.dev/rules/?cwe=CWE-601) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Redirect to a route by name (redirect()-&gt;route('home'), $this-&gt;redirectToRoute('home')) and carry state in the session. If a return URL is needed, accept it only when it starts with '/' and not '//' (and has no backslash), or when parse\_url($u, PHP\_URL\_HOST) is in an allowlist. In WordPress use wp\_safe\_redirect().

**References:** [https://www.php.net/manual/en/function.header.php](https://www.php.net/manual/en/function.header.php) · [https://laravel.com/framework/docs/responses#redirects](https://laravel.com/framework/docs/responses#redirects) · [https://symfony.com/doc/current/controller.html#redirecting](https://symfony.com/doc/current/controller.html#redirecting) · [https://www.php.net/manual/en/function.parse-url.php](https://www.php.net/manual/en/function.parse-url.php)

## keryn.general.php.unserialize-untrusted-input

`error` php data-flow rule php, symfony, laravel, wordpress

A request value is passed to unserialize() without allowed\_classes set to false. PHP builds any class the attacker names and runs its magic methods (\_\_wakeup, \_\_destruct), which leads to object- injection and often remote code execution. Use json\_decode() for data, or pass ['allowed\_classes' =&gt; false].

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [depends_on](https://keryn.dev/docs/edges/edge-kinds/#depends-on) something relies on a component it does not verify; it crosses the [`supply-chain`](https://keryn.dev/docs/edges/trust-boundaries/#supply-chain) boundary (code, models or prompts pulled in from outside).

[CWE-502](https://keryn.dev/taxonomies/cwe/#cwe-502-deserialization-of-untrusted-data) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Exchange data as JSON (json\_decode($raw, true)). If unserialize() cannot be avoided, pass ['allowed\_classes' =&gt; false] so only plain arrays and scalars come back, and sign the payload with hash\_hmac and verify it with hash\_equals before decoding. Never unserialize cookies, form fields or request bodies.

**References:** [https://www.php.net/manual/en/function.unserialize.php](https://www.php.net/manual/en/function.unserialize.php) · [https://www.php.net/manual/en/function.json-decode.php](https://www.php.net/manual/en/function.json-decode.php) · [https://www.php.net/manual/en/language.oop5.magic.php](https://www.php.net/manual/en/language.oop5.magic.php)

## keryn.general.php.zip-slip-archive-entry-name

`error` php data-flow rule php, ziparchive, phar

An archive entry name (ZipArchive getNameIndex / statIndex, or a PharData entry) is used to build the destination of a created file or directory. A crafted entry such as ../../public/shell.php writes outside the extraction folder (Zip Slip). Take basename(), or realpath the target and check it still starts with the extraction directory before writing.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [reads](https://keryn.dev/docs/edges/edge-kinds/#reads) one thing reads from another.

[CWE-22](https://keryn.dev/taxonomies/cwe/#cwe-22-path-traversal) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** For each entry compute $target = $dest . DIRECTORY\_SEPARATOR . $name, create the parent directory, then require str\_starts\_with(realpath(dirname($target)), realpath($dest) . DIRECTORY\_SEPARATOR) before file\_put\_contents. Skip entries with absolute paths. Or use basename($name) and flatten the archive.

**References:** [https://www.php.net/manual/en/ziparchive.getnameindex.php](https://www.php.net/manual/en/ziparchive.getnameindex.php) · [https://www.php.net/manual/en/ziparchive.statindex.php](https://www.php.net/manual/en/ziparchive.statindex.php) · [https://www.php.net/manual/en/class.phardata.php](https://www.php.net/manual/en/class.phardata.php) · [https://www.php.net/manual/en/function.realpath.php](https://www.php.net/manual/en/function.realpath.php)

## keryn.general.php.zip-extractto-uploaded-archive-audit

`warning` php php, ziparchive, laravel, symfony

ZipArchive::extractTo() runs on an archive that was opened from an uploaded file. Confirm the entries are validated first (names, symlinks, number and size of files) and that the destination is not web-reachable; the archive content is attacker-controlled.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [writes](https://keryn.dev/docs/edges/edge-kinds/#writes) one thing writes to another.

[CWE-22](https://keryn.dev/taxonomies/cwe/#cwe-22-path-traversal) [CWE-409](https://keryn.dev/rules/?cwe=CWE-409) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Loop over the entries first: reject names that are absolute, contain .. or end in an executable extension, and cap numFiles and the total uncompressed size from statIndex. Pass only the validated names as the second argument of extractTo($dest, $names). Extract into a private directory, not under the web root.

**References:** [https://www.php.net/manual/en/ziparchive.extractto.php](https://www.php.net/manual/en/ziparchive.extractto.php) · [https://www.php.net/manual/en/ziparchive.getnameindex.php](https://www.php.net/manual/en/ziparchive.getnameindex.php) · [https://www.php.net/manual/en/ziparchive.statindex.php](https://www.php.net/manual/en/ziparchive.statindex.php)
