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

Modularizing the discouraged functions #633

Merged
merged 8 commits into from
Dec 30, 2016
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
11 changes: 10 additions & 1 deletion WordPress-Extra/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- Generic PHP best practices.
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/pull/382 -->
<rule ref="Generic.PHP.DeprecatedFunctions"/>
<rule ref="WordPress.WP.DeprecatedFunctions"/>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="Generic.Functions.CallTimePassByReference"/>
<rule ref="Generic.Formatting.DisallowMultipleStatements"/>
Expand Down Expand Up @@ -34,7 +35,15 @@
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/73 -->
<rule ref="WordPress.CSRF.NonceVerification" />

<rule ref="WordPress.PHP.DiscouragedFunctions"/>
<rule ref="WordPress.PHP.DevelopmentFunctions"/>
<rule ref="WordPress.PHP.DiscouragedPHPFunctions">
<!-- WP core still supports PHP 5.2+ -->
<exclude name="WordPress.WP.DiscouragedFunctions.create_function" />
</rule>
<rule ref="WordPress.PHP.RestrictedFunctions"/>
<rule ref="WordPress.WP.DeprecatedFunctions"/>
<rule ref="WordPress.WP.AlternativeFunctions"/>
<rule ref="WordPress.WP.DiscouragedFunctions"/>

<!-- Scripts & style should be enqueued.
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/35 -->
Expand Down
38 changes: 38 additions & 0 deletions WordPress-VIP/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,45 @@
<!-- https://vip.wordpress.com/documentation/code-review-what-we-look-for/#commented-out-code-debug-code-or-output -->
<rule ref="Squiz.PHP.CommentedOutCode" />

<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#eval-and-create_function -->
<rule ref="WordPress.PHP.RestrictedFunctions" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The links justify adding this sniff for the eval and runtime_configuration sections.
Additional links to the VIP documentation should be added for the newly introduced system_calls and obfuscation sections. If these are not documented, they should be excluded from the ruleset or added to the VIP documentation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of us can update the VIP documentation. There is a check in the VIP scanner that does the same thing that could backup including the two new sections. https://github.com/Automattic/vip-scanner/blob/master/vip-scanner/checks/ForbiddenPHPFunctionsCheck.php


<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#eval-and-create_function -->
<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#serializing-data -->
<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#encoding-values-used-when-creating-a-url-or-passed-to-add_query_arg -->
<!-- https://github.com/Automattic/vip-scanner/blob/master/vip-scanner/checks/ForbiddenPHPFunctionsCheck.php -->
<rule ref="WordPress.PHP.DiscouragedPHPFunctions">
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/pull/633#issuecomment-266634811 -->
<exclude name="WordPress.PHP.DiscouragedPHPFunctions.obfuscation" />
</rule>
<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#settings-alteration -->
<rule ref="WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration">
<type>error</type>
</rule>

<!-- https://vip.wordpress.com/documentation/code-review-what-we-look-for/#commented-out-code-debug-code-or-output -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link should probably also be added to cover the prevent_path_disclosure section: https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#settings-alteration

<rule ref="WordPress.PHP.DevelopmentFunctions" />
<rule ref="WordPress.PHP.DevelopmentFunctions.error_log">
<type>error</type>
</rule>
<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#settings-alteration -->
<rule ref="WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure">
<type>error</type>
</rule>

<!-- https://vip.wordpress.com/documentation/code-review-what-we-look-for/#using-in_array-without-strict-parameter -->
<rule ref="WordPress.PHP.StrictInArray" />

<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#use-wp_parse_url-instead-of-parse_url -->
<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#use-wp_json_encode-over-json_encode -->
<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#filesystem-writes -->
<!-- https://vip.wordpress.com/documentation/vip/code-review-what-we-look-for/#remote-calls -->
<rule ref="WordPress.WP.AlternativeFunctions" />
<!-- VIP recommends other functions -->
<rule ref="WordPress.WP.AlternativeFunctions.curl">
<message>Using cURL functions is highly discouraged within VIP context. Check (Fetching Remote Data) on VIP Documentation.</message>
</rule>
<rule ref="WordPress.WP.AlternativeFunctions.file_get_contents">
<message>%s() is highly discouraged, please use vip_safe_wp_remote_get() instead.</message>
</rule>
</ruleset>
2 changes: 1 addition & 1 deletion WordPress/AbstractFunctionRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function register() {
* Set up the regular expressions for each group.
*
* @param string $key The group array index key where the input for the regular expression can be found.
* @return bool True is the groups were setup. False if not.
* @return bool True if the groups were setup. False if not.
*/
protected function setup_groups( $key ) {
// Prepare the function group regular expressions only once.
Expand Down
62 changes: 62 additions & 0 deletions WordPress/Sniffs/PHP/DevelopmentFunctionsSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* WordPress Coding Standard.
*
* @package WPCS\WordPressCodingStandards
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
* @license https://opensource.org/licenses/MIT MIT
*/

/**
* Restrict the use of various development functions.
*
* @package WPCS\WordPressCodingStandards
*
* @since 0.11.0
*/
class WordPress_Sniffs_PHP_DevelopmentFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff {

/**
* 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(
'error_log' => array(
'type' => 'warning',
'message' => '%s() found. Debug code should not normally be used in production.',
'functions' => array(
'error_log',
'var_dump',
'var_export',
'print_r',
'trigger_error',
'set_error_handler',
'debug_backtrace',
'debug_print_backtrace',
'wp_debug_backtrace_summary',
),
),

'prevent_path_disclosure' => array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This group is new to the extra ruleset. With that in mind and looking at the functions included, I believe this group should be moved back to the VIP.RestrictedFunctions or else, the error level should be lowered to warning (for VIP it can be changed back to error from the ruleset).

The error message itself is also worded too strongly for usage in extra as AFAICS there is no documentation that these functions are forbidden in plugins or core.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with changing it to warning but I believe they should belong in the category of development functions. We could always remove it from the extra ruelset. I don't think every plugin needs to use these functions and they definitely should not be used in themes.

By having the development functions together it can be easily enabled or disabled.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both trigger_error and set_error_handler would appear to me to be legitimate functions for production code (especially when integrating third-party PHP libraries).

'type' => 'warning',
'message' => '%s() can lead to full path disclosure.',
'functions' => array(
'error_reporting',
'phpinfo',
),
),

);
} // end getGroups()

} // end class
77 changes: 19 additions & 58 deletions WordPress/Sniffs/PHP/DiscouragedFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,26 @@
* @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' );
}

/**
* Discourages the use of various functions and suggests (WordPress) alternatives.
* Discourages the use of various native PHP functions and suggests alternatives.
*
* @package WPCS\WordPressCodingStandards
* @package WPCS\WordPressCodingStandards
*
* @since 0.1.0
* @since 0.10.0 The checks for the POSIX functions have been replaced by the stand-alone
* sniff WordPress_Sniffs_PHP_POSIXFunctionsSniff.
* @since 0.1.0
* @since 0.10.0 The checks for the POSIX functions have been replaced by the stand-alone
* sniff WordPress_Sniffs_PHP_POSIXFunctionsSniff.
* @deprecated 0.11.0 The checks for the PHP development functions have been replaced by the
* stand-alone sniff WordPress_Sniffs_PHP_DevelopmentFunctionsSniff.
* The checks for the WP deprecated functions have been replaced by the
* stand-alone sniff WordPress_Sniffs_WP_DeprecatedFunctionsSniff.
* The checks for the PHP functions which have a WP alternative has been replaced
* by the stand-alone sniff WordPress_Sniffs_WP_AlternativeFunctionsSniff.
* The checks for the WP discouraged functions have been replaced by the
* stand-alone sniff WordPress_Sniffs_WP_DiscouragedFunctionsSniff.
* The checks for the PHP discouraged functions have been replaced by the
* stand-alone sniff WordPress_Sniffs_WP_DiscouragedPHPFunctionsSniff.
* The check for the `register_globals` has been removed as there is no such
* function. To check for `register_globals` ini directive use
* PHPCompatibility_Sniffs_PHP_DeprecatedIniDirectivesSniff from wimg/PHPCompatibility.
*/
class WordPress_Sniffs_PHP_DiscouragedFunctionsSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff {

/**
* A list of forbidden functions with their alternatives.
*
* The value is NULL if no alternative exists. I.e. the
* function should just not be used.
*
* @var array(string => string|null)
*/
public $forbiddenFunctions = array(
// Development.
'print_r' => null,
'debug_print_backtrace' => null,
'var_dump' => null,
'var_export' => null,

// Discouraged.
'json_encode' => 'wp_json_encode',

// WordPress deprecated.
'find_base_dir' => 'WP_Filesystem::abspath',
'get_base_dir' => 'WP_Filesystem::abspath',
'dropdown_categories' => 'wp_link_category_checklist',
'dropdown_link_categories' => 'wp_link_category_checklist',
'get_link' => 'get_bookmark',
'get_catname' => 'get_cat_name',
'register_globals' => null,
'wp_setcookie' => 'wp_set_auth_cookie',
'wp_get_cookie_login' => null,
'wp_login' => 'wp_signon',
'get_the_attachment_link' => 'wp_get_attachment_link',
'get_attachment_icon_src' => 'wp_get_attachment_image_src',
'get_attachment_icon' => 'wp_get_attachment_image',
'get_attachment_innerHTML' => 'wp_get_attachment_image',

// WordPress discouraged.
'query_posts' => 'WP_Query',
'wp_reset_query' => 'wp_reset_postdata',
);

/**
* If true, an error will be thrown; otherwise a warning.
*
* @var bool
*/
public $error = false;

} // End class.
class WordPress_Sniffs_PHP_DiscouragedFunctionsSniff {}
108 changes: 108 additions & 0 deletions WordPress/Sniffs/PHP/DiscouragedPHPFunctionsSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* WordPress Coding Standard.
*
* @package WPCS\WordPressCodingStandards
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
* @license https://opensource.org/licenses/MIT MIT
*/

/**
* Discourages the use of various native PHP functions and suggests alternatives.
*
* @package WPCS\WordPressCodingStandards
*
* @since 0.11.0
*/
class WordPress_Sniffs_PHP_DiscouragedPHPFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff {

/**
* Groups of functions to discourage.
*
* 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(
'create_function' => array(
'type' => 'warning',
'message' => '%s() is discouraged, please use anonymous functions instead.',
'functions' => array(
'create_function',
),
),

'serialize' => array(
'type' => 'warning',
'message' => '%s() found. Serialized data has known vulnerability problems with Object Injection. JSON is generally a better approach for serializing data. See https://www.owasp.org/index.php/PHP_Object_Injection',
'functions' => array(
'serialize',
'unserialize',
),
),

'urlencode' => array(
'type' => 'warning',
'message' => '%s() should only be used when dealing with legacy applications rawurlencode() should now be used instead. See http://php.net/manual/en/function.rawurlencode.php and http://www.faqs.org/rfcs/rfc3986.html',
'functions' => array(
'urlencode',
),
),

'runtime_configuration' => array(
'type' => 'warning',
'message' => '%s() found. Changing configuration at runtime is rarely necessary.',
'functions' => array(
'error_reporting',
'ini_alter',
'ini_restore',
'ini_set',
'apache_setenv',
'putenv',
'set_include_path',
'restore_include_path',
// This alias was DEPRECATED in PHP 5.3.0, and REMOVED as of PHP 7.0.0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these last three functions included here because they are deprecated/removed of because they touch the runtime_configuration ?
Should they (also) be in a separate WordPress.PHP.DeprecatedFunctions sniff ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are included because they touch the runtime. For PHP compatibility we can use the PHPcompatibility sniffs.

'magic_quotes_runtime',
// Warning This function was DEPRECATED in PHP 5.3.0, and REMOVED as of PHP 7.0.0.
'set_magic_quotes_runtime',
// Warning This function was removed from most SAPIs in PHP 5.3.0, and was removed from PHP-FPM in PHP 7.0.0.
'dl',
),
),

'system_calls' => array(
'type' => 'warning',
'message' => '%s() found. PHP system calls are often disabled by server admins.',
'functions' => array(
'exec',
'passthru',
'proc_open',
'shell_exec',
'system',
'popen',
),
),

'obfuscation' => array(
'type' => 'warning',
'message' => '%s() can be used to obfuscate code which is strongly discouraged. Please verify that the function is used for benign reasons.',
'functions' => array(
'base64_decode',
'base64_encode',
'convert_uudecode',
'convert_uuencode',
'str_rot13',
),
),

);
} // end getGroups()

} // End class.
8 changes: 4 additions & 4 deletions WordPress/Sniffs/PHP/POSIXFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @package WPCS\WordPressCodingStandards
*
* @since 0.10.0 Previously this check was contained within WordPress_Sniffs_VIP_RestrictedFunctionsSniff
* and the WordPress_Sniffs_PHP_DiscouragedFunctionsSniff.
* and the WordPress_Sniffs_PHP_DiscouragedPHPFunctionsSniff.
*/
class WordPress_Sniffs_PHP_POSIXFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff {

Expand All @@ -38,7 +38,7 @@ public function getGroups() {
return array(
'ereg' => array(
'type' => 'error',
'message' => '%s has been deprecated since PHP 5.3 and removed in PHP 7.0, please use preg_match() instead.',
'message' => '%s() has been deprecated since PHP 5.3 and removed in PHP 7.0, please use preg_match() instead.',
'functions' => array(
'ereg',
'eregi',
Expand All @@ -48,7 +48,7 @@ public function getGroups() {

'ereg_replace' => array(
'type' => 'error',
'message' => '%s has been deprecated since PHP 5.3 and removed in PHP 7.0, please use preg_replace() instead.',
'message' => '%s() has been deprecated since PHP 5.3 and removed in PHP 7.0, please use preg_replace() instead.',
'functions' => array(
'ereg_replace',
'eregi_replace',
Expand All @@ -57,7 +57,7 @@ public function getGroups() {

'split' => array(
'type' => 'error',
'message' => '%s has been deprecated since PHP 5.3 and removed in PHP 7.0, please use explode(), str_split() or preg_split() instead.',
'message' => '%s() has been deprecated since PHP 5.3 and removed in PHP 7.0, please use explode(), str_split() or preg_split() instead.',
'functions' => array(
'split',
'spliti',
Expand Down
Loading