testing/adversarial_headers.ts

Adversarial header attack test suite.

Provides standard header injection test cases and a convenience wrapper for exercising middleware stacks with adversarial headers.

Importing this module mocks query modules for the whole test file. It imports testing/middleware.ts, whose hoisted module-level vi.mock replaces api_token_queries, account_queries, and role_grant_queries with mocks in every file that imports it: the bearer-path queries become vi.fn() stubs returning undefined, and any other export of those modules throws on access — keep describe_standard_adversarial_headers in its own test file.

view source

Declarations
#

3 declarations

AdversarialHeaderCase
#

testing/adversarial_headers.ts view source

AdversarialHeaderCase import type {AdversarialHeaderCase} from '@fuzdev/fuz_app/testing/adversarial_headers.js';

A header-level attack case for middleware stack testing.

name

type string

headers

type Record<string, string>

expected_status

type number

expected_error?

The error definition the JSON body must be — its code, and a body its schema accepts.

type RestErrorDef<number, string, SchemaEntries>

validate_expectation

Whether the request should reach token validation or be short-circuited by earlier middleware.

type "called" | "not_called"

create_standard_adversarial_cases
#

testing/adversarial_headers.ts view source

(allowed_origin: string): AdversarialHeaderCase[] import {create_standard_adversarial_cases} from '@fuzdev/fuz_app/testing/adversarial_headers.js';

7 standard adversarial header cases applicable to any middleware stack.

Origin verification is Origin-only — fuz_app's verify_request_source no longer falls back to Referer (matches zzz_server::auth::is_request_origin_allowed). Bearer auth still treats a Referer header as a browser-context indicator and silently discards the bearer token — so Referer-bearing requests reach the route as unauthenticated rather than 403.

allowed_origin

an origin that passes the origin check

type string

returns

AdversarialHeaderCase[]

describe_standard_adversarial_headers
#

testing/adversarial_headers.ts view source

(suite_name: string, options: TestMiddlewareStackOptions, allowed_origin: string, extra_cases?: AdversarialHeaderCase[] | undefined): void import {describe_standard_adversarial_headers} from '@fuzdev/fuz_app/testing/adversarial_headers.js';

Create a middleware stack app with standard adversarial header tests.

Convenience wrapper combining create_test_middleware_stack_app and create_standard_adversarial_cases. Generates one test() per case inside a describe() block — asserts body content for both error and success cases, and verifies that mock_validate was (or was not) reached per the case's validate_expectation flag, ensuring earlier middleware actually short-circuits before token validation in the rejection cases.

suite_name

the describe block name

type string

options

middleware stack configuration

allowed_origin

an origin that passes the origin check (used for standard cases)

type string

extra_cases?

additional cases appended after the 7 standard ones

optional

returns

void

Depends on
#