Python: Reduce log injection false positives#22231
Open
Ali-Razmjoo wants to merge 1 commit into
Open
Conversation
Add `SimpleTypeSanitizer` in a new `semmle.python.security.Sanitizers`, the
Python counterpart of the class the Java and C# queries already use. A routed
parameter annotated with a simple type such as `int` or `uuid.UUID` is validated
by the web framework before the request handler runs, so it cannot contain a
line break and cannot be used to forge a log entry. The annotation is only
trusted on routed parameters, since Python does not enforce annotations at run
time. `Annotated[T, ...]`, `Optional[T]` and `T | None` are unwrapped.
Recognize more ways of neutralizing a log message: `str.translate`,
`str.encode("unicode_escape")`, a `re.sub` or `re.compile(...).sub` whose
pattern matches control characters, and escaping conversions such as `repr` and
`json.dumps`.
Recognize a `logging.Formatter` subclass that strips control characters from the
records it renders. Such a formatter sanitizes when the record is written rather
than where it is created, so no sanitizing call appears between the source and
the logging call. Callees are resolved through module-level definitions, which
local flow does not reach from a nested scope.
Ali-Razmjoo
force-pushed
the
python-log-injection
branch
from
July 24, 2026 12:11
38867cd to
d9af087
Compare
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.
Add
SimpleTypeSanitizerin a newsemmle.python.security.Sanitizers, the Python counterpart of the class the Java and C# queries already use. A routed parameter annotated with a simple type such asintoruuid.UUIDis validated by the web framework before the request handler runs, so it cannot contain a line break and cannot be used to forge a log entry. The annotation is only trusted on routed parameters, since Python does not enforce annotations at run time.Annotated[T, ...],Optional[T]andT | Noneare unwrapped.Recognize more ways of neutralizing a log message:
str.translate,str.encode("unicode_escape"), are.suborre.compile(...).subwhose pattern matches control characters, and escaping conversions such asreprandjson.dumps.Recognize a
logging.Formattersubclass that strips control characters from the records it renders. Such a formatter sanitizes when the record is written rather than where it is created, so no sanitizing call appears between the source and the logging call. Callees are resolved through module-level definitions, which local flow does not reach from a nested scope.