import crypto from 'node:crypto'; import { createNativeInertiaClient } from './lib/native-inertia-client.mjs'; /** Exercise native Fortify sessions and read routes, without business commands or a browser claim. */ export async function verifyNativeReviewHttp(origin, fixture, { assets, snapshot }) { const base = new URL(origin); if (base.protocol !== 'http:' || !['localhost', '127.0.0.1'].includes(base.hostname) || !base.port || typeof snapshot !== 'function') throw Error('Owned loopback review required'); let checks = 0; const check = (condition, label) => { if (!condition) throw Error(`Native HTTP verification failed: ${label}`); checks++; }; const client = () => createNativeInertiaClient(base.href, check); const guest = client(); check((await guest.request('/auditoria/eventos')).status === 401, 'anonymous metadata access refused'); check(Array.isArray(assets) && assets.length > 0, 'private candidate assets supplied'); for (const asset of assets) { const file = await guest.request(`/build/${asset.file}`); check(file.status === 200, 'private candidate asset served'); check(crypto.createHash('sha256').update(Buffer.from(await file.arrayBuffer())).digest('hex') === asset.sha256, 'HTTP asset bytes match private build'); } const reviewer = client(); const auditor = client(); const requester = client(); await reviewer.login(fixture.synthetic_login); await auditor.login(fixture.synthetic_metadata_login); await requester.login(fixture.synthetic_requester_login); // Login/logout may legitimately rotate native authentication fields. Compare the read window only. const before = await snapshot(); const budgets = await reviewer.page('/orcamentos', 'budgets/index'); check(budgets.props.budgets.length > 0, 'native synthetic budget is visible to its authorised reader'); const budgetHeaders = await reviewer.request('/orcamentos'); check(budgetHeaders.headers.get('Cache-Control')?.includes('no-store'), 'private budget response not cacheable'); check((await requester.request('/orcamentos')).status === 403, 'requester cannot read operational budget preparations'); for (const actor of [reviewer, requester]) { await actor.page('/conflitos', 'conflicts/index'); const response = await actor.request('/conflitos'); check(response.headers.get('Cache-Control')?.includes('no-store'), 'personal conflicts not cacheable'); } for (const [route, component] of [['/stock', 'stock/index'], ['/stock/operacoes', 'stock/operations']]) { await reviewer.page(route, component); const response = await reviewer.request(route); check(response.headers.get('Cache-Control')?.includes('no-store'), 'stock workspace not cacheable'); check((await requester.request(route)).status === 403, 'requester cannot read operational stock workspace'); } for (const [route, component] of [['/equipamentos', 'equipment/index'], ['/regras-investimento', 'investment/index']]) { const page = await reviewer.page(route, component); check(page.props.canManage === true, 'native administrative preparation scope retained'); const response = await reviewer.request(route); check(response.headers.get('Cache-Control')?.includes('no-store'), 'equipment and investment preparation not cacheable'); check((await requester.request(route)).status === 403, 'requester cannot inherit equipment or investment administration'); } const companyConfiguration = await reviewer.page('/administracao/parametrizacao', 'company/parameters'); check(companyConfiguration.props.ready === true && Array.isArray(companyConfiguration.props.profiles) && companyConfiguration.props.profiles.length > 0, 'existing synthetic company configuration remains consultable'); const companyResponse = await reviewer.request('/administracao/parametrizacao'); check(companyResponse.headers.get('Cache-Control')?.includes('no-store'), 'company configuration not cacheable'); check((await requester.request('/administracao/parametrizacao')).status === 403, 'requester cannot inherit configuration administration'); const detail = await reviewer.page(`/requisicoes/${fixture.historical_decision_request}`, 'requisitions/show'); check(detail.props.canReadAuditMetadata === true, 'native request audit navigation permission'); const history = await reviewer.page(`/requisicoes/${fixture.historical_decision_request}/prazos`, 'requisitions/expiry'); check(history.props.history.data[0].request.total_minor === 10000, 'original cycle amount survives native HTTP'); const query = new URLSearchParams({ request_reference: fixture.audit_request_reference }); const metadata = await reviewer.page(`/auditoria/eventos?${query}`, 'audit/metadata'); check(metadata.props.requestContext?.number === fixture.audit_request_reference, 'authorised native context locator'); const minimalEvents = page => { check(page.props.events.data.length > 0, 'filtered native events present'); check(page.props.events.data.every(e => JSON.stringify(Object.keys(e).sort()) === JSON.stringify(['action', 'created_at', 'id', 'user_id'])), 'events contain only four metadata fields'); }; minimalEvents(metadata); for (const [view, component] of [['agreements', 'supply/index'], ['contracts', 'supply/index'], ['calendar', 'supply/index'], ['imports', 'supply/index'], ['history', 'supply/history'], ['suggestions', 'supply/suggestions'], ['reservations', 'supply/coordination'], ['deliveries', 'supply/coordination'], ['alternatives', 'supply/coordination']]) { const page = await reviewer.page(`/compras/fornecimento?view=${view}`, component); if (['imports', 'reservations', 'deliveries', 'alternatives'].includes(view)) check(page.props.canPrepare === false, `${view} read does not grant preparation`); } const operationalControls = await reviewer.page('/revisao-compras', 'purchase-controls/index'); check(operationalControls.props.policies.length === 1 && operationalControls.props.policies[0].status === 'pending' && operationalControls.props.policies[0].enabled === false, 'existing synthetic policy remains pending and disabled in operational view'); await reviewer.page('/ordens/cancelamentos', 'purchase-controls/orders'); const ownControls = await requester.page('/revisao-compras', 'purchase-controls/index'); check(ownControls.props.policies.length === 0 && ownControls.props.awards.length === 0 && ownControls.props.cancellations.length === 0, 'requester does not inherit operational policy or cancellation data'); const ownRequest = await requester.page(`/requisicoes/${fixture.historical_decision_request}`, 'requisitions/show'); check(ownRequest.props.canReadAuditMetadata === false, 'request ownership does not grant metadata audit'); check((await requester.request('/auditoria/eventos')).status === 403, 'requester audit route refuses absent capability'); const limited = await auditor.page(`/auditoria/eventos?${query}`, 'audit/metadata'); minimalEvents(limited); check(limited.props.requestContext === null, 'metadata-only actor receives no private process locator'); check((await auditor.request(`/requisicoes/${fixture.historical_decision_request}`)).status === 404, 'metadata-only actor cannot read private request'); check((await auditor.request('/compras/fornecimento?view=agreements')).status === 403, 'metadata grant does not grant supply access'); const csv = async actor => { const response = await actor.request(`/auditoria/eventos.csv?${query}`); check(response.status === 200 && response.headers.get('Content-Type')?.includes('text/csv'), 'native metadata CSV'); check(response.headers.get('Cache-Control')?.includes('no-store'), 'private CSV not cacheable'); const body = await response.text(); check(body.includes(fixture.audit_request_reference), 'CSV preserves exact criteria'); return body; }; check(await csv(reviewer) === await csv(auditor), 'both metadata grants export the same scoped metadata without private context'); const after = await snapshot(); check(JSON.stringify(before) === JSON.stringify(after), 'all native table rows and columns unchanged during read window'); await reviewer.logout(); await auditor.logout(); await requester.logout(); return { checks, fortify_sessions: 3, missing_csrf_refused: true, stale_inertia_version_refused: true, supply_views: 9, purchase_control_views: 2, requester_scope_verified: true, encrypted_history_verified: true, budget_view_verified: true, conflict_index_verified: true, stock_views_verified: 2, equipment_view_verified: true, investment_view_verified: true, company_configuration_verified: true, metadata_scope_isolation_verified: true, readonly_tables_equal: Object.keys(before).length, private_asset_verified: true, private_assets_verified: assets.length, business_commands_sent: 0, browser_visual_acceptance: false }; }