const { test, expect } = require('@playwright/test'); require('dotenv').config(); const { BASE_URL, absoluteUrl } = require('../fixtures/url'); 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 = absoluteUrl(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-DSEC-001", "module": "Document Security Deep", "role": "Firm Admin", "scenario": "Invalid document download returns safe response", "type": "VAPT", "route": "/documents/999999/download", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Firm Admin" }, { "variantId": "V251-DSEC-002", "module": "Document Security Deep", "role": "Firm Admin", "scenario": "Invalid document version download safe", "type": "VAPT", "route": "/documents/999999/versions/999999/download", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Firm Admin" }, { "variantId": "V251-DSEC-003", "module": "Document Security Deep", "role": "Firm Admin", "scenario": "Invalid permanent document download safe", "type": "VAPT", "route": "/documents/permanent/999999/download", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Firm Admin" }, { "variantId": "V251-DSEC-004", "module": "Document Security Deep", "role": "Firm Admin", "scenario": "Deleted document invalid download safe", "type": "VAPT", "route": "/documents/999999/deleted/download", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Firm Admin" }, { "variantId": "V251-DSEC-005", "module": "Document Security Deep", "role": "Client", "scenario": "Client cannot download other document ID", "type": "VAPT", "route": "/documents/999999/download", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Client" }, { "variantId": "V251-DSEC-006", "module": "Document Security Deep", "role": "Consultant", "scenario": "Consultant cannot download unshared document", "type": "VAPT", "route": "/documents/999999/download", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Consultant" }, { "variantId": "V251-DSEC-007", "module": "Document Security Deep", "role": "Staff", "scenario": "Staff cannot access storage admin", "type": "VAPT", "route": "/documents/storage/nodes", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Staff" }, { "variantId": "V251-DSEC-008", "module": "Document Security Deep", "role": "Public", "scenario": "Storage agent jobs require auth", "type": "VAPT", "route": "/storage-agent/jobs", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-009", "module": "Document Security Deep", "role": "Public", "scenario": "Storage agent heartbeat without secret rejected", "type": "VAPT", "route": "/storage-agent/heartbeat", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-010", "module": "Document Security Deep", "role": "Public", "scenario": "Download token invalid safe", "type": "VAPT", "route": "/documents/download-token/invalid-token", "_kind": "safe-page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-011", "module": "Document Security Deep", "role": "Firm Admin", "scenario": "Document search route does not leak across tenant", "type": "VAPT", "route": "/documents?tenant_id=999999", "_kind": "page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Firm Admin" }, { "variantId": "V251-DSEC-012", "module": "Document Security Deep", "role": "Firm Admin", "scenario": "Document repository loads for Firm Admin", "type": "UAT", "route": "/documents", "_kind": "page", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": "Firm Admin" }, { "variantId": "V251-DSEC-013", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document upload double extension rejected", "type": "VAPT", "route": "/documents/upload", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-014", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document upload MIME mismatch rejected", "type": "VAPT", "route": "/documents/upload", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-015", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document upload large file rejected or handled", "type": "VAPT", "route": "/documents/upload", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-016", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document upload path traversal filename rejected", "type": "VAPT", "route": "/documents/upload", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-017", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document delete CSRF-less POST rejected", "type": "VAPT", "route": "/documents/999999/delete", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-018", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document version delete CSRF-less POST rejected", "type": "VAPT", "route": "/documents/999999/versions/999999/delete", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-019", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Permanent document delete CSRF-less POST rejected", "type": "VAPT", "route": "/documents/permanent/999999/delete", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-020", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document move cross-tenant CSRF-less POST rejected", "type": "VAPT", "route": "/documents/999999/move", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-021", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document share with client invalid ID rejected", "type": "VAPT", "route": "/documents/999999/share-client", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-022", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document share with consultant invalid ID rejected", "type": "VAPT", "route": "/documents/999999/share-consultant", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-023", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Storage node create CSRF-less POST rejected", "type": "VAPT", "route": "/documents/storage/nodes", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-024", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Storage agent ack without secret rejected", "type": "VAPT", "route": "/storage-agent/jobs/999999/ack", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-025", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Storage agent upload without secret rejected", "type": "VAPT", "route": "/storage-agent/jobs/999999/upload", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-026", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Download request expiry invalid safe", "type": "VAPT", "route": "/documents/download-requests/999999", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-027", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document tag update CSRF-less POST rejected", "type": "VAPT", "route": "/documents/999999/tags", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null }, { "variantId": "V251-DSEC-028", "module": "Document Security Deep", "role": "Anonymous/Attacker", "scenario": "Document metadata update invalid ID rejected", "type": "VAPT", "route": "/documents/999999/edit", "_kind": "post", "_method": "GET", "_file": "document-security-deep.spec.js", "_login_role": null } ]; test.describe("v2.5.1 Additions - Document Security Deep", () => { 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); }); } });