Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -68,6 +68,7 @@ public class MaliciousTrafficDetectorTask implements Task {

private static final DataActor dataActor = DataActorFactory.fetchInstance();
private static final LoggerMaker logger = new LoggerMaker(MaliciousTrafficDetectorTask.class);
Map<String, Object> varMap = new HashMap<>();

private static final ObjectMapper objectMapper = new ObjectMapper();

Expand Down Expand Up @@ -147,16 +148,7 @@ private Map<String, FilterConfig> getFilters() {
private boolean validateFilterForRequest(
FilterConfig apiFilter, RawApi rawApi, ApiInfo.ApiInfoKey apiInfoKey, String message) {
try {
Map<String, Object> varMap = apiFilter.resolveVarMap();
VariableResolver.resolveWordList(
varMap,
new HashMap<ApiInfo.ApiInfoKey, List<String>>() {
{
put(apiInfoKey, Collections.singletonList(message));
}
},
apiInfoKey);

varMap.clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to do resolve var map ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, varmap basically contains all the variables via extract command, or wordlist variables. Right now our threat templates are simple, can add this later on

String filterExecutionLogId = UUID.randomUUID().toString();
ValidationResult res =
TestPlugin.validateFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public void setOperand(String operand) {
this.operand = operand;
}

public DataOperandFilterRequest modify(Object data, Object queryset, String operand) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This whole class is a subset of filterActionRequest with one extra field data.

What if we delete this whole class and just use the filterActionRequest ??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no performance impact for now, let's do this later

this.data = data;
this.queryset = queryset;
this.operand = operand;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ public FilterActionRequest(Object querySet, RawApi rawApi, RawApi testRunRawApi,
this.collectionProperty = collectionProperty;
}

public FilterActionRequest modify(Object querySet, RawApi rawApi, RawApi testRunRawApi, ApiInfo.ApiInfoKey apiInfoKey,
String concernedProperty, String concernedSubProperty, List<String> matchingKeySet, List<BasicDBObject> contextEntities,
String operand, String context, Boolean keyValOperandSeen, String bodyOperand, String contextProperty, String collectionProperty) {
this.querySet = querySet;
this.rawApi = rawApi;
this.testRunRawApi = testRunRawApi;
this.apiInfoKey = apiInfoKey;
this.concernedProperty = concernedProperty;
this.concernedSubProperty = concernedSubProperty;
this.matchingKeySet = matchingKeySet;
this.contextEntities = contextEntities;
this.operand = operand;
this.context = context;
this.keyValOperandSeen = keyValOperandSeen;
this.bodyOperand = bodyOperand;
this.contextProperty = contextProperty;
this.collectionProperty = collectionProperty;
return this;
}

public FilterActionRequest() { }

public Object getQuerySet() {
Expand Down
35 changes: 21 additions & 14 deletions libs/utils/src/main/java/com/akto/test_editor/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class Filter {

private FilterAction filterAction;
private static final LoggerMaker loggerMaker = new LoggerMaker(Filter.class);
private final DataOperandFilterRequest dataOperandFilterRequest = new DataOperandFilterRequest();
private final Set<String> s1 = new HashSet<>();
private final Set<String> s2 = new HashSet<>();
private final List<String> output = new ArrayList<>();
private final FilterActionRequest filterActionRequest = new FilterActionRequest();
private final Map<FilterNode, String> childNodeVsValidationReason = new HashMap<>();

public Filter() {
this.filterAction = new FilterAction();
Expand All @@ -40,22 +46,22 @@ public DataOperandsFilterResponse isEndpointValid(FilterNode node, RawApi rawApi
if (node.getOperand().equalsIgnoreCase(TestEditorEnums.PredicateOperator.COMPARE_GREATER.toString())) {
Object updatedQuerySet = filterAction.resolveQuerySetValues(null, node.fetchNodeValues(), varMap);
List<Object> val = (List<Object>) updatedQuerySet;
DataOperandFilterRequest dataOperandFilterRequest = new DataOperandFilterRequest(val.get(0), Arrays.asList(val.get(1)), "gt");
dataOperandFilterRequest.modify(val.get(0), Arrays.asList(val.get(1)), "gt");
ValidationResult validationResult = filterAction.invokeFilter(dataOperandFilterRequest);
return new DataOperandsFilterResponse(validationResult.getIsValid(), matchingKeySet, contextEntities, null, validationResult.getValidationReason());
}
if (node.getOperand().equalsIgnoreCase(TestEditorEnums.PredicateOperator.SSRF_URL_HIT.toString())) {
Object updatedQuerySet = filterAction.resolveQuerySetValues(null, node.fetchNodeValues(), varMap);
List<Object> val = (List<Object>) updatedQuerySet;
DataOperandFilterRequest dataOperandFilterRequest = new DataOperandFilterRequest(null, val, "ssrf_url_hit");
dataOperandFilterRequest.modify(null, val, "ssrf_url_hit");
ValidationResult validationResult = filterAction.invokeFilter(dataOperandFilterRequest);
return new DataOperandsFilterResponse(validationResult.getIsValid(), matchingKeySet, contextEntities, null, validationResult.getValidationReason());
}
if (! (node.getNodeType().toLowerCase().equals(OperandTypes.Data.toString().toLowerCase()) || node.getNodeType().toLowerCase().equals(OperandTypes.Extract.toString().toLowerCase()) || node.getNodeType().toLowerCase().equals(OperandTypes.Context.toString().toLowerCase() ))) {
return new DataOperandsFilterResponse(false, null, null, null);
}
String operand = node.getOperand();
FilterActionRequest filterActionRequest = new FilterActionRequest(node.getValues(), rawApi, testRawApi, apiInfoKey, node.getConcernedProperty(), node.getSubConcernedProperty(), matchingKeySet, contextEntities, operand, context, keyValOperandSeen, node.getBodyOperand(), node.getContextProperty(), node.getCollectionProperty());
filterActionRequest.modify(node.getValues(), rawApi, testRawApi, apiInfoKey, node.getConcernedProperty(), node.getSubConcernedProperty(), matchingKeySet, contextEntities, operand, context, keyValOperandSeen, node.getBodyOperand(), node.getContextProperty(), node.getCollectionProperty());
Object updatedQuerySet = filterAction.resolveQuerySetValues(filterActionRequest, node.fetchNodeValues(), varMap);
filterActionRequest.setQuerySet(updatedQuerySet);
if (node.getOperand().equalsIgnoreCase(ExtractOperator.EXTRACT.toString()) || node.getOperand().equalsIgnoreCase(ExtractOperator.EXTRACTMULTIPLE.toString())) {
Expand Down Expand Up @@ -94,7 +100,7 @@ public DataOperandsFilterResponse isEndpointValid(FilterNode node, RawApi rawApi
FilterNode firstExtractNode = null;
StringBuilder validationReason = new StringBuilder();
try {
Map<FilterNode, String> childNodeVsValidationReason = new HashMap<>();
childNodeVsValidationReason.clear();
for (int i = 0; i < childNodes.size(); i++) {
FilterNode childNode = childNodes.get(i);
boolean skipExecutingExtractNode = skipExtractExecution;
Expand Down Expand Up @@ -152,28 +158,29 @@ public DataOperandsFilterResponse isEndpointValid(FilterNode node, RawApi rawApi
}

public List<String> evaluateMatchingKeySet(List<String> oldSet, List<String> newMatches, String operand) {
Set<String> s1 = new HashSet<>();
s1.clear();
s2.clear();
output.clear();

if (newMatches == null) {
return new ArrayList<>();
return output;
}

if (oldSet == null) {
// doing this for initial step where oldset would be null, hence assigning initially with newmatches
s1 = new HashSet<>(newMatches);
s1.addAll(newMatches);
} else {
s1 = new HashSet<>(oldSet);
s1.addAll(oldSet);
}
Set<String> s2 = new HashSet<>(newMatches);
s2.addAll(newMatches);

if (operand == "and") {
if (operand.equals("and")) {
s1.retainAll(s2);
} else {
s1.addAll(s2);
}

List<String> output = new ArrayList<>();
for (String s: s1) {
output.add(s);
}
output.addAll(s1);
return output;
}

Expand Down
Loading
Loading