Skip to content

HTMLExecutingFunctions: Add more functions#437

Merged
rebeccahum merged 2 commits intodevelopfrom
fix/xss-js
Jul 3, 2020
Merged

HTMLExecutingFunctions: Add more functions#437
rebeccahum merged 2 commits intodevelopfrom
fix/xss-js

Conversation

@GaryJones
Copy link
Contributor

There are different ways to insert variables into the DOM, and our sniffs only covered some of them.

The sniff now has an expanded list, which covers functions that have a syntax where the content is in the function arg, and also where the function arg is the target and the content is in the preceding method's arg.

New functions:

  • after
  • appendTo
  • before
  • insertAfter
  • insertBefore
  • prepend
  • prependTo
  • replaceAll
  • replaceWith

Fixes #435.


Not included is the ability to handle insertAdjacentHTML() or consolidating assignment to innerHTML (and outerHTML) into this sniff.

Copy link
Contributor

@david-binda david-binda left a comment

Choose a reason for hiding this comment

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

Looks good to me! However, I left some minor feedback/questions inline.


$prevPrevToken = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, $prevToken - 1, null, true, null, true );

if ( T_CLOSE_PARENTHESIS !== $this->tokens[ $prevPrevToken ]['code'] ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that we should also check whether the keyword we have found is a function call. As, for instance, the code, as it is now, would flag following code:

foo( variable ).appendTo;


while ( $prevPrevToken > $parenthesis_opener ) {
$prevPrevToken = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, $prevPrevToken - 1, null, true, null, true );
if ( T_STRING === $this->tokens[ $prevPrevToken ]['code'] ) { // Contains a variable.
Copy link
Contributor

Choose a reason for hiding this comment

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

I know that this code has been in place prior this PR, but I don't think that the // Contains a variable inline comment is true, as even a function call would match the T_STRING code. Eg.:

$( foo() ).appendTo;

@GaryJones
Copy link
Contributor Author

Decided to prove to myself that these did indeed execute JavaScript: https://jsbin.com/pohefecucu/edit?html,js,output (creates 15 alerts that you'll need to click through!)

There are some subtleties:

  • using append() on a jQuery object will evaluate (run) what is added, but using append() with a JavaScript Node will not. There's simply no way to accurately determine what append() is being called on from the tokens, hence needing to treat them all the same.
  • Script elements inserted using innerHTML do not execute when they are inserted, but there are other ways to mis-use innerHTML and result in XSS. We already check for the presence of a variable in our innerHTML sniff.

GaryJones added 2 commits July 3, 2020 20:32
There are different ways to insert variables into the DOM, and our sniffs only covered some of them.

The sniff now has an expanded list, which covers functions that have a syntax where the content is in the function arg, and also where the function arg is the target and the content is in the preceding method's arg.

Fixes #435.
The extra unit test cases:

 - consider if the contents of a function was not a string but a function call (and update the inline comments to reflect that); this case was already correctly handled.
 - consider the case when the object that `appendTo()` (and similar) is being called in is a variable, and not a `$()` call (which we already check to see if it contains a simple string to avoid unnecessary violations) or other function call.
@rebeccahum rebeccahum merged commit 48e83cc into develop Jul 3, 2020
@rebeccahum rebeccahum deleted the fix/xss-js branch July 3, 2020 19:40
@GaryJones GaryJones modified the milestones: 2.2.0, 2.1.0 Jul 4, 2020
@GaryJones GaryJones removed the request for review from rogertheriault July 4, 2020 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Propably missing a warning on prepend method

3 participants