-
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
Open
porcupineyhairs
wants to merge
4
commits into
github:main
Choose a base branch
from
porcupineyhairs:goCorsBypass
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,107 @@ | ||
/** | ||
* @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%"]) and not s.toLowerCase().matches(["%original%"]) | ||
} | ||
|
||
/** | ||
* An argument to a Gorilla's OriginValidator Function taken as a source | ||
*/ | ||
class GorillaOriginFuncSource extends DataFlow::Node { | ||
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 | ||
| | ||
this = DataFlow::parameterNode(f.getParameter(0)) | ||
) | ||
} | ||
} | ||
|
||
private class MaybeOrigin extends DataFlow::Node { | ||
MaybeOrigin() { | ||
exists(RemoteFlowSource r | | ||
// Any write where the variables name could suggest it has something to do with cors. | ||
exists(Write w | | ||
// Take `origin` in `origin := r.Header.Get(header)` as a source. | ||
mayBeCors(w.getLhs().getName()) and | ||
TaintTracking::localTaint(r, w.getRhs()) | ||
| | ||
this = r //or this.asInstruction() = w | ||
) | ||
or | ||
// Take the receiver, call or call arguments as source when any of their names match `maybeCors` | ||
exists(DataFlow::CallNode c, DataFlow::ArgumentNode a | | ||
TaintTracking::localTaint(r, a) and | ||
a.argumentOf(c.asExpr(), _) and | ||
mayBeCors([a.getStringValue(), c.getTarget().getName()]) | ||
| | ||
// When argument or receiver is `maybeCors` take them as origin source. | ||
// When the call name is `maybeCors` take the result as origin source | ||
// (this = c.getResult(_) or this = a) | ||
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 or node instanceof GorillaOriginFuncSource | ||
} | ||
|
||
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" or "domain:port" | ||
not ( | ||
// Filter ".domain.com" | ||
a.asExpr().(StringLit).getExactValue().matches(".%") | ||
or | ||
// Filter "domain.com:port" | ||
a.asExpr().(StringLit).getExactValue().matches("%:%") | ||
or | ||
exists(AddExpr w | | ||
// match ".domain" | ||
w.getLeftOperand().getStringValue().matches(".%") | ||
or | ||
// match "str"+ "domain:port" | ||
w.getRightOperand().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" |
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
Singleton set literal Warning