security: clamp int → int32 casts (CodeQL go/incorrect-integer-conversion)#9
Merged
Conversation
…rect-integer-conversion) CodeQL flagged three call sites where an `int` value (env-var-derived or parsed from an attacker-controllable Kafka header) was cast to `int32` without bounds-checking. In normal operation the values are tiny (connection pool sizes ≤ a few hundred, retry attempts ≤ a few dozen), so the cast doesn't actually overflow — but a malicious or misconfigured input could wrap into a negative number, corrupting the pool config or silently bypassing the max-attempts guard in the Kafka requeue path. - `internal/db/postgres.go` — new `clampInt32(int) int32` helper. ≤ 0 becomes 0 (lets pgxpool fall back to its own defaults), ≥ MaxInt32 pins to MaxInt32. Used for `MaxConns` / `MinConns`. Test in `postgres_test.go` covers boundary values. - `internal/queue/backend_kafka.go` — clamp the parsed `x-attempts` header before the `int32` cast and the `+1` increment. Closes the three open `go/incorrect-integer-conversion` alerts at postgres.go:29-30 and backend_kafka.go:312.
…per bound CodeQL's data-flow analysis didn't track the int → int32 narrowing across the if-else clamp from the previous attempt — even though `n` was bounded, the cast `int32(n) + 1` still got flagged as `go/incorrect-integer-conversion`. Restructure so the cast is preceded by an explicit upper-bound check on the exact same variable: do the `+1` in int64 (where overflow is impossible for any int input), then clamp into [1, MaxInt32], then cast. Same effective behavior, but the bound is now visible to CodeQL right at the cast site.
hacka0wi
added a commit
that referenced
this pull request
May 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CodeQL flagged three places where an
int(env-var-derived or parsedfrom a Kafka header) was cast to
int32without bounds-checking. Inpractice the values are tiny — connection pool sizes ≤ 200, attempt
counts ≤ 30 — but a malicious or misconfigured input could wrap into a
negative number, corrupting the pool config or silently bypassing the
max-attempts guard.
This patch:
clampInt32(int) int32ininternal/db/postgres.go(≤ 0 → 0,≥ MaxInt32 → MaxInt32) and routes the two pool-size assignments
through it.
x-attemptsheader before the int32 cast ininternal/queue/backend_kafka.go.clampInt32covering MinInt32, 0, normal range,MaxInt32, and overflow.
Closes alerts
go/incorrect-integer-conversionatpostgres.go:29go/incorrect-integer-conversionatpostgres.go:30go/incorrect-integer-conversionatbackend_kafka.go:312What this does NOT close
The two remaining open alerts are
js/clear-text-loggingin the seedscripts (
prisma/seed.ts:91,prisma/seed-demo.ts:456) — both ofthose
console.logcalls intentionally print the auto-generated adminpassword so the operator can capture it from the seed output. That's
a documented operator-tool pattern, not a runtime bug. I'll dismiss
those alerts separately with reason "won't fix — intentional".
Test plan
go test ./internal/db/... -run TestClampInt32passesgo vet ./...cleango build ./...clean🤖 Generated with Claude Code