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(); const privateAuditor = client(); await reviewer.login(fixture.synthetic_login); await auditor.login(fixture.synthetic_metadata_login); await requester.login(fixture.synthetic_requester_login); await privateAuditor.login(fixture.synthetic_private_audit_login); // Login/logout may legitimately rotate native authentication fields. Compare the read window only. const before = await snapshot(); const privateAudit = await privateAuditor.page('/auditoria/privada', 'audit/private'); check(privateAudit.props.families.length === 4 && privateAudit.props.exports.data.length === 0, 'dedicated private authority opens approved families without generating a package'); for (const actor of [reviewer, auditor, requester]) check((await actor.request('/auditoria/privada')).status === 403, 'other authorities cannot open private audit'); check((await privateAuditor.request('/orcamentos')).status === 403, 'private audit never grants native financial workspace'); const operationalOrders = await reviewer.page('/ordens-operacionais', 'requisitions/operational-orders'); check((await auditor.request('/ordens-operacionais')).status === 403, 'metadata authority never grants operational order register access'); check(operationalOrders.props.canConfigure === true && operationalOrders.props.policy === null, 'native admin can configure origin but a GET does not select an origin'); const ownOperationalOrders = await requester.page('/ordens-operacionais', 'requisitions/operational-orders'); check(ownOperationalOrders.props.canConfigure === false && ownOperationalOrders.props.allowedTypes.length === 0, 'requester receives no production workshop or procurement order creation grant'); const urgentStock = await reviewer.page(`/requisicoes/${fixture.historical_decision_request}/urgencia-stock`, 'requisitions/urgent-stock'); check(urgentStock.props.eligible.length === 0, 'ordinary historical source receives no implicit urgent reduction'); check((await requester.request(`/requisicoes/${fixture.historical_decision_request}/urgencia-stock`)).status === 403, 'request ownership does not grant stock threshold preparation or quotation oversight'); 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'); const auditInput = {operation_key: crypto.randomUUID(), families: ['purchasing', 'stock', 'finance', 'configuration'], purpose: 'Owned synthetic native QA export'}; await privateAuditor.post('/auditoria/privada', auditInput); const prepared = await privateAuditor.page('/auditoria/privada', 'audit/private'); check(prepared.props.exports.data.length === 1, 'one private package prepared after the unchanged read window'); const packageRow = prepared.props.exports.data[0]; const download = await privateAuditor.request(packageRow.download_url); check(download.status === 200 && download.headers.get('Cache-Control')?.includes('no-store'), 'authenticated private package download'); const bytes = Buffer.from(await download.arrayBuffer()); check(bytes.length === packageRow.size && crypto.createHash('sha256').update(bytes).digest('hex') === packageRow.sha256, 'native download bytes match immutable package size and SHA256'); await privateAuditor.post('/auditoria/privada', auditInput); const replay = await privateAuditor.page('/auditoria/privada', 'audit/private'); check(replay.props.exports.data.length === 1 && replay.props.exports.data[0].sha256 === packageRow.sha256, 'replay preserves the same archive'); check((await auditor.request(packageRow.download_url)).status === 403, 'metadata cannot download private package'); await reviewer.logout(); await auditor.logout(); await requester.logout(); await privateAuditor.logout(); return { checks, fortify_sessions: 4, 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, operational_orders_verified: true, urgent_stock_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, audit_export_commands_sent: 2, private_audit_download_verified: true, browser_visual_acceptance: false }; }