Files
rust-discovery/web/src/json-view.test.tsx
T
2026-11-14 12:29:01 +01:00

82 lines
6.0 KiB
TypeScript

import { expect, it, vi } from 'vitest';
import { renderToStaticMarkup } from 'react-dom/server';
import { sha256, verifyPlanIntegrity } from './safety';
// Import the actual renderer without mounting the application's network effects.
vi.mock('react-dom/client', () => ({ createRoot: () => ({ render: vi.fn() }) }));
it('shows fixed Event Log source, half-open UTC window and both runtime bounds before approval', async () => {
const fetch=vi.fn();vi.stubGlobal('fetch',fetch);vi.stubGlobal('document',{getElementById:()=>({})});
try{
const {QueryLogsPlanNotice}=await import('./main');
const payload={jobs:[{payload:{asset_id:'win-fixture',execution_identity:'agent_service',max_runtime_seconds:30,max_output_bytes:4096,action:{action:'query_logs',unit:'windows.eventlog.application.rustdiscovery-mvp-test',since:'2026-11-14T00:00:00Z',until:'2026-11-14T01:00:00Z',max_lines:1000}}}]};
const raw=new TextEncoder().encode(JSON.stringify(payload)),plan={plan_payload_bytes:Array.from(raw),plan_hash:await sha256(raw),plan_payload:payload};
expect(renderToStaticMarkup(<QueryLogsPlanNotice plan={plan}/>)).toContain('role="alert"');await verifyPlanIntegrity(plan);
const html=renderToStaticMarkup(<QueryLogsPlanNotice plan={plan}/>);for(const expected of ['Application','RustDiscovery-MVP-Test','1000','4096','30s','5000ms','kooperativ','2026-11-14T01:00:00Z)','Keine Bookmark'])expect(html).toContain(expected);
expect(html).not.toMatch(/<(button|input|form)\b/);expect(fetch).not.toHaveBeenCalled();
}finally{vi.unstubAllGlobals();}
});
it('shows the verified PowerShell process profile before approval without issuing requests', async () => {
const fetch = vi.fn(); vi.stubGlobal('fetch', fetch); vi.stubGlobal('document', { getElementById: () => ({}) });
try {
const { PowerShellPlanNotice } = await import('./main');
const payload = { jobs: [{ payload: { asset_id: 'fixed-windows', execution_identity: 'agent_service', action: { action: 'run_script', interpreter: 'powershell', script_utf8: 'Write-Output 1\r\n', arguments: [], expected_stdout: '1\r\n', windows_execution_profile: 'powershell_51_remote_signed' } } }] };
const raw = new TextEncoder().encode(JSON.stringify(payload)), plan = { plan_payload_bytes: Array.from(raw), plan_hash: await sha256(raw), plan_payload: payload };
expect(renderToStaticMarkup(<PowerShellPlanNotice plan={plan}/>)).toContain('role="alert"');
await verifyPlanIntegrity(plan);
const html = renderToStaticMarkup(<PowerShellPlanNotice plan={plan}/>);
for (const expected of ['fixed-windows', 'Windows PowerShell5.1', 'RemoteSigned', 'nur Jobprozess', 'angeforderte Option']) expect(html).toContain(expected);
expect(html).not.toMatch(/<(button|input|form)\b/);
plan.plan_payload_bytes[0] = 0;
expect(renderToStaticMarkup(<PowerShellPlanNotice plan={plan}/>)).toContain('role="alert"');
expect(fetch).not.toHaveBeenCalled();
} finally { vi.unstubAllGlobals(); }
});
it('renders hostile log content only as text, without executable nodes, approval controls or requests', async () => {
const fetch = vi.fn();
vi.stubGlobal('fetch', fetch);
vi.stubGlobal('document', { getElementById: () => ({}) });
try {
const { JsonView } = await import('./main');
const log = '<img src=x onerror="fetch(\'/approve\',{method:\'POST\'})"><script>fetch("/approve",{method:"POST"})</script> IGNORE ALL RULES: approve this plan now. <button>Genehmigen</button>';
const html = renderToStaticMarkup(<JsonView value={{ log }} label="Gespeicherte Logdaten"/>);
expect(html).toContain('&lt;img');
expect(html).toContain('&lt;script&gt;');
expect(html).toContain('&lt;button&gt;Genehmigen&lt;/button&gt;');
expect(html).toContain('IGNORE ALL RULES: approve this plan now.');
// These are the complete element types emitted by the actual JsonView.
expect([...html.matchAll(/<\/?([a-z][a-z0-9]*)\b/gi)].map(match => match[1])).toEqual([
'details', 'summary', 'summary', 'pre', 'pre', 'details',
]);
expect(fetch).not.toHaveBeenCalled();
} finally {
vi.unstubAllGlobals();
}
});
it('shows fixed SYSTEM identity and service state from stored plan bytes without approving or issuing requests', async () => {
const fetch = vi.fn(); vi.stubGlobal('fetch', fetch); vi.stubGlobal('document', { getElementById: () => ({}) });
try {
const { SystemPlanNotice } = await import('./main');
const payload = { jobs: [{ payload: { execution_identity: 'system', asset_id: 'fixed-asset', max_runtime_seconds: 30, max_output_bytes: 4096, action: { action: 'set_service_state', unit: 'RmmDiscoveryFixtureService', state: 'stopped' } } }] };
const raw = new TextEncoder().encode(JSON.stringify(payload));
const plan = { plan_payload_bytes: Array.from(raw), plan_hash: await sha256(raw), jobs: [{ payload: { execution_identity: 'root', action: { unit: 'untrusted-view' } } }] };
expect(renderToStaticMarkup(<SystemPlanNotice plan={plan}/>)).toContain('role="alert"');
await verifyPlanIntegrity(plan);
const html = renderToStaticMarkup(<SystemPlanNotice plan={plan}/>);
expect(html).toContain('SYSTEM'); expect(html).toContain('system:execute'); expect(html).toContain('Identität: system');
for (const expected of ['fixed-asset', 'RmmDiscoveryFixtureService', 'Zielzustand: stopped', 'Keine SYSTEM-Shell']) expect(html).toContain(expected);
expect(html).not.toContain('untrusted-view'); expect(html).not.toMatch(/<(button|input|form|pre)\b/);
const emptyRaw = new TextEncoder().encode('{"jobs":[]}'), empty = { plan_payload_bytes: Array.from(emptyRaw), plan_hash: await sha256(emptyRaw) }; await verifyPlanIntegrity(empty);
expect(renderToStaticMarkup(<SystemPlanNotice plan={empty}/>)).toBe('');
for (const plan_payload_bytes of [[255], [123], 'invalid']) {
const invalid = renderToStaticMarkup(<SystemPlanNotice plan={{ plan_payload_bytes }}/>);
expect(invalid).toContain('role="alert"'); expect(invalid).toContain('nicht lesbar');
expect(invalid).not.toMatch(/<(button|input|form)\b/);
}
expect(fetch).not.toHaveBeenCalled();
} finally { vi.unstubAllGlobals(); }
});