From eaf2721569c41d6bb9b1d49c078ecdd696b394ad Mon Sep 17 00:00:00 2001 From: Alan Buscaglia Date: Tue, 25 Nov 2025 13:19:49 +0100 Subject: [PATCH] fix: add filters for mongo providers and findings (#9311) --- ui/actions/findings/findings.ts | 65 ++++++++++++++++++++++++++++++- ui/actions/providers/providers.ts | 21 +++++++++- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/ui/actions/findings/findings.ts b/ui/actions/findings/findings.ts index f0bab61f7d..45c63bb331 100644 --- a/ui/actions/findings/findings.ts +++ b/ui/actions/findings/findings.ts @@ -4,6 +4,61 @@ import { redirect } from "next/navigation"; import { apiBaseUrl, getAuthHeaders } from "@/lib"; import { handleApiResponse } from "@/lib/server-actions-helper"; +import { FindingsResponse } from "@/types"; + +interface IncludedItem { + type: string; + id: string; + attributes?: { provider?: string }; + relationships?: { provider?: { data?: { id: string } } }; +} + +type FindingsApiResponse = FindingsResponse & { + included?: IncludedItem[]; +}; + +const filterMongoFindings = ( + result: T, +): T => { + if (!result?.data) return result; + + const included = (result as FindingsApiResponse).included || []; + + // Get IDs of providers containing "mongo" in included items + const mongoProviderIds = new Set( + included + .filter( + (item) => + item.type === "providers" && + item.attributes?.provider?.toLowerCase().includes("mongo"), + ) + .map((item) => item.id), + ); + + // Filter out findings associated with mongo providers + result.data = result.data.filter((finding) => { + const scanId = finding.relationships?.scan?.data?.id; + // Find the scan in included items + const scan = included.find( + (item) => item.type === "scans" && item.id === scanId, + ); + const providerId = scan?.relationships?.provider?.data?.id; + return !providerId || !mongoProviderIds.has(providerId); + }); + + // Filter out mongo-related included items + if ((result as FindingsApiResponse).included) { + (result as FindingsApiResponse).included = included.filter( + (item) => + !( + item.type === "providers" && + item.attributes?.provider?.toLowerCase().includes("mongo") + ), + ); + } + + return result; +}; export const getFindings = async ({ page = 1, @@ -33,7 +88,10 @@ export const getFindings = async ({ const findings = await fetch(url.toString(), { headers, }); - return handleApiResponse(findings); + + const result = await handleApiResponse(findings); + + return filterMongoFindings(result); } catch (error) { console.error("Error fetching findings:", error); return undefined; @@ -70,7 +128,10 @@ export const getLatestFindings = async ({ const findings = await fetch(url.toString(), { headers, }); - return handleApiResponse(findings); + + const result = await handleApiResponse(findings); + + return filterMongoFindings(result); } catch (error) { console.error("Error fetching findings:", error); return undefined; diff --git a/ui/actions/providers/providers.ts b/ui/actions/providers/providers.ts index 08e708b19f..9d782051ff 100644 --- a/ui/actions/providers/providers.ts +++ b/ui/actions/providers/providers.ts @@ -39,7 +39,26 @@ export const getProviders = async ({ headers, }); - return handleApiResponse(response); + const result = (await handleApiResponse(response)) as + | ProvidersApiResponse + | undefined; + + if (result?.data) { + // Filter out providers with provider type containing "mongo" + result.data = result.data.filter( + (provider) => + !provider.attributes?.provider?.toLowerCase().includes("mongo"), + ); + + // Also filter out mongo-related included items if present + if (result.included) { + result.included = result.included.filter( + (item) => !item.type.toLowerCase().includes("mongo"), + ); + } + } + + return result; } catch (error) { console.error("Error fetching providers:", error); return undefined;