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

Conversation

grappler
Copy link
Member

@grappler grappler commented Jul 25, 2016

This is an intial start of fixing #582 & #576

Do you think I am on the right path?

TODO

* @author Shady Sharaf <shady@x-team.com>
* @author Ulrich Pogson <ulrich@pogson.ch>
*/
class WordPress_Sniffs_PHP_DeprecatedFunctionsSniff extends WordPress_AbstractFunctionRestrictionsSniff {
Copy link
Member

Choose a reason for hiding this comment

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

FYI: This whole file can be removed as this is covered in #608.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I will rebase once it is merged.

@grappler
Copy link
Member Author

As I am going through DiscouragedFunctionsSniff I see a check for register_globals() but I cannot find any reference to that function in WordPress other than WP::register_globals. Is that what the sniff is trying to catch?

@grappler grappler force-pushed the feature/discouraged-functions branch 3 times, most recently from 4d753f5 to 0682c3c Compare September 28, 2016 04:30
@grappler
Copy link
Member Author

A quick update. I have separated the different functions. Currently Deprecated function are not done yet. Waiting for a response from #576 (comment)

Travis is failing becuase of another issue. My unit tests are passing.

This list of functions is still in WordPress_Sniffs_VIP_RestrictedFunctionsSniff. I think it should be moved out so that others have access to it but I am not sure where.

            'runtime_configuration' => array(
                'type'      => 'error',
                'message'   => '%s is prohibited, changing configuration at runtime is not allowed on VIP Production.',
                'functions' => array(
                    'dl',
                    'error_reporting',
                    'ini_alter',
                    'ini_restore',
                    'ini_set',
                    'magic_quotes_runtime',
                    'set_magic_quotes_runtime',
                    'apache_setenv',
                    'putenv',
                    'set_include_path',
                    'restore_include_path',
                ),
            ),

We also have a list of functions that are blocked by the theme review team that we could maybe add to the same sniff. https://github.com/WPTRT/WordPress-Coding-Standards/pull/10/files#diff-75f603ed5bb071632d90df9854203926R29

Would love some feedback! 😄

@JDGrimes
Copy link
Contributor

RE register_globals(): maybe that was a reference to this?

@grappler
Copy link
Member Author

So register_globals is a deprecated PHP ini config. I think it is best left of the check. Do we need to document this in the class changelog?

@jrfnl
Copy link
Member

jrfnl commented Sep 28, 2016

So register_globals is a deprecated PHP ini config. I think it is best left of the check. Do we need to document this in the class changelog?

As the WordPress_AbstractFunctionRestrictionsSniff class which is used as the base only checks for function calls, this should probably be removed - or better yet: be moved to a separate sniff which checks for ini directives.

@grappler
Copy link
Member Author

The check is already included in PHPCompatibility DeprecatedIniDirectivesSniff.php

What do you think of including the TRT restricted PHP functions in the PHP folder?

@jrfnl
Copy link
Member

jrfnl commented Sep 29, 2016

The check is already included in PHPCompatibility DeprecatedIniDirectivesSniff.php

The PHPCompatibility sniffs are a separate ruleset which does not get shipped with WPCS, so unless people add that to their custom ruleset, they won't get the benefit of it.

What do you think of including the TRT restricted PHP functions in the PHP folder?

You mean including the array from the TR sniff in the WordPress_Sniffs_PHP_RestrictedFunctionsSniff ? Possibly, but only if it doesn't impact any of the other WP rulesets.
I'd be more inclined to add the function groups each in a separate sniff file which could then go in the PHP folder (so it is easier to include them in the rulesets).

For that matter - the splitting out of the files as you're doing in this PR will also affect some rulesets, so you may want to add an action item to review & adjust the existing rulesets for the changes you are making.

@grappler
Copy link
Member Author

The PHPCompatibility sniffs are a separate ruleset which does not get shipped with WPCS, so unless people add that to their custom ruleset, they won't get the benefit of it.

I am not that keen on duplicating checks just so that we have backwards compatibility. I expect in most WP projects there is no php.ini file. This check was looking for a function called register_globals() and not an ini directive.

I'd be more inclined to add the function groups each in a separate sniff file which could then go in the PHP folder (so it is easier to include them in the rulesets).

This is what I meant but I am not sure do we want to create a separate sniff for each item or if we can bundle a few together.

so you may want to add an action item to review & adjust the existing rulesets for the changes you are making.

Thought of that, I added an action item. 😄

@jrfnl
Copy link
Member

jrfnl commented Sep 29, 2016

The PHPCompatibility sniffs are a separate ruleset which does not get shipped with WPCS, so unless people add that to their custom ruleset, they won't get the benefit of it.
I am not that keen on duplicating checks just so that we have backwards compatibility. I expect in most WP projects there is no php.ini file. This check was looking for a function called register_globals() and not an ini directive.

So the original sniff was wrong, but that doesn't mean it shouldn't be sniffed for. register_globals is evil and should not be touched.
Whether a project has a php.ini file is less relevant as a) those won't be parsed by PHPCS anyway and b) register_globals could - when it was still in PHP - be changed via ini_set() AFAICR which is what should be sniffed for.
Maybe leave it out of this PR, but open a new issue for this ?

@grappler grappler force-pushed the feature/discouraged-functions branch 6 times, most recently from ff995f4 to 03e5524 Compare September 29, 2016 06:19
@grappler
Copy link
Member Author

The rulesets should be should checking the same things are were doing before the functions were modularized.

