-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Go: promote html-template-escaping-bypass-xss
#19386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
owen-mc
merged 22 commits into
github:main
from
owen-mc:go/promote/html-template-escaping-bypass-xss
Jun 6, 2025
Merged
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5bce70f
Move files out of experimental (no changes)
owen-mc 1530ac1
Update path in qlref and update test results
owen-mc 1926ffd
Convert XSS tests to use inline expectations
owen-mc c2ebdf5
Change query id to `go/html-template-escaping-bypass-xss`
owen-mc ca85f0b
Update query metadata
owen-mc ce4be6d
Refactor to use flow state instead of 3 flow configs (copilot)
owen-mc 4e5a865
Manually fix copilot's mistakes and get query working
owen-mc cbdbb03
Tidy up test (remove duplicated `main`)
owen-mc b90aba2
Refactor class for unescaped types
owen-mc 7f007e1
Minor refactor - removed unused argument
owen-mc 3cce4ba
Improve QLDocs
owen-mc cba0bec
Rename files
owen-mc e6c19b0
Modernize tests
owen-mc 3b934b8
Add comment on importance of `Function.getACall()`
owen-mc 38dcc1c
Fix QLDoc
owen-mc f879186
Add missing metadata
owen-mc 6e3b959
Reword qhelp slightly
owen-mc 00cc430
Make examples in qhelp shorter and more realistic
owen-mc 8283d30
Avoid deprecated function in qhelp examples in same folder
owen-mc bef38a4
Add change note
owen-mc 9ba47eb
Update query suite inclusion integration tests
owen-mc c933ab4
Apply suggestions from code review
owen-mc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
119 changes: 119 additions & 0 deletions
119
go/ql/src/Security/CWE-079/HtmlTemplateEscapingBypassXss.ql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* @name HTML template escaping bypass cross-site scripting | ||
* @description Converting user input to a special type that avoids escaping | ||
* when fed into an HTML template allows for a cross-site | ||
* scripting vulnerability. | ||
* @kind path-problem | ||
* @problem.severity error | ||
* @security-severity 6.1 | ||
* @precision high | ||
* @id go/html-template-escaping-bypass-xss | ||
* @tags security | ||
* external/cwe/cwe-079 | ||
* external/cwe/cwe-116 | ||
*/ | ||
|
||
import go | ||
|
||
/** | ||
* A type that will not be escaped when passed to a `html/template` template. | ||
*/ | ||
class UnescapedType extends Type { | ||
UnescapedType() { | ||
this.hasQualifiedName("html/template", | ||
["CSS", "HTML", "HTMLAttr", "JS", "JSStr", "Srcset", "URL"]) | ||
} | ||
} | ||
|
||
/** | ||
* Holds if the sink is a data value argument of a template execution call. | ||
* | ||
* Note that this is slightly more general than | ||
* `SharedXss::HtmlTemplateSanitizer` because it uses `Function.getACall()`, | ||
* which finds calls through interfaces which the receiver implements. This | ||
* finds more results in practice. | ||
*/ | ||
predicate isSinkToTemplateExec(DataFlow::Node sink) { | ||
exists(Method fn, string methodName, DataFlow::CallNode call | | ||
fn.hasQualifiedName("html/template", "Template", methodName) and | ||
call = fn.getACall() | ||
| | ||
methodName = "Execute" and sink = call.getArgument(1) | ||
or | ||
methodName = "ExecuteTemplate" and sink = call.getArgument(2) | ||
) | ||
} | ||
|
||
/** | ||
* Data flow configuration that tracks flows from untrusted sources to template execution calls | ||
* which go through a conversion to an unescaped type. | ||
*/ | ||
module UntrustedToTemplateExecWithConversionConfig implements DataFlow::StateConfigSig { | ||
private newtype TConversionState = | ||
TUnconverted() or | ||
TConverted(UnescapedType unescapedType) | ||
|
||
/** | ||
* The flow state for tracking whether a conversion to an unescaped type has | ||
* occurred. | ||
*/ | ||
class FlowState extends TConversionState { | ||
predicate isBeforeConversion() { this instanceof TUnconverted } | ||
|
||
predicate isAfterConversion(UnescapedType unescapedType) { this = TConverted(unescapedType) } | ||
|
||
/** Gets a textual representation of this element. */ | ||
string toString() { | ||
this.isBeforeConversion() and result = "Unconverted" | ||
or | ||
exists(UnescapedType unescapedType | this.isAfterConversion(unescapedType) | | ||
result = "Converted to " + unescapedType.getQualifiedName() | ||
) | ||
} | ||
} | ||
|
||
predicate isSource(DataFlow::Node source, FlowState state) { | ||
state.isBeforeConversion() and source instanceof ActiveThreatModelSource | ||
} | ||
|
||
predicate isSink(DataFlow::Node sink, FlowState state) { | ||
state.isAfterConversion(_) and isSinkToTemplateExec(sink) | ||
} | ||
|
||
predicate isBarrier(DataFlow::Node node) { | ||
node instanceof SharedXss::Sanitizer and not node instanceof SharedXss::HtmlTemplateSanitizer | ||
or | ||
node.getType() instanceof NumericType | ||
} | ||
|
||
/** | ||
* When a conversion to a passthrough type is encountered, transition the flow state. | ||
*/ | ||
predicate isAdditionalFlowStep( | ||
DataFlow::Node pred, FlowState predState, DataFlow::Node succ, FlowState succState | ||
) { | ||
exists(ConversionExpr conversion, UnescapedType unescapedType | | ||
// If not yet converted, look for a conversion to a passthrough type | ||
predState.isBeforeConversion() and | ||
succState.isAfterConversion(unescapedType) and | ||
succ.(DataFlow::TypeCastNode).getExpr() = conversion and | ||
pred.asExpr() = conversion.getOperand() and | ||
conversion.getType().getUnderlyingType*() = unescapedType | ||
) | ||
} | ||
} | ||
|
||
module UntrustedToTemplateExecWithConversionFlow = | ||
TaintTracking::GlobalWithState<UntrustedToTemplateExecWithConversionConfig>; | ||
|
||
import UntrustedToTemplateExecWithConversionFlow::PathGraph | ||
|
||
from | ||
UntrustedToTemplateExecWithConversionFlow::PathNode untrustedSource, | ||
UntrustedToTemplateExecWithConversionFlow::PathNode templateExecCall, UnescapedType unescapedType | ||
where | ||
UntrustedToTemplateExecWithConversionFlow::flowPath(untrustedSource, templateExecCall) and | ||
templateExecCall.getState().isAfterConversion(unescapedType) | ||
select templateExecCall.getNode(), untrustedSource, templateExecCall, | ||
"Data from an $@ will not be auto-escaped because it was converted to template." + | ||
unescapedType.getName(), untrustedSource.getNode(), "untrusted source" |
13 changes: 13 additions & 0 deletions
13
go/ql/src/Security/CWE-079/HtmlTemplateEscapingBypassXssBad.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"html/template" | ||
"net/http" | ||
) | ||
|
||
func bad(w http.ResponseWriter, r *http.Request) { | ||
r.ParseForm() | ||
username := r.Form.Get("username") | ||
tmpl, _ := template.New("test").Parse(`<b>Hi {{.}}</b>`) | ||
tmpl.Execute(w, template.HTML(username)) | ||
} |
13 changes: 13 additions & 0 deletions
13
go/ql/src/Security/CWE-079/HtmlTemplateEscapingBypassXssGood.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"html/template" | ||
"net/http" | ||
) | ||
|
||
func good(w http.ResponseWriter, r *http.Request) { | ||
r.ParseForm() | ||
username := r.Form.Get("username") | ||
tmpl, _ := template.New("test").Parse(`<b>Hi {{.}}</b>`) | ||
tmpl.Execute(w, username) | ||
} |
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
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
4 changes: 4 additions & 0 deletions
4
go/ql/src/change-notes/2025-05-01-html-template-escaping-bypass-xss.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: newQuery | ||
--- | ||
* A new query (`go/html-template-escaping-bypass-xss`) has been promoted to the main query suite. This query finds potential cross-site scripting (XSS) vulnerabilities when using the `html/template` package, caused by user input being cast to a type which bypasses the HTML autoescaping. It was originally contributed to the experimental query pack by @gagliardetto in <https://github.com/github/codeql-go/pull/493>. | ||
owen-mc marked this conversation as resolved.
Show resolved
Hide resolved
|
153 changes: 0 additions & 153 deletions
153
go/ql/src/experimental/CWE-79/HTMLTemplateEscapingPassthrough.ql
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.