Skip to content

fix(G203): treat constant template arguments as safe - #1770

Open
LuisFigueroaG wants to merge 2 commits into
securego:masterfrom
LuisFigueroaG:fix/g203-constant-arguments
Open

LuisFigueroaG wants to merge 2 commits into
securego:masterfrom
LuisFigueroaG:fix/g203-constant-arguments

Conversation

@LuisFigueroaG

Copy link
Copy Markdown

G203 already treats hardcoded strings as safe (template.HTML("<b>x</b>") isn't reported), but it decides that by checking for an *ast.BasicLit. A named constant, or a concatenation of constants, is just as hardcoded, yet it gets reported:

const banner = "<b>Scheduled maintenance tonight</b>"

template.HTML(banner)                          // reported
template.HTML("<i>" + greeting + "</i>")       // reported, greeting is a const

The change in rules/templates.go is one line: instead of the *ast.BasicLit check, look at the constant value the type checker records for the argument. Any compile-time constant is treated like a literal. Anything that mixes in a variable, such as "<b>" + name + "</b>", is still reported, and so is a local variable initialized from a literal (as the existing samples expect).

Tests: three new G203 samples. Two are negatives that fail on master (a named constant passed to template.HTML, and constant concatenations passed to template.HTML and template.JS). One is positive: a variable concatenated between literals.

Validation:

  • make test (fmt, vet, gosec self-scan, govulncheck, ginkgo): pass
  • go test -count=1 ./...: pass
  • golangci-lint run (v2.14.0): 0 issues
  • built CLI, G203 only, master vs this branch on x/tools, x/net, x/telemetry and google/pprof: same findings (all of them pass non-constant values).

G203 considers string literals safe, but only when the argument is an
*ast.BasicLit. A named constant or a concatenation of constants, which
an attacker can not influence any more than a literal, was reported:

	const banner = "<b>maintenance tonight</b>"
	template.HTML(banner)

Use the constant value recorded by the type checker instead, so any
compile-time constant argument is treated like a literal. Arguments
that mix in variables are still reported.
@LuisFigueroaG
LuisFigueroaG deployed to security-review October 7, 2026 16:31 — with GitHub Actions Active
@ccojocar

ccojocar commented Oct 9, 2026

Copy link
Copy Markdown
Member

Please can you rebase? Thanks

@LuisFigueroaG

Copy link
Copy Markdown
Author

Thanks! Brought the branch up to date with master; tests pass.

@LuisFigueroaG
LuisFigueroaG deployed to security-review October 9, 2026 20:28 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
security-review — e7f4b38a Deployed Oct 9, 2026 by LuisFigueroaG via barry-ai-security-review #2656
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.

2 participants