Ban causes, limit kinds, alert events, actions and dispositions get named types #428

Open
opened 2026-11-19 17:12:20 +01:00 by clawbot · 1 comment
Collaborator

From the architecture review, "internal/config, and values with a fixed set of choices": #283 (comment). Approved for building: #283 (comment). The code was read at 809d266 on next. Line numbers are that commit's.

The review's proposal is split in two so that each half is one PR. The settings are the other half: #427. The two halves share no identifier and can be built in either order.

What is there

  • More than thirty exported constants are untyped strings that name one of a fixed set of values:
    • ban causes: bans.CauseLimit, CauseAttack, CauseAdmin, CauseAbuseIPDB;
    • limit kinds: ratelimit.KindRequests, KindBytes, KindRefusals;
    • alert events: the eight alerts.Event…;
    • alert destinations: the three alerts.Destination…;
    • rule actions: rules.ActionLog, ActionBlock, ActionBan;
    • the log line's actions: the twelve requestlog.Action…, with OffenceLimit and the two WAFLine…;
    • the dispositions: DispositionAllow, DispositionDeny, DispositionClose in internal/waf/waf.go.
  • The fields and parameters that carry them are string: ban(…, cause, reason string, …) in internal/bans/bans.go:766, verdict.action and refusal.action in internal/waf, Ban.Cause, Hit.Kind, Rule.Action, Alert.Event, Line.Action.
  • rproxy's setting WAFBackendNoAnswerDisposition (internal/config/rproxy.go:35) is a string whose values are two of the dispositions.
  • One place already does it the other way: routewatch.Missing is a named type.

Why it matters

  • Any string is accepted where a cause is expected, and two neighbouring string parameters can be swapped unnoticed: cause, reason string.
  • A reader cannot find the allowed values from a field's type.

What changes

  • Each set becomes a named string type with its constants, in the package that owns it. Fields, parameters and comparisons use the type.
  • The dispositions are typed in whichever package holds them when this is built: internal/waf today, or the package for what rproxy and waf send each other (#425) once that is merged. WAFBackendNoAnswerDisposition takes that type.
  • .golangci.yml turns every linter on, exhaustive among them. It then checks each switch over these types for a missing case. Where it asks for one in an existing switch, the case is added; the linter's config is not touched.

What stays

  • Every value keeps its string. The JSON in the logs, the state files and the alerts, and the labels of the metrics, do not change: a named string type is written as its string.
  • No existing constant is renamed. Renames belong to the symbol names audit (#213).
  • reputation.AbuseIPDBSource and the figure names in internal/waf/heuristics.go stay plain strings: each names one value, not one of a set.

Done when

  • each set above has a named type, and the fields and parameters that carry it use the type;
  • behaviour is unchanged: every existing test passes with no assertion changed;
  • make check passes;
  • the PR body lists every new type name. Each says what the thing is to a reader of that one file, the standard of the symbol names audit.

If this is more than one PR can carry, say so on this issue instead of starting.

Model: fable-5-2

From the architecture review, "`internal/config`, and values with a fixed set of choices": https://git.eeqj.de/sneak/smallwebwaf/issues/283#issuecomment-173841. Approved for building: https://git.eeqj.de/sneak/smallwebwaf/issues/283#issuecomment-183550. The code was read at `809d266` on `next`. Line numbers are that commit's. The review's proposal is split in two so that each half is one PR. The settings are the other half: https://git.eeqj.de/sneak/smallwebwaf/issues/427. The two halves share no identifier and can be built in either order. ### What is there - More than thirty exported constants are untyped strings that name one of a fixed set of values: - ban causes: `bans.CauseLimit`, `CauseAttack`, `CauseAdmin`, `CauseAbuseIPDB`; - limit kinds: `ratelimit.KindRequests`, `KindBytes`, `KindRefusals`; - alert events: the eight `alerts.Event…`; - alert destinations: the three `alerts.Destination…`; - rule actions: `rules.ActionLog`, `ActionBlock`, `ActionBan`; - the log line's actions: the twelve `requestlog.Action…`, with `OffenceLimit` and the two `WAFLine…`; - the dispositions: `DispositionAllow`, `DispositionDeny`, `DispositionClose` in `internal/waf/waf.go`. - The fields and parameters that carry them are `string`: `ban(…, cause, reason string, …)` in `internal/bans/bans.go:766`, `verdict.action` and `refusal.action` in `internal/waf`, `Ban.Cause`, `Hit.Kind`, `Rule.Action`, `Alert.Event`, `Line.Action`. - `rproxy`'s setting `WAFBackendNoAnswerDisposition` (`internal/config/rproxy.go:35`) is a `string` whose values are two of the dispositions. - One place already does it the other way: `routewatch.Missing` is a named type. ### Why it matters - Any string is accepted where a cause is expected, and two neighbouring `string` parameters can be swapped unnoticed: `cause, reason string`. - A reader cannot find the allowed values from a field's type. ### What changes - Each set becomes a named string type with its constants, in the package that owns it. Fields, parameters and comparisons use the type. - The dispositions are typed in whichever package holds them when this is built: `internal/waf` today, or the package for what `rproxy` and `waf` send each other (https://git.eeqj.de/sneak/smallwebwaf/issues/425) once that is merged. `WAFBackendNoAnswerDisposition` takes that type. - `.golangci.yml` turns every linter on, `exhaustive` among them. It then checks each `switch` over these types for a missing case. Where it asks for one in an existing `switch`, the case is added; the linter's config is not touched. ### What stays - Every value keeps its string. The JSON in the logs, the state files and the alerts, and the labels of the metrics, do not change: a named string type is written as its string. - No existing constant is renamed. Renames belong to the symbol names audit (https://git.eeqj.de/sneak/smallwebwaf/issues/213). - `reputation.AbuseIPDBSource` and the figure names in `internal/waf/heuristics.go` stay plain strings: each names one value, not one of a set. ### Done when - each set above has a named type, and the fields and parameters that carry it use the type; - behaviour is unchanged: every existing test passes with no assertion changed; - `make check` passes; - the PR body lists every new type name. Each says what the thing is to a reader of that one file, the standard of the symbol names audit. If this is more than one PR can carry, say so on this issue instead of starting. Model: fable-5-2
clawbot self-assigned this 2026-11-19 17:12:20 +01:00
Author
Collaborator

PR: #464 (branch issue-428-named-value-types).

Deviation: WAFBackendNoAnswerDisposition stays string. waf imports config, so config cannot use waf.Disposition. It can take the type once #425 moves the dispositions to their own package.

Model: opus-5-5

PR: https://git.eeqj.de/sneak/smallwebwaf/pulls/464 (branch `issue-428-named-value-types`). Deviation: `WAFBackendNoAnswerDisposition` stays `string`. `waf` imports `config`, so `config` cannot use `waf.Disposition`. It can take the type once https://git.eeqj.de/sneak/smallwebwaf/issues/425 moves the dispositions to their own package. Model: opus-5-5
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/smallwebwaf#428