-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
WIP: Go: CORS Bypass due to incorrect checks #16813
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||
* @name Incorrect check on url | ||||||||||||||||||||||||||
* @description If a CORS policy is configured to accept an origin value obtained from the request data, it can lead to a policy bypass. | ||||||||||||||||||||||||||
* @kind path-problem | ||||||||||||||||||||||||||
* @problem.severity warning | ||||||||||||||||||||||||||
* @id go/cors-bypass | ||||||||||||||||||||||||||
* @tags security | ||||||||||||||||||||||||||
* experimental | ||||||||||||||||||||||||||
* external/cwe/cwe-942 | ||||||||||||||||||||||||||
* external/cwe/cwe-346 | ||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
import go | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
bindingset[s] | ||||||||||||||||||||||||||
private predicate mayBeCors(string s) { s.toLowerCase().matches(["%origin%", "%cors%"]) } | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||
* An argument to a Gorilla's OriginValidator Function taken as a source | ||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||
class GorillaOriginFuncSource extends RemoteFlowSource::Range { | ||||||||||||||||||||||||||
GorillaOriginFuncSource() { | ||||||||||||||||||||||||||
exists(FuncDef f, DataFlow::CallNode c | | ||||||||||||||||||||||||||
// Find a func passed to `AllowedOriginValdiator` as a validator. | ||||||||||||||||||||||||||
// The string parameter supplied to the validator is a remote controlled string supplied in the origin header. | ||||||||||||||||||||||||||
// `gh.AllowedOriginValidator(func(origin string) bool{})` | ||||||||||||||||||||||||||
f.getParameter(0).getType() instanceof StringType and | ||||||||||||||||||||||||||
f.getNumParameter() = 1 and | ||||||||||||||||||||||||||
c.getTarget().hasQualifiedName("github.com/gorilla/handlers", "AllowedOriginValidator") and | ||||||||||||||||||||||||||
c.getArgument(0).asExpr() = f | ||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||
DataFlow::localFlow(DataFlow::parameterNode(f.getParameter(0)), this) | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There is no point saying data flows from |
||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @owen-mc This does not seem to do what I need it to do. I tried quick-eval for just these lines. I can see they identify the source correct and the sinks correctly but the moment I run a dataflow query, it fails to see the flow. I can see there is no modification done to the source before the sink. So, i don't know what could be missing here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please can you give me an example of a path you'd expect to see? I've downloaded the db you provided in the bounty issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get a different result from you - when I quick-eval this class on the current db for that repo I get 6 results - all the uses of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I can see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private class MaybeOrigin extends RemoteFlowSource { | ||||||||||||||||||||||||||
MaybeOrigin() { | ||||||||||||||||||||||||||
exists(RemoteFlowSource r | | ||||||||||||||||||||||||||
// Any write where the variables name could suggest it has something to do with cors. | ||||||||||||||||||||||||||
exists(Write w, Variable v | | ||||||||||||||||||||||||||
mayBeCors(w.getLhs().getName()) | ||||||||||||||||||||||||||
or | ||||||||||||||||||||||||||
v.getAWrite() = w and mayBeCors(v.getName()) | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pattern that causes awful performance. In the first part of the
Suggested change
|
||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||
w = r.getASuccessor*().asInstruction() | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line seems off - why would the write instruction be a data flow successor of a remote flow source? I could imagine the rhs of the write instruction might be. But that also gives me no results. |
||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
or | ||||||||||||||||||||||||||
// Any argument or a receiver whose name could suggest it has something to do with cors. | ||||||||||||||||||||||||||
exists(DataFlow::CallNode c, DataFlow::ArgumentNode a | | ||||||||||||||||||||||||||
c.getArgument(_) = r.getASuccessor*() | ||||||||||||||||||||||||||
or | ||||||||||||||||||||||||||
c.getReceiver() = r.getASuccessor*() and | ||||||||||||||||||||||||||
a.argumentOf(c.asExpr(), _) | ||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||
mayBeCors([a.getStringValue(), c.getTarget().getName()]) | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same pattern that produces awful performance that I mentioned above, with the twist that
Suggested change
Note that this matches the receiver and all arguments of any function whose name satisfies There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change makes the result in goZero disappear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The result in goZero starts at this part of the code: Maybe I haven't fully understood what this code is meant to do (ignoring the variable write side for now). My current understanding is "find any argument or a receiver whose name could suggest it has something to do with cors and which has taint flowing to it from a remote flow source - |
||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||
this = r | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private module UrlFlow implements DataFlow::ConfigSig { | ||||||||||||||||||||||||||
Check warning Code scanning / CodeQL Data flow configuration module naming Warning
Modules implementing a data flow configuration should end in Config.
|
||||||||||||||||||||||||||
predicate isSource(DataFlow::Node node) { node instanceof MaybeOrigin } | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
predicate isSink(DataFlow::Node node) { | ||||||||||||||||||||||||||
exists(DataFlow::CallNode mc, DataFlow::ArgumentNode a | | ||||||||||||||||||||||||||
// Get a call to `strings.HasSuffix(origin, allowedDomain)` | ||||||||||||||||||||||||||
mc.getTarget().hasQualifiedName("strings", "HasSuffix") and | ||||||||||||||||||||||||||
a = mc.getArgument(1) and | ||||||||||||||||||||||||||
// should not match ".domain.com" | ||||||||||||||||||||||||||
not a.asExpr().(StringLit).getExactValue().matches(".%") and | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are testing it against the non-vulnerable version. There are two databases. One named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I somehow didn't find all the dbs before. I have them now. I've also read a bit more about the vulnerability and I understand what you're trying to find now. |
||||||||||||||||||||||||||
not exists(AddExpr w | w.getLeftOperand().getStringValue().matches(".%") | | ||||||||||||||||||||||||||
DataFlow::localFlow(DataFlow::exprNode(w), a) | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||
mc.getArgument(0) = node | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private module Flow = TaintTracking::Global<UrlFlow>; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private import Flow::PathGraph | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
from Flow::PathNode source, Flow::PathNode sink | ||||||||||||||||||||||||||
where Flow::flowPath(source, sink) | ||||||||||||||||||||||||||
select sink.getNode(), source, sink, | ||||||||||||||||||||||||||
"This can lead to a Cross Origin Resource Sharing(CORS) policy bypass" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you actually want this to extend
RemoteFlowSource::Range
. Since this is a.ql
file, it can't be imported by any other files, so the only effects are within this file. Looking at the uses ofRemoteFlowSource
in this file, the only effect is thatMaybeOrigin
can include nodes fromGorillaOriginFuncSource
. But asMaybeOrigin
is only used innode instanceof MaybeOrigin or node instanceof GorillaOriginFuncSource
, this makes no difference.