fix(analyzer): make E2 whitespace-tolerant and detect all os.environ read forms - #331
Open
weed33834 wants to merge 1 commit into
Open
fix(analyzer): make E2 whitespace-tolerant and detect all os.environ read forms#331weed33834 wants to merge 1 commit into
weed33834 wants to merge 1 commit into
Conversation
…read forms
The E2 (Env Variable Harvesting) regex patterns used `os\.environ` without
optional whitespace between `os` and `.environ`, so inserting PEP8-irrelevant
whitespace (`os . environ . copy ()`) bypassed detection entirely. This meant
a skill scoring `DO_NOT_INSTALL` with canonical syntax could be rewritten to
`SAFE` by adding spaces.
Changes:
- Add `\s*` between `os` and `\.` in all Python os.environ patterns so that
whitespace variants are detected uniformly.
- Add patterns for `dict(os.environ)` and `{**os.environ}`, the two most
common alternative forms of reading the full environment mapping.
- Add 5 regression tests covering whitespace-tolerant and alternative forms.
Fixes NVIDIA#329
Signed-off-by: weed33834 <weed33834@users.noreply.github.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.
Summary
The E2 (Env Variable Harvesting) regex patterns are spell-checkers, not behavior detectors.
os\.environwithout optional whitespace betweenosand.means that inserting PEP8-irrelevant whitespace —os . environ . copy ()— bypasses detection entirely. A skill scoringDO_NOT_INSTALLwith canonical syntax can be rewritten toSAFEby adding spaces. The same blind spot letsdict(os.environ)and{**os.environ}pass clean, even though they do exactly what the rule is meant to catch.Changes
Whitespace-tolerant
os.environmatching —\s*betweenosand\.in all Python E2 patterns, so whitespace variants no longer bypass detection.New patterns —
dict(os.environ)and{**os.environ}(the two most common alternative forms of reading the full environment mapping) are now detected.5 regression tests added to
test_patterns.py.Testing
Fixes #329