waf: list its own endpoints on a chi router in routes.go (closes #426) #459

Open
clawbot wants to merge 2 commits from issue-426-chi-routes into next
pull from: issue-426-chi-routes
Collaborator

internal/waf/routes.go now lists every one of waf's own endpoints on a chi router. One middleware checks a token, and a second writes each request's log line. answerOwnEndpoint, answerAdmin and endpoint() are gone, and the endpoint handlers are no longer methods on request. A question never reaches the router: ServeHTTP still answers it first.

closes #426

New names:

  • routes.go: the router of waf's own endpoints and its middleware.
  • routes: returns that router.
  • matchDecodedPath: has the router match the decoded path. chi matches the path as sent when it is encoded unusually, so /_sww/%68ealthz would otherwise answer 404. The new test TestHealthEndpointIsFoundAtItsPathPercentEncoded covers it.
  • logOwnEndpoint: writes the log line with the action own_endpoint.
  • countedBody: a request body that counts the bytes read of it, for request_bytes.
  • requireToken: lets a request through only with the token. It answers 404 while the token is unset, and 401 without it.
  • answerNotFound: the 404 for a path with no endpoint, and for a method the path has none for. It replaces chi's 405.
  • handleHealth, handleListBans, handleAddBan, handleLiftBans, handleShowClient, handleReport: each answers its endpoint.
  • handler.ownEndpoints: the router, built once in newHandler.

Things the diff does not show:

  • The report route sits outside the logging group, so it writes no line of its own and is not counted as a request under way. The 404 handlers are set inside that group, which chi wraps in the group's middleware, so an unknown path still writes its line.
  • Judgement call: a ban body over 4 KiB now logs request_bytes 4097, the bytes actually read, where it logged 4096. http.MaxBytesReader reads one byte past its limit.

Model: opus-5-5

`internal/waf/routes.go` now lists every one of `waf`'s own endpoints on a `chi` router. One middleware checks a token, and a second writes each request's log line. `answerOwnEndpoint`, `answerAdmin` and `endpoint()` are gone, and the endpoint handlers are no longer methods on `request`. A question never reaches the router: `ServeHTTP` still answers it first. closes https://git.eeqj.de/sneak/smallwebwaf/issues/426 New names: - `routes.go`: the router of `waf`'s own endpoints and its middleware. - `routes`: returns that router. - `matchDecodedPath`: has the router match the decoded path. `chi` matches the path as sent when it is encoded unusually, so `/_sww/%68ealthz` would otherwise answer 404. The new test `TestHealthEndpointIsFoundAtItsPathPercentEncoded` covers it. - `logOwnEndpoint`: writes the log line with the action `own_endpoint`. - `countedBody`: a request body that counts the bytes read of it, for `request_bytes`. - `requireToken`: lets a request through only with the token. It answers 404 while the token is unset, and 401 without it. - `answerNotFound`: the 404 for a path with no endpoint, and for a method the path has none for. It replaces `chi`'s 405. - `handleHealth`, `handleListBans`, `handleAddBan`, `handleLiftBans`, `handleShowClient`, `handleReport`: each answers its endpoint. - `handler.ownEndpoints`: the router, built once in `newHandler`. Things the diff does not show: - The report route sits outside the logging group, so it writes no line of its own and is not counted as a request under way. The 404 handlers are set inside that group, which `chi` wraps in the group's middleware, so an unknown path still writes its line. - Judgement call: a ban body over 4 KiB now logs `request_bytes` 4097, the bytes actually read, where it logged 4096. `http.MaxBytesReader` reads one byte past its limit. Model: opus-5-5
clawbot added the needs-review label 2026-11-19 17:44:29 +01:00
clawbot self-assigned this 2026-11-19 17:44:29 +01:00
clawbot added 1 commit 2026-11-19 17:44:29 +01:00
routes.go lists every own endpoint. One middleware checks a token and
another writes the log line. answerOwnEndpoint, answerAdmin and
endpoint() are gone; the handlers return http.HandlerFunc. The router
matches the decoded path, as the old code did.

Model: opus-5-5
clawbot added 1 commit 2026-11-19 18:07:22 +01:00
No test read the request_bytes of an own endpoint's log line, so
removing the counting in logOwnEndpoint went unnoticed. The test checks
that a ban to add is logged with its body's bytes, and that the health
endpoint, which reads no body, logs 0.

Model: opus-5-5
Author
Collaborator

Verdict: PASS
Head: e84156ef04

routes.go lists every one of waf's own endpoints on a chi router behind one token middleware, keeps each answer, token check and log line as before, and meets the definition of done of #426.

  • Judgement call: a ban body over 4 KiB now logs request_bytes 4097 where it logged 4096. I accepted this, since 4097 is the bytes read, which is how docs/request-log.md defines the field.

Model: opus-5-5

Verdict: PASS Head: e84156ef042d191282171004fb15392a4fe09925 `routes.go` lists every one of `waf`'s own endpoints on a `chi` router behind one token middleware, keeps each answer, token check and log line as before, and meets the definition of done of https://git.eeqj.de/sneak/smallwebwaf/issues/426. - Judgement call: a ban body over 4 KiB now logs `request_bytes` 4097 where it logged 4096. I accepted this, since 4097 is the bytes read, which is how `docs/request-log.md` defines the field. Model: opus-5-5
Author
Collaborator

Verdict: PASS
Head: b7e7557047

routes.go lists every one of waf's own endpoints on a chi router behind one token middleware and one log middleware, keeps every answer and log line as before but for the case below, and meets the definition of done of #426.

  • Judgement call: a ban body over 4 KiB now logs request_bytes 4097 where it logged 4096. I accepted this, since docs/request-log.md defines the field as the bytes read.

Model: opus-5-5

Verdict: PASS Head: b7e7557047efb01cd37da7c6ab8d053686d2ea68 `routes.go` lists every one of `waf`'s own endpoints on a `chi` router behind one token middleware and one log middleware, keeps every answer and log line as before but for the case below, and meets the definition of done of https://git.eeqj.de/sneak/smallwebwaf/issues/426. - Judgement call: a ban body over 4 KiB now logs `request_bytes` 4097 where it logged 4096. I accepted this, since `docs/request-log.md` defines the field as the bytes read. Model: opus-5-5
Some checks are pending
check / check (push) Waiting to run
You are not authorized to merge this pull request.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin issue-426-chi-routes:issue-426-chi-routes
git checkout issue-426-chi-routes
Sign in to join this conversation.