PHP › Static Analysis (PHPStan & Psalm)

How does Psalm's concept of taint analysis help detect potential security vulnerabilities like SQL injection or cross site scripting during static analysis?

Intermediate

Answer

Taint analysis tracks how data flows through an application starting from an untrusted source, such as user submitted form input, following that data through every function call and variable assignment it passes through, and flags a potential security issue if that tainted, untrusted data ever reaches a sensitive destination, called a sink, such as being directly concatenated into a raw SQL query or echoed directly into HTML output, without first being properly sanitized or escaped somewhere along that path.
vendor/bin/psalm --taint-analysis
Real-world example A security conscious team runs Psalm's taint analysis feature as part of their continuous integration pipeline, automatically catching a case where a developer accidentally passed raw, unescaped user input directly into an HTML template without first sanitizing it.

Common follow-ups: Can taint analysis realistically catch every possible security vulnerability in an application?;How does a team properly mark a specific data sanitization function as safely removing the taint from a value?