I have place all of the restricted functions in WordPress.PHP.RestrictedFunctions. It is better to have all of the restricted functions together. If a ruleset does not need a certain check then it can still be excluded like so:

<rule ref="WordPress.WP.AlternativeFunctions">
    <properties>
        <property name="exclude" value="curl,file_get_contents"/>
    </properties>
</rule>

The only remaining thing are the deprecated functions which are being discussed in #576 (comment)

@grappler
Copy link
Member Author

I have added a start for a check for deprecated based on WPTT/WPThemeReview#77

@JDGrimes Would be interested on your inital feedback on the deprecated functions check. It is just a start.

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

Choose a reason for hiding this comment

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

Small typo here, "de" instead of "be".

@grappler grappler force-pushed the feature/discouraged-functions branch from 9ee9089 to 33b9489 Compare December 29, 2016 20:48
@grappler
Copy link
Member Author

Updated Summary of the PR

extra ruleset

The following "functions" previously being sniffed for have been removed:

  • register_globals (ini directive)

The following sniffs has been deprecated and the checks moved to other sniffs.

WordPress.PHP.DiscouragedFunctions
  • 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.

The following functions which were previously not sniffed for are being newly introduced into the extra ruleset:

  • WordPress.PHP.DevelopmentFunctions

    • section error_log: error_log(), trigger_error(), set_error_handler(), debug_backtrace(), wp_debug_backtrace_summary()
    • section prevent_path_disclosure: error_reporting(), phpinfo()
  • WordPress.PHP.DiscouragedPHPFunctions

    • create_function() - Excluded from the Extra ruleset
    • serialize(), unserialize()
    • urlencode()
  • WordPress.PHP.RestrictedFunctions

    • section eval: eval()
    • section runtime_configuration: dl, error_reporting, ini_alter, ini_restore, ini_set, magic_quotes_runtime, set_magic_quotes_runtime, apache_setenv, putenv, set_include_path, restore_include_path
    • section system_calls: exec, passthru, proc_open, shell_exec, system, popen
    • section obfuscation: base64_decode, base64_encode, convert_uudecode, convert_uuencode, str_rot13
  • WordPress.WP.DeprecatedFunctions

    • Lots of new functions, too many to list. By default, it is set to presume that a project will support the current WP version and up to three releases before.
  • WordPress.WP.AlternativeFunctions

    • curl_*()
    • parse_url()
    • file_get_contents()
    • section file_system_read: readfile, fopen, fsockopen, pfsockopen, fclose, fread, fwrite

VIP ruleset

The following functions which were previously not sniffed for are being newly introduced into the VIP ruleset:

  • WordPress.PHP.DevelopmentFunctions

    • section error_log: var_export(), debug_backtrace(), debug_print_backtrace(), wp_debug_backtrace_summary()
  • WordPress.PHP.RestrictedFunctions

    • section system_calls: exec, passthru, proc_open, shell_exec, system, popen
  • WordPress.WP.AlternativeFunctions

    • json_encode()
    • section file_system_read: readfile, fopen, fsockopen, pfsockopen, fclose, fread, fwrite

Changes to WordPress.VIP.RestrictedFunctions

  • The checks for create_function(), serialize()/unserialize() and urlencode have been moved to the stand-alone sniff WordPress_Sniffs_PHP_DiscouragedPHPFunctionsSniff.
    The checks for PHP developer functions, error_reporting and phpinfohave moved to the stand-alone sniff WordPress_Sniffs_PHP_DevelopmentFunctionsSnif.
  • The check for parse_url() and curl_* have been moved to the stand-alone WordPress_Sniffs_WP_AlternativeFunctionsSniff.
  • The check for eval() has been moved to the stand-alone sniff WordPress_Sniffs_PHP_RestrictedFunctionsSniff.

Also note that the WordPress.WP.DeprecatedFunctions sniff has not been added to the VIP ruleset.

@jrfnl
Copy link
Member

jrfnl commented Dec 29, 2016

Anyone else still wants to have a look at this before merging ? Please let me know, if not, I'll merge this tomorrow.

@jrfnl jrfnl merged commit a92b51b into WordPress:develop Dec 30, 2016
@grappler grappler deleted the feature/discouraged-functions branch December 31, 2016 08:03
@grappler grappler mentioned this pull request Jan 31, 2017
11 tasks
lkraav added a commit to lkraav/WordPress-Coding-Standards that referenced this pull request Mar 22, 2017
TODO
* @file_get_contents WordPress#633
lkraav added a commit to lkraav/WordPress-Coding-Standards that referenced this pull request Aug 4, 2017
TODO
* @file_get_contents WordPress#633
lkraav added a commit to lkraav/WordPress-Coding-Standards that referenced this pull request Nov 7, 2017
TODO
* @file_get_contents WordPress#633
lkraav added a commit to lkraav/WordPress-Coding-Standards that referenced this pull request Feb 15, 2018
TODO
* @file_get_contents WordPress#633
lkraav added a commit to lkraav/WordPress-Coding-Standards that referenced this pull request Feb 16, 2018
TODO
* @file_get_contents WordPress#633
lkraav added a commit to lkraav/WordPress-Coding-Standards that referenced this pull request Jul 28, 2018
TODO
* @file_get_contents WordPress#633
lkraav added a commit to lkraav/WordPress-Coding-Standards that referenced this pull request Sep 11, 2018
TODO
* @file_get_contents WordPress#633
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants