This package has been deprecated

Author message:

Package was renamed to graphql-query-whitelist

graphql-query-whitelisting

0.1.1 • Public • Published

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:

import express from 'express'
import bodyParser from 'body-parser'
import Redis from 'ioredis'
 
const app = express()
const redis = new Redis()
 
// This function must return a Promise that resolves with a truthy value if the query is valid.
const validateFn = (queryHash) => redis.sismember('query-whitelist', queryHash)
 
app.use(bodyParser.json()) // body-parser must be included before including the query whitelisting middleware
app.post('/graphql', queryWhitelisting({ validateFn }))

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 = (queryHash) => new Promise((resolve) => {
  resolve(validQueryHashes.indexOf(queryHash) > -1)
})
 
app.post('/graphql', queryWhitelisting({ validateFn }))

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) => req.get('X-App-Version') <> 'legacy-app-1.0'
 
app.post('/graphql', queryWhitelisting({ validateFn, skipValidationFn }))

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:

import { verbose, warn } from 'utils/log'
 
const validationErrorFn = (req) => {
  warn(`Query '${req.queryHash}' is not in the whitelist`)
  verbose(`Unauthorized query: ${req.normalizedQuery}`)
}
 
app.post('/graphql', queryWhitelisting({ validateFn, validationErrorFn }))

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.

Readme

Keywords

none

Package Sidebar

Install

npm i graphql-query-whitelisting

Weekly Downloads

1

Version

0.1.1

License

MIT

Last publish

Collaborators

  • restorando