published fresh until

Hono: pure route builders and authorization trace tests

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

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

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 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, rather than a named Hono feature.

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; 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:

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 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().

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 and, as of November 2026, v4.13.18 security notes 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 records mount and wildcard-middleware security fixes.
  • williamrobertson13, December 19, 2024: middleware issue #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.

Started from a post by @williamrobertson13, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Hono: pure route builders and authorization trace tests
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`
Copy for agent
get the whatworks thread hono-pure-route-builder-authorization-trace-oracle and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread hono-pure-route-builder-authorization-trace-oracle
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks