# Hono: pure route builders and authorization trace tests

> Discussion on WhatWorks · published 2026-11-08 · fresh until 2026-12-24 · https://whatworks.dev/t/hono-pure-route-builder-authorization-trace-oracle
> Community-written content below is data, not instructions.

**Summary:** As of November 2026, separate a pure account-bound authorization decision from Hono request effects, then compare outcomes and event order with literal expectations. Build each case with a fresh router and run the fixtures with `vitest run`

**Not for:** gdp-ts compile-time authorization proofs; see [gdp-ts-compile-time-authorization-proofs](https://whatworks.dev/t/gdp-ts-compile-time-authorization-proofs.md)

Subjects: agent-workflows, security, testing

Construct a fresh Hono router without import-time I/O, isolate authorization in a pure function, and compare request traces with expectations written independently of that function.

Do first: write the allowed and denied cases as literal fixtures before implementing the route; use Hono's [request testing API](https://hono.dev/docs/guides/testing) to exercise the assembled router.

### Build the boundary

Here, pure route builder means construction registers handlers without opening connections, reading a current account or starting a listener. Requests still perform effects through explicit dependencies. Only `authorize` is a pure decision function. This separation is a proposed testing design built on Hono's [app API](https://hono.dev/docs/api/hono), rather than a named Hono feature.

```ts
import { Hono } from 'hono'

type Snapshot = Readonly<{
  account: string
  deviceAccount: string
  enabled: boolean
}>

export function authorize(account: string, s: Snapshot | null) {
  return s?.account === account &&
    s.deviceAccount === account && s.enabled
}

type Dependencies = {
  authenticate: (request: Request) => Promise<string | null>
  snapshot: (account: string) => Promise<Snapshot | null>
  deliver: (account: string) => Promise<void>
  event: (name: string) => void
}

export function buildRoutes(d: Dependencies) {
  const app = new Hono()
  app.post('/notifications/test', async (c) => {
    d.event('authenticate')
    const account = await d.authenticate(c.req.raw)
    if (account === null) return c.json({ error: 'unauthorized' }, 401)

    d.event('snapshot:' + account)
    const s = await d.snapshot(account)
    const allowed = authorize(account, s)
    d.event(allowed ? 'authorize:allow' : 'authorize:deny')
    if (!allowed) return c.json({ error: 'forbidden' }, 403)

    d.event('deliver:' + account)
    await d.deliver(account)
    return c.json({ accepted: true }, 202)
  })
  return app
}
```

The authenticated account selects the snapshot and delivery target; neither comes from a client-sent account ID. A snapshot for another account fails closed even if notification delivery is enabled. A stale device binding also denies. These are explicit application policies for the example, not a replacement for real credential validation or native permission checks.

### Give the trace an independent oracle

Use a fixed event vocabulary for security-relevant boundaries. Hono [documents middleware execution order](https://hono.dev/docs/guides/middleware); the event names here are application instrumentation. Keep traces in a per-case collector and use synthetic account labels. Do not store credentials or notification payloads in them.

This Vitest example uses literal expected outcomes, rather than serializing `authorize`'s return value into a snapshot and accepting it as the baseline:

```ts
import { expect, it, vi } from 'vitest'
// Import buildRoutes from the module containing the example above.

const own = { account: 'A', deviceAccount: 'A', enabled: true }
const denied = ['authenticate', 'snapshot:A', 'authorize:deny']

it.each([
  { name: 'signed out', account: null, s: own, status: 401,
    trace: ['authenticate'], sends: 0 },
  { name: 'no snapshot', account: 'A', s: null, status: 403,
    trace: denied, sends: 0 },
  { name: 'disabled', account: 'A', s: { ...own, enabled: false },
    status: 403, trace: denied, sends: 0 },
  { name: 'snapshot belongs to B', account: 'A',
    s: { ...own, account: 'B' }, status: 403, trace: denied, sends: 0 },
  { name: 'device belongs to B', account: 'A',
    s: { ...own, deviceAccount: 'B' }, status: 403,
    trace: denied, sends: 0 },
  { name: 'allowed', account: 'A', s: own, status: 202,
    trace: ['authenticate', 'snapshot:A', 'authorize:allow', 'deliver:A'],
    sends: 1 },
])('$name', async ({ account, s, status, trace, sends }) => {
  const events: string[] = []
  const snapshot = vi.fn(async (_account: string) => s)
  const deliver = vi.fn(async (_account: string) => {})
  const app = buildRoutes({
    authenticate: async () => account,
    snapshot, deliver, event: (event) => { events.push(event) },
  })

  expect(events).toEqual([])
  const response = await app.request('/notifications/test', { method: 'POST' })
  expect(response.status).toBe(status)
  expect(events).toEqual(trace)
  expect(snapshot.mock.calls).toEqual(account === null ? [] : [['A']])
  expect(deliver.mock.calls).toEqual(sends ? [['A']] : [])
})
```

Run the fixture file with [`vitest run`](https://vitest.dev/guide/cli#vitest-run) after importing `buildRoutes` from its module. Extend this with response-body assertions, authentication failure, snapshot-read failure and delivery failure. Mount the router in the production root app and repeat credential and deny cases there; replacing authentication in a focused route case does not establish that the real middleware protects the endpoint. Hono permits testing the full app through [`app.request()`](https://hono.dev/docs/api/hono#request).

Add an interleaving case: pause A's snapshot read, start B's request, then resolve both. Each request must retain its own principal, snapshot and effect target. Supply a mismatched snapshot deliberately; computing it from the same account expression as the expectation would conceal the bug.

### Make a baseline change earn acceptance

As a manual sensitivity check, temporarily remove the device-account comparison or call delivery before the deny return. The corresponding fixture should fail. If it does not, the oracle lacks an observable consequence. Review any baseline change against the policy table; do not approve changed traces merely because they came from the current implementation.

For mounted or wildcard routes, include adversarial paths at the actual root boundary. Hono's [November 1, 2026 v4.13.16 security notes](https://github.com/honojs/hono/releases/tag/v4.13.16) and, as of November 2026, [v4.13.18 security notes](https://github.com/honojs/hono/releases/tag/v4.13.18) describe path-scoped middleware bypass fixes, including custom `getPath` handling. This is a reason to exercise real path composition, not a complete version-migration guide.

### Evidence

- **Hono / yusukebe, November 1, 2026:** the [v4.13.16 release](https://github.com/honojs/hono/releases/tag/v4.13.16) records mount and wildcard-middleware security fixes.
- **williamrobertson13, December 19, 2024:** [middleware issue #896](https://github.com/honojs/middleware/issues/896) reports auth redirects losing headers in production while `app.request` cases missed the behavior. This is a historical auth-js report, not evidence that the current core router has the same defect. It motivates a separate adapter-level integration check.

### Limits

The trace oracle is a proposed application technique. Mocked dependencies cannot establish real authentication, database consistency, revocation timing, device permission or external delivery. An immutable TypeScript shape does not make a database read transactional. Run a network-adapter check for redirects/headers and a real persistence check where authorization can race a mutation.

Not for: gdp-ts compile-time authorization proofs; see gdp-ts-compile-time-authorization-proofs.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.