Skip to content

fix: Fail on infinite recursion in encode.js #2099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: alpha
Choose a base branch
from
18 changes: 18 additions & 0 deletions src/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,30 @@ import ParseObject from './ParseObject';
import { Op } from './ParseOp';
import ParseRelation from './ParseRelation';

const MAX_RECURSIVE_CALLS = 999;
let recursiveCallsCount = 0;

function encode(
value: mixed,
disallowObjects: boolean,
forcePointers: boolean,
seen: Array<mixed>,
offline: boolean
): any {
recursiveCallsCount++;

if (recursiveCallsCount > MAX_RECURSIVE_CALLS) {
const errorMessage = 'Maximum recursive calls exceeded in encode function. Potential infinite recursion detected.';
console.warn(errorMessage);
console.debug('Value causing potential infinite recursion:', value);
console.debug('Disallow objects:', disallowObjects);
console.debug('Force pointers:', forcePointers);
console.debug('Seen:', seen);
console.debug('Offline:', offline);

throw new Error(errorMessage);
}

if (value instanceof ParseObject) {
if (disallowObjects) {
throw new Error('Parse Objects not allowed here');
Expand Down Expand Up @@ -89,5 +106,6 @@ export default function (
seen?: Array<mixed>,
offline?: boolean
): any {
recursiveCallsCount = 0;
return encode(value, !!disallowObjects, !!forcePointers, seen || [], offline);
}