Repository navigation
GROOVY-12680: do not resolve private members of Closure through a closure's metaclass - #3201
codeconsole wants to merge 1 commit into
Conversation
…sure's metaclass ClosureMetaClass.getProperty, setProperty and getMetaProperty delegate to the metaclass of Closure itself, which indexes the private fields and properties of Closure, such as bcw, mopPerturbed, serialVersionUID and thisType. A closure class does not inherit those members, and the MetaClassImpl of any other subclass reports them as missing. Through the closure's metaclass, or through property access on a closure with the TO_SELF resolve strategy, they could be read and overwritten; overwriting thisType breaks the closure's access to private members of its enclosing class from a subclass instance. Such names now take the missing-property path, and getMetaProperty and hasProperty return null for them. A sender of Closure keeps access, as a declaring class does for the metaclass of a subclass.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3201 +/- ##
==================================================
+ Coverage 74.1533% 74.1563% +0.0031%
- Complexity 43751 43763 +12
==================================================
Files 1692 1692
Lines 149516 149526 +10
Branches 28366 28371 +5
==================================================
+ Hits 110871 110883 +12
+ Misses 28448 28438 -10
- Partials 10197 10205 +8
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: 2acb70c Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
Looks correct for GROOVY-12680.
ClosureMetaClass.getProperty / setProperty / getMetaProperty delegated to a MetaClassImpl of Closure itself, exposing and overwriting private fields (thisType, bcw, serialVersionUID). Overwriting thisType breaks private-field access of the enclosing class when the owner is a subclass instance. Routing private names (including GROOVY-9618 capitalised forms) through invokeMissingProperty, returning null from getMetaProperty, and keeping a Closure sender privileged matches ordinary subclass metaclass rules. Public properties, owner/delegate resolution, and attribute access (c.@owner) are intentionally unchanged (GROOVY-12684 noted for @thisType / @bcw).
Complements #3196’s getProperties filter. CI green.
Land note: #3210 already includes GROOVY-12680 and shares the private-member check across list and lookup. Prefer that bundle. COMMENT for land order.
ClosureMetaClass.getProperty,setPropertyandgetMetaPropertydelegate to aMetaClassImplofgroovy.lang.Closureitself, which indexes Closure's own private fields and properties. A closure's metaclass therefore exposed and overwrote them:The same path is reached by property access on a closure with the
TO_SELFresolve strategy (c.thisType = String). OverwritingthisTypebreaks a closure's access to private fields of its enclosing class when the owner is a subclass instance. The metaclass of any other subclass reports a superclass's private fields as missing. Seen in 5.0.8 and 6.0.0 (and the read side in 4.0.33).getPropertyandsetPropertynow take the missing-property path for a name that resolves to a private member of Closure, including the capitalised formMetaClassImpldecapitalises (GROOVY-9618).getMetaProperty, and sohasProperty, return null for such names. A sender ofClosurekeeps access, as a declaring class does for the metaclass of a subclass. Public properties (delegate,owner,thisObject,resolveStrategy,directive,maximumNumberOfParameters,parameterTypes,metaClass,class), the public constants, and property resolution through owner and delegate are unchanged. Attribute access (c.@delegate,c.@owner) is left as it is, since existing code relies on it; whether it should decline the implementation fields (c.@thisType,c.@bcw) is raised in GROOVY-12684. This complements #3196 (GROOVY-12675), which removed the same members fromClosureMetaClass.getProperties; the two merge cleanly.Compatibility
c.metaClass.getProperty(c, name),setProperty,getMetaPropertyandhasPropertyno longer see the private fields ofgroovy.lang.Closure(thisType,bcw,serialVersionUID): a read or write throwsMissingPropertyExceptionandgetMetaProperty/hasPropertyreturn null, as for a private field of the superclass of any other class. Closure's public properties, resolution through owner and delegate, and attribute access (c.@owner) are unchanged. With theTO_SELFresolve strategy,c.thisTypeand similar names now throwMissingPropertyExceptioninstead of reading or overwriting the field.Tests
ClosureTest.testMetaClassDoesNotExposePrivateMembersOfClosure,testMetaClassCannotOverwritePrivateMembersOfClosureandtestToSelfDoesNotResolvePrivateMembersOfClosurefail without the change.testMetaClassStillExposesPublicMembersOfClosureandtestOwnerAndDelegatePropertiesNamedLikePrivateMembersOfClosurecover the public members and owner/delegate resolution.groovy.lang.*,org.codehaus.groovy.runtime.*,groovy.*Closure*,bugs.*,gls.invocation.*,groovy.transform.stc.*,org.codehaus.groovy.classgen.asm.sc.*andgroovy.*MetaClass*pass. The Checkstyle and CodeNarc gates pass.