-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replaces jsonpath-plus module (#1547)
OKTA-819401 fix: replaces jsonpath-plus module
- Loading branch information
1 parent
a3efc35
commit 710288d
Showing
6 changed files
with
141 additions
and
23 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,8 +1,33 @@ | ||
import { JSONPath, JSONPathOptions } from 'jsonpath-plus'; | ||
const jsonpathRegex = /\$?(?<step>\w+)|(?:\[(?<index>\d+)\])/g; | ||
|
||
export function jsonpath(options: JSONPathOptions): any { | ||
// eslint-disable-next-line new-cap | ||
return JSONPath({ | ||
// Disable javascript evaluation by default | ||
preventEval: true, ...options, }); | ||
/* eslint complexity:[0,8] */ | ||
export function jsonpath({ path, json }) { | ||
const steps: string[] = []; | ||
let match: RegExpExecArray | null; | ||
while ((match = jsonpathRegex.exec(path)) !== null) { | ||
const step = match?.groups?.step ?? match?.groups?.index; | ||
if (step) { | ||
steps.push(step); | ||
} | ||
} | ||
|
||
if (steps.length < 1) { | ||
return undefined; | ||
} | ||
|
||
// array length check above guarantees .pop() will return a value | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const lastStep = steps.pop()!; | ||
let curr = json; | ||
for (const step of steps) { | ||
if (Object.prototype.hasOwnProperty.call(curr, step)) { | ||
if (typeof curr[step] !== 'object') { | ||
return undefined; | ||
} | ||
|
||
curr = curr[step]; | ||
} | ||
} | ||
|
||
return curr[lastStep]; | ||
} |
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 |
---|---|---|
@@ -1,14 +1,102 @@ | ||
import { jsonpath } from '../../../lib/util/jsonpath'; | ||
|
||
describe('jsonpath', () => { | ||
it('should throw if vulnerable for RCE (remote code execution)', () => { | ||
expect(() => { | ||
|
||
it('should parse json paths from objects', () => { | ||
expect(jsonpath({ | ||
path: '$.foo.bar', | ||
json: { foo: { bar: 'pass' } } | ||
})).toEqual('pass'); | ||
|
||
expect(jsonpath({ | ||
path: 'foo.bar', | ||
json: { foo: { bar: { baz: 'pass' } } } | ||
})).toEqual({ baz: 'pass' }); | ||
|
||
const arrValues1 = Array(30).fill('fail'); | ||
arrValues1[12] = 'pass'; | ||
expect(jsonpath({ | ||
path: '$.foo.bar[12]', | ||
json: { foo: { bar: arrValues1 } } | ||
})).toEqual('pass'); | ||
|
||
const arrValues2 = Array(30).fill({ bar: 'fail' }); | ||
arrValues2[25].bar = 'pass'; | ||
expect(jsonpath({ | ||
path: 'foo[22].bar', | ||
json: { foo: arrValues2 } | ||
})).toEqual('pass'); | ||
|
||
expect(jsonpath({ | ||
path: 'not.a.path', | ||
json: { foo: 1 } | ||
})).toEqual(undefined); | ||
|
||
expect(jsonpath({ | ||
path: '$.foo.bar[12]', | ||
json: { foo: { bar: [] } } | ||
})).toEqual(undefined); | ||
|
||
expect(jsonpath({ | ||
path: '$.foo.bar', | ||
json: { foo: { bar: {} } } | ||
})).toEqual({}); | ||
|
||
expect(jsonpath({ | ||
path: '$.foo.bar.baz', | ||
json: { foo: { bar: [] } } | ||
})).toEqual(undefined); | ||
|
||
expect(jsonpath({ | ||
path: '', | ||
json: { foo: { bar: [] } } | ||
})).toEqual(undefined); | ||
|
||
expect(jsonpath({ | ||
path: '[]', | ||
json: { foo: { bar: [] } } | ||
})).toEqual(undefined); | ||
|
||
expect(jsonpath({ | ||
path: '{}', | ||
json: { foo: { bar: [] } } | ||
})).toEqual(undefined); | ||
}); | ||
|
||
it('should gracefully handle RCE (remote code execution) attempts', () => { | ||
const evalSpy = jest.spyOn(global, 'eval'); | ||
global.alert = jest.fn(); | ||
const alertSpy = jest.spyOn(global, 'alert'); | ||
|
||
expect( | ||
jsonpath({ | ||
path: '$..[?(' + '(function a(arr){' + 'a([...arr, ...arr])' + '})([1]);)]', | ||
json: { | ||
nonEmpty: 'object', | ||
}, | ||
}) | ||
).toBeUndefined(); | ||
|
||
expect( | ||
jsonpath({ | ||
path: `$[(this.constructor.constructor("eval(alert('foo'))")())]`, | ||
json: { | ||
nonEmpty: 'object', | ||
}, | ||
}) | ||
).toBeUndefined(); | ||
|
||
expect( | ||
jsonpath({ | ||
path: '$..[?(' + '(function a(arr){' + 'a([...arr, ...arr])' + '})([1]);)]', | ||
json: { | ||
nonEmpty: 'object', | ||
}, | ||
}); | ||
}).toThrow(); | ||
path: `$[(this.constructor.constructor("require("child_process").exec("echo 'foo'")")())]`, | ||
json: { | ||
nonEmpty: 'object', | ||
}, | ||
}) | ||
).toBeUndefined(); | ||
|
||
expect(evalSpy).not.toHaveBeenCalled(); | ||
expect(alertSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
}); |
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