graphql-query-whitelisting
A simple GraphQL query whitelisting middleware for express
Rationale
One of the security concerns for a typical GraphQL app is that it lacks of a security mechanism out of the box.
By default, anyone can query any field of your GraphQL app, and if your schema supports nested queries, a malicious attacker could make a query that consumes all the resources of the server.
Example:
query RecursiveQuery {
friends {
username
friends {
username
friends {
username
friends { ... }
}
}
}
}
This middleware avoids this type of queries checking if the incoming query is whitelisted or not.
(more info: source 1, source 2)
Installation
npm install --save graphql-query-whitelisting
In your app:
const app = const redis = // This function must return a Promise that resolves with a truthy value if the query is valid.const validateFn = redis app // body-parser must be included before including the query whitelisting middlewareapp
Options
validateFn
This property is mandatory and must be a function that receives the query hash and returns a promise that resolves with a boolean value.
Example:
const validQueryHashes = '947lZUnKBKmjhPEOqdsIB0XVL0leG61cMeKR3HDxQK4=' 'GJEJeNmzrZEUZ1bYDiXoR4cFHGdRjTntkQYeY33ZmQ8=' const validateFn = { } app
skipValidationFn
This property is optional and must be a function that receives the express
request object and returns a boolean value. If a truthy value is returned, the whitelist check is skipped and the query is executed.
This option is very useful to skip the whitelist check for certain apps that are already sending dynamic queries that are impossible to add to the whitelist.
Example:
const skipValidationFn = req <> 'legacy-app-1.0' app
validationErrorFn
This property is optional and must be a function that receives the express request object and will be called for every query that is prevented to be executed by this middleware.
Example:
const validationErrorFn = { } app
License
Copyright (c) 2016 Restorando
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.