Skip to content

httpstatuscode: context detection gates on hardcoded identifier spellings (status/statusCode/.StatusCode), not on type — false n [Content truncated due to length] #42004

Closed as not planned

Description

@github-actions

Summary

The new httpstatuscode linter (registered 36th in cmd/linters/main.go:72) decides whether an integer-literal comparison is an HTTP status comparison via isHTTPStatusContext (pkg/linters/httpstatuscode/httpstatuscode.go:173-195). That gate is a hardcoded allow-list of identifier/field spellings rather than a domain/type test, which produces both false negatives and false positives. The two branches are also inconsistent — the selector branch type-resolves to an integer, the identifier branch performs no type check at all.

Root cause

func isHTTPStatusContext(pass *analysis.Pass, expr ast.Expr) bool {
	switch e := expr.(type) {
	case *ast.Ident:
		return e.Name == "status" || e.Name == "statusCode"   // name-only, no type check
	case *ast.SelectorExpr:
		if e.Sel.Name != "StatusCode" {                         // only the field spelling "StatusCode"
			return false
		}
		// ... type-resolves to integer (good)
	}
	return false
}

Three spellings are accepted (status, statusCode, and field .StatusCode); everything else is rejected, and nothing about the value or type being an HTTP status is verified on the identifier path.

False negatives (coverage gap)

HTTP status codes held in any other-named variable or field escape entirely. The gap is visible in this repository:

  • pkg/cli/firewall_policy.go:56 and :65 declare Status int (an HTTP status field). A direct entry.Status == 200 is not flagged, because the selector branch only accepts .StatusCode, not .Status. The code at firewall_policy.go:236-241 only gets linted because the author first copies it into a local named status (status := entry.Status). Inline the field access and the diagnostic vanishes.
  • pkg/cli/gateway_logs_timeline.go:99 declares HTTPStatus int // HTTP response status code — a field that can never be matched.
  • Common local spellings (code, httpStatus, respStatus, sc, wantStatus, gotStatus) are all missed.

False positives

Any non-HTTP integer named status/statusCode (process/exit status, job-state enum, custom protocol code, state-machine value) compared against a literal in 100–599 is wrongly flagged — with a hard-asserting, misleading message ("use http.StatusX instead of magic HTTP status code N") and a wrong fix suggestion. The identifier branch does not confirm the operand's type the way the selector branch does. The testdata implicitly acknowledges this fragility: compareNonHTTP (testdata :49-54) stays silent only because the operand is named buildNumber — rename it to status and it becomes a false positive.

Why this matters

This is the recurring syntactic_match / pattern_set_too_narrow family (cf. #40243, #40244, #40581) applied to a brand-new linter. Because detection hinges on naming convention, the linter's effectiveness is coupled to how engineers happen to name variables, and its precision degrades silently as code is refactored (field-access → local-var changes behavior).

Recommendation

Replace spelling-based gating with type/context resolution, matching the rigor already present in the selector branch:

  1. Identifier path: resolve the operand's type via pass.TypesInfo and confirm it is an integer; ideally also require an HTTP-related origin (assigned from resp.StatusCode, compared against http.Status* elsewhere, or a field/param whose declared type/name indicates HTTP). At minimum add the integer-type check the selector branch already performs, and broaden the accepted name set deliberately rather than the current two-name list.
  2. Selector path: accept HTTP status fields named Status / HTTPStatus (with the existing integer-type guard), not only StatusCode, so fields like AuditLogEntry.Status and the HTTPStatus field are covered.
  3. Add testdata for: entry.Status == 200 (field named Status), an HTTPStatus field comparison, a non-HTTP status int enum that must stay silent, and an HTTP code in a differently-named local (e.g. code := resp.StatusCode; if code == 404).

Before / after intent

  • Before: entry.Status == 200 silent; jobStatus == 200 (non-HTTP, named status) flagged.
  • After: HTTP status fields/locals flagged regardless of spelling; non-HTTP integers not coerced into HTTP diagnostics.

Validation checklist

  • entry.Status == 200 (field Status int, HTTP) is flagged.
  • A field HTTPStatus int comparison is flagged.
  • A non-HTTP status int (state enum) compared to 200/404 is not flagged.
  • An HTTP code in a non-status-named local is flagged.
  • Existing testdata + firewall_policy.go / firewall_log.go true positives still pass.

Effort

Small–medium: single file (httpstatuscode.go) plus testdata. Mirror the type-resolution already in the selector branch and widen both name gates.

Evidence

  • pkg/linters/httpstatuscode/httpstatuscode.go:173-195 (isHTTPStatusContext), :104-136 (run).
  • pkg/cli/firewall_policy.go:56,65,236-241; pkg/cli/gateway_logs_timeline.go:99.
  • Registered at cmd/linters/main.go:72.

References:

Generated by 🤖 Sergo - Serena Go Expert · 226.5 AIC · ⌖ 13.7 AIC · ⊞ 5.9K · ◷

  • expires on Jul 4, 2026, 9:17 PM UTC-08:00

Activity

  1. added
    cookie
    on Jun 28, 2026
  2. github-actions commented on Jun 28, 2026

    @github-actions
    ContributorAuthor

    🍪 Issue Monster selected this for Copilot

    I've identified this issue as a good candidate for automated resolution and requested assignment to the Copilot coding agent.

    If assignment succeeds, the Copilot coding agent will analyze the issue and create a pull request with the fix.

    Om nom nom! 🍪

    🍪 Om nom nom by Issue Monster · 3.86 AIC · ⌖ 15.7 AIC · ⊞ 6.1K · ◷

  3. github-actions commented on Jul 5, 2026

    @github-actions
    ContributorAuthor

    This issue was automatically closed because it expired on 2026-07-05T05:17:29.217Z.

    Closed by Workflow

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

Metadata

Metadata

Labels

cookiesergo

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions