Skip to content

Commit 6aa81f9

Browse files
authored
🐚 harden get() against prototype-path traversal (__proto__ / constructor / prototype) (#13479)
* Initial plan * fix: block prototype path traversal in get utility Agent-Logs-Url: https://github.com/react-hook-form/react-hook-form/sessions/e68bef2f-a1ad-41e4-9f8c-115c3d0677c4 * refactor: share prototype keyword guard constant Agent-Logs-Url: https://github.com/react-hook-form/react-hook-form/sessions/724cb05a-d110-42d1-ae6f-6597ff2acbcc --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]>
1 parent 645478b commit 6aa81f9

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎src/__tests__/utils/get.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ describe('get', () => {
6060
expect(get(object, 'With " dobule quote')).toEqual(undefined);
6161
});
6262

63+
it('should not retrieve prototype properties through path traversal', () => {
64+
const pollutedKey = '__reactHookFormPolluted__';
65+
const object = { name: 'John' };
66+
67+
Object.prototype[pollutedKey] = 'SECRET_DATA';
68+
69+
expect(get(object, `__proto__.${pollutedKey}`, 'default')).toBe('default');
70+
expect(get(object, `__proto__[${pollutedKey}]`, 'default')).toBe('default');
71+
72+
delete Object.prototype[pollutedKey];
73+
});
74+
6375
describe('get - preserveNull option', () => {
6476
const obj = {
6577
a: {

‎src/constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ export const INPUT_VALIDATION_RULES = {
2828
export const FORM_ERROR_TYPE = 'form';
2929

3030
export const ROOT_ERROR_TYPE = 'root';
31+
32+
export const PROTOTYPE_KEYWORDS = ['__proto__', 'constructor', 'prototype'];

‎src/utils/get.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PROTOTYPE_KEYWORDS } from '../constants';
2+
13
import isKey from './isKey';
24
import isNullOrUndefined from './isNullOrUndefined';
35
import isObject from './isObject';
@@ -14,6 +16,9 @@ export default <T>(
1416
}
1517

1618
const paths = isKey(path) ? [path] : stringToPath(path);
19+
if (paths.some((key) => PROTOTYPE_KEYWORDS.includes(key))) {
20+
return defaultValue;
21+
}
1722

1823
const result = paths.reduce<any>((result, key) => {
1924
return isNullOrUndefined(result) ? undefined : result[key];

‎src/utils/set.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PROTOTYPE_KEYWORDS } from '../constants';
12
import type { FieldPath, FieldValues } from '../types';
23

34
import isKey from './isKey';
@@ -28,7 +29,7 @@ export default (
2829
: {};
2930
}
3031

31-
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
32+
if (PROTOTYPE_KEYWORDS.includes(key)) {
3233
return;
3334
}
3435

0 commit comments

Comments
 (0)