Skip to content

Commit 19e45db

Browse files
authored
Merge pull request #1686 from WordPress-Coding-Standards/feature/validatedsanitizedinput-allow-for-other-unslashing-functions
ValidatedSanitizedInput: allow for more unslashing functions
2 parents 363ff11 + 2ac765f commit 19e45db

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

WordPress/Sniff.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ abstract class Sniff implements PHPCS_Sniff {
315315
'sizeof' => true,
316316
);
317317

318+
/**
319+
* Functions which unslash the data passed to them.
320+
*
321+
* @since 2.1.0
322+
*
323+
* @var array
324+
*/
325+
protected $unslashingFunctions = array(
326+
'stripslashes_deep' => true,
327+
'stripslashes_from_strings_only' => true,
328+
'wp_unslash' => true,
329+
);
330+
318331
/**
319332
* List of PHP native functions to test the type of a variable.
320333
*
@@ -1801,8 +1814,8 @@ protected function is_safe_casted( $stackPtr ) {
18011814
* @since 0.5.0
18021815
*
18031816
* @param int $stackPtr The index of the token in the stack.
1804-
* @param bool $require_unslash Whether to give an error if wp_unslash() isn't
1805-
* used on the variable before sanitization.
1817+
* @param bool $require_unslash Whether to give an error if no unslashing function
1818+
* is used on the variable before sanitization.
18061819
*
18071820
* @return bool Whether the token being sanitized.
18081821
*/
@@ -1833,10 +1846,10 @@ protected function is_sanitized( $stackPtr, $require_unslash = false ) {
18331846
return true;
18341847
}
18351848

1836-
$valid_functions = $this->sanitizingFunctions;
1837-
$valid_functions += $this->unslashingSanitizingFunctions;
1838-
$valid_functions += $this->arrayWalkingFunctions;
1839-
$valid_functions['wp_unslash'] = true;
1849+
$valid_functions = $this->sanitizingFunctions;
1850+
$valid_functions += $this->unslashingSanitizingFunctions;
1851+
$valid_functions += $this->unslashingFunctions;
1852+
$valid_functions += $this->arrayWalkingFunctions;
18401853

18411854
$functionPtr = $this->is_in_function_call( $stackPtr, $valid_functions );
18421855

@@ -1851,12 +1864,15 @@ protected function is_sanitized( $stackPtr, $require_unslash = false ) {
18511864

18521865
$functionName = $this->tokens[ $functionPtr ]['content'];
18531866

1854-
// Check if wp_unslash() is being used.
1855-
if ( 'wp_unslash' === $functionName ) {
1867+
// Check if an unslashing function is being used.
1868+
if ( isset( $this->unslashingFunctions[ $functionName ] ) ) {
18561869

18571870
$is_unslashed = true;
18581871

1859-
unset( $valid_functions['wp_unslash'] );
1872+
// Remove the unslashing functions.
1873+
$valid_functions = array_diff_key( $valid_functions, $this->unslashingFunctions );
1874+
1875+
// Check is any of the remaining (sanitizing) functions is used.
18601876
$higherFunctionPtr = $this->is_in_function_call( $functionPtr, $valid_functions );
18611877

18621878
// If there is no other valid function being used, this value is unsanitized.
@@ -1909,7 +1925,7 @@ protected function is_sanitized( $stackPtr, $require_unslash = false ) {
19091925
}
19101926

19111927
/**
1912-
* Add an error for missing use of wp_unslash().
1928+
* Add an error for missing use of unslashing.
19131929
*
19141930
* @since 0.5.0
19151931
*
@@ -1918,7 +1934,7 @@ protected function is_sanitized( $stackPtr, $require_unslash = false ) {
19181934
public function add_unslash_error( $stackPtr ) {
19191935

19201936
$this->phpcsFile->addError(
1921-
'Missing wp_unslash() before sanitization.',
1937+
'%s data not unslashed before sanitization. Use wp_unslash() or similar',
19221938
$stackPtr,
19231939
'MissingUnslash',
19241940
array( $this->tokens[ $stackPtr ]['content'] )

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,12 @@ function test_null_coalesce_equals_validation() {
322322
$key = sanitize_text_field( wp_unslash( $_POST['key'] ) ); // OK, validated via null coalesce equals.
323323
$another_key = sanitize_text_field( wp_unslash( $_POST['another_key'] ) ); // Bad, not validated, different key.
324324
}
325+
326+
function test_using_different_unslashing_functions() {
327+
if ( ! isset( $_GET['test'] ) ) {
328+
return;
329+
}
330+
331+
$sane = sanitize_text_field(stripslashes_deep($_GET['test'])); // Ok.
332+
$sane = sanitize_text_field( stripslashes_from_strings_only( $_GET['test'] ) ); // OK.
333+
}

0 commit comments

Comments
 (0)