From 1fe4baa24d0d35e71e25ffc450977c7b6c2e96a7 Mon Sep 17 00:00:00 2001 From: Ark2307 Date: Wed, 18 Dec 2024 11:55:59 +0530 Subject: [PATCH 1/2] Fixing default block filter --- .../akto/listener/InitializerListener.java | 37 +++++++++++++++++-- .../com/akto/utils/TrafficFilterUtil.java | 5 ++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/apps/dashboard/src/main/java/com/akto/listener/InitializerListener.java b/apps/dashboard/src/main/java/com/akto/listener/InitializerListener.java index c7f56ee795..4567c35154 100644 --- a/apps/dashboard/src/main/java/com/akto/listener/InitializerListener.java +++ b/apps/dashboard/src/main/java/com/akto/listener/InitializerListener.java @@ -2820,16 +2820,45 @@ private static void makeFirstUserAdmin(BackwardCompatibility backwardCompatibili } private static void addDefaultAdvancedFilters(BackwardCompatibility backwardCompatibility){ - if(backwardCompatibility.getAddDefaultFilters() == 0){ + if(backwardCompatibility.getAddDefaultFilters() == 0 || backwardCompatibility.getAddDefaultFilters() < 1734502264){ String contentAllow = "id: DEFAULT_ALLOW_FILTER\nfilter:\n url:\n regex: '.*'"; - String contentBlock = "id: DEFAULT_BLOCK_FILTER\nfilter:\n response_code:\n gte: 400"; + String contentBlock = "id: DEFAULT_BLOCK_FILTER\n" + + "filter:\n" + + " or:\n" + + " - response_code:\n" + + " gte: 400\n" + + " - response_headers:\n" + + " for_one:\n" + + " key:\n" + + " eq: content-type\n" + + " value:\n" + + " contains_either:\n" + + " - html\n" + + " - text/html\n" + + " - request_headers:\n" + + " for_one:\n" + + " key:\n" + + " eq: host\n" + + " value:\n" + + " regex: .*localhost.*"; + AdvancedTrafficFiltersAction action = new AdvancedTrafficFiltersAction(); action.setYamlContent(contentAllow); action.saveYamlTemplateForTrafficFilters(); - action.setYamlContent(contentBlock); - action.saveYamlTemplateForTrafficFilters(); + if(backwardCompatibility.getAddDefaultFilters() != 0){ + Bson defaultFilterQ = Filters.eq(Constants.ID, "DEFAULT_BLOCK_FILTER"); + YamlTemplate blockTemplate = AdvancedTrafficFiltersDao.instance.findOne(defaultFilterQ); + if((blockTemplate.getUpdatedAt() - blockTemplate.getCreatedAt()) <= 10){ + AdvancedTrafficFiltersDao.instance.deleteAll(defaultFilterQ); + action.setYamlContent(contentBlock); + action.saveYamlTemplateForTrafficFilters(); + } + }else{ + action.setYamlContent(contentBlock); + action.saveYamlTemplateForTrafficFilters(); + } BackwardCompatibilityDao.instance.updateOne( Filters.eq("_id", backwardCompatibility.getId()), diff --git a/apps/dashboard/src/main/java/com/akto/utils/TrafficFilterUtil.java b/apps/dashboard/src/main/java/com/akto/utils/TrafficFilterUtil.java index 980eef9084..cc249401f0 100644 --- a/apps/dashboard/src/main/java/com/akto/utils/TrafficFilterUtil.java +++ b/apps/dashboard/src/main/java/com/akto/utils/TrafficFilterUtil.java @@ -38,8 +38,9 @@ public static BasicDBList getFilterTemplates(Map configs){ public static List getDbUpdateForTemplate(String content, String userEmail) throws Exception{ try { String author = userEmail; - int createdAt = Context.now(); - int updatedAt = Context.now(); + int timeNow = Context.now(); + int createdAt = timeNow; + int updatedAt = timeNow; List updates = new ArrayList<>( Arrays.asList( From 0026eacaa74c51dcd9a9a6b7f952211a9ba71c2d Mon Sep 17 00:00:00 2001 From: Ark2307 Date: Wed, 18 Dec 2024 14:15:22 +0530 Subject: [PATCH 2/2] Adding check only for metered accounts --- .../main/java/com/akto/listener/InitializerListener.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/dashboard/src/main/java/com/akto/listener/InitializerListener.java b/apps/dashboard/src/main/java/com/akto/listener/InitializerListener.java index 4567c35154..f1e67c1893 100644 --- a/apps/dashboard/src/main/java/com/akto/listener/InitializerListener.java +++ b/apps/dashboard/src/main/java/com/akto/listener/InitializerListener.java @@ -2842,12 +2842,16 @@ private static void addDefaultAdvancedFilters(BackwardCompatibility backwardComp " value:\n" + " regex: .*localhost.*"; + if(!DashboardMode.isMetered()){ + contentBlock = "id: DEFAULT_BLOCK_FILTER\nfilter:\n response_code:\n gte: 400"; + } + AdvancedTrafficFiltersAction action = new AdvancedTrafficFiltersAction(); action.setYamlContent(contentAllow); action.saveYamlTemplateForTrafficFilters(); - if(backwardCompatibility.getAddDefaultFilters() != 0){ + if(backwardCompatibility.getAddDefaultFilters() != 0 && DashboardMode.isMetered()){ Bson defaultFilterQ = Filters.eq(Constants.ID, "DEFAULT_BLOCK_FILTER"); YamlTemplate blockTemplate = AdvancedTrafficFiltersDao.instance.findOne(defaultFilterQ); if((blockTemplate.getUpdatedAt() - blockTemplate.getCreatedAt()) <= 10){