WordPress / WordPress/WordPress-Coding-Standards

False positive: sanitize_html_class() incorrectly flagged for missing unslash

Open Beginner friendly
#2,516 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Extra Type: Bug
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

Bug Description

When using sanitize_html_class() to sanitize an HTML class, PHP_CodeSniffer reports that the passed variable is not unslashed before sanitization. However, sanitize_html_class() strips the string down to A-Z, a-z, 0-9, _, and - (code reference), and therefore it should be listed as an unslashing sanitizing function, similar to sanitize_key(). Instead, it is currently listed as a general sanitizing function.

Minimal Code Snippet

The issue happens when running this command:

phpcs --standard=WordPress --sniffs=WordPress.Security.ValidatedSanitizedInput test.php

... over a file containing this code:

if (! isset($_GET['html_class']) || ! is_string($_GET['html_class'])) {
	return;
}

$htmlClass = sanitize_html_class($_GET['html_class']);

Error Code

WordPress.Security.ValidatedSanitizedInput.MissingUnslash

Environment

Question Answer
PHP version 8.2.4
PHP_CodeSniffer version 3.11.2
WordPressCS version 3.1.0
PHPCSUtils version 1.0.12
PHPCSExtra version 1.2.1
WordPressCS install type Composer project local
IDE (if relevant) N/A

Tested Against develop Branch?

  • I have verified the issue still exists in the develop branch of WordPressCS.

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 in WordPress/Helpers/SanitizationHelperTrait.php, comparing the unslashing sanitizing functions near lines 139-149 with the general sanitizing functions near line 94. Reproduce the report with the provided phpcs command and PHP snippet. Done means sanitize_html_class() no longer triggers WordPress.Security.ValidatedSanitizedInput.MissingUnslash.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.