-
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
Draft
owen-mc
wants to merge
14
commits into
github:main
Choose a base branch
from
owen-mc:go/promote/html-template-escaping-bypass-xss
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
346662c
Move files out of experimental (no changes)
owen-mc 75e9844
Update path in qlref and update test results
owen-mc 56745ef
Convert XSS tests to use inline expectations
owen-mc 6220d63
Change query id to `go/html-template-escaping-bypass-xss`
owen-mc 3d3b64f
Update query metadata
owen-mc ccbea66
Refactor to use flow state instead of 3 flow configs (copilot)
owen-mc 3a32fbf
Manually fix copilot's mistakes and get query working
owen-mc 53c108c
Tidy up test (remove duplicated `main`)
owen-mc 11fbcc4
Refactor class for unescaped types
owen-mc bd8f782
Minor refactor - removed unused argument
owen-mc fa30bb8
Improve QLDocs
owen-mc 9b1a4df
Rename files
owen-mc 3991b77
Modernize tests
owen-mc 896cf81
Add comment on importance of `Function.getACall()`
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
116 changes: 116 additions & 0 deletions
116
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,116 @@ | ||
/** | ||
* @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 | ||
* @id go/html-template-escaping-bypass-xss | ||
* @tags security | ||
* external/cwe/cwe-79 | ||
* 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) | ||
|
||
/** | ||
* 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" |
File renamed without changes.
File renamed without changes.
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.
Check warning
Code scanning / CodeQL
Class QLDoc style. Warning