A security review of a WordPress plugin with PHPCS and Semgrep
Most vulnerabilities in WordPress plugins belong to a few patterns: output without escaping, input without sanitizing, a missing nonce or capability check, SQL built from variables, a file path from a request. Static analysis tools find many of them in minutes, before a human review starts. Two tools cover the WordPress-specific part well: PHP_CodeSniffer with the WordPress Coding Standards’ security sniffs, and Semgrep with rules for data flowing from a request into a dangerous function.
Problem: An agency takes over a custom plugin of 15,000 lines and has to judge its security before the next release. A manual review of every file would take days, and the team does not know where to start.
Solution: Run PHPCS with the WordPress.Security and WordPress.DB sniffs to list unescaped output, unsanitized input, missing nonce checks and unprepared queries; run Semgrep with a taint rule from $_GET/$_POST/$_REQUEST to SQL, file and command functions; review the findings by risk, and add both tools to CI so new code is checked on every pull request.
PHPCS with the WordPress security sniffs
composer require --dev squizlabs/php_codesniffer wp-coding-standards/wpcs dealerdirect/phpcodesniffer-composer-installer
<?xml version="1.0"?>
<!-- phpcs-security.xml -->
<ruleset name="HA security">
<file>./</file>
<exclude-pattern>vendor/*</exclude-pattern>
<exclude-pattern>node_modules/*</exclude-pattern>
<rule ref="WordPress.Security"/>
<rule ref="WordPress.DB.PreparedSQL"/>
<rule ref="WordPress.DB.PreparedSQLPlaceholders"/>
<rule ref="WordPress.WP.Capabilities"/>
<config name="minimum_wp_version" value="6.4"/>
</ruleset>
vendor/bin/phpcs --standard=phpcs-security.xml --report=summary vendor/bin/phpcs --standard=phpcs-security.xml --report=full includes/admin/
The sniffs that matter most:
WordPress.Security.EscapeOutput:echoof a variable withoutesc_html(),esc_attr(),wp_kses_post().WordPress.Security.ValidatedSanitizedInput: superglobals used withoutsanitize_*()orwp_unslash().WordPress.Security.NonceVerification:$_POSTprocessed withoutcheck_admin_referer()/wp_verify_nonce()in the same function.WordPress.DB.PreparedSQL: variables in SQL strings passed to$wpdbwithoutprepare().
The sniffs work per function and report patterns, not proven bugs: a value escaped in another function is still reported. Read each finding; mark checked false positives with // phpcs:ignore and a reason.
Semgrep for data flow
# .semgrep/wp-taint.yml
rules:
- id: ha-request-to-sql
mode: taint
languages: [php]
severity: ERROR
message: Request data reaches a SQL query without $wpdb->prepare()
pattern-sources:
- pattern-either:
- pattern: $_GET
- pattern: $_POST
- pattern: $_REQUEST
- pattern: $_COOKIE
pattern-sanitizers:
- pattern: $WPDB->prepare(...)
- pattern: absint(...)
- pattern: intval(...)
pattern-sinks:
- pattern: $WPDB->query(...)
- pattern: $WPDB->get_results(...)
- pattern: $WPDB->get_var(...)
- pattern: $WPDB->get_row(...)
- id: ha-request-to-file
mode: taint
languages: [php]
severity: ERROR
message: Request data reaches a file function
pattern-sources:
- pattern-either: [ { pattern: $_GET }, { pattern: $_POST }, { pattern: $_REQUEST } ]
pattern-sinks:
- pattern-either:
- pattern: include $X
- pattern: require $X
- pattern: file_get_contents(...)
- pattern: unlink(...)
- pattern: readfile(...)
semgrep scan --config .semgrep/ --config p/php
Taint mode follows a value through assignments and function calls inside a file: a $_GET['id'] that ends in $wpdb->get_results() four lines later without passing prepare() is reported, even if the variable was renamed. The public p/php ruleset adds general PHP rules (unserialize, eval, command execution).
Review by risk
Start with what an anonymous visitor can reach: wp_ajax_nopriv_* handlers, REST routes with __return_true permissions, shortcodes and front-end forms. Then authenticated admin code, where the main risks are missing capability checks (any logged-in subscriber calling an admin AJAX action) and CSRF. The basics for fixing what the tools find: prepared statements and escaping and nonces and sanitizing in AJAX handlers.
NOTE: Tools find patterns, not logic flaws: an endpoint that lets a customer read another customer’s order because it checks login but not ownership passes every sniff. Keep a manual review of every permission check in the list of entry points. RIPS, a PHP analyzer often named in older articles, is no longer available as a standalone product.