Skip to content

Flag restricted array members accessed with double-quoted keys - #908

Merged
GaryJones merged 1 commit into
developfrom
GaryJones/restricted-variables-double-quotes
Oct 10, 2026
Merged

GaryJones merged 1 commit into
developfrom
GaryJones/restricted-variables-double-quotes

Conversation

@GaryJones

Copy link
Copy Markdown
Contributor

WordPressVIPMinimum.Variables.RestrictedVariables warns about $_SERVER['REMOTE_ADDR'] and $_SERVER['HTTP_USER_AGENT'], but says nothing about $_SERVER["REMOTE_ADDR"] or {$_SERVER["HTTP_USER_AGENT"]} in a heredoc. They're the same variables, so the cache constraints warning should apply to both. I spotted this while reviewing #901.

The cause is in test_patterns(), which turns each configured pattern into a regex. Its second replacement, '[\'"]', was meant to be a character class matching either quote, but PCRE reads the [ and ] as the delimiters. It only ever matched a literal '" pair, so it never did anything. Fixing the delimiters alone wouldn't help either: rewriting the pattern's quotes to single quotes changes nothing when the patterns are already single-quoted and it's the code being checked that uses double quotes.

So quotes in a pattern now become ['"], matching either quote style in the code. This applies to every sniff built on AbstractVariableRestrictionsSniff, which within VIPCS is just RestrictedVariables.

Sites with double-quoted $_SERVER["REMOTE_ADDR"] or $_SERVER["HTTP_USER_AGENT"] will start seeing the warning they should always have had. Both ruleset fixtures already contained $_SERVER["REMOTE_ADDR"] under the ServerVariables section, and those lines now expect the extra warning too.

RestrictedVariables never flagged `$_SERVER["REMOTE_ADDR"]` or
`$_SERVER["HTTP_USER_AGENT"]`, only their single-quoted forms. The quote
normalisation in test_patterns() used `[` and `]` as regex delimiters
rather than as a character class, so it only ever matched a literal `'"`
pair and never ran. Even when fixed as intended, rewriting the pattern's
quotes to single quotes would not help, because the code being checked
is what uses the double quotes.

Quotes in a pattern now match either quote style. The ruleset fixtures
already contained `$_SERVER["REMOTE_ADDR"]` under ServerVariables, which
now also correctly raises the RestrictedVariables warning.
@GaryJones
GaryJones requested a review from a team as a code owner October 9, 2026 21:45
@GaryJones GaryJones added this to the 3.x milestone Oct 9, 2026
@GaryJones GaryJones self-assigned this Oct 9, 2026
@GaryJones
GaryJones merged commit d597080 into develop Oct 10, 2026
26 checks passed
@GaryJones
GaryJones deleted the GaryJones/restricted-variables-double-quotes branch October 10, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant