Skip to content

feat: check keys in direct map access - #90

Open
dunglas wants to merge 2 commits into
lasiar:mainfrom
dunglas:feat/map-access
Open

dunglas wants to merge 2 commits into
lasiar:mainfrom
dunglas:feat/map-access

Conversation

@dunglas

@dunglas dunglas commented Sep 30, 2026

Copy link
Copy Markdown

Closes #80

Indexing http.Header, delete and http.Header{...} literals skip canonicalization, so a non-canonical key silently misses. This reports literal and constant keys in those positions. Keys must match http.CanonicalHeaderKey exactly: exclusions and well-known spellings such as ETag don't apply, since net/http never stores them. Deliberate non-canonical writes need //nolint:canonicalheader.

The first commit fixes a detection bug found while testing: net/http.Header was taken from the first object named Header in TypesInfo.Uses, which also holds the ResponseWriter.Header method and iterates in random order. w.Header().Set("foo", "x") was skipped whenever the method won.

Both commits add diagnostics golangci-lint users will see on upgrade. On Caddy (caddyserver/caddy#8025), the map check finds 2 deliberate RFC 6455 spellings in non-test code and nothing else.

This overlaps with #87 in analyzer.go; happy to rebase if it lands first.

The analyzer resolved net/http.Header from the first object named
"Header" found in TypesInfo.Uses. That map also holds the
ResponseWriter.Header method and the Request.Header field, and its
iteration order is random, so calls such as w.Header().Set("foo", "x")
were silently skipped whenever the method won.

Compare the receiver type against net/http.Header directly.
Indexing http.Header, deleting from it and building a composite literal
all skip the canonicalization Get, Set, Add, Del and Values perform. A
non-canonical key in those positions is a silent lookup miss, which is
worse than the extra work the method checks catch.

Report string literals and constants used as keys in index expressions,
the delete builtin and http.Header composite literals. Keys must match
http.CanonicalHeaderKey exactly: exclusions and well-known spellings
such as "ETag" are ignored, since net/http never stores them.

Closes lasiar#80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: support map access

1 participant