-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
JS: Add Permissive CORS query (CWE-942) #14342
JS: Add Permissive CORS query (CWE-942) #14342
Conversation
...ipt/ql/lib/semmle/javascript/security/dataflow/CorsPermissiveConfigurationCustomizations.qll
Fixed
Show fixed
Hide fixed
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.
Here is a first review. I've only really looked at the implementation, and I haven't taken a good look at the actual vulnerability yet.
javascript/ql/lib/semmle/javascript/frameworks/ApolloGraphQL.qll
Outdated
Show resolved
Hide resolved
javascript/ql/lib/semmle/javascript/frameworks/ApolloGraphQL.qll
Outdated
Show resolved
Hide resolved
javascript/ql/lib/semmle/javascript/frameworks/ApolloGraphQL.qll
Outdated
Show resolved
Hide resolved
javascript/ql/lib/semmle/javascript/frameworks/ApolloGraphQL.qll
Outdated
Show resolved
Hide resolved
javascript/ql/lib/semmle/javascript/frameworks/ApolloGraphQL.qll
Outdated
Show resolved
Hide resolved
javascript/ql/lib/semmle/javascript/frameworks/ApolloGraphQL.qll
Outdated
Show resolved
Hide resolved
...ipt/ql/lib/semmle/javascript/security/dataflow/CorsPermissiveConfigurationCustomizations.qll
Outdated
Show resolved
Hide resolved
} | ||
|
||
/** true and null are considered bad values */ | ||
class BadValues extends Source instanceof DataFlow::Node { |
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.
class BadValues extends Source instanceof DataFlow::Node { | |
class BadValues extends Source { |
Adding DataFlow::Node
should make no difference here.
javascript/ql/src/Security/CWE-942/CorsPermissiveConfiguration.ql
Outdated
Show resolved
Hide resolved
javascript/ql/src/Security/CWE-942/CorsPermissiveConfiguration.ql
Outdated
Show resolved
Hide resolved
QHelp previews: javascript/ql/src/experimental/Security/CWE-942/CorsPermissiveConfiguration.qhelpoverly CORS configurationA server can use RecommendationWhen the On the other hand, if the If the ExampleIn the example below, the import { ApolloServer } from 'apollo-server';
var https = require('https'),
url = require('url');
var server = https.createServer(function () { });
server.on('request', function (req, res) {
// BAD: origin is too permissive
const server_1 = new ApolloServer({
cors: { origin: true }
});
let user_origin = url.parse(req.url, true).query.origin;
// BAD: CORS is controlled by user
const server_2 = new ApolloServer({
cors: { origin: user_origin }
});
}); In the example below, the import { ApolloServer } from 'apollo-server';
var https = require('https'),
url = require('url');
var server = https.createServer(function () { });
server.on('request', function (req, res) {
// GOOD: origin is restrictive
const server_1 = new ApolloServer({
cors: { origin: false }
});
let user_origin = url.parse(req.url, true).query.origin;
// GOOD: user data is properly sanitized
const server_2 = new ApolloServer({
cors: { origin: (user_origin === "https://allowed1.com" || user_origin === "https://allowed2.com") ? user_origin : false }
});
}); References
|
The QLDoc check is still complaining about missing documentation string on |
Since I used |
^ express cors library covered |
What do you mean by |
The value of origin, wildcard includes any origin |
Hmm. Yes you can, but it's not simple, so maybe you should just skip it. Also, you should move the rest of your implementation into the |
Sorry for the delay. Should I move |
Yes please. |
Moved 👍 |
javascript/ql/src/experimental/Security/CWE-942/CorsPermissiveConfigurationCustomizations.qll
Outdated
Show resolved
Hide resolved
* An express route setup configured with the `cors` package. | ||
*/ | ||
class CorsConfiguration extends DataFlow::MethodCallNode { | ||
CorsConfiguration() { exists(Express::RouteSetup setup | this = setup | setup.isUseCall()) } |
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.
Not quite.
Your Cors::Cors getArgument()
predicate on the CorsConfiguration
class doesn't work, because that predicate just assumes that the first argument is the cors
configuration.
I've added some suggestions to use a field such that the result of getArgument
depends on the routesetup in the characteristic predicate.
Co-authored-by: Erik Krogh Kristensen <[email protected]>
Co-authored-by: Erik Krogh Kristensen <[email protected]>
The You can use |
One last thing, and then we can merge. The |
The |
It should compile, I don't know why it didn't, let's try again. |
The error was due to a warning which was caused by Lets see if the CI goes green. |
Now it's working! Thanks 😄 |
ping @erik-krogh :) |
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.
ping @erik-krogh :)
Sorry, I've had a lot to look at.
One more file move, and then I think we'll be there.
/** | ||
* Provides classes for working with Apollo GraphQL connectors. | ||
*/ | ||
|
||
import javascript |
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.
This file is only used within your experimental query, so it should probably be moved to the same folder.
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.
done 👍
--- | ||
category: minorAnalysis | ||
--- |
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.
Oh yeah, this change-note should probably be in the javascript/ql/src/change-notes
folder instead.
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.
Looks good, but now there is a nit about imports.
You are importing from a file named Apollo
, which imports a module also named Apollo
, which means that there could be confusion about which Apollo
is referenced at the import, and that is what the compiler-warning is about.
I added suggestions to fix it, by not directly importing anything named Apollo
.
javascript/ql/src/experimental/Security/CWE-942/CorsPermissiveConfigurationCustomizations.qll
Outdated
Show resolved
Hide resolved
javascript/ql/src/experimental/Security/CWE-942/CorsPermissiveConfigurationCustomizations.qll
Outdated
Show resolved
Hide resolved
…ConfigurationCustomizations.qll Co-authored-by: Erik Krogh Kristensen <[email protected]>
…ConfigurationCustomizations.qll Co-authored-by: Erik Krogh Kristensen <[email protected]>
A CI job keeps failing. I think that is from your branch being quite out-of-date, can you do a merge with Sorry it's taking so long. |
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.
Yep. Now it works.
Thanks.
This pull request adds a query for Permissive CORS to prevent CSRF attacks for Apollo Server. I plan to add a couple more libraries, so I'll leave it in draft for now. 😁
Looking forward to your suggestions.