fix: replace assert with raise ValueError in convertors#3160
Closed
omnislash157 wants to merge 1 commit intoKludex:mainfrom
Closed
fix: replace assert with raise ValueError in convertors#3160omnislash157 wants to merge 1 commit intoKludex:mainfrom
omnislash157 wants to merge 1 commit intoKludex:mainfrom
Conversation
Assert statements used as runtime guards are stripped by `python -O`, silently removing input validation. Under optimization, invalid values like NaN, Infinity, path separators, and negative numbers are silently accepted into URL parameters instead of being rejected. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Problem
assertstatements inconvertors.pyare used as runtime input validation, butassertis stripped bypython -O. When running with optimization enabled, invalid values are silently accepted instead of being rejected.I ran into this while auditing ASGI apps that run with
python -Oin production (common in Docker deployments for performance). The validation just disappears.What breaks under
python -OI wrote a small test to confirm each one:
-OmodeStringConvertor.to_string()"admin/../../etc/passwd"AssertionError: May not contain path separatorsStringConvertor.to_string()""AssertionError: Must not be emptyIntegerConvertor.to_string()-1AssertionError: Negative integers are not supportedFloatConvertor.to_string()float('nan')AssertionError: NaN values are not supportedFloatConvertor.to_string()float('inf')AssertionError: Infinite values are not supportedFloatConvertor.to_string()-1.0AssertionError: Negative floats are not supportedThe path separator one is the scariest —
"admin/../../etc/passwd"passes straight through the convertor if any downstream code uses it to build a file path.Proof of concept
What changed
assert "/" not in value→if "/" in value: raise ValueError(...)assert value→if not value: raise ValueError(...)assert value >= 0→if value < 0: raise ValueError(...)assert not math.isnan(value)→if math.isnan(value): raise ValueError(...)assert not math.isinf(value)→if math.isinf(value): raise ValueError(...)Same error messages, same behavior when running without
-O. The only difference is that validation now works regardless of optimization level.Test plan
pytest tests/test_convertors.py(8/8 passed)-O-O