winplaybox / composer-plugin-guard
Offline CI audit of the locked Composer plugin set against a reviewed allowlist, with baselines, type-change detection and SARIF output.
Package info
github.com/winplaybox-npm/composer-plugin-guard
pkg:composer/winplaybox/composer-plugin-guard
Requires
- php: >=8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.75
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Offline CI check of the locked Composer plugin set against a reviewed allowlist. It runs before composer install, so a transitive dependency cannot quietly become an executable Composer plugin.
It reads composer.json and composer.lock as data only. Composer, plugins and scripts never run, and the tool makes no network requests.
Why
Composer plugins ("type": "composer-plugin") run code inside Composer on every install and update. Composer's config.allow-plugins is the real gate, and this tool complements it:
- It fails CI when a new plugin appears in the lock file, or when a package changes type from library to plugin.
- It flags
allow-plugins: true, cross-vendor wildcards, and plugins with no decision. - It keeps one reviewed policy in version control, which is useful for open-source projects and for teams without Private Packagist plugin restrictions.
It is not a vulnerability scanner or malware detector. Use composer audit and Composer's dependency policies for that.
Install
composer require --dev winplaybox/composer-plugin-guard
Requires PHP 8.2+. Reads Composer 2.2+ lock files.
Usage
vendor/bin/composer-plugin-guard check --locked # uses .composer-plugin-guard.json if present vendor/bin/composer-plugin-guard check --policy ci/plugins.json --format sarif -o plugins.sarif vendor/bin/composer-plugin-guard check --report-only # onboarding: never fail vendor/bin/composer-plugin-guard check --write-baseline # record reviewed package types
41 locked package(s), 2 plugin(s)
plugin composer/installers v2.3.0 (composer-plugin)
plugin php-http/discovery 1.20.0 (composer-plugin)
error[CPG003] dependency-plugin: composer.lock: php-http/discovery changed type from "library" (1.14.3) to "composer-plugin" (1.20.0) and can now execute during Composer runs
note[CPG010] root-script: composer.json: root script "post-install-cmd" defines 1 command(s) and runs automatically on this Composer event; root scripts are your own code, unlike dependency plugins
1 error(s), 0 warning(s), 1 note(s)
Exit codes: 0 ok, 1 findings at or above --fail-on (default error), 2 usage or input error (missing or malformed files).
Without a policy file every plugin is reported as a warning (CPG001), so the first run does not fail. Add a policy to enforce the allowlist.
Since the tool only reads files, run it before composer install in CI:
- run: composer global require winplaybox/composer-plugin-guard - run: ~/.composer/vendor/bin/composer-plugin-guard check --locked --format sarif -o plugins.sarif - uses: github/codeql-action/upload-sarif@v4 if: always() with: sarif_file: plugins.sarif - run: composer install
Policy (.composer-plugin-guard.json)
{
"version": 1,
"allow": ["composer/installers", "php-http/discovery", "phpstan/*"],
"deny": ["evil-vendor/*"],
"wildcards": "report",
"baseline": ".composer-plugin-guard.baseline.json",
"includeDev": true,
"reportScripts": true,
"reportRepositories": true,
"severity": { "CPG004": "error", "CPG010": "off" }
}
allow/deny: exactvendor/packagenames, or wildcards limited to one literal vendor (acme/*,acme/composer-*).*and*/xare rejected. Deny wins over allow.wildcards: how broadallow-pluginswildcards in composer.json are treated:report(warning),deny(error) orallow(ignored).baseline: relative to the policy file. Write it with--write-baselineand commit it.severity: per-ruleerror,warning,noteoroff.- Unknown keys and malformed entries are rejected.
Rules
| ID | Name | Category | Default |
|---|---|---|---|
| CPG001 | plugin-not-allowed | dependency-plugin | error (warning without a policy) |
| CPG002 | plugin-denied | dependency-plugin | error |
| CPG003 | plugin-type-changed | dependency-plugin | error |
| CPG004 | new-plugin | dependency-plugin | warning |
| CPG005 | allow-plugins-true | configuration | error |
| CPG006 | broad-wildcard | configuration | warning |
| CPG007 | missing-decision | configuration | warning |
| CPG008 | blocked-by-composer | configuration | note |
| CPG009 | custom-repository | repository | note |
| CPG010 | root-script | root-script | note |
| CPG011 | legacy-installer | dependency-plugin | warning |
| CPG012 | baseline-missing | configuration | note |
| CPG013 | stale-decision | configuration | note |
| CPG014 | plugin-removed | dependency-plugin | note |
IDs are stable. JSON and SARIF findings include rule ID, category, severity, file, package, version and type. Output is sorted, so it is deterministic.
Execution surfaces
- Dependency plugins (
composer-plugin, legacycomposer-installer) run code fromvendor/inside Composer whenallow-pluginspermits it. This is the main target of the tool. - Root scripts (
scriptsin your own composer.json) run on Composer events. They are reported as notes for review, with event name and command count only. Script bodies are never printed. - Custom repositories (
vcs,path,artifact,package, non-Packagistcomposer) decide where code comes from. Credentials in URLs are masked.
Baseline review
- Run
--write-baselineand commit the file. - Each PR that adds a plugin (CPG004) or turns a library into a plugin (CPG003) is reported.
- After reviewing, update
allowand re-run--write-baselinein the same PR.
Library
use Winplaybox\ComposerPluginGuard\{Audit, Policy, Severity}; $report = Audit::run('composer.json', 'composer.lock', Policy::fromFile('.composer-plugin-guard.json')); if ($report->failsAt(Severity::Error)) { echo $report->toText(); }
False positives
- A plugin you trust: add it to
allow. - A rule you don't need: set it to
offunderseverity. - Dev-only plugins: set
"includeDev": falseor pass--no-dev.
Safety
- Parses JSON with size (32 MiB) and depth limits, and rejects malformed or ambiguous lock files, such as a package locked twice.
- No subprocesses, no network access, no telemetry, and no environment variables are read. A test checks the source for process and network calls.
- The only file it writes is the baseline, and only with
--write-baseline.
License
MIT