-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from oslabs-beta/master
Obsidian 3.1 - Added support for variables and directives and the ability to limit query depth
- Loading branch information
Showing
16 changed files
with
1,159 additions
and
291 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# sudo: required | ||
# services: | ||
# - docker | ||
# - docker | ||
# before_install: | ||
# - docker build -t open-source-labs/obsidian . | ||
# - docker build -t open-source-labs/obsidian . | ||
# script: | ||
# - docker run open-source-labs/obsidian test --allow-net --allow-read --allow-env --unstable deno.test.ts | ||
# - docker run open-source-labs/obsidian test --allow-net --allow-read --allow-env --unstable deno.test.ts | ||
# env: | ||
# global: | ||
# secure: sfaHXoKGXnAkwS/QK2pdTPC1NVqd9+pVWImEcz8W9IXFRsOcHpt9lVsmB0dFvDpVm+9KFpcBwnpfOtiyoj6Q9NGIY71jG58kYHdbcWBlR3onS7/JBvgEu94DC7HZR+rQ4/GW+ROh4avBt6RjDSuLk4qQ73Yc3+SDKAl+M0PTADlVZpkicCID59qcdynbAjXu5W8lW2Hp0hqO72Prx/8hgmchI0I7zSYcPBFSy3WaEPJa52yKesVwsHcFtzOBMrDAdE+R028AzdBAXUoiqh6cTVeLSTL1jnIWbCBtfAROlTR82cZyo4c7PJxYyqT3mhRSZvBN/3hdW7+xMOzq6gmpmcl1UO2Q5i4xXEGnatfuzMVa/8SqJZoG2IFIWZ4mvelwufHVuLgF+6JvK2BKSpjFfSUGo0p9G0bMg+GHwRipTPIq1If3ELkflAM6QJwL7TritwtWzWXfAfoZ3KALdPTiFzJAKyQfFvSwWbfXqAgqZIbLjlzSgOJ4QKWD6CBksU7b4Oky6hr/+R+ZihzQLtWKkk/8cklEG/NJlknS2vPRG8xRRF7/C+vSFPrCkmsakPc8c1iGfai8J3Vc09Pg0UeShJDWkSQ6QP165ub6LEL5nz0Qzp0CD1sSQu5re5/M5ef9V69L2pdYhEj0RaZ241DF5efzYAgLI8SvMr5TcTr06+8= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"deno.enable": true, | ||
"deno.lint": true, | ||
"deno.unstable": true | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import destructureQueries from './destructure.js'; | ||
|
||
// Interface representing shape of query object after destructuring | ||
interface queryObj { | ||
queries?: Array<object>, | ||
mutations?: Array<object>, | ||
} | ||
|
||
/** | ||
* Tests whether a queryString (string representation of query) exceeds the maximum nested depth levels (queryDepthLimit) allowable for the instance of obsidian | ||
* @param {*} queryString the string representation of the graphql query | ||
* @param {*} queryDepthLimit number representation of the maximum query depth limit. Default 0 will return undefined. Root query doesn't count toward limit. | ||
* @returns boolean indicating whether the query depth exceeded maximum allowed query depth | ||
*/ | ||
export default function queryDepthLimiter(queryString: string, queryDepthLimit: number = 0): void { | ||
const queryObj = destructureQueries(queryString) as queryObj; | ||
/** | ||
*Function that tests whether the query object debth exceeds maximum depth | ||
* @param {*} qryObj an object representation of the query (after destructure) | ||
* @param {*} qryDepthLim the maximum query depth | ||
* @param {*} depth indicates current depth level | ||
* @returns boolean indicating whether query depth exceeds maximum depth | ||
*/ | ||
const queryDepthCheck = (qryObj: queryObj, qryDepthLim: number, depth: number = 0): boolean => { | ||
// Base case 1: check to see if depth exceeds limit, if so, return error (true means depth limit was exceeded) | ||
if (depth > qryDepthLim) return true; | ||
// Recursive case: Iterate through values of queryObj, and check if each value is an object, | ||
for (let value = 0; value < Object.values(qryObj).length; value++) { | ||
// if the value is an object, return invokation queryDepthCheck on nested object and iterate depth | ||
const currentValue = Object.values(qryObj)[value]; | ||
if (typeof currentValue === 'object') { | ||
return queryDepthCheck(currentValue, qryDepthLim, depth + 1); | ||
}; | ||
}; | ||
// Base case 2: reach end of object keys iteration,return false - depth has not been exceeded | ||
return false; | ||
}; | ||
|
||
// Check if queryObj has query or mutation root type, if so, call queryDepthCheck on each element, i.e. each query or mutation | ||
if (queryObj.queries) { | ||
for(let i = 0; i < queryObj.queries.length; i++) { | ||
if(queryDepthCheck(queryObj.queries[i], queryDepthLimit)) { | ||
throw new Error( | ||
'Security Error: Query depth exceeded maximum query depth limit' | ||
); | ||
}; | ||
}; | ||
}; | ||
|
||
if (queryObj.mutations){ | ||
for (let i = 0; i < queryObj.mutations.length; i++) { | ||
if (queryDepthCheck(queryObj.mutations[i], queryDepthLimit)) { | ||
throw new Error( | ||
'Security Error: Query depth exceeded maximum mutation depth limit' | ||
); | ||
}; | ||
}; | ||
}; | ||
} |
Oops, something went wrong.