Raise FeatureNotAvailable for unsupported comprehension targets#190
Open
gaoflow wants to merge 1 commit into
Open
Raise FeatureNotAvailable for unsupported comprehension targets#190gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
A comprehension assignment target that is not a name or a tuple/list (e.g. a subscript x[0] or attribute x.attr) made recurse_targets access target.elts, raising a raw AttributeError instead of a SimpleEval exception (issue danthedeckie#76). Handle Name and Tuple/List explicitly and raise FeatureNotAvailable for anything else. This keeps subscript and attribute writes out of the sandbox, so the self-referential-list construction noted in the issue is rejected cleanly instead of being built.
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
Fixes #76. A list/dict comprehension whose
forclause uses a non-name assignment target — a subscript likex[0]or an attribute likex.attr— crashes with a raw, non-SimpleEval exception:recurse_targetsonly special-casedast.Nameand assumed every other target was a tuple/list with an.eltsattribute.Fix
Handle
ast.Nameandast.Tuple/ast.Listexplicitly, and raise the library's ownFeatureNotAvailablefor any other target type. This resolves the actual complaint in the issue (a rawAttributeErrorleaking instead of aSimpleEvalexception).I deliberately did not implement subscript assignment (
container[key] = value). The same target path is what enables the self-referential-list construction the reporter flagged in the thread:i.e. a list that contains itself — a potential DoS vector (e.g.
RecursionErroron comparison). For a sandboxing evaluator, rejecting these targets is the safer behaviour, and it keeps attribute/subscript writes out of the sandbox (attribute writes were never supported). After the fix both the subscript and attribute target forms raiseFeatureNotAvailable.Verification
main(afd1ffc): subscript target raises a rawAttributeError; after the fix it raisesFeatureNotAvailable, as does the self-referential construction and an attribute target.test_subscript_target_fails_cleanly,test_attribute_target_fails_cleanly,test_self_referential_target_blocked); each fails before the change (rawAttributeError) and passes after.ruff==0.6.9andmypy==1.11.2clean.This pull request was prepared with the assistance of AI, under my direction and review.