INNER CODE UNIT · TypeScript
queryDepthLimiter
open-source-labs/obsidian · src/DoSSecurity.ts:15
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);