Redundant Canonical Header Key

ID

go.redundant_canonical_header_key

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Code Smell

Language

Go

Tags

CWE:1164, code-style, readability

Description

Reports header.Get(http.CanonicalHeaderKey(k)) (and the Set / Add / Del methods) on an http.Header.

Rationale

The http.Header accessor methods already canonicalise the key argument themselves, so wrapping it in http.CanonicalHeaderKey is redundant work that adds noise. The rule fires only when the receiver resolves to net/http.Header and the key argument is exactly a bare CanonicalHeaderKey call; a concatenation, a wrapping call, or a CanonicalHeaderKey on the value argument are left alone.

import "net/http"

func fn(headers http.Header) {
    headers.Get(http.CanonicalHeaderKey("X-Foo")) // FLAW — Get canonicalises already
    headers.Get("X-Foo")                          // OK
}

Remediation

Pass the key directly; the method canonicalises it for you.