Skip to content

keep escaped variables literal when substitution in variables is enabled - #797

Open
dxbjavid wants to merge 1 commit into
apache:masterfrom
dxbjavid:substitutor-nested-escape
Open

dxbjavid wants to merge 1 commit into
apache:masterfrom
dxbjavid:substitutor-nested-escape

Conversation

@dxbjavid

Copy link
Copy Markdown
Contributor

With setEnableSubstitutionInVariables(true), StringSubstitutor substitutes an escaped variable whose name starts with a nested variable whenever no later } follows it in the input. With a=1 and 1=one, $${${a}} returns one, while $${${a}} ${a} returns ${1} 1. The deprecated StrSubstitutor returns ${1} in both cases.

The cause is in substitute: after deleting the escape it moves pos back to the escape position when the escaped name starts with a prefix and hasLaterSuffix finds nothing. That rescan is there for the default mode, where the first suffix found may be the inner one ($${${a} stays ${${a}). In nested mode the suffix found is already the balanced outer one, so the rescan reads the escaped expression again and resolves it. The fix skips the rescan when substitution in variables is enabled. Default-mode behaviour is unchanged.

This matters when a template escapes an expression so that it comes out literally and the inner value is not trusted. For example, with StringLookupFactory.INSTANCE.interpolatorStringLookup(map) and user=file:UTF-8:LICENSE.txt, $${${user}} returns the contents of LICENSE.txt instead of ${file:UTF-8:LICENSE.txt}.

StringSubstitutorTest#testReplaceInVariableEscaped fails without the change to StringSubstitutor (expected: <${1}> but was: <one>) and passes with it. The default mvn build passes (2045 tests; Checkstyle, PMD, SpotBugs and Javadoc are clean).


  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

With setEnableSubstitutionInVariables(true), StringSubstitutor resolved
an escaped variable whose name starts with a nested variable, such as
$${${a}}, whenever no later suffix followed it in the input. The rescan
from the escape position is only needed in the default mode, where the
first suffix found may be the inner one; in nested mode it re-reads the
escaped expression and substitutes it. StrSubstitutor returns ${1} for
the same input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant