Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<body>
<release version="3.7.2" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
<!-- FIX -->
<action dev="henrib" due-to="Claude" type="fix" issue="JEXL-477">Interpolation strings are parsed with the engine features instead of those of their script.</action>
Comment thread
henrib marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept as is, same as the earlier thread: in this file, fix entries state the problem as the JIRA title does.

<action dev="henrib" due-to="Claude" type="fix" issue="JEXL-475">A jexl.options pragma modifies the engine options when evaluated with a plain context.</action>
<!-- ADD -->
<action dev="henrib" due-to="Claude" type="add" issue="JEXL-476">Add the safeUndefined option (JexlOptions.setSafeUndefined(boolean), JexlBuilder.safeUndefined(boolean)) to restrict safe navigation to null, undefined variables, properties and methods remaining errors; safe access operators treat properties, methods and array accesses alike.</action>
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/apache/commons/jexl3/internal/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,21 @@ protected ASTJexlScript parse(final JexlInfo info, final JexlFeatures parsingf,
* @return The parsed tree
*/
protected ASTJexlScript jxltParse(final JexlInfo info, final boolean expr, final String src, final Scope scope) {
return jxltParse(info, expr ? this.expressionFeatures : this.scriptFeatures, src, scope);
}

/**
* Parses a Jexl expression or script.
*
* @param info The JexlInfo
* @param features The features to parse with
* @param src The source to parse
* @param scope The scope, maybe null
* @return The parsed tree
*/
protected ASTJexlScript jxltParse(final JexlInfo info, final JexlFeatures features, final String src, final Scope scope) {
synchronized(parsing) {
return parser.jxltParse(info, expr ? this.expressionFeatures : this.scriptFeatures, src, scope);
return parser.jxltParse(info, features, src, scope);
}
}

Expand Down
72 changes: 61 additions & 11 deletions src/main/java/org/apache/commons/jexl3/internal/TemplateEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,19 @@ protected JexlOptions options(final JexlContext context) {
*/
final class NestedExpression extends JexlBasedExpression {
private final Scope scope;
private final JexlFeatures features;
/**
* Creates a nested unified expression.
*
* @param expr The unified expression as a string
* @param node The unified expression as an AST
* @param sc The scope
* @param ftrs The features to parse the prepared expression with
*/
NestedExpression(final CharSequence expr, final JexlNode node, final Scope sc) {
NestedExpression(final CharSequence expr, final JexlNode node, final Scope sc, final JexlFeatures ftrs) {
super(expr, node, null);
this.scope = sc;
this.features = ftrs;
if (source != this) {
throw new IllegalArgumentException("Nested TemplateExpression cannot have a source");
}
Expand Down Expand Up @@ -544,7 +548,7 @@ public boolean isImmediate() {
@Override
protected TemplateExpression prepare(final Interpreter interpreter) {
final String value = interpreter.interpret(node).toString();
final JexlNode dnode = jexl.jxltParse(node.jexlInfo(), noscript, value, scope);
final JexlNode dnode = jexl.jxltParse(node.jexlInfo(), features, value, scope);
return new ImmediateExpression(value, dnode, this);
}
}
Expand Down Expand Up @@ -912,24 +916,38 @@ public JxltEngine.Expression createExpression(final JexlInfo jexlInfo, final Str
}

public JxltEngine.Expression createExpression(final JexlInfo jexlInfo, final String expression, final Scope scope) {
return createExpression(jexlInfo, expression, scope, null);
}

/**
* Creates an expression that is embedded in a script, an interpolation.
*
* @param jexlInfo The source info
* @param expression The expression source
* @param scope The scope
* @param ftrs The features of the script the expression is embedded in, null for those of the engine
* @return The expression
*/
public JxltEngine.Expression createExpression(final JexlInfo jexlInfo, final String expression, final Scope scope,
final JexlFeatures ftrs) {
final JexlInfo info = jexlInfo == null ? jexl.createInfo() : jexlInfo;
Exception xuel = null;
TemplateExpression stmt = null;
final JexlFeatures features = noscript ? jexl.expressionFeatures : jexl.scriptFeatures;
// do not cache interpolation expression, they are stored in AST node
final JexlFeatures features = features(ftrs);
// expressions shorter than the cache threshold are cached, keyed by features, symbols and source
final boolean cached = cache != null && expression.length() < jexl.cacheThreshold;
final JexlEngine je = Engine.putThreadEngine(jexl);
try {
if (!cached) {
stmt = parseExpression(info, expression, scope);
stmt = parseExpression(info, expression, scope, features);
} else {
final Source source = new Source(features, Scope.getSymbolsMap(scope), expression);
final Object c = cache.get(source);
stmt = c instanceof TemplateExpression ? (TemplateExpression) c : null;
if (stmt != null) {
return stmt;
}
stmt = parseExpression(info, expression, scope);
stmt = parseExpression(info, expression, scope, features);
cache.put(source, stmt);
}
} catch (final JexlException xjexl) {
Expand Down Expand Up @@ -987,7 +1005,39 @@ char getImmediateChar() {
* @return The unified expression instance
* @throws JexlException if an error occurs during parsing
*/
TemplateExpression parseExpression(final JexlInfo info, final String expr, final Scope scope) { // CSOFF: MethodLength
TemplateExpression parseExpression(final JexlInfo info, final String expr, final Scope scope) {
return parseExpression(info, expr, scope, features(null));
}

/**
* Gets the features used to parse the JEXL expressions of a unified expression.
*
* @param ftrs The features of the script the unified expression is embedded in, null for those of the engine
* @return The features
*/
private JexlFeatures features(final JexlFeatures ftrs) {
if (ftrs == null) {
return noscript ? jexl.expressionFeatures : jexl.scriptFeatures;
}
if (!noscript || !ftrs.supportsScript()) {
return ftrs;
}
// the script was created with the features of the engine, no need to derive them
return ftrs == jexl.scriptFeatures ? jexl.expressionFeatures : new JexlFeatures(ftrs).script(false);
}
Comment thread
henrib marked this conversation as resolved.
Resolved
Comment on lines +1018 to +1027

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a hot path: the features are derived when the parser builds an interpolation node, once per node, and evaluation uses the expression stored in the node. Still, scripts created with the features of the engine did not need a copy before this change; e0f00f2 reuses the engine expression features for them, so only scripts created with their own features derive one.


/**
* Parses a unified expression.
*
* @param info The source info
* @param expr The string expression
* @param scope The template scope
* @param features The features to parse the JEXL expressions with
* @return The unified expression instance
* @throws JexlException if an error occurs during parsing
*/
TemplateExpression parseExpression(final JexlInfo info, final String expr, final Scope scope,
final JexlFeatures features) { // CSOFF: MethodLength
final int size = expr.length();
final ExpressionBuilder builder = new ExpressionBuilder(0);
final StringBuilder strb = new StringBuilder(size);
Expand Down Expand Up @@ -1058,7 +1108,7 @@ TemplateExpression parseExpression(final JexlInfo info, final String expr, final
final String src = escapeString(strb);
final JexlInfo srcInfo = info.at(lineno, column);
final TemplateExpression iexpr = new ImmediateExpression(src,
jexl.jxltParse(srcInfo, noscript, src, scope), null);
jexl.jxltParse(srcInfo, features, src, scope), null);
builder.add(iexpr);
strb.delete(0, Integer.MAX_VALUE);
state = ParseState.CONST;
Expand Down Expand Up @@ -1106,12 +1156,12 @@ TemplateExpression parseExpression(final JexlInfo info, final String expr, final
if (nested) {
dexpr = new NestedExpression(
escapeString(expr.substring(inested, column + 1)),
jexl.jxltParse(srcInfo, noscript, src, scope),
scope);
jexl.jxltParse(srcInfo, features, src, scope),
scope, features);
} else {
dexpr = new DeferredExpression(
src,
jexl.jxltParse(srcInfo, noscript, src, scope));
jexl.jxltParse(srcInfo, features, src, scope));
}
builder.add(dexpr);
strb.delete(0, Integer.MAX_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.commons.jexl3.parser;

import org.apache.commons.jexl3.JexlFeatures;
import org.apache.commons.jexl3.JxltEngine;
import org.apache.commons.jexl3.internal.Scope;

Expand Down Expand Up @@ -50,8 +51,8 @@ public void setExpression(final JxltEngine.Expression tp) {
jxltExpression = tp;
}

public void setIdentifier(final String src, final Scope scope) {
public void setIdentifier(final String src, final Scope scope, final JexlFeatures features) {
super.setIdentifier(src);
this.jxltExpression = JexlParser.parseInterpolation(jexlInfo(), src, scope);
this.jxltExpression = JexlParser.parseInterpolation(jexlInfo(), src, scope, features);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.commons.jexl3.parser;

import org.apache.commons.jexl3.JexlFeatures;
import org.apache.commons.jexl3.JxltEngine;
import org.apache.commons.jexl3.internal.Scope;

Expand Down Expand Up @@ -68,9 +69,9 @@ public void setExpression(final JxltEngine.Expression e) {
this.jxltExpression = e;
}

void setLiteral(final String src, final Scope scope) {
void setLiteral(final String src, final Scope scope, final JexlFeatures features) {
this.literal = src;
this.jxltExpression = JexlParser.parseInterpolation(jexlInfo(), src, scope);
this.jxltExpression = JexlParser.parseInterpolation(jexlInfo(), src, scope, features);
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1170,19 +1170,21 @@ public ASTJexlScript jxltParse(final JexlInfo info, final JexlFeatures features,
* @param info The JexlInfo
* @param src The source to parse
* @param scope The scope
* @param features The features of the script the expression is embedded in
* @return The expression
*/
static JxltEngine.Expression parseInterpolation(final JexlInfo info, final String src, final Scope scope) {
static JxltEngine.Expression parseInterpolation(final JexlInfo info, final String src, final Scope scope,
final JexlFeatures features) {
final JexlEngine jexl = JexlEngine.getThreadEngine();
if (jexl != null) {
// interpolation uses default $ and # as expression markers;
// the cache size is negative to reuse the engine cache
final JxltEngine jxlt = jexl.createJxltEngine(true, -1, '$', '#');
if (jxlt instanceof TemplateEngine) {
return ((TemplateEngine) jxlt).createExpression(info, src, scope);
return ((TemplateEngine) jxlt).createExpression(info, src, scope, features);
}
}
throw new IllegalStateException("engine is not a accessible");
throw new IllegalStateException("engine is not accessible");
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ void JxltLiteral() #JxltLiteral :
}
{
t=<JXLT_LITERAL>
{ jjtThis.setLiteral(src = Parser.buildString(t.image, true), getScope()); }
{ jjtThis.setLiteral(src = Parser.buildString(t.image, true), getScope(), getFeatures()); }
}

void RegexLiteral() :
Expand Down Expand Up @@ -1259,15 +1259,15 @@ void IdentifierAccess() #void :
|
t=<STRING_LITERAL> { jjtThis.setIdentifier(Parser.buildString(t.image, true)); } #IdentifierAccess
|
t=<JXLT_LITERAL> { jjtThis.setIdentifier(Parser.buildString(t.image, true), getScope()); } #IdentifierAccessJxlt
t=<JXLT_LITERAL> { jjtThis.setIdentifier(Parser.buildString(t.image, true), getScope(), getFeatures()); } #IdentifierAccessJxlt
)
|
<QDOT> (
t=<DOT_IDENTIFIER> { jjtThis.setIdentifier(t.image); } #IdentifierAccessSafe
|
t=<STRING_LITERAL> { jjtThis.setIdentifier(Parser.buildString(t.image, true)); } #IdentifierAccessSafe
|
t=<JXLT_LITERAL> { jjtThis.setIdentifier(Parser.buildString(t.image, true), getScope()); } #IdentifierAccessSafeJxlt
t=<JXLT_LITERAL> { jjtThis.setIdentifier(Parser.buildString(t.image, true), getScope(), getFeatures()); } #IdentifierAccessSafeJxlt
)
}

Expand Down
16 changes: 13 additions & 3 deletions src/test/java/org/apache/commons/jexl3/ClassCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,19 @@ void testMany() throws Exception {

// attempt to force GC:
// while we still have a MB free, create & store big objects
for (int b = 0; b < 1024 && Runtime.getRuntime().freeMemory() > MEGA; ++b) {
final BigObject big = new BigObject(b);
stuff.add(new InstanceReference(big, queue));
try {
for (int b = 0; b < 1024 && Runtime.getRuntime().freeMemory() > MEGA; ++b) {
final BigObject big = new BigObject(b);
stuff.add(new InstanceReference(big, queue));
}
} catch (final OutOfMemoryError oom) {
// exhausting the heap is the intent; if the JVM fails an allocation instead of
// clearing the soft references, release the big objects and carry on
for (final Reference<?> soft : stuff) {
if (soft instanceof InstanceReference) {
soft.clear();
}
}
}
Comment on lines +380 to 393

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept as is: what the test asserts is unchanged, a generated class must be collected and reloaded (gced >= 0, second pass). Filling the heap is only a means to provoke a GC, and the clearing of soft references is the JVM's business, not what is under test.

// hint it...
System.gc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -79,4 +81,40 @@ void testPermissiveTemplateStillWorks() {
t.evaluate(new MapContext(), strw);
assertEquals("hello 1\nhello 2\n", strw.toString());
}

@Test
void testInterpolationHonorsScriptFeatures() {
final JexlFeatures permissive = new JexlFeatures();
final JexlFeatures restricted = new JexlFeatures().methodCall(false);
final JexlContext ctxt = new MapContext();
ctxt.set("s", "abc");
final String src = "`${s.length()}`";
// the features of the script are more restrictive than those of the engine
final JexlEngine jexl = new JexlBuilder().features(permissive).create();
assertEquals("3", jexl.createScript(src).execute(ctxt).toString());
assertThrows(JexlException.Feature.class, () -> jexl.createScript(restricted, null, "s.length()"));
for (final String istr : Arrays.asList(src, "`#{s.length()}`", "s.`${s.length()}`", "s?.`${s.length()}`")) {
final JxltEngine.Exception xjxlt = assertThrows(JxltEngine.Exception.class,
() -> jexl.createScript(restricted, null, istr),
"interpolation must honor the features of its script: " + istr);
assertInstanceOf(JexlException.Feature.class, xjxlt.getCause());
}
// the features of the script are more permissive than those of the engine
final JexlEngine rjexl = new JexlBuilder().features(restricted).create();
assertThrows(JxltEngine.Exception.class, () -> rjexl.createScript(src));
assertEquals("3", rjexl.createScript(permissive, null, src).execute(ctxt).toString());
assertThrows(JxltEngine.Exception.class, () -> rjexl.createScript(src));
}

@Test
void testInterpolationHonorsDeclaredAntish() {
final JexlFeatures declared = new JexlFeatures().antishTest(JexlFeatures.namePatterns(Collections.singletonList("x.y")));
final JexlContext ctxt = new MapContext();
ctxt.set("x.y", 42);
// the antish variable is declared by the features of the script, not by those of the engine
final JexlEngine jexl = new JexlBuilder().strict(true).create();
assertEquals(42, jexl.createScript(declared, null, "x.y").execute(ctxt));
assertEquals("42", jexl.createScript(declared, null, "`${x.y}`").execute(ctxt).toString());
assertEquals("42", jexl.createScript(declared, null, "`#{x.y}`").execute(ctxt).toString());
}
}
Loading