Skip to content

test(email): cover crypto, rate limits, schemas and sanitization - #219

Merged
Hydralerne merged 4 commits into
oblien:feat/bugfix-batch-2026-09-16from
ahmedhesham6:test/email-server-coverage
Sep 16, 2026
Merged

Hydralerne merged 4 commits into
oblien:feat/bugfix-batch-2026-09-16from
ahmedhesham6:test/email-server-coverage

Conversation

@ahmedhesham6

@ahmedhesham6 ahmedhesham6 commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds webmail regression coverage for authenticated encryption and tampering, client-IP selection, rate-limit isolation and expiry, input schemas, and encoded HTML sanitization attacks. The tests run with the email server’s existing Bun runner and restore their clocks, environment variables, and timers after each case.

Validation: all 110 email-server tests pass, including 33 added cases; the server TypeScript check passes. Existing privacy and sanitizer coverage is retained.

Related to #216. Integrates into the bug-review branch in #892; the contributor’s original commits are preserved.

apps/email/server had no tests. This adds 43 covering the modules where a
silent regression is most costly, all pure logic with no IMAP/SMTP or
network dependency.

- crypto.ts      session credential encryption (AES-256-GCM)
- rate-limit.ts  the sign-in brute-force guard
- sanitize.ts    the XSS boundary for HTML from arbitrary senders
- client-ip.ts   the key every rate limiter buckets on
- schemas.ts     sign-in validation and settings bounds

Cases target the boundaries rather than the happy path: GCM tamper
detection in the IV, ciphertext and tag separately; the exact 28/27-byte
payload framing; IV uniqueness across identical plaintexts; the rate
limiter's exact max/max+1 and rollover instants plus the retryAfter
floor; that x-forwarded-for stays untrusted even with no x-real-ip; and
that signInSchema keeps rejecting client-supplied imapHost/smtpHost,
which is what stops a phishing page redirecting a password to an
attacker-controlled mail host.

Each test was checked by mutating the source and confirming it fails:
trusting x-forwarded-for fails 2, dropping .strict() fails 1, removing
the rel/target link hardening fails 6, a rate-limit off-by-one fails 7,
and disabling the GCM auth tag fails 4.

Where sanitize-html's real behavior is weaker than one might assume, the
test asserts what actually happens and comments the residual risk rather
than asserting an untrue guarantee: <style> is allowed and keeps
javascript: in CSS, and a percent-encoded scheme survives the scheme
check. No sanitizer behavior is changed here.

Wiring notes: @zero/server is nested below the apps/* workspace glob, so
it is not a turbo member and a test script there alone would never run in
CI; the @repo/email orchestrator forwards to it using the same
"cd server &&" pattern as its lint script. tsconfig.json did not include
test/, so the tests were unchecked by tsc -- adding it surfaced one real
type error, now fixed. build is tsc --noEmit and the release entrypoint
is server/src/main.ts, so nothing under test/ ships.

Refs oblien#216

No files under apps/email/server/src were modified.
…rkspace

The first pass used `bun test`, on the reasoning that the package is
Bun-native (bun:sqlite, hono/bun, @types/bun) and had no vitest
dependency. That was the wrong call: seven of the eight workspaces that
test at all use `vitest run`, so a lone Bun runner would have been an
odd one out for a contributor to trip over.

Converted all five files. Two needed more than an import swap:

- rate-limit: `setSystemTime`/`Bun.sleep` -> vi fake timers. The GC case
  is stronger for it. It used to sleep 20ms of real time and hope the
  sweep had run; it now advances the timer queue so the interval
  actually fires. Note this needs advanceTimersByTime, not
  setSystemTime -- the latter moves Date.now() while leaving the timer
  pending, so nothing is ever collected and the test passes vacuously.

- client-ip: `hono/bun`'s barrel also pulls the SSG and websocket
  adapters, which dereference the `Bun` global at import, so the file
  threw `Bun is not defined` under vitest's Node runtime before any test
  ran. Mocked to the one function used, getConnInfo, whose own module is
  runtime-agnostic. The logic under test (header precedence, trimming,
  the throwing-fallback path) is all clientIp's own and stays real -- I
  re-ran the x-forwarded-for mutation through the mock and it still
  fails 2 tests, same as before.

vitest ^4.0.18 added as a devDependency, matching the spec used by the
other seven. apps/email/server keeps its own bun.lock (it is not a
member of the root apps/* workspace glob), so the dependency had to be
installed there rather than inherited by hoisting -- hence the lockfile
change. Pinned resolution is 4.0.18, identical to root; a plain install
had drifted to 4.1.10.

@types/bun stays: the server genuinely runs on Bun at runtime. Only the
test runner moved.

Re-verified all six mutations under the new runner, with failure counts
unchanged: XFF trust 2, .strict() 1, rel/target 6, off-by-one 7,
setAuthTag 4, static IV 1. Root `bun run test` 8/8 and `bun run build`
12/12 pass; the release bundle contains no test files and no vitest.
@Hydralerne Hydralerne changed the title test(email): add unit tests for webmail server security primitives test(email): cover crypto, rate limits, schemas and sanitization Sep 16, 2026
@Hydralerne
Hydralerne changed the base branch from main to feat/bugfix-batch-2026-09-16 September 16, 2026 06:24
@Hydralerne
Hydralerne merged commit b6a4116 into oblien:feat/bugfix-batch-2026-09-16 Sep 16, 2026
5 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants