JS: Add Permissive CORS query (CWE-942)#14342
Conversation
...ipt/ql/lib/semmle/javascript/security/dataflow/CorsPermissiveConfigurationCustomizations.qll
Fixed
Show fixed
Hide fixed
erik-krogh
left a comment
There was a problem hiding this comment.
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.
| 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
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.
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 <erik-krogh@github.com>
Co-authored-by: Erik Krogh Kristensen <erik-krogh@github.com>
|
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 :) |
erik-krogh
left a comment
There was a problem hiding this comment.
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.
This file is only used within your experimental query, so it should probably be moved to the same folder.
| --- | ||
| category: minorAnalysis | ||
| --- |
There was a problem hiding this comment.
Oh yeah, this change-note should probably be in the javascript/ql/src/change-notes folder instead.
erik-krogh
left a comment
There was a problem hiding this comment.
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 <erik-krogh@github.com>
…ConfigurationCustomizations.qll Co-authored-by: Erik Krogh Kristensen <erik-krogh@github.com>
|
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. |
erik-krogh
left a comment
There was a problem hiding this comment.
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.