-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(iast): redaction algorithms refactor II [backport 2.9] (#9509)
Backport 8d67869 from #9163 to 2.9. # Summarize Refactor of the IAST redaction system. The old algorithms had several problems: ## Description This PR continues this #9126 - Migrate SQL Injection to this new algorithm - Remove deprecated code ## Checklist - [x] Change(s) are motivated and described in the PR description - [x] Testing strategy is described if automated tests are not included in the PR - [x] Risks are described (performance impact, potential for breakage, maintainability) - [x] Change is maintainable (easy to change, telemetry, documentation) - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed or label `changelog/no-changelog` is set - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)) - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) - [x] If this PR changes the public interface, I've notified `@DataDog/apm-tees`. - [x] If change touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. ## Reviewer Checklist - [x] Title is accurate - [x] All changes are related to the pull request's stated goal - [x] Description motivates each change - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - [x] Testing strategy adequately addresses listed risks - [x] Change is maintainable (easy to change, telemetry, documentation) - [x] Release note makes sense to a user of the library - [x] Author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) Co-authored-by: Alberto Vara <alberto.vara@datadoghq.com>
- Loading branch information
1 parent
547bdd4
commit a568c63
Showing
28 changed files
with
476 additions
and
807 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
70 changes: 70 additions & 0 deletions
70
ddtrace/appsec/_iast/_evidence_redaction/sql_sensitive_analyzer.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,70 @@ | ||
import re | ||
|
||
from ddtrace.appsec._iast.constants import DBAPI_MARIADB | ||
from ddtrace.appsec._iast.constants import DBAPI_MYSQL | ||
from ddtrace.appsec._iast.constants import DBAPI_PSYCOPG | ||
from ddtrace.appsec._iast.constants import DBAPI_SQLITE | ||
from ddtrace.internal.logger import get_logger | ||
|
||
|
||
log = get_logger(__name__) | ||
|
||
|
||
STRING_LITERAL = r"'(?:''|[^'])*'" | ||
POSTGRESQL_ESCAPED_LITERAL = r"\$([^$]*)\$.*?\$\1\$" | ||
MYSQL_STRING_LITERAL = r'"(?:\\\\"|[^"])*"|\'(?:\\\\\'|[^\'])*\'' | ||
LINE_COMMENT = r"--.*$" | ||
BLOCK_COMMENT = r"/\*[\s\S]*?\*/" | ||
EXPONENT = r"(?:E[-+]?\\d+[fd]?)?" | ||
INTEGER_NUMBER = r"(?<!\w)\d+" | ||
DECIMAL_NUMBER = r"\d*\.\d+" | ||
HEX_NUMBER = r"x'[0-9a-f]+'|0x[0-9a-f]+" | ||
BIN_NUMBER = r"b'[0-9a-f]+'|0b[0-9a-f]+" | ||
NUMERIC_LITERAL = ( | ||
r"[-+]?(?:" + "|".join([HEX_NUMBER, BIN_NUMBER, DECIMAL_NUMBER + EXPONENT, INTEGER_NUMBER + EXPONENT]) + r")" | ||
) | ||
|
||
patterns = { | ||
DBAPI_MYSQL: re.compile( | ||
f"({NUMERIC_LITERAL})|({MYSQL_STRING_LITERAL})|({LINE_COMMENT})|({BLOCK_COMMENT})", re.IGNORECASE | re.MULTILINE | ||
), | ||
DBAPI_PSYCOPG: re.compile( | ||
f"({NUMERIC_LITERAL})|({POSTGRESQL_ESCAPED_LITERAL})|({STRING_LITERAL})|({LINE_COMMENT})|({BLOCK_COMMENT})", | ||
re.IGNORECASE | re.MULTILINE, | ||
), | ||
} | ||
patterns[DBAPI_SQLITE] = patterns[DBAPI_MYSQL] | ||
patterns[DBAPI_MARIADB] = patterns[DBAPI_MYSQL] | ||
|
||
|
||
def sql_sensitive_analyzer(evidence, name_pattern, value_pattern): | ||
pattern = patterns.get(evidence.dialect, patterns[DBAPI_MYSQL]) | ||
tokens = [] | ||
|
||
regex_result = pattern.search(evidence.value) | ||
while regex_result is not None: | ||
start = regex_result.start() | ||
end = regex_result.end() | ||
start_char = evidence.value[start] | ||
if start_char == "'" or start_char == '"': | ||
start += 1 | ||
end -= 1 | ||
elif end > start + 1: | ||
next_char = evidence.value[start + 1] | ||
if start_char == "/" and next_char == "*": | ||
start += 2 | ||
end -= 2 | ||
elif start_char == "-" and start_char == next_char: | ||
start += 2 | ||
elif start_char.lower() == "q" and next_char == "'": | ||
start += 3 | ||
end -= 2 | ||
elif start_char == "$": | ||
match = regex_result.group(0) | ||
size = match.find("$", 1) + 1 | ||
if size > 1: | ||
start += size | ||
end -= size | ||
tokens.append({"start": start, "end": end}) | ||
regex_result = pattern.search(evidence.value, regex_result.end()) | ||
return tokens |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.