Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(console): stop reporting a failed IOC lookup as a clean verdict (#965)

Beenu Aroraf0467aac52ed
diff --git a/apps/web/src/components/threat-intel/ThreatIntelLookupHonesty.test.tsx b/apps/web/src/components/threat-intel/ThreatIntelLookupHonesty.test.tsx
new file mode 100644
index 000000000..a6580ff08
--- /dev/null
+++ b/apps/web/src/components/threat-intel/ThreatIntelLookupHonesty.test.tsx
@@ -0,0 +1,160 @@
+/**
+ * Data-honesty gate for the IOC lookup panel.
+ *
+ * `LookupForm` held a `result` and a `notFound` flag, and its `catch` had
+ * nowhere to put "this did not work" except the flag that means "we checked,
+ * and it is clean". So every failure — a transport error, a 5xx, an endpoint
+ * that does not exist — rendered a green **CLEAN · No threat indicators found
+ * for this IOC**.
+ *
+ * It was not a latent branch. The lookup called `/api/v1/enrichment/lookup`,
+ * which the API does not serve; measured against a running CORE stack that
+ * path answers 404, so the panel took the `catch` on every lookup and had
+ * only ever been capable of saying "clean". An analyst checking a live C2
+ * address got an all-clear from a feature that had never once reached a
+ * threat-intel store.
+ *
+ * The property under test is not that the wording changed. It is that a
+ * lookup which did not complete can never render as a safety verdict, in
+ * either direction: a failure must not read clean, and a genuine clean answer
+ * must still read clean rather than being buried under a blanket warning.
+ */
+
+import { describe, expect, it, vi, afterEach } from 'vitest';
+import { cleanup, render, screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+
+const threatIntelApi = vi.hoisted(() => ({
+ lookup: vi.fn(),
+ list: vi.fn(),
+}));
+
+vi.mock('@/lib/api', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('@/lib/api')>();
+ return { ...actual, threatIntelApi };
+});
+
+vi.mock('swr', () => ({
+ __esModule: true,
+ default: () => ({ data: undefined, error: undefined, isLoading: false, mutate: vi.fn() }),
+}));
+
+async function lookUp(value: string) {
+ const { ThreatIntelView } = await import('./ThreatIntelView');
+ render(<ThreatIntelView />);
+ await userEvent.type(screen.getByPlaceholderText(/Enter IP, domain, hash, or URL/i), value);
+ await userEvent.click(screen.getByRole('button', { name: /^Lookup$/i }));
+}
+
+afterEach(() => {
+ cleanup();
+ vi.clearAllMocks();
+});
+
+describe('a lookup that did not complete is never a safety verdict', () => {
+ it('does not render "clean" when the service could not be reached', async () => {
+ threatIntelApi.lookup.mockResolvedValue({
+ status: 'failed',
+ reason: 'the threat-intel service could not be reached',
+ });
+
+ await lookUp('45.155.205.233');
+
+ await waitFor(() => expect(screen.getByText(/not checked/i)).toBeInTheDocument());
+ expect(screen.queryByText(/^clean$/i)).not.toBeInTheDocument();
+ expect(screen.queryByText(/No threat indicators found/i)).not.toBeInTheDocument();
+ });
+
+ it('names the indicator it did not check, so the answer cannot be read as being about it', async () => {
+ threatIntelApi.lookup.mockResolvedValue({
+ status: 'failed',
+ reason: 'the threat-intel service answered HTTP 500',
+ });
+
+ await lookUp('45.155.205.233');
+
+ await waitFor(() => expect(screen.getByText(/HTTP 500/)).toBeInTheDocument());
+ expect(screen.getByText('45.155.205.233')).toBeInTheDocument();
+ expect(screen.getByText(/never checked/i)).toBeInTheDocument();
+ });
+
+ it('does not render "clean" when the lookup call itself rejects', async () => {
+ // The shape the old code could only get wrong. `catch { setNotFound(true) }`
+ // is the whole defect, so this is the assertion that distinguishes the
+ // fixed panel from the broken one; against the pre-fix component it finds
+ // the green CLEAN badge and fails.
+ threatIntelApi.lookup.mockRejectedValue(new Error('Network error talking to /api/v1/enrichment/lookup'));
+
+ await lookUp('45.155.205.233');
+
+ await waitFor(() => expect(screen.getByText(/not checked/i)).toBeInTheDocument());
+ expect(screen.queryByText(/No threat indicators found/i)).not.toBeInTheDocument();
+ });
+
+ it('still says clean when the store answered and holds nothing', async () => {
+ // The other direction. A gate that only forbids the green badge would
+ // pass on a panel that had simply deleted it.
+ threatIntelApi.lookup.mockResolvedValue({ status: 'clean' });
+
+ await lookUp('8.8.8.8');
+
+ await waitFor(() => expect(screen.getByText(/No threat indicators found/i)).toBeInTheDocument());
+ expect(screen.queryByText(/not checked/i)).not.toBeInTheDocument();
+ });
+
+ it('reports a match as malicious', async () => {
+ threatIntelApi.lookup.mockResolvedValue({
+ status: 'match',
+ indicator: {
+ id: 'i-1',
+ type: 'ip',
+ value: '45.155.205.233',
+ confidence: 91,
+ severity: 'high',
+ malicious: true,
+ sources: ['cisa-kev'],
+ description: 'Known command-and-control address',
+ },
+ });
+
+ await lookUp('45.155.205.233');
+
+ await waitFor(() => expect(screen.getByText(/Malicious indicator/i)).toBeInTheDocument());
+ expect(screen.getByText(/Known command-and-control address/i)).toBeInTheDocument();
+ expect(screen.queryByText(/No threat indicators found/i)).not.toBeInTheDocument();
+ });
+});
+
+describe('the client turns a failed request into a failed outcome, not an empty one', () => {
+ it('reports transport failure rather than resolving to no match', async () => {
+ const { threatIntelApi: realApi, ApiError } = await vi.importActual<typeof import('@/lib/api')>('@/lib/api');
+ const fetchMock = vi.fn().mockRejectedValue(new Error('connection refused'));
+ vi.stubGlobal('fetch', fetchMock);
+
+ const outcome = await realApi.lookup('45.155.205.233');
+
+ expect(outcome.status).toBe('failed');
+ expect(ApiError).toBeDefined();
+ vi.unstubAllGlobals();
+ });
+
+ it('treats a degraded store as a failed lookup, because a partial index cannot support "we hold nothing"', async () => {
+ const { threatIntelApi: realApi } = await vi.importActual<typeof import('@/lib/api')>('@/lib/api');
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn().mockResolvedValue({
+ ok: true,
+ status: 200,
+ headers: { get: () => 'application/json' },
+ json: async () => ({ indicators: [], degraded: true, reason: 'vector store unreachable' }),
+ text: async () => '{}',
+ }),
+ );
+
+ const outcome = await realApi.lookup('45.155.205.233');
+
+ expect(outcome.status).toBe('failed');
+ expect(outcome.status === 'failed' && outcome.reason).toMatch(/vector store unreachable/);
+ vi.unstubAllGlobals();
+ });
+});
diff --git a/apps/web/src/components/threat-intel/ThreatIntelView.tsx b/apps/web/src/components/threat-intel/ThreatIntelView.tsx
index 9a5bb4609..3433b3b09 100644
--- a/apps/web/src/components/threat-intel/ThreatIntelView.tsx
+++ b/apps/web/src/components/threat-intel/ThreatIntelView.tsx
@@ -6,6 +6,7 @@ import {
threatIntelApi,
type AlertSeverity,
type IndicatorType,
+ type IOCLookupOutcome,
type ThreatIndicator,
} from '@/lib/api';
import { clsx } from 'clsx';
@@ -109,20 +110,23 @@ const SEVERITY_CONFIG: Record<AlertSeverity, string> = {
function LookupForm() {
const [query, setQuery] = useState('');
- const [result, setResult] = useState<ThreatIndicator | null>(null);
+ // One outcome rather than a `result` plus a `notFound` flag. The two-flag
+ // shape is what allowed a failed lookup to render as a clean one: the
+ // `catch` had nowhere to put "this did not work" except the flag that meant
+ // "we checked, and it is clean".
+ const [outcome, setOutcome] = useState<IOCLookupOutcome | null>(null);
const [isLooking, setIsLooking] = useState(false);
- const [notFound, setNotFound] = useState(false);
const handleLookup = async () => {
if (!query.trim()) return;
setIsLooking(true);
- setNotFound(false);
- setResult(null);
+ setOutcome(null);
try {
- const r = await threatIntelApi.lookup(query.trim());
- setResult(r);
- } catch {
- setNotFound(true);
+ setOutcome(await threatIntelApi.lookup(query.trim()));
+ } catch (err) {
+ // `lookup` reports transport failure in its return value, so reaching
+ // here means the client itself threw. Still not a clean verdict.
+ setOutcome({ status: 'failed', reason: (err as Error)?.message || 'the lookup did not complete' });
} finally {
setIsLooking(false);
}
@@ -149,29 +153,41 @@ function LookupForm() {
</button>
</div>
- {notFound && (
+ {outcome?.status === 'failed' && (
+ <div className="mt-3 flex items-start gap-2 text-sm text-amber-300 bg-amber-500/10 border border-amber-500/20 rounded-lg px-3 py-2">
+ <span className="text-[10px] font-semibold uppercase tracking-wider pt-0.5 shrink-0">
+ not checked
+ </span>
+ <span>
+ Lookup failed — {outcome.reason}. This says nothing about{' '}
+ <span className="font-mono">{query.trim()}</span>; it was never checked.
+ </span>
+ </div>
+ )}
+
+ {outcome?.status === 'clean' && (
<div className="mt-3 flex items-center gap-2 text-sm text-green-400 bg-green-500/10 border border-green-500/20 rounded-lg px-3 py-2">
<span className="text-[10px] font-semibold uppercase tracking-wider">clean</span>
<span>No threat indicators found for this IOC</span>
</div>
)}
- {result && (
+ {outcome?.status === 'match' && (
<div className="mt-3 bg-red-500/5 border border-red-500/20 rounded-lg p-3 space-y-2">
<div className="flex items-center gap-2">
<span className="text-red-400 font-medium text-sm">Malicious indicator</span>
- <span className="text-xs text-gray-500">Confidence: {result.confidence}%</span>
+ <span className="text-xs text-gray-500">Confidence: {outcome.indicator.confidence}%</span>
</div>
- {result.description ? (
- <p className="text-xs text-gray-400">{result.description}</p>
+ {outcome.indicator.description ? (
+ <p className="text-xs text-gray-400">{outcome.indicator.description}</p>
) : null}
<div className="flex flex-wrap gap-1">
- {(result.tags ?? []).map((t) => (
+ {(outcome.indicator.tags ?? []).map((t) => (
<span key={t} className="text-xs bg-gray-800 text-gray-400 px-1.5 py-0.5 rounded">{t}</span>
))}
</div>
<div className="text-xs text-gray-500">
- Sources: {result.sources.join(', ')}
+ Sources: {(outcome.indicator.sources ?? []).join(', ')}
</div>
</div>
)}
diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts
index 6fbda6ac8..31d85ac94 100644
--- a/apps/web/src/lib/api.ts
+++ b/apps/web/src/lib/api.ts
@@ -3053,11 +3053,65 @@ export interface IOCLookup extends ThreatIndicator {
raw?: Record<string, unknown>;
}
+/**
+ * The outcome of one IOC lookup. Three states, deliberately — "the store
+ * answered and holds nothing on this indicator" and "the store did not
+ * answer" are different facts, and only the first of them is good news.
+ *
+ * Collapsing them is what the console used to do: the lookup `catch` set a
+ * `notFound` flag that rendered a green **CLEAN — no threat indicators found
+ * for this IOC**, so a transport error, a 5xx, or a route that did not exist
+ * all read to an analyst as an all-clear on the indicator they were checking.
+ * It called `/api/v1/enrichment/lookup`, which the API does not serve, so
+ * every lookup took that branch and the panel had only ever said "clean".
+ */
+export type IOCLookupOutcome =
+ | { status: 'match'; indicator: ThreatIndicator }
+ | { status: 'clean' }
+ | { status: 'failed'; reason: string };
+
export const threatIntelApi = {
- lookup: (ioc: string) =>
- request<IOCLookup>('/api/v1/enrichment/lookup', {
- params: { ioc },
- }),
+ /**
+ * Look one indicator up in the tenant's threat-intel store.
+ *
+ * Goes to `/api/v1/threat-intel/indicators`, the route that serves the
+ * page's own list — so the lookup reads the same store the table does, and
+ * a match here is an indicator the deployment actually holds. The response
+ * carries `degraded`/`reason`, which the backend already sets when it
+ * answered from a fallback or could not reach its index; that is treated as
+ * a failed lookup rather than a clean one, because a partial store cannot
+ * support "we hold nothing on this".
+ */
+ lookup: async (ioc: string): Promise<IOCLookupOutcome> => {
+ let response: {
+ indicators?: ThreatIndicator[];
+ degraded?: boolean;
+ reason?: string;
+ };
+ try {
+ response = await request('/api/v1/threat-intel/indicators', { params: { q: ioc } });
+ } catch (err) {
+ const status = err instanceof ApiError ? err.status : undefined;
+ // status 0 is `request`'s own marker for "never reached the server".
+ const detail =
+ status === undefined || status === 0
+ ? 'the threat-intel service could not be reached'
+ : `the threat-intel service answered HTTP ${status}`;
+ return { status: 'failed', reason: detail };
+ }
+
+ if (response.degraded) {
+ return {
+ status: 'failed',
+ reason: response.reason?.trim() || 'the threat-intel store answered from a degraded source',
+ };
+ }
+
+ const match = (response.indicators ?? []).find(
+ (candidate) => candidate.value?.toLowerCase() === ioc.toLowerCase(),
+ );
+ return match ? { status: 'match', indicator: match } : { status: 'clean' };
+ },
bulkLookup: (iocs: string[]) =>
request<{ results: IOCLookup[] }>('/api/v1/enrichment/bulk', {