Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSystemWritesDisallow: Use the WPCS abstract function call sniff #800

Merged
merged 2 commits into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 66 additions & 58 deletions WordPress/Sniffs/VIP/FileSystemWritesDisallowSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
* @license https://opensource.org/licenses/MIT MIT
*/

if ( ! class_exists( 'Generic_Sniffs_PHP_ForbiddenFunctionsSniff', true ) ) {
throw new PHP_CodeSniffer_Exception( 'Class Generic_Sniffs_PHP_ForbiddenFunctionsSniff not found' );
}

/**
* Disallow Filesystem writes.
*
Expand All @@ -19,44 +15,10 @@
* @package WPCS\WordPressCodingStandards
*
* @since 0.3.0
* @since 0.11.0 Extends the WordPress_AbstractFunctionRestrictionsSniff instead of the
* Generic_Sniffs_PHP_ForbiddenFunctionsSniff.
*/
class WordPress_Sniffs_VIP_FileSystemWritesDisallowSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff {

/**
* A list of forbidden functions with their alternatives.
*
* The value is NULL if no alternative exists. IE, the
* function should just not be used.
*
* @var array(string => string|null)
*/
public $forbiddenFunctions = array(
'file_put_contents' => null,
'fwrite' => null,
'fputcsv' => null,
'fputs' => null,
'ftruncate' => null,
'link' => null,
'symlink' => null,
'mkdir' => null,
'rename' => null,
'rmdir' => null,
'tempnam' => null,
'touch' => null,
'unlink' => null,
'is_writable' => null,
'is_writeable' => null,
'lchgrp' => null,
'lchown' => null,
'fputcsv' => null,
'delete' => null,
'chmod' => null,
'chown' => null,
'chgrp' => null,
'chmod' => null,
'chmod' => null,
'flock' => null,
);
class WordPress_Sniffs_VIP_FileSystemWritesDisallowSniff extends WordPress_AbstractFunctionRestrictionsSniff {

/**
* If true, an error will be thrown; otherwise a warning.
Expand All @@ -66,28 +28,74 @@ class WordPress_Sniffs_VIP_FileSystemWritesDisallowSniff extends Generic_Sniffs_
public $error = true;

/**
* Generates the error or warning for this sniff.
* Groups of functions to restrict.
*
* Overloads parent addError method.
* Example: groups => array(
* 'lambda' => array(
* 'type' => 'error' | 'warning',
* 'message' => 'Use anonymous functions instead please!',
* 'functions' => array( 'eval', 'create_function' ),
* )
* )
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the forbidden function
* in the token array.
* @param string $function The name of the forbidden function.
* @param string $pattern The pattern used for the match.
*
* @return void
* @return array
*/
protected function addError( $phpcsFile, $stackPtr, $function, $pattern = null ) {
$data = array( $function );
$error = 'Filesystem writes are forbidden, you should not be using %s()';
public function getGroups() {
$groups = array(
'file_ops' => array(
'type' => 'error',
'message' => 'Filesystem writes are forbidden, you should not be using %s()',
'functions' => array(
'delete',
'file_put_contents',
'flock',
'fputcsv',
'fputs',
'fwrite',
'ftruncate',
'is_writable',
'is_writeable',
'link',
'rename',
'symlink',
'tempnam',
'touch',
'unlink',
),
),
'directory' => array(
'type' => 'error',
'message' => 'Filesystem writes are forbidden, you should not be using %s()',
'functions' => array(
'mkdir',
'rmdir',
),
),
'chmod' => array(
'type' => 'error',
'message' => 'Filesystem writes are forbidden, you should not be using %s()',
'functions' => array(
'chgrp',
'chown',
'chmod',
'lchgrp',
'lchown',
),
),
);

if ( true === $this->error ) {
$phpcsFile->addError( $error, $stackPtr, 'FileWriteDetected', $data );
} else {
$phpcsFile->addWarning( $error, $stackPtr, 'FileWriteDetected', $data );
/*
* Maintain old behaviour - allow for changing the error type from the ruleset
* using the `error` property.
*/
if ( false === $this->error ) {
foreach ( $groups as $group_name => $details ) {
$groups[ $group_name ]['type'] = 'warning';
}
}

}
return $groups;

} // End getGroups().

} // End class.
30 changes: 26 additions & 4 deletions WordPress/Tests/VIP/FileSystemWritesDisallowUnitTest.inc
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
<?php

file_put_contents( $file, $text, FILE_APPEND );
file_put_contents( $file, $text, FILE_APPEND ); // Bad.

$fp = fopen( $file, 'a+' );
stream_set_blocking( $fp, 0 );

while ( $count > $loop ) {
if ( flock( $fp, LOCK_EX ) ) {
fwrite( $fp, $text );
if ( flock( $fp, LOCK_EX ) ) { // Bad.
fwrite( $fp, $text ); // Bad.
}
flock( $fp, LOCK_UN );
flock( $fp, LOCK_UN ); // Bad.
}

fclose( $fp );

delete();
fputcsv();
fputs();
ftruncate();
is_writable();
is_writeable();
link();
rename();
symlink();
tempnam();
touch();
unlink();

mkdir();
rmdir();

chgrp();
chown();
chmod();
lchgrp();
lchown();
19 changes: 19 additions & 0 deletions WordPress/Tests/VIP/FileSystemWritesDisallowUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ public function getErrorList() {
9 => 1,
10 => 1,
12 => 1,
17 => 1,
18 => 1,
19 => 1,
20 => 1,
21 => 1,
22 => 1,
23 => 1,
24 => 1,
25 => 1,
26 => 1,
27 => 1,
28 => 1,
30 => 1,
31 => 1,
33 => 1,
34 => 1,
35 => 1,
36 => 1,
37 => 1,
);

}
Expand Down