From 7951c7f68dc1cbb8e575f1eff10f0d68551480f2 Mon Sep 17 00:00:00 2001 From: Ark2307 Date: Tue, 12 Nov 2024 17:37:19 +0530 Subject: [PATCH] Fixing replace body in test roles --- .../src/main/java/com/akto/dto/RawApi.java | 2 +- .../java/com/akto/dto/test_editor/Util.java | 62 +++++++++++++++++++ .../akto/dto/testing/HardcodedAuthParam.java | 20 +----- .../dto/testing/LoginRequestAuthParam.java | 24 +------ .../main/java/com/akto/dto/testing/Utils.java | 36 +++++++++++ .../akto/util/JsonStringPayloadModifier.java | 26 ++++---- .../com/akto/util/TokenPayloadModifier.java | 1 - .../main/java/com/akto/test_editor/Utils.java | 52 +--------------- 8 files changed, 116 insertions(+), 107 deletions(-) create mode 100644 libs/dao/src/main/java/com/akto/dto/test_editor/Util.java create mode 100644 libs/dao/src/main/java/com/akto/dto/testing/Utils.java diff --git a/libs/dao/src/main/java/com/akto/dto/RawApi.java b/libs/dao/src/main/java/com/akto/dto/RawApi.java index d1b70ea2f3..f815f5f6b1 100644 --- a/libs/dao/src/main/java/com/akto/dto/RawApi.java +++ b/libs/dao/src/main/java/com/akto/dto/RawApi.java @@ -44,7 +44,7 @@ public BasicDBObject fetchReqPayload() { reqBody = "{\"json\": "+reqBody+"}"; } try { - payload = BasicDBObject.parse(reqBody); + payload = BasicDBObject.parse(reqBody); } catch (Exception e) { payload = new BasicDBObject(); } diff --git a/libs/dao/src/main/java/com/akto/dto/test_editor/Util.java b/libs/dao/src/main/java/com/akto/dto/test_editor/Util.java new file mode 100644 index 0000000000..20bb0945a6 --- /dev/null +++ b/libs/dao/src/main/java/com/akto/dto/test_editor/Util.java @@ -0,0 +1,62 @@ +package com.akto.dto.test_editor; + +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import com.mongodb.BasicDBList; +import com.mongodb.BasicDBObject; + +public class Util { + public static boolean modifyValueInPayload(Object obj, String parentKey, String queryKey, Object queryVal){ + boolean res = false; + if (obj instanceof BasicDBObject) { + BasicDBObject basicDBObject = (BasicDBObject) obj; + + Set keySet = basicDBObject.keySet(); + + for(String key: keySet) { + if (key == null) { + continue; + } + Object value = basicDBObject.get(key); + + if (!( (value instanceof BasicDBObject) || (value instanceof BasicDBList) )) { + if (key.equalsIgnoreCase(queryKey)) { + basicDBObject.remove(key); + basicDBObject.put(queryKey, queryVal); + return true; + } + } + + if (value instanceof BasicDBList) { + BasicDBList valList = (BasicDBList) value; + if (valList.size() == 0 && key.equalsIgnoreCase(queryKey)) { + List queryList = Collections.singletonList(queryVal); + basicDBObject.remove(key); + basicDBObject.put(queryKey, queryList); + return true; + } else if (valList.size() > 0 && !( (valList.get(0) instanceof BasicDBObject) || (valList.get(0) instanceof BasicDBList) ) && key.equalsIgnoreCase(queryKey)) { + List queryList = Collections.singletonList(queryVal); + basicDBObject.remove(key); + basicDBObject.put(queryKey, queryList); + return true; + } + } + + res = modifyValueInPayload(value, key, queryKey, queryVal); + if (res) { + break; + } + } + } else if (obj instanceof BasicDBList) { + for(Object elem: (BasicDBList) obj) { + res = modifyValueInPayload(elem, parentKey, queryKey, queryVal); + if (res) { + break; + } + } + } + return res; + } +} diff --git a/libs/dao/src/main/java/com/akto/dto/testing/HardcodedAuthParam.java b/libs/dao/src/main/java/com/akto/dto/testing/HardcodedAuthParam.java index 3b68b4d421..4b34559d4f 100644 --- a/libs/dao/src/main/java/com/akto/dto/testing/HardcodedAuthParam.java +++ b/libs/dao/src/main/java/com/akto/dto/testing/HardcodedAuthParam.java @@ -1,14 +1,7 @@ package com.akto.dto.testing; -import com.akto.dto.HttpRequestParams; import com.akto.dto.OriginalHttpRequest; -import com.akto.util.CookieTransformer; -import com.akto.util.JSONUtils; -import com.akto.util.JsonStringPayloadModifier; import com.akto.util.TokenPayloadModifier; -import com.mongodb.BasicDBObject; - -import java.util.*; public class HardcodedAuthParam extends AuthParam { private Location where; @@ -40,18 +33,7 @@ public boolean removeAuthTokens(OriginalHttpRequest request) { @Override public boolean authTokenPresent(OriginalHttpRequest request) { - if (this.key == null) return false; - String k = this.key.toLowerCase().trim(); - - if (where.toString().equals(AuthParam.Location.BODY.toString())) { - BasicDBObject basicDBObject = BasicDBObject.parse(request.getBody()); - BasicDBObject data = JSONUtils.flattenWithDots(basicDBObject); - return data.keySet().contains(this.key); - } else { - Map> headers = request.getHeaders(); - List cookieList = headers.getOrDefault("cookie", new ArrayList<>()); - return headers.containsKey(k) || CookieTransformer.isKeyPresentInCookie(cookieList, k); - } + return Utils.isRequestKeyPresent(this.key, request, where); } public Location getWhere() { diff --git a/libs/dao/src/main/java/com/akto/dto/testing/LoginRequestAuthParam.java b/libs/dao/src/main/java/com/akto/dto/testing/LoginRequestAuthParam.java index 0858466109..2bfc45d162 100644 --- a/libs/dao/src/main/java/com/akto/dto/testing/LoginRequestAuthParam.java +++ b/libs/dao/src/main/java/com/akto/dto/testing/LoginRequestAuthParam.java @@ -1,17 +1,7 @@ package com.akto.dto.testing; import com.akto.dto.OriginalHttpRequest; -import com.akto.util.CookieTransformer; -import com.akto.util.JSONUtils; -import com.akto.util.JsonStringPayloadModifier; import com.akto.util.TokenPayloadModifier; -import com.mongodb.BasicDBObject; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - public class LoginRequestAuthParam extends AuthParam { private Location where; @@ -43,19 +33,7 @@ public boolean removeAuthTokens(OriginalHttpRequest request) { @Override public boolean authTokenPresent(OriginalHttpRequest request) { - if (this.key == null) return false; - String k = this.key.toLowerCase().trim(); - - if (where.toString().equals(AuthParam.Location.BODY.toString())) { - String body = request.getBody(); - BasicDBObject basicDBObject = BasicDBObject.parse(request.getBody()); - BasicDBObject data = JSONUtils.flattenWithDots(basicDBObject); - return data.keySet().contains(this.key); - } else { - Map> headers = request.getHeaders(); - List cookieList = headers.getOrDefault("cookie", new ArrayList<>()); - return headers.containsKey(k) || CookieTransformer.isKeyPresentInCookie(cookieList, k); - } + return Utils.isRequestKeyPresent(this.key, request, where); } public Location getWhere() { diff --git a/libs/dao/src/main/java/com/akto/dto/testing/Utils.java b/libs/dao/src/main/java/com/akto/dto/testing/Utils.java new file mode 100644 index 0000000000..3fddbaf5d3 --- /dev/null +++ b/libs/dao/src/main/java/com/akto/dto/testing/Utils.java @@ -0,0 +1,36 @@ +package com.akto.dto.testing; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.akto.dto.OriginalHttpRequest; +import com.akto.dto.testing.AuthParam.Location; +import com.akto.util.CookieTransformer; +import com.akto.util.JSONUtils; +import com.mongodb.BasicDBObject; + +public class Utils { + public static boolean isRequestKeyPresent(String key, OriginalHttpRequest request, Location where){ + if (key == null) return false; + String k = key.toLowerCase().trim(); + if (where.toString().equals(AuthParam.Location.BODY.toString())) { + BasicDBObject basicDBObject = BasicDBObject.parse(request.getBody()); + BasicDBObject data = JSONUtils.flattenWithDots(basicDBObject); + boolean exists = data.keySet().contains(key); + if(!exists){ + for(String payloadKey: data.keySet()){ + if(payloadKey.contains(key)){ + exists = true; + break; + } + } + } + return exists; + } else { + Map> headers = request.getHeaders(); + List cookieList = headers.getOrDefault("cookie", new ArrayList<>()); + return headers.containsKey(k) || CookieTransformer.isKeyPresentInCookie(cookieList, k); + } + } +} diff --git a/libs/dao/src/main/java/com/akto/util/JsonStringPayloadModifier.java b/libs/dao/src/main/java/com/akto/util/JsonStringPayloadModifier.java index fce56f6c43..017d4f27ad 100644 --- a/libs/dao/src/main/java/com/akto/util/JsonStringPayloadModifier.java +++ b/libs/dao/src/main/java/com/akto/util/JsonStringPayloadModifier.java @@ -1,35 +1,37 @@ package com.akto.util; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mongodb.BasicDBObject; public class JsonStringPayloadModifier { private static final ObjectMapper mapper = new ObjectMapper(); - public static String jsonStringPayloadModifier(String data, String path, String newVal) throws Exception { try { JsonNode origRequestNode = mapper.readValue(data, JsonNode.class); JsonNode node = origRequestNode; - JsonNode parentNode = origRequestNode; String []keys = path.split("\\."); for (int i=0; i keySet = basicDBObject.keySet(); - - for(String key: keySet) { - if (key == null) { - continue; - } - Object value = basicDBObject.get(key); - - if (!( (value instanceof BasicDBObject) || (value instanceof BasicDBList) )) { - if (key.equalsIgnoreCase(queryKey)) { - basicDBObject.remove(key); - basicDBObject.put(queryKey, queryVal); - return true; - } - } - - if (value instanceof BasicDBList) { - BasicDBList valList = (BasicDBList) value; - if (valList.size() == 0 && key.equalsIgnoreCase(queryKey)) { - List queryList = Collections.singletonList(queryVal); - basicDBObject.remove(key); - basicDBObject.put(queryKey, queryList); - return true; - } else if (valList.size() > 0 && !( (valList.get(0) instanceof BasicDBObject) || (valList.get(0) instanceof BasicDBList) ) && key.equalsIgnoreCase(queryKey)) { - List queryList = Collections.singletonList(queryVal); - basicDBObject.remove(key); - basicDBObject.put(queryKey, queryList); - return true; - } - } - - res = modifyValueInPayload(value, key, queryKey, queryVal); - if (res) { - break; - } - } - } else if (obj instanceof BasicDBList) { - for(Object elem: (BasicDBList) obj) { - res = modifyValueInPayload(elem, parentKey, queryKey, queryVal); - if (res) { - break; - } - } - } - - return res; + return com.akto.dto.test_editor.Util.modifyValueInPayload(obj, parentKey, queryKey, queryVal); } public static String applyRegexModifier(String data, String regex, String replaceWith) {