WordPress / WordPress/WordPress-Coding-Standards

Custom sanitizing rules ignored

Open
#1,766 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Helpers Focus: Security Type: Enhancement
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

Bug Description

When I run vendor/bin/phpcs class-foo.php, I get this error:

------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------
 11 | ERROR | Detected usage of a non-sanitized input variable: $_POST['foo']
------------------------------------------------------------------------------

I would expect the error to be suppressed because sanitize_promos_array is whitelisted.

Minimal Code Snippet

<?php

class Foo {

	public static function sanitize_promos_array( $input ) {
		// sanitize me
		return $input;
	}

	public static function bar() {
		$taxonomies = isset( $_POST['foo'] ) ? self::sanitize_promos_array( wp_unslash( $_POST['foo'] ) ) : []; // phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected,WordPress.Security.NonceVerification.Missing
	}

}

phpcs.xml:

<?xml version="1.0"?>
<ruleset name="MyRuleSet">
	<rule ref="WordPress-Extra" />
	<rule ref="WordPress.Security.ValidatedSanitizedInput">
		<properties>
			<property name="customSanitizingFunctions" type="array">
				<element value="sanitize_promos_array" />
				<element value="sanitize_announcements_array" />
			</property>
		</properties>
	</rule>
</ruleset>

Environment

Question Answer
PHP version 7.3.3
PHP_CodeSniffer version 3.4.2
WPCS version 2.1.1
WPCS install type Composer project local

Additional Context (optional)

This worked fine with WPCS 1.x. The rule was defined like so:
	<rule ref="WordPress.Security.ValidatedSanitizedInput">
		<properties>
			<property name="customSanitizingFunctions" value="sanitize_promos_array,sanitize_announcements_array" type="array" />
		</properties>
	</rule>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided vendor/bin/phpcs command with the phpcs.xml configuration and inspect the implementation of WordPress.Security.ValidatedSanitizedInput. Trace how the customSanitizingFunctions property is read and applied, then add or update a regression test showing that sanitize_promos_array suppresses the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.