fix: add filters for mongo providers and findings (#9311)

This commit is contained in:
Alan Buscaglia
2025-11-25 13:19:49 +01:00
committed by GitHub
parent 10838de636
commit a63a3d3f68
2 changed files with 83 additions and 3 deletions

View File

@@ -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 = <T extends FindingsApiResponse | null | undefined>(
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;

View File

@@ -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;