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

Better handling of IfExp (ternary) #179

Merged
merged 1 commit into from
Oct 30, 2018
Merged

Conversation

bcaller
Copy link
Collaborator

@bcaller bcaller commented Oct 29, 2018

Reduces false positives.

As an example:

result = "a" if TAINT else "c"

In AST, the assignment value is IfExp(test=TAINT, body="a", orelse="c").

Even though TAINT is inside the assignment of result, it can't
actually taint result as it is part of the boolean test expression.

Previously, result would have been tainted, which was a false
positive.

We don't want to completely ignore the test though in case it contains a
sink function.

Therefore, if the test contains expressions we transform it as so:

result = "a" if b(c) + 2 else "d"

to the multi line:

__if_exp_0 = b(c) + 2
result = "a" if __if_exp_0 else "d"

This way if b is a sink and c is tainted we see a vulnerability, but
even if c is tainted we don't taint result.

Reduces false positives.

As an example:

result = "a" if TAINT else "c"

In AST, the assignment value is `IfExp(test=TAINT, body="a", orelse="c")`.

Even though `TAINT` is inside the assignment of `result`, it can't
actually taint `result` as it is part of the boolean test expression.

Previously, `result` would have been tainted, which was a false
positive.

We don't want to completely ignore the test though in case it contains a
sink function.

Therefore, if the test contains expressions we transform it as so:

result = "a" if b(c) + 2 else "d"

to the multi line:

__if_exp_0 = b(c) + 2
result = "a" if __if_exp_0 else "d"

This way if `b` is a sink and `c` is tainted we see a vulnerability, but
even if `c` is tainted we don't taint `result`.
@bcaller bcaller requested a review from KevinHock October 29, 2018 15:56
Copy link
Collaborator

@KevinHock KevinHock left a comment

Choose a reason for hiding this comment

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

Beautiful :D

@bcaller bcaller merged commit 0932cc9 into python-security:master Oct 30, 2018
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.

2 participants