perturbatio / phpstan-cwe
Identify *potential* CWE (Common Weakness Enumeration) vulnerabilities in your application
Requires
- php: ^8.3
- phpstan/phpstan: ^2.1
Requires (Dev)
- cweagans/composer-patches: ^2.0
- evankford/pest-plugin-mutate-lines: ^0.1.1
- larastan/larastan: ^3.0
- laravel/framework: ^13.0
- laravel/pint: ^1.32
- pestphp/pest: 5.*
- spatie/invade: ^2.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
PHPStan CWE is a PHPStan extension. It reports possible security weaknesses in PHP code and shows a severity, a probability score, a CWE reference, and a stable error identifier for each finding.
The package requires PHP 8.3 or later and PHPStan 2.1 or later in the 2.x series. Laravel is not required. Laravel-specific rules check supported Laravel code when you use them.
Severity describes possible harm. The probability score estimates confidence in the finding. It is not a measured probability of an attack. Each rule has its own supported cases and limitations. Review its documentation before you rely on a finding or treat the absence of a finding as proof of safety. See the rule documentation for supported cases and limitations.
Installation
Install the package with Composer:
composer require --dev perturbatio/phpstan-cwe
Add the extension to your phpstan.neon. Set paths to the code you want
PHPStan to analyse. The full extension enables all rules, including the
Laravel-specific checks.
includes: - vendor/perturbatio/phpstan-cwe/extension.neon parameters: level: 5 paths: - app - routes phpstanCwe: cwe306CriticalOperations: - App\Actions\DeleteAccount::__invoke
Replace the example operation with an exact function or method that your application requires callers to authenticate before it runs. The package cannot choose critical operations safely for every application. If you leave the list empty, CWE-306 reports a configuration error. See the CWE-306 Laravel rule document for wildcard patterns and other options.
Run PHPStan:
vendor/bin/phpstan analyse --configuration=phpstan.neon
The auth and auth:sanctum route middleware aliases are recognised by
default. cwe306MiddlewareAliases is for other aliases or an overridden
auth alias. cwe306GlobalMiddleware defaults to null (unknown); set it
only after you check the effective global middleware list.
minProbability defaults to 0, so PHPStan reports findings at every
probability score. Raise it to filter lower-confidence findings.
Configuration files
The default file (vendor/perturbatio/phpstan-cwe/extension.neon) enables all
rules.
To use one rule, include its public configuration file from config/ instead
of extension.neon. For example, this file enables the PHP HTML rule:
includes: - vendor/perturbatio/phpstan-cwe/config/cwe79.neon
Use config/cwe22.neon for native PHP file functions. Use
config/cwe22-laravel.neon to also check Laravel request input and supported
Laravel filesystem calls. See the CWE-22 rule document
for supported cases and known limits.
Rule configuration files are grouped as follows:
| Path | Purpose |
|---|---|
extension.neon |
Main entry point. Enables all rules. |
config/cwe*.neon |
Public entry points for individual rules. |
config/parameters.neon |
Shared defaults and parameter schema. |
config/rules/ |
Internal rule and collector service definitions. |
Use config/cwe95.neon to check PHP evaluation of PHP code including user provided input. Use
config/cwe95-laravel.neon to also check Laravel request input. See the
CWE-95 rule document for ratings, limitations,
and fingerprint-based ignore annotations.
Use config/cwe98.neon to check dynamic PHP include and require filenames.
See the CWE-98 rule document for ratings and
limitations.
Use config/cwe352-laravel.neon to check Laravel routes for missing CSRF
middleware. The rule reports uncertain cases at a lower probability score.
See the CWE-352 Laravel rule document
for supported route forms and limitations.
If your project does not use Laravel, select the rules you need instead of including the full extension.
Note
This package helps you find common CWE risks in your code. Use its checks as part of a broader security review.
Suppress reports from one rule while keeping all rules enabled
You cannot exclude a specific rule once loaded, so PHPStan still processes it. You can suppress the errors it produces.
In this case, add the following to your phpstan.neon
parameters: ignoreErrors: - identifier: phpstanCwe.cwe79 # ignore CWE-79 reports
This keeps all package rules enabled. A later version may add rules and findings. Review the release notes when you upgrade.
Setting Blade directories
The Blade rule scans resources/views and its child directories when the
directory exists. It runs when PHPStan analyses at least one PHP file.
Set other directories in phpstan.neon if necessary:
parameters: phpstanCwe: bladeDirectories: - resources/views - packages/shop/resources/views
Relative paths start at the current working directory. Set the list to
[] to stop the Blade scan.
To suppress a report for one Blade raw echo, put a Blade comment with the fingerprint from the report tip immediately before it:
{{-- @phpstan-ignore phpstanCwe.cwe79.<fingerprint> (This value is trusted.) --}} {!! $trustedHtml !!}
See the shared ignore-annotation guide.
An ignore annotation with no matching raw echo gets a report. See the Blade rule document for the exact placement.
For a step-by-step guide to adding a rule, see the CWE rule tutorial.
Current rules implemented:
| CWE | Description |
|---|---|
| CWE-22 | Path traversal |
| CWE-78 | Command injection |
| CWE-79 | Cross-site scripting (XSS) |
| CWE-89 | SQL injection |
| CWE-95 | PHP evaluation of user input |
| CWE-98 | Dynamic include/require filename |
| CWE-306 | Missing authentication for critical operations |
| CWE-352 | Cross-site request forgery in Laravel routes |
Development of this package was made possible thanks to LiberEat