From 88a95cee3f08eb6c71525b9d87e7c6b65eab4631 Mon Sep 17 00:00:00 2001 From: Avneesh Hota Date: Wed, 14 Aug 2024 13:59:45 +0530 Subject: [PATCH 1/2] fix first url param merging --- .../java/com/akto/runtime/APICatalogSync.java | 6 ++-- .../java/com/akto/parsers/TestMergingNew.java | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/apps/api-runtime/src/main/java/com/akto/runtime/APICatalogSync.java b/apps/api-runtime/src/main/java/com/akto/runtime/APICatalogSync.java index 6c93a80b27..61d6e2f03e 100644 --- a/apps/api-runtime/src/main/java/com/akto/runtime/APICatalogSync.java +++ b/apps/api-runtime/src/main/java/com/akto/runtime/APICatalogSync.java @@ -709,9 +709,7 @@ public static boolean isNumber(String val) { public static URLTemplate tryParamteresingUrl(URLStatic newUrl){ String[] tokens = tokenize(newUrl.getUrl()); - if(tokens.length < 2){ - return null; - } + boolean tokensBelowThreshold = tokens.length < 2; Pattern pattern = patternToSubType.get(SingleTypeInfo.UUID); boolean allNull = true; SuperType[] newTypes = new SuperType[tokens.length]; @@ -733,7 +731,7 @@ public static URLTemplate tryParamteresingUrl(URLStatic newUrl){ if(tokens[i] != null){ SubType tempSubType = KeyTypes.findSubType(tokens[i], ""+i, null,true); - if(isValidSubtype(tempSubType)){ + if(!tokensBelowThreshold && isValidSubtype(tempSubType)){ newTypes[i] = SuperType.STRING; tokens[i] = null; }else if(isAlphanumericString(tempToken)){ diff --git a/apps/api-runtime/src/test/java/com/akto/parsers/TestMergingNew.java b/apps/api-runtime/src/test/java/com/akto/parsers/TestMergingNew.java index 72ab95fdbf..c452b12ba1 100644 --- a/apps/api-runtime/src/test/java/com/akto/parsers/TestMergingNew.java +++ b/apps/api-runtime/src/test/java/com/akto/parsers/TestMergingNew.java @@ -164,6 +164,34 @@ public void testmultipleUUIDForceMerge(){ assertNotNull(singleTypeInfo2); } + @Test + public void testFirstUrlParameterMerging(){ + SingleTypeInfoDao.instance.getMCollection().drop(); + ApiCollectionsDao.instance.getMCollection().drop(); + HttpCallParser parser = new HttpCallParser("userIdentifier", 1, 1, 1, true); + List responseParams = new ArrayList<>(); + List urls = new ArrayList<>(); + urls.add("/D654447FF7"); // merges to /STRING + urls.add("/c7e5e544-4040-4405-b2a7-22bf9c5286fb"); // merges to /STRING + urls.add("/3"); // merges to /INTEGER + urls.add(new ObjectId().toHexString()); // merges to /OBJECT_ID + urls.add("test@akto.io"); //this shouldn't get merge because tokensBelowThreshold and subtype match + + int i = 0; + for (String c: urls) { + HttpResponseParams resp = createDifferentHttpResponseParams(i*100, c); + responseParams.add(resp); + i +=1; + } + + parser.syncFunction(responseParams, false, true, null); + parser.apiCatalogSync.syncWithDB(false, true, SyncLimit.noLimit); + parser.apiCatalogSync.buildFromDB(false, true); + assertEquals(1, parser.apiCatalogSync.getDbState(123).getStrictURLToMethods().size()); + assertEquals(3, parser.apiCatalogSync.getDbState(123).getTemplateURLToMethods().size()); + + } + @Test public void testUUIDForceMerge() { SingleTypeInfoDao.instance.getMCollection().drop(); From 8e2f819ad3d2da61ae4b6a96b234a3ede25b70f1 Mon Sep 17 00:00:00 2001 From: Avneesh Hota Date: Wed, 14 Aug 2024 14:59:54 +0530 Subject: [PATCH 2/2] fix first url param merging in hybrid runtime --- .../main/java/com/akto/hybrid_runtime/APICatalogSync.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/apps/mini-runtime/src/main/java/com/akto/hybrid_runtime/APICatalogSync.java b/apps/mini-runtime/src/main/java/com/akto/hybrid_runtime/APICatalogSync.java index 9a32eabc8c..b98d977dbc 100644 --- a/apps/mini-runtime/src/main/java/com/akto/hybrid_runtime/APICatalogSync.java +++ b/apps/mini-runtime/src/main/java/com/akto/hybrid_runtime/APICatalogSync.java @@ -526,9 +526,7 @@ public static boolean isNumber(String val) { public static URLTemplate tryParamteresingUrl(URLStatic newUrl){ String[] tokens = tokenize(newUrl.getUrl()); - if(tokens.length < 2){ - return null; - } + boolean tokensBelowThreshold = tokens.length < 2; Pattern pattern = patternToSubType.get(SingleTypeInfo.UUID); boolean allNull = true; SuperType[] newTypes = new SuperType[tokens.length]; @@ -550,7 +548,7 @@ public static URLTemplate tryParamteresingUrl(URLStatic newUrl){ if(tokens[i] != null){ SubType tempSubType = KeyTypes.findSubType(tokens[i], ""+i, null,true); - if(isValidSubtype(tempSubType)){ + if(!tokensBelowThreshold && isValidSubtype(tempSubType)){ newTypes[i] = SuperType.STRING; tokens[i] = null; }else if(isAlphanumericString(tempToken)){