Skip to content
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

Enforce term limits during EdgeQueries #2274

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,6 @@ public Object visit(ASTReferenceExpression node, Object data) {
*/
@Override
public Object visit(ASTAndNode node, Object data) {
if (termCount > maxTerms) {
log.error("Query has too many terms");
throw new IllegalArgumentException("Too many search terms " + termCount);
}

// run the visitor against all of the children
List<List<? extends EdgeContext>> childContexts = new ArrayList<>(node.jjtGetNumChildren());
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
Expand Down Expand Up @@ -274,11 +269,6 @@ public Object visit(ASTAndNode node, Object data) {
*/
@Override
public Object visit(ASTOrNode node, Object data) {
if (termCount > maxTerms) {
log.error("Query has too many terms");
throw new IllegalArgumentException("Too many search terms " + termCount);
}

// run the visitor against all of the children
List<List<? extends EdgeContext>> childContexts = new ArrayList<>(node.jjtGetNumChildren());
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
Expand Down Expand Up @@ -410,10 +400,18 @@ private void checkNotExclusion(IdentityContext context, String msg) {
*/
@Override
public Object visit(ASTEQNode node, Object data) {
termCount++;
incrementTermCountAndCheck();
return visitExpresionNode(node, EQUALS);
}

private void incrementTermCountAndCheck() {
if (++termCount > maxTerms) {
String message = "Exceeded max term limit of " + maxTerms;
log.error(message);
throw new IllegalArgumentException(message);
}
}

/**
* Equals node (=~) should have exactly two children (reference nodes) one child will have the identifier eg: SOURCE ... the other child will have the
* string literal eg: 'searchTerm'
Expand All @@ -428,7 +426,7 @@ public Object visit(ASTEQNode node, Object data) {
*/
@Override
public Object visit(ASTERNode node, Object data) {
termCount++;
incrementTermCountAndCheck();
List<IdentityContext> contexts = (List<IdentityContext>) visitExpresionNode(node, EQUALS_REGEX);
if (contexts.get(0).getIdentity().equals(EDGE_SOURCE)) {
sawEquivalenceRegexSource = true;
Expand All @@ -440,13 +438,13 @@ public Object visit(ASTERNode node, Object data) {

@Override
public Object visit(ASTNRNode node, Object data) {
termCount++;
incrementTermCountAndCheck();
return visitExpresionNode(node, NOT_EQUALS_REGEX);
}

@Override
public Object visit(ASTNENode node, Object data) {
termCount++;
incrementTermCountAndCheck();
return visitExpresionNode(node, NOT_EQUALS);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package datawave.query.jexl.visitors;

import datawave.query.tables.edge.EdgeQueryLogic;
import org.apache.commons.jexl3.JexlFeatures;
import org.apache.commons.jexl3.parser.ASTJexlScript;
import org.apache.commons.jexl3.parser.Parser;
import org.apache.commons.jexl3.parser.StringProvider;
import org.junit.Before;
import org.junit.Test;

import static java.util.Collections.emptyList;
import static org.junit.Assert.assertThrows;

public class EdgeTableRangeBuildingVisitorTest {

private int termLimit = 3;
private Parser parser;

private EdgeTableRangeBuildingVisitor visitor;

@Before
public void setup() {
parser = new Parser(new StringProvider(";"));

visitor = new EdgeTableRangeBuildingVisitor(false, emptyList(), termLimit, emptyList());
}

@Test
public void shouldEnforceTermLimit() {
ASTJexlScript parsedQuery = parseQuery("TYPE == 'like it' OR TYPE == 'love it' OR TYPE == 'gotta have it' OR TYPE == 'hand it over or else'");

assertThrows(IllegalArgumentException.class, () -> parsedQuery.jjtAccept(visitor, null));
}

private ASTJexlScript parseQuery(String query) {
return parser.parse(null, new JexlFeatures(), EdgeQueryLogic.fixQueryString(query), null);
}
}
Loading