Repository navigation
JEXL-476: Add the safeUndefined option to restrict safe navigation to null #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,7 +275,7 @@ protected Object call(final JexlNode node, final Object target, final Object fun | |
| // ...(x)(y) | ||
| methodName = null; | ||
| cacheable = false; | ||
| } else if (!node.isSafeLhs(isSafe())) { | ||
| } else if (!isSafeUndefined() || !node.isSafeLhs(isSafe())) { | ||
| return unsolvableMethod(node, "?(...)"); | ||
| } else { | ||
| // safe lhs | ||
|
|
@@ -391,7 +391,7 @@ protected Object call(final JexlNode node, final Object target, final Object fun | |
| throw invocationException(node, methodName, xany); | ||
| } | ||
| // we have either evaluated and returned or no method was found | ||
| return node.isSafeLhs(isSafe()) | ||
| return isSafeUndefined() && node.isSafeLhs(isSafe()) | ||
| ? null | ||
| : unsolvableMethod(node, methodName, argv); | ||
| } | ||
|
|
@@ -1721,7 +1721,7 @@ private Object visit(final ASTMethodNode node, final Object antish, final Object | |
| object = data; | ||
| if (object == null) { | ||
| // no object, we fail | ||
| return node.isSafeLhs(isSafe()) | ||
| return isSafe() || isSafeAccess(methodNode) | ||
| ? null | ||
| : unsolvableMethod(methodNode, "<null>.<?>(...)"); | ||
| } | ||
|
|
@@ -1736,9 +1736,9 @@ private Object visit(final ASTMethodNode node, final Object antish, final Object | |
| for (int a = 1; a < node.jjtGetNumChildren(); ++a) { | ||
| if (result == null) { | ||
| // no method, we fail// variable unknown in context and not a local | ||
| return node.isSafeLhs(isSafe()) | ||
| return isSafeUndefined() && node.isSafeLhs(isSafe()) | ||
| ? null | ||
| : unsolvableMethod(methodNode, "<?>.<null>(...)"); | ||
| : unsolvableMethod(node, "<?>.<null>(...)"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intended: the ternary, |
||
| } | ||
| final ASTArguments argNode = (ASTArguments) node.jjtGetChild(a); | ||
| result = call(node, object, result, argNode); | ||
|
|
@@ -1902,6 +1902,8 @@ protected Object visit(final ASTReference node, final Object data) { | |
| boolean antish = !(parent instanceof ASTReference) && options.isAntish(); | ||
| // the index of the access to apply to the object | ||
| int deref = 0; | ||
| // whether the variable the reference starts with is undefined | ||
| boolean undef = false; | ||
| main: | ||
| for (int c = 0; c < numChildren; c++) { | ||
| objectNode = node.jjtGetChild(c); | ||
|
|
@@ -1939,6 +1941,10 @@ protected Object visit(final ASTReference node, final Object data) { | |
| object = objectNode.jjtAccept(this, object); | ||
| deref = c + 1; | ||
| cancelCheck(node); | ||
| if (object == null && c == 0 && objectNode instanceof ASTIdentifier) { | ||
| // a null variable or an undefined one ? | ||
| undef = !isVariableDefined(frame, ((ASTIdentifier) objectNode).getName()); | ||
| } | ||
| if (object != null) { | ||
| // disallow mixing antish variable & bean with same root; avoid ambiguity | ||
| antish = false; | ||
|
|
@@ -1972,7 +1978,10 @@ protected Object visit(final ASTReference node, final Object data) { | |
| break main; | ||
| } | ||
| // solve antish | ||
| object = context.get(ant.toString()); | ||
| final String aname = ant.toString(); | ||
| object = context.get(aname); | ||
| // an antish variable may be defined and null | ||
| undef = undef && object == null && !context.has(aname); | ||
| } else if (c != numChildren - 1) { | ||
| // only the last one may be null | ||
| ptyNode = c == 0 && numChildren > 1 ? node.jjtGetChild(1) : objectNode; | ||
|
|
@@ -1981,9 +1990,11 @@ protected Object visit(final ASTReference node, final Object data) { | |
| } | ||
| // dealing with null | ||
| if (object == null) { | ||
| // safe navigation applies to null, to an undefined variable as an option | ||
| final boolean nullable = !undef || isSafeUndefined(); | ||
| if (ptyNode != null) { | ||
| // the access applied to null decides, as in x?.y, x?.y() or x?[y] | ||
| if (isSafe() || deref < numChildren && isSafeAccess(node.jjtGetChild(deref))) { | ||
| if (nullable && (isSafe() || deref < numChildren && isSafeAccess(node.jjtGetChild(deref)))) { | ||
| return null; | ||
| } | ||
| if (ant != null) { | ||
|
|
@@ -1995,7 +2006,7 @@ protected Object visit(final ASTReference node, final Object data) { | |
| stringifyProperty(ptyNode), ptyNode == objectNode, null); | ||
| } | ||
| if (antish) { | ||
| if (node.isSafeLhs(isSafe())) { | ||
| if (nullable && node.isSafeLhs(isSafe())) { | ||
| return null; | ||
| } | ||
| final String aname = Objects.toString(ant, "?"); | ||
|
|
@@ -2010,20 +2021,6 @@ protected Object visit(final ASTReference node, final Object data) { | |
| return object; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether an access in a reference is explicitly safe, as in ?.y, ?.y() or ?[y]. | ||
| * | ||
| * @param access the access, a method node being checked through the member it calls | ||
| * @return true if the access is safe, false otherwise | ||
| */ | ||
| private static boolean isSafeAccess(final JexlNode access) { | ||
| final JexlNode member = access instanceof ASTMethodNode ? access.jjtGetChild(0) : access; | ||
| if (member instanceof ASTIdentifierAccess) { | ||
| return ((ASTIdentifierAccess) member).isSafe(); | ||
| } | ||
| return member instanceof ASTArrayAccess && ((ASTArrayAccess) member).isSafeChild(0); | ||
| } | ||
|
|
||
| @Override | ||
| protected Object visit(final ASTReferenceExpression node, final Object data) { | ||
| return node.jjtGetChild(0).jjtAccept(this, data); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -663,8 +663,8 @@ protected Object getAttribute(final Object object, final Object attribute, final | |
| + ", property: " + attribute; | ||
| throw new UnsupportedOperationException(error, xcause); | ||
| } | ||
| final boolean safe = node instanceof ASTIdentifierAccess && ((ASTIdentifierAccess) node).isSafe(); | ||
| if (safe) { | ||
| // safe navigation may apply to an undefined property, an access that failed remains an error | ||
| if (xcause == null && isSafeUndefined(node)) { | ||
| return null; | ||
| } | ||
| final String attrStr = Objects.toString(attribute, null); | ||
|
|
@@ -706,7 +706,8 @@ protected Object getVariable(final Frame frame, final LexicalScope block, final | |
| if (!context.has(name)) { | ||
| // not defined, ignore in some cases... | ||
| final boolean ignore = identifier.jjtGetParent() instanceof ASTReference | ||
| || isSafe() && (symbol >= 0 || identifier.jjtGetParent() instanceof ASTAssignment); | ||
| || isSafeUndefined() && isSafe() | ||
| && (symbol >= 0 || identifier.jjtGetParent() instanceof ASTAssignment); | ||
| if (!ignore) { | ||
| return undefinedVariable(identifier, name); // undefined | ||
| } | ||
|
|
@@ -770,6 +771,82 @@ protected boolean isSilent() { | |
| return options.isSilent(); | ||
| } | ||
|
|
||
| /** | ||
| * Whether safe navigation applies to what is undefined in this interpreter. | ||
| * | ||
| * @return true if safe navigation applies to undefined and null, false if it only applies to null | ||
| */ | ||
| protected boolean isSafeUndefined() { | ||
| return options.isSafeUndefined(); | ||
| } | ||
|
|
||
| /** | ||
| * Whether safe navigation excuses an undefined property. | ||
| * <p>The access to the property is explicitly safe, as in x?.y or x?[y], or the access that follows it is, | ||
| * as in x.y?.z; the safe option does not excuse an undefined property.</p> | ||
| * | ||
| * @param node the node accessing the property | ||
| * @return true if the property evaluates as null, false if it is an error | ||
| */ | ||
| protected boolean isSafeUndefined(final JexlNode node) { | ||
| if (!isSafeUndefined()) { | ||
| return false; | ||
| } | ||
| JexlNode access = node; | ||
| JexlNode parent = node.jjtGetParent(); | ||
| if (parent instanceof ASTArrayAccess) { | ||
| // an index, as in x?[y] or x[y]?[z] | ||
| final ASTArrayAccess indexed = (ASTArrayAccess) parent; | ||
| final int next = indexOf(indexed, node) + 1; | ||
| if (indexed.isSafeChild(next - 1)) { | ||
| return true; | ||
| } | ||
|
Comment on lines
+800
to
+803
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not an issue: |
||
| if (next < indexed.jjtGetNumChildren()) { | ||
| return indexed.isSafeChild(next); | ||
| } | ||
| access = indexed; | ||
| parent = indexed.jjtGetParent(); | ||
| } else if (isSafeAccess(node)) { | ||
| return true; | ||
| } | ||
| // the access that follows, as in x.y?.z | ||
| if (parent == null) { | ||
| return false; | ||
| } | ||
| final int next = indexOf(parent, access) + 1; | ||
| return next > 0 && next < parent.jjtGetNumChildren() && isSafeAccess(parent.jjtGetChild(next)); | ||
| } | ||
|
|
||
| /** | ||
| * Seeks a node in its parent. | ||
| * | ||
| * @param parent the parent | ||
| * @param child the child | ||
| * @return the index of the child in its parent, -1 if not found | ||
| */ | ||
| private static int indexOf(final JexlNode parent, final JexlNode child) { | ||
| for (int c = 0; c < parent.jjtGetNumChildren(); ++c) { | ||
| if (parent.jjtGetChild(c) == child) { | ||
| return c; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether an access in a reference is explicitly safe, as in ?.y, ?.y() or ?[y]. | ||
| * | ||
| * @param access the access, a method node being checked through the member it calls | ||
| * @return true if the access is safe, false otherwise | ||
| */ | ||
| protected static boolean isSafeAccess(final JexlNode access) { | ||
| final JexlNode member = access instanceof ASTMethodNode ? access.jjtGetChild(0) : access; | ||
| if (member instanceof ASTIdentifierAccess) { | ||
| return ((ASTIdentifierAccess) member).isSafe(); | ||
| } | ||
| return member instanceof ASTArrayAccess && ((ASTArrayAccess) member).isSafeChild(0); | ||
| } | ||
|
|
||
| /** | ||
| * Whether this interpreter is currently evaluating with a strict engine flag. | ||
| * | ||
|
|
@@ -1134,7 +1211,7 @@ protected Object unsolvableMethod(final JexlNode node, final String method) { | |
| * @return throws JexlException if strict and not silent, null otherwise | ||
| */ | ||
| protected Object unsolvableMethod(final JexlNode node, final String method, final Object[] args) { | ||
| if (isStrictEngine()) { | ||
| if (isStrictEngine() && !isTernaryProtected(node)) { | ||
| throw new JexlException.Method(node, method, args); | ||
| } | ||
| if (logger.isDebugEnabled()) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cast could not throw, it is inside the
if (methodNode instanceof ASTIdentifierAccess)branch. Replaced byisSafeAccess(methodNode)in 4e2f8b3 anyway, which is equivalent there and reads better.