Settings with a fixed set of values get named types #427

Open
opened 2026-11-19 17:12:15 +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 and setting names are that commit's.

The review's proposal is split in two so that each half is one PR. This issue covers the settings. The other fixed sets of values (ban causes, limit kinds, alert events, actions, dispositions) are the other half: #428. The two halves share no identifier and can be built in either order.

What is there

  • Five settings with a fixed set of values are string fields of config.WAF (internal/config/waf.go): LookupSource, BytesCounted, BlocklistAction, DNSBLAbuseIPDBAction, CoreRuleSetMode.
  • rproxy's WAFBackendNoAnswerDisposition is a sixth. Its values are two of the dispositions, so it is typed with them, in the other half.
  • Their users compare with string literals:
    • cfg.LookupSource != "file" at internal/sww/waf.go:195; == "file" at internal/waf/request.go:424 and internal/waf/lookup.go:60; == "off" at internal/waf/lookup.go:52;
    • == "limit" at internal/waf/biased.go:66 and :75;
    • case "response" and case "request" at internal/waf/bans.go:89.
  • The same values exist again as private constants: fileSource and off in config, deny in internal/waf/reputation.go.
  • One setting already has exported constants, though untyped: config.CoreRuleSetModeOff, CoreRuleSetModeDetect, CoreRuleSetModeBlock.

Why it matters

  • A mistyped literal compiles, and is false when the daemon is up.
  • A reader cannot find the allowed values from the field's type.

What changes

  • Each of the five sets becomes a named string type with its constants, declared in config. The fields, the code in config that reads them, and every comparison use the type and its constants.
  • No string literal for one of these values is left outside config, and the private duplicates go.
  • .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.

Before (internal/waf/lookup.go):

if rq.h.config.LookupSource == "off" || !canBePlaced(rq.client) {

After (the names are placeholders):

if rq.h.config.LookupSource == config.LookupSourceOff || !canBePlaced(rq.client) {

What stays

  • Every setting keeps its name, its values and its default.
  • The settings line logged at start, the metrics' mode label and the alerts' mode detail carry the same strings: a named string type is written as its string.

Done when

  • the five fields have named types, and no comparison with a bare string is left for them;
  • behaviour is unchanged: every existing test passes with no assertion changed;
  • make check passes;
  • the PR body lists every new type and constant name. Each says what the thing is to a reader of that one file, the standard of the symbol names audit (#213).

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 and setting names are that commit's. The review's proposal is split in two so that each half is one PR. This issue covers the settings. The other fixed sets of values (ban causes, limit kinds, alert events, actions, dispositions) are the other half: https://git.eeqj.de/sneak/smallwebwaf/issues/428. The two halves share no identifier and can be built in either order. ### What is there - Five settings with a fixed set of values are `string` fields of `config.WAF` (`internal/config/waf.go`): `LookupSource`, `BytesCounted`, `BlocklistAction`, `DNSBLAbuseIPDBAction`, `CoreRuleSetMode`. - `rproxy`'s `WAFBackendNoAnswerDisposition` is a sixth. Its values are two of the dispositions, so it is typed with them, in the other half. - Their users compare with string literals: - `cfg.LookupSource != "file"` at `internal/sww/waf.go:195`; `== "file"` at `internal/waf/request.go:424` and `internal/waf/lookup.go:60`; `== "off"` at `internal/waf/lookup.go:52`; - `== "limit"` at `internal/waf/biased.go:66` and `:75`; - `case "response"` and `case "request"` at `internal/waf/bans.go:89`. - The same values exist again as private constants: `fileSource` and `off` in `config`, `deny` in `internal/waf/reputation.go`. - One setting already has exported constants, though untyped: `config.CoreRuleSetModeOff`, `CoreRuleSetModeDetect`, `CoreRuleSetModeBlock`. ### Why it matters - A mistyped literal compiles, and is false when the daemon is up. - A reader cannot find the allowed values from the field's type. ### What changes - Each of the five sets becomes a named string type with its constants, declared in `config`. The fields, the code in `config` that reads them, and every comparison use the type and its constants. - No string literal for one of these values is left outside `config`, and the private duplicates go. - `.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. Before (`internal/waf/lookup.go`): ```go if rq.h.config.LookupSource == "off" || !canBePlaced(rq.client) { ``` After (the names are placeholders): ```go if rq.h.config.LookupSource == config.LookupSourceOff || !canBePlaced(rq.client) { ``` ### What stays - Every setting keeps its name, its values and its default. - The settings line logged at start, the metrics' `mode` label and the alerts' `mode` detail carry the same strings: a named string type is written as its string. ### Done when - the five fields have named types, and no comparison with a bare string is left for them; - behaviour is unchanged: every existing test passes with no assertion changed; - `make check` passes; - the PR body lists every new type and constant name. Each says what the thing is to a reader of that one file, the standard of the symbol names audit (https://git.eeqj.de/sneak/smallwebwaf/issues/213). 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:15 +01:00
Author
Collaborator

Built: #466, branch issue-427-typed-fixed-value-settings, into next.

config now declares BytesCounted, LookupSource, ReputationAction and a typed CoreRuleSetMode. Every comparison in waf and sww uses their constants, and the private copies fileSource and deny are gone. The PR body lists every name.

  • Judgement call: BlocklistAction and DNSBLAbuseIPDBAction share ReputationAction, since they have one value set and one reader.
  • Judgement call: the private off in config stays, because timeouts, sizes and rate limits still use it.

Model: opus-5-5

Built: https://git.eeqj.de/sneak/smallwebwaf/pulls/466, branch `issue-427-typed-fixed-value-settings`, into `next`. `config` now declares `BytesCounted`, `LookupSource`, `ReputationAction` and a typed `CoreRuleSetMode`. Every comparison in `waf` and `sww` uses their constants, and the private copies `fileSource` and `deny` are gone. The PR body lists every name. - Judgement call: `BlocklistAction` and `DNSBLAbuseIPDBAction` share `ReputationAction`, since they have one value set and one reader. - Judgement call: the private `off` in `config` stays, because timeouts, sizes and rate limits still use it. 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#427