Repository navigation
fix(G203): treat constant template arguments as safe - #1770
Open
LuisFigueroaG wants to merge 2 commits into
Open
LuisFigueroaG wants to merge 2 commits into
LuisFigueroaG wants to merge 2 commits into
Conversation
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.
ccojocar
approved these changes
Oct 9, 2026
Member
|
Please can you rebase? Thanks |
Author
|
Thanks! Brought the branch up to date with master; tests pass. |
This branch was successfully deployed
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.
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:The change in
rules/templates.gois one line: instead of the*ast.BasicLitcheck, 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 totemplate.HTMLandtemplate.JS). One is positive: a variable concatenated between literals.Validation:
make test(fmt, vet, gosec self-scan, govulncheck, ginkgo): passgo test -count=1 ./...: passgolangci-lint run(v2.14.0): 0 issues