Skip to content

Authentication

The toRequireAuthentication() matcher tests that endpoints require authentication. Pass a Playwright APIRequestContext and a URL.

import { test, expect } from "@orlalabs/kovar";
test("admin API requires authentication", async ({ request }) => {
await expect(request).toRequireAuthentication("/api/admin/users");
});

The matcher sends requests without credentials and verifies:

  • The endpoint returns 401 or 403 instead of 2xx.
  • Error responses don’t contain sensitive keywords (password, secret, api_key, token, private_key, credit_card, ssn, social_security).

CWE mappings:

  • CWE-306 (Missing Authentication)
  • CWE-284 (Improper Access Control)
  • CWE-209 (Information Exposure Through Error Message)
await expect(request).toRequireAuthentication("/api/admin/users", {
endpoints: ["/api/admin/users", "/api/admin/settings"], // check multiple endpoints
methods: ["GET", "POST", "PUT", "DELETE"], // HTTP methods to test (default: GET, POST, PUT, DELETE)
expectedStatus: 401, // expected rejection status
});
OptionTypeDescription
endpointsstring[]Multiple endpoints to check
methodsstring[]HTTP methods to test (default: GET, POST, PUT, DELETE)
expectedStatusnumberExpected rejection status code
import { test, expect } from "@orlalabs/kovar";
test("auth audit", async ({ page, security }) => {
await page.goto("/dashboard");
// Throws on critical/high findings:
await security.auth.assert();
// Or inspect findings:
const authFindings = await security.auth.check({
endpoints: ["/api/admin/users", "/api/admin/settings"],
});
expect(authFindings).toHaveLength(0);
});

A common pattern is to test all protected endpoints together:

import { test, expect } from "@orlalabs/kovar";
test("API endpoints are secure", async ({ request }) => {
// CSRF: verify state-changing endpoints reject tokenless requests
await expect(request).toBeCSRFProtected("/api/transfer");
// CORS: verify no origin reflection or wildcard misconfiguration
await expect(request).toHaveSecureCORS("/api/data");
// Auth: verify endpoints reject unauthenticated requests
await expect(request).toRequireAuthentication("/api/admin/users");
});
  • CSRF — CSRF protections complement authentication.
  • CORS — CORS misconfigurations can bypass authentication.
  • Full Audit — include auth checks in a comprehensive audit with checks: ["auth"].
  • Standalone API — use checkAuth() outside Playwright.