WordPress / WordPress/WordPress-Coding-Standards
False positive: sanitize_html_class() incorrectly flagged for missing unslash
Nobody has claimed this yet.
- 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
developbranch of WordPressCS.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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