const { test, expect } = require('@playwright/test'); require('dotenv').config(); const { login } = require('../fixtures/auth'); const { expectNoBackendError, readBody, blockedOrNotFound } = require('../fixtures/v204-helpers'); const { expectBlockedOrSafe } = require('../fixtures/assertions'); async function safeGoto(page, route) { const resp = await page.goto(route).catch(() => null); await page.waitForLoadState('domcontentloaded').catch(() => {}); return resp; } async function expectRouteAvailable(page, resp) { await expectNoBackendError(page); if (resp) { expect(resp.status(), `Expected route to exist but got ${resp.status()} at ${page.url()}`).not.toBe(404); expect(resp.status()).toBeLessThan(500); } } async function expectNoSecrets(page) { const body = await readBody(page); expect(body).not.toMatch(/Password@123|Pass@123|smtp_password|IMAP_PASSWORD|SMTP_PASSWORD|otp\s*[:=]\s*\d{4,8}|reset_token|access_token|refresh_token/i); } async function expectPublicNoSensitive(page) { await expectNoBackendError(page); const body = await readBody(page); expect(body).not.toMatch(/tenant_id|branch_id|internal note|audit log|smtp|password|secret|token/i); } async function apiCall(request, method, route) { const url = `${process.env.BASE_URL}${route}`; const opts = { data: { csrf_token: '', test_payload: 'uat-vapt' }, headers: { 'Content-Type': 'application/json' } }; if (method === 'GET') return await request.get(url).catch(() => null); if (method === 'POST') return await request.post(url, opts).catch(() => null); if (method === 'PUT') return await request.put(url, opts).catch(() => null); if (method === 'DELETE') return await request.delete(url).catch(() => null); if (method === 'OPTIONS') return await request.fetch(url, { method: 'OPTIONS' }).catch(() => null); return await request.fetch(url, { method }).catch(() => null); } async function expectApiSafe(resp) { expect(resp, 'API response should be available').toBeTruthy(); expect(resp.status()).toBeLessThan(500); const text = await resp.text().catch(() => ''); expect(text).not.toMatch(/Traceback|Exception in ASGI application|OperationalError|ProgrammingError|AttributeError|UndefinedError|Password@123|Pass@123/i); } const cases = [ { "variantId": "V251-AUD-001", "module": "Audit Logs", "role": "System Admin", "scenario": "Audit logs page loads for System Admin", "type": "UAT", "route": "/system-settings/audit-logs", "_kind": "page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "System Admin" }, { "variantId": "V251-AUD-002", "module": "Audit Logs", "role": "Firm Admin", "scenario": "Firm Admin audit logs page loads or safely restricted", "type": "UAT", "route": "/system-settings/audit-logs", "_kind": "page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "Firm Admin" }, { "variantId": "V251-AUD-003", "module": "Audit Logs", "role": "Staff", "scenario": "Staff cannot access audit logs", "type": "VAPT", "route": "/system-settings/audit-logs", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "Staff" }, { "variantId": "V251-AUD-004", "module": "Audit Logs", "role": "Client", "scenario": "Client cannot access audit logs", "type": "VAPT", "route": "/system-settings/audit-logs", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "Client" }, { "variantId": "V251-AUD-005", "module": "Audit Logs", "role": "System Admin", "scenario": "Audit log invalid detail safe", "type": "UAT", "route": "/system-settings/audit-logs/999999", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "System Admin" }, { "variantId": "V251-AUD-006", "module": "Audit Logs", "role": "Staff", "scenario": "Audit log export requires permission", "type": "VAPT", "route": "/system-settings/audit-logs/export", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "Staff" }, { "variantId": "V251-AUD-007", "module": "Audit Logs", "role": "System Admin", "scenario": "Audit log filters do not crash", "type": "UAT", "route": "/system-settings/audit-logs?module=clients&action=create", "_kind": "page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "System Admin" }, { "variantId": "V251-AUD-008", "module": "Audit Logs", "role": "System Admin", "scenario": "Audit logs do not expose password", "type": "VAPT", "route": "/system-settings/audit-logs", "_kind": "no-secret", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "System Admin" }, { "variantId": "V251-AUD-009", "module": "Audit Logs", "role": "System Admin", "scenario": "Audit logs do not expose OTP", "type": "VAPT", "route": "/system-settings/audit-logs", "_kind": "no-secret", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "System Admin" }, { "variantId": "V251-AUD-010", "module": "Audit Logs", "role": "System Admin", "scenario": "Audit logs do not expose reset token", "type": "VAPT", "route": "/system-settings/audit-logs", "_kind": "no-secret", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": "System Admin" }, { "variantId": "V251-AUD-011", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Audit log export CSRF-less POST rejected", "type": "VAPT", "route": "/system-settings/audit-logs/export", "_kind": "post", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-012", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Audit log delete invalid ID rejected", "type": "VAPT", "route": "/system-settings/audit-logs/999999/delete", "_kind": "post", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-013", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Audit log tamper invalid POST rejected", "type": "VAPT", "route": "/system-settings/audit-logs/999999", "_kind": "post", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-014", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Permission denied event generated safely by blocked route", "type": "VAPT", "route": "/system-settings/rbac/roles", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-015", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Login failure is handled without stack trace", "type": "VAPT", "route": "/login", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-016", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Document download audit invalid ID safe", "type": "VAPT", "route": "/documents/999999/download", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-017", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Billing payment audit invalid ID safe", "type": "VAPT", "route": "/billing/invoices/999999/payments", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-018", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Notice case audit invalid ID safe", "type": "VAPT", "route": "/notice-cases/999999", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-019", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Audit log tenant scope query safe", "type": "VAPT", "route": "/system-settings/audit-logs?tenant_id=999999", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null }, { "variantId": "V251-AUD-020", "module": "Audit Logs", "role": "Anonymous/Attacker", "scenario": "Audit log branch scope query safe", "type": "VAPT", "route": "/system-settings/audit-logs?branch_id=999999", "_kind": "safe-page", "_method": "GET", "_file": "audit-logs.spec.js", "_login_role": null } ]; test.describe("v2.5.1 Additions - Audit Logs", () => { for (const c of cases) { test(`[${c.variantId}] ${c.scenario}`, async ({ page, request }) => { const role = c._login_role || c.role; const kind = c._kind; const method = c._method || 'GET'; if (kind === 'api') { const resp = await apiCall(request, method, c.route); await expectApiSafe(resp); return; } if (kind === 'post') { const resp = await apiCall(request, method || 'POST', c.route); await expectApiSafe(resp); const safe = [400, 401, 403, 404, 405, 409, 422, 429].includes(resp.status()); expect(safe, `Unsafe POST status ${resp.status()} for ${c.route}`).toBeTruthy(); return; } if (role && role !== 'Public' && !/Anonymous|Attacker|API/.test(role)) { await login(page, role); } const resp = await safeGoto(page, c.route); if (kind === 'page') { await expectRouteAvailable(page, resp); if (/password|secret|token|otp/i.test(c.scenario)) await expectNoSecrets(page); return; } if (kind === 'public') { await expectRouteAvailable(page, resp); return; } if (kind === 'public-no-sensitive') { await expectPublicNoSensitive(page); return; } if (kind === 'no-secret') { await expectRouteAvailable(page, resp); await expectNoSecrets(page); return; } await expectNoBackendError(page); await expectBlockedOrSafe(page, resp); }); } });