-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distinguish betwen strings and attributes (#388)
Much of the code is now based on converting attributes and/or identifier nodes into strings to identify whether that string matches a suspicious call as part of a Rule. However, the code needs to distinguish between a string representing an attribute/identifier and a true regular string. To do this, a convenience utils class was added to detect true strings from tree-sitter node text. Luckily they appear different because they have extra quotes. This should fix some critical false positive/negative cases where an identifier assignment was to a string and not a suspicious function. Signed-off-by: Eric Brown <[email protected]>
- Loading branch information
Showing
13 changed files
with
127 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2024 Secure Saurce LLC | ||
|
||
|
||
def is_str(value) -> bool: | ||
""" | ||
True if the value is a tree-sitter node string. | ||
:return: if value is a string | ||
:rtype: bool | ||
""" | ||
if isinstance(value, str) and ( | ||
value.startswith('b"""') | ||
or value.startswith("b'''") | ||
or value.startswith('b"') | ||
or value.startswith("b'") | ||
or value.startswith('"""') | ||
or value.startswith("'''") | ||
or value.startswith('"') | ||
or value.startswith("'") | ||
): | ||
return True | ||
return False | ||
|
||
|
||
def to_str(value: str) -> str: | ||
""" | ||
Converts a tree-sitter node string value to a | ||
true string. | ||
:return: value as string | ||
:rtype: str | ||
""" | ||
if isinstance(value, str): | ||
value_str = value | ||
bytestr = False | ||
if value_str and value_str[0] == "b": | ||
value_str = value_str[1:] | ||
bytestr = True | ||
if value_str.startswith('"""') or value_str.startswith("'''"): | ||
value_str = value_str[3:-3] | ||
elif value_str.startswith('"') or value_str.startswith("'"): | ||
value_str = value_str[1:-1] | ||
if bytestr is True: | ||
value_str = bytes(value_str, encoding="utf-8") | ||
|
||
return value_str |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
3 changes: 3 additions & 0 deletions
3
tests/unit/rules/python/stdlib/hashlib/examples/hashlib_md5_as_identifier.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# level: NONE | ||
hashlib = "hashlib" | ||
hashlib.md5() |
This file contains 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