Repository navigation
Conversation
The const lookup is documented to resolve static final fields, but it read any public static field, including mutable ones, and cached the value. It also initialised the named class before looking for the field, so a lookup of a missing field still ran the class's static initialiser. Load the class without initialising it and return null unless the field is static and final.
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.
ConstantStringLookup backs the default
constlookup used byStringSubstitutor.createInterpolator(). Its Javadoc says it resolves the "constant (i.e. static final) member field" of a class, butresolveFieldreturnedclazz.getField(name).get(null)with no modifier check, andfetchClassloaded the class withClassUtils.getClass(className), which initialises it.When the interpolated text is not fully trusted,
${const:some.Class.someField}therefore returns the value of any public static field on the classpath, including mutable fields that hold runtime state rather than constants, and that value then stays in the staticCONSTANT_CACHEeven if the field changes later. Separately,${const:any.Class.NO_SUCH_FIELD}runs the static initialiser of any class on the classpath even though nothing gets resolved.The class is now loaded without being initialised, and the lookup returns null unless the field is static and final, the same null it already returns for a private or missing field. A class is only initialised when one of its constants is actually read, and existing uses such as
${const:java.awt.event.KeyEvent.VK_ESCAPE}resolve as before.testLookupNonFinalandtestLookupNonExistingDoesNotInitializeClassinConstantStringLookupTestfail without the change in main and pass with it. The defaultmvnbuild (tests, checkstyle, PMD, SpotBugs, japicmp, Javadoc) is green locally.mvn; that'smvnon the command line by itself.