-
Notifications
You must be signed in to change notification settings - Fork 42
ProperEscapingFunction: Flag when esc_attr is being used outside of HTML attributes #624
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Rebecca, left some questions again for you to consider.
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
@rebeccahum Shall I continue with the questions based on the new code or shall we talk it through in a call ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Next round of questions for now, we can talk them through in a call next week if you like.
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
1b9c439
to
910c581
Compare
Ready for another round, @jrfnl! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Rebecca, as discussed in the call: well done! This is looking really good!
Just some nitpicks and minor cleanup left to do, but other than that, ready to merge!
General remark regarding the tests: I'm missing tests with a T_DOUBLE_QUOTED_STRING
token before the call to esc_*()
.
Not for this PR, but for a possible future iteration: using strip_quotes()
for all three checks and only when the token is a single/double quoted string.
WordPressVIPMinimum/Tests/Security/ProperEscapingFunctionUnitTest.inc
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Tests/Security/ProperEscapingFunctionUnitTest.inc
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Tests/Security/ProperEscapingFunctionUnitTest.inc
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
@jrfnl Besides having a hard time kicking the yoda habit, I think this should be good for one more review! |
…g concatenation operator
…logical order and added more tests
…tring tokens In the case that PHP adds another type of string token
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rebeccahum We're inching closer and closer.
Not for this PR, but for a possible future iteration: using strip_quotes() for all three checks and only when the token is a single/double quoted string.
As you've started to address this remark within this PR now anyway, it, unfortunately, has opened up the road to yet more changes which will be needed to finish that refactor off properly.
Let me know if you want to talk my current remarks through in another call. Or even if you'd want me to add a commit to the PR to address that last part for the attr_expects_url()
and the is_html_attr()
functions.
|
||
$echo_or_string_concat = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, $stackPtr - 1, null, true ); | ||
// Use $textStringTokens b/c heredoc and nowdoc tokens shouldn't be matched anyways. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Use $textStringTokens b/c heredoc and nowdoc tokens shouldn't be matched anyways. | |
// Use $textStringTokens b/c heredoc and nowdoc tokens will never be encountered in this context anyways. |
WordPressVIPMinimum/Sniffs/Security/ProperEscapingFunctionSniff.php
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Tests/Security/ProperEscapingFunctionUnitTest.inc
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Tests/Security/ProperEscapingFunctionUnitTest.inc
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Tests/Security/ProperEscapingFunctionUnitTest.inc
Outdated
Show resolved
Hide resolved
WordPressVIPMinimum/Tests/Security/ProperEscapingFunctionUnitTest.inc
Outdated
Show resolved
Hide resolved
…f.php Co-authored-by: Juliette <[email protected]>
…est.inc Co-authored-by: Juliette <[email protected]>
…est.inc Co-authored-by: Juliette <[email protected]>
…lash in $attr_endings and add more tests (courtesy of Juliette)
While the list of functions being looked for in the `$escaping_functions` property has been expanded, a lot of these were not being tested and when the tests are adjusted for this, it shows that the "using `esc_html` for attributes" check is not correctly applied to all relevant functions. This is caused by the preliminary check for whether to examine a function for escaping HTML attributes correctly still using a hard-coded reference to `esc_html` instead of using the list from the property. Fixed now. Tested by adjusting some existing tests.
As the other checks now each have a condition checking the `$escaping_type` in the `process()` method, having that condition in the `is_outside_html_attr_context()` function for the "esc_attr being used outside HTML tags" check makes it inconsistent and raises the cognitive complexity. It is more intuitive to have a similar "prelim function type" check in the condition in the `process()` method. This also removes the need to pass the `$function_name` to the `is_outside_html_attr_context()` function.
While not recommended, HTML attributes for which the value does not contain whitespace, can be passed without the value being quoted. This adds allowance for that situation. Includes unit test.
Function names in PHP are case-insensitive and should be compared against lists in a case-insensitive manner. As the list is in lowercase, lowercase the input received from the token stream before comparing it against the list. Tested by adjusting one of the existing unit tests.
While unlikely, a class or constant could be declared which uses the same name as one of the target functions. In that case, the sniff should not trigger an error. This adds a safeguard to verify if something is a function call and bows out if not. Includes unit test.
In namespaced files, it is a good habit to use fully qualified function calls or `use function ...` statements for global functions to prevent PHP from looking for the function in the current namespace. As things were, fully qualified function calls would be ignored by the sniff, leading to false negatives. Tested by adjusting some existing tests.
The `public` `$escaping_functions` property was never _intended_ for allowing it to be changed via a custom ruleset. As the array _format_ of the property has now changed, adjusted values for this property set in a custom ruleset will no longer work. This is a breaking change, but as: 1. The property was never intended to be changed from a custom ruleset and 2. The chances of any custom rulesets actually existing which _do_ change this property being very, very small... I believe it is justified to make the breaking change in the format in a minor release as the format change provides a real functional benefit. Now, as this breaking change will be made anyway, we may as well do it right and mark the property as `protected` preventing it from ever being changed via a custom ruleset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving with the addition of the seven extra commits which we discussed in a videocall.
See the commit messages of each of these commits for details.
One final note:
The format of the public
property $escaping_functions
has changed - from the "function names as values" to "function names as keys with their type as value".
This format change is a breaking change as the sniff will no longer function correctly if the old property format is used in a custom ruleset.
As discussed in our call and documented in the commit message of the last commit:
This is a breaking change, but as:
- The property was never intended to be changed from a custom ruleset and
- The chances of any custom rulesets actually existing which do change this property being very, very small...
...I believe it is justified to make the breaking change in the format in a minor release as the format change provides a real functional benefit.
With that in mind, the property has now also been made protected
.
Deprecating the public
property and setting up a complex array merge mechanism with the "new" protected
property just in case there is a custom ruleset which does overrule the value seems overkill as the chances of this actually breaking anything in real life are so small.
This breaking change, however, will need a very clear entry in the changelog on the off-chance of a custom ruleset using the property.
/cc @GaryJones
Fixes #155.
Also replaced "NOK" in the test with "Error" since "NOK" is harder to distinguish from "OK".