Skip to content

Commit edfe2d7

Browse files
authored
Merge pull request github#12944 from github/mbg/go/html-template-sanitizers
Go: Add `html/template` functions as sanitisers for XSS queries
2 parents 205bb76 + 5a44fae commit edfe2d7

File tree

6 files changed

+223
-312
lines changed

6 files changed

+223
-312
lines changed

go/ql/lib/semmle/go/security/Xss.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,20 @@ module SharedXss {
127127
)
128128
}
129129
}
130+
131+
/**
132+
* A `Template` from `html/template` will HTML-escape data automatically
133+
* and therefore acts as a sanitizer for XSS vulnerabilities.
134+
*/
135+
class HtmlTemplateSanitizer extends Sanitizer, DataFlow::Node {
136+
HtmlTemplateSanitizer() {
137+
exists(Method m, DataFlow::CallNode call | m = call.getCall().getTarget() |
138+
m.hasQualifiedName("html/template", "Template", "ExecuteTemplate") and
139+
call.getArgument(2) = this
140+
or
141+
m.hasQualifiedName("html/template", "Template", "Execute") and
142+
call.getArgument(1) = this
143+
)
144+
}
145+
}
130146
}

go/ql/src/experimental/CWE-79/HTMLTemplateEscapingPassthrough.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class FlowConfFromUntrustedToPassthroughTypeConversion extends TaintTracking::Co
6464
}
6565

6666
override predicate isSink(DataFlow::Node sink) { isSinkToPassthroughType(sink, dstTypeName) }
67+
68+
override predicate isSanitizer(DataFlow::Node sanitizer) {
69+
sanitizer instanceof SharedXss::Sanitizer or sanitizer.getType() instanceof NumericType
70+
}
6771
}
6872

6973
/**
@@ -100,7 +104,7 @@ class FlowConfPassthroughTypeConversionToTemplateExecutionCall extends TaintTrac
100104
PassthroughTypeName getDstTypeName() { result = dstTypeName }
101105

102106
override predicate isSource(DataFlow::Node source) {
103-
isSourceConversionToPassthroughType(source, _)
107+
isSourceConversionToPassthroughType(source, dstTypeName)
104108
}
105109

106110
private predicate isSourceConversionToPassthroughType(
@@ -141,10 +145,6 @@ class FlowConfFromUntrustedToTemplateExecutionCall extends TaintTracking::Config
141145
override predicate isSource(DataFlow::Node source) { source instanceof UntrustedFlowSource }
142146

143147
override predicate isSink(DataFlow::Node sink) { isSinkToTemplateExec(sink, _) }
144-
145-
override predicate isSanitizer(DataFlow::Node sanitizer) {
146-
sanitizer instanceof SharedXss::Sanitizer or sanitizer.getType() instanceof NumericType
147-
}
148148
}
149149

150150
/**

0 commit comments

Comments
 (0)