Skip to content

Commit

Permalink
Add Theme forbidden functions sniff + unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Jul 27, 2016
1 parent 5d60b7b commit fd3ae5b
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
4 changes: 4 additions & 0 deletions WordPress-Theme/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
<!-- No hard coding of scripts and styles. Everything should be enqueued. -->
<rule ref="WordPress.WP.EnqueuedResources" />


<rule ref="WordPress.PHP.DiscouragedFunctions"/>


</ruleset>
72 changes: 72 additions & 0 deletions WordPress/Sniffs/Theme/RestrictedPHPFunctionsSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* WordPress_Sniffs_Theme_RestrictedPHPFunctionsSniff.
*
* Forbids the use of certain exec and obfuscation functions within Themes.
*
* @category Theme
* @package PHP_CodeSniffer
* @author Juliette Reinders Folmer <wpplugins_nospam@adviesenzo.nl>
*/
class WordPress_Sniffs_Theme_RestrictedPHPFunctionsSniff extends WordPress_Sniffs_Functions_FunctionRestrictionsSniff {

/**
* Groups of functions to restrict
*
* Example: groups => array(
* 'lambda' => array(
* 'type' => 'error' | 'warning',
* 'message' => 'Use anonymous functions instead please!',
* 'functions' => array( 'eval', 'create_function' ),
* )
* )
*
* @return array
*/
public function getGroups() {
return array(

'eval' => array(
'type' => 'error',
'message' => '%s() is not allowed.',
'functions' => array(
'eval',
),
),

'system_calls' => array(
'type' => 'error',
'message' => 'PHP system calls are often disabled by server admins and should not be in themes. Found %s.',
'functions' => array(
'exec',
'passthru',
'proc_open',
'shell_exec',
'system',
'popen',
),
),

'ini_set' => array(
'type' => 'error',
'message' => '%s is prohibited, themes should not change server PHP settings.',
'functions' => array(
'ini_set',
),
),

'obfuscation' => array(
'type' => 'error',
'message' => '%s() is not allowed.',
'functions' => array(
'base64_decode',
'base64_encode',
'convert_uudecode',
'convert_uuencode',
'str_rot13',
),
),
);

}
} // end class
20 changes: 20 additions & 0 deletions WordPress/Tests/Theme/RestrictedPHPFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

// Obfuscation and evil functions.
base64_decode( 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==');
base64_encode( 'This is an encoded string' );
convert_uudecode( "+22!L;W9E(%!(4\"$`\n`" );
convert_uuencode( "test\ntext text\r\n" );
str_rot13( 'The quick brown fox jumps over the lazy dog.' );
eval( "\$str = \"$str\";" );

// Themes should not change server PHP settings.
ini_set( 'memory_limit' );

// PHP system calls are often disabled by server admins and should not be in themes.
exec( 'whoami' );
passthru( 'cat myfile.zip', $err );
$process = proc_open( 'php', $descriptorspec, $pipes, $cwd, $env );
$output = shell_exec( 'ls -lart' );
$last_line = system( 'ls', $retval );
$handle = popen( '/bin/ls', 'r' );
55 changes: 55 additions & 0 deletions WordPress/Tests/Theme/RestrictedPHPFunctionsUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Unit test class for the Theme_RestrictedPHPFunctions sniff.
*
* A sniff unit test checks a .inc file for expected violations of a single
* coding standard. Expected errors and warnings are stored in this class.
*
* @category Theme
* @package PHP_CodeSniffer
* @author Juliette Reinders Folmer <wpplugins_nospam@adviesenzo.nl>
*/
class WordPress_Tests_Theme_RestrictedPHPFunctionsUnitTest extends AbstractSniffUnitTest {

/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array(int => int)
*/
public function getErrorList() {
return array(
4 => 1,
5 => 1,
6 => 1,
7 => 1,
8 => 1,
9 => 1,
12 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
19 => 1,
20 => 1,
);

} // end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array(int => int)
*/
public function getWarningList() {
return array();

} // end getWarningList()

} // end class

0 comments on commit fd3ae5b

Please sign in to comment.