Chained function calls separated into multiple assignments#171
Merged
Conversation
Take the example from examples/vulnerable_code/sql/sqli.py:
`result = session.query(User).filter("username={}".format(TAINT))`
The `filter` function is marked as a sink. However, previously this did
not get marked as a vulnerability.
The call label used to be `session.query`, ignoring the filter function.
Now, when the file is read, it is transformed into 2 lines:
```
__chain_tmp_1 = session.query(User)
result = __chain_tmp_1.filter("username={}".format(TAINT))
```
And the vulnerability is found.
We don't find everything here: just ordinary assignments and return
statements. We can't just transform all Call nodes here since Call nodes
can appear in many different scenarios e.g. comprehensions, bare
function calls.
KevinHock
approved these changes
Sep 2, 2018
Collaborator
KevinHock
left a comment
There was a problem hiding this comment.
LGTM, sorry it took a few days to review, just moved :)
| class ChainedFunctionTransformer(): | ||
| def visit_chain(self, node, depth=1): | ||
| if ( | ||
| isinstance(node.value, ast.Call) and |
Collaborator
There was a problem hiding this comment.
Love how you did
node.value
node.value.func
node.value.func.value
in that order, it's super clean.
| inner_calls = self.visit_chain(unvisited_inner_call, depth + 1) | ||
| for inner_call_node in inner_calls: | ||
| ast.copy_location(inner_call_node, node) | ||
| outer_call = self.generic_visit(type(node)( |
Collaborator
There was a problem hiding this comment.
Indentation nit: This is awesomely smart but more indentation may make it clearer
outer_call = self.generic_visit(
type(node)(
value=ast.Call(
func=ast.Attribute(
value=ast.Name(id=temp_var_id, ctx=ast.Load()),
attr=call_node.func.attr,
ctx=ast.Load(),
),
args=call_node.args,
keywords=call_node.keywords,
),
**{field: value for field, value in ast.iter_fields(node) if field != 'value'} # e.g. targets
)
)Nit: w/r/t generic_visit, since it isn't defined in this class, maybe inheriting from ast.NodeTransformer in both classes would be clearer.
| self.assertEqual(ast.dump(transformed), ast.dump(sync_tree)) | ||
|
|
||
| def test_chained_function(self): | ||
| chained_tree = ast.parse("\n".join([ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Take the example from examples/vulnerable_code/sql/sqli.py:
The
filterfunction is marked as a sink. However, previously this did not get marked as a vulnerability. The call label used to besession.query, ignoring the filter function.Now, when the file is read, it is transformed into 2 lines:
before converting to a CFG. The vulnerability is now found.
We don't find everything here: just ordinary assignments and return statements. We can't just transform all Call nodes here since Call nodes can appear in many different scenarios e.g. comprehensions, bare function calls. Because of this, it may be worth thinking about how to do this via the CFG everywhere function calls are chained.