Created
February 20, 2018 10:23
-
-
Save mxstbr/8afee198114fdf3139e074aaaa816327 to your computer and use it in GitHub Desktop.
Code snippets for "Securing Your GraphQL API from Malicious Queries" blog post
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
app.use('/api', graphqlServer({ | |
validationRules: [depthLimit(10)] | |
})) |
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
query evilQuery { | |
thread(id: "54887141-57a9-4386-807c-ed950c4d5132") { | |
messageConnection(first: 100) { ... } | |
participants(first: 100) { | |
threadConnection(first: 100) { ... } | |
communityConnection { ... } | |
channelConnection { ... } | |
everything(first: 100) { ... } | |
} | |
} | |
} |
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
query maliciousQuery { | |
thread(id: "some-id") { | |
messages(first: 99999) { | |
thread { | |
messages(first: 99999) { | |
thread { | |
messages(first: 99999) { | |
thread { | |
# ...repeat times 10000... | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
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
{ | |
"scripts": { | |
"postbuild": "persistgraphql src api/query-whitelist.json" | |
} | |
} |
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
const PaginationAmount = GraphQLInputInt({ | |
name: 'PaginationAmount', | |
min: 1, | |
max: 100, | |
}); |
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
type Participant { | |
# The complexity of getting one thread in a thread connection is 3, and multiply that by the amount of threads fetched | |
threadConnection(first: PaginationAmount, after: String): ThreadConnection @cost(complexity: 3, multiplier: "first") | |
} | |
type Thread { | |
participants(first: PaginationAmount,...): [Participant] @cost(complexity: 2, multiplier: "first") | |
} |
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
app.use('/api', graphqlServer((req, res) => { | |
const query = req.query.query || req.body.query; | |
// TODO: Get whitelist somehow | |
if (!whitelist[query]) { | |
throw new Error('Query is not in whitelist.'); | |
} | |
/* ... */ | |
})) |
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
app.use('*', (req, res, next) => { | |
const query = req.query.query || req.body.query || ''; | |
if (query.length > 2000) { | |
throw new Error('Query too large'); | |
} | |
next(); | |
}) |
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
type Thread { | |
messages(first: PaginationAmount, after: String): [Message] | |
} |
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
type Thread { | |
messages(first: Int, after: String): [Message] | |
} | |
type Message { | |
thread: Thread | |
} | |
type Query { | |
thread(id: ID!): Thread | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment