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

TSX tags seem to be matched incorrectly and fixed incorrectly #2685

Closed
yoav-lavi opened this issue Mar 3, 2021 · 13 comments
Closed

TSX tags seem to be matched incorrectly and fixed incorrectly #2685

yoav-lavi opened this issue Mar 3, 2021 · 13 comments
Assignees
Labels
bug Something isn't working feature:autofix priority:medium user:external requested by someone outside of r2c

Comments

@yoav-lavi
Copy link

Describe the bug
When matching TSX tags for a fix, Semgrep seems to match the element name and other non matching elements rather than the entire tag. This causes the fix to be implemented on the tag name -

<$TAG></$TAG>

matches

<div></div>

and

<div>{3}</div>

plus the suggested fix is

<<div />></div>

To Reproduce
https://semgrep.dev/s/N4Lz/

Expected behavior
TSX tags should be matched in their entirety and not replace just the element name

Screenshots
Screen Shot 2021-03-03 at 19 08 56

What is the priority of the bug to you?
P1

@yoav-lavi
Copy link
Author

This may be related to the deep expression syntax as it uses < and > as well

@ievans
Copy link
Member

ievans commented Mar 3, 2021

thanks for the great bug report @yoav-lavi, autofix is still experimental (https://semgrep.dev/docs/experiments/overview/#autofix) but this is probably an easy fix and one we should definitely do!

@yoav-lavi
Copy link
Author

@ievans the matching itself also seems to be wrong in this instance (see <div>{3}</div> which is being matched even though it does not fit the pattern)

@yoav-lavi
Copy link
Author

It looks like it's matching div and div>{3 respectively which is an issue with the match itself rather than the fix (the autofix may actually be fine here based on what it matched)

@yoav-lavi
Copy link
Author

yoav-lavi commented Mar 3, 2021

Just to give the motivation for using this rule - the idea was to detect empty opening and closing tags to suggest / fix with self closing tags

<div></div>

to

<div />

@yoav-lavi
Copy link
Author

@ievans Also notice in the image that the fix suggestion is constx and consty with no spacing between the keyword and the variable name, which is a separate issue

@aryx
Copy link
Collaborator

aryx commented Mar 4, 2021

We have some builtin equivalences for JSX which might get in the way ...

@nbrahms nbrahms added the user:external requested by someone outside of r2c label Mar 18, 2021
@mschwager
Copy link
Contributor

We have some builtin equivalences for JSX which might get in the way ...

Do you mean get in the way of the autofix or FP here?

Since autofix is experimental functionality I think we should focus on the FP. @aryx shouldn't the pattern require a ... to match the first example here? https://semgrep.dev/s/Av7g/

@aryx
Copy link
Collaborator

aryx commented Mar 18, 2021

I can take a look

@aryx aryx self-assigned this Mar 18, 2021
aryx added a commit to semgrep/pfff that referenced this issue Mar 24, 2021
aryx added a commit that referenced this issue Mar 24, 2021
@aryx
Copy link
Collaborator

aryx commented Mar 24, 2021

Right now in semgrep, the pattern <$EL /> will match lots of JSX elements: <foo />, but also <foo ></foo>, also <foo>xxx</foo>, etc.
and internally a pattern like <$EL></$EL> is actually transformed in <$EL />. This is maybe too much because it prevents users like @yoav-lavi to explicitely match code like <div></div> but not <div />.

@minusworld @mschwager @ievans maybe we should disable most of those equivalences. That means we will have to rewrite some of our semgrep rules like https://github.com/returntocorp/semgrep-rules/blob/develop/typescript/react/security/audit/react-css-injection.yaml to use instead of patterns like <$EL style={$STYLE} />
the more verbose <$EL style={$STYLE} >...</$EL>.
What do you think? We should even maybe require to use ... for the attributes to says more attributes are ok (to be consistent with what we do elsewhere @mschwager ?)
Then the only builtin equivalence we can still do is that <$EL ...>...<$EL> would still match code like <foo></foo> or <foo />.
Feedback?

aryx added a commit to semgrep/pfff that referenced this issue Mar 24, 2021
* [JSX] add more tokens in AST for JSX construct

This will help
semgrep/semgrep#2685

test plan:
make test

* add more tokens
aryx added a commit that referenced this issue Mar 24, 2021
* [JSX] add more tokens in generic AST for JSX constructs

this will help #2685

test plan:
make test

* add more tokens

test plan:
make test in semgrep-rules now also work
@aryx
Copy link
Collaborator

aryx commented Mar 24, 2021

@yoav-lavi the matching and autofix seems to work better on the latest: https://semgrep.dev/s/N4Lz/?version=develop
Now remains to fix how we want semgrep JSX pattern to match code and which equivalence to put by default (as I mentioned above).
Progress still!

@yoav-lavi
Copy link
Author

yoav-lavi commented Mar 24, 2021

Yes, it seems to work better in that version, thanks! By the way, the editor seems to treat JSX as an error for some reason @aryx

@aryx
Copy link
Collaborator

aryx commented Mar 24, 2021

ok, closing this issue. For the equivalance thing I've created another issue: #2812

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature:autofix priority:medium user:external requested by someone outside of r2c
Development

No branches or pull requests

5 participants