Skip to content
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

Fix: issue #5756 and fixed the Typescript for baseSet function and isIndex #5776

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/.internal/baseSet.ts
Expand Up @@ -14,7 +14,7 @@ import toKey from './toKey.js'
* @param {Function} [customizer] The function to customize path creation.
* @returns {Object} Returns `object`.
*/
function baseSet(object, path, value, customizer) {
function baseSet<T>(object: T, path: string | string[], value: any, customizer?: (objValue: any, key: string, nested: T) => any): T {
if (!isObject(object)) {
return object
}
Expand All @@ -24,15 +24,24 @@ function baseSet(object, path, value, customizer) {
const lastIndex = length - 1

let index = -1
let nested = object
let nested: any = object

while (nested != null && ++index < length) {
const key = toKey(path[index])
let newValue = value

if (index !== lastIndex) {
const objValue = nested[key]
newValue = customizer ? customizer(objValue, key, nested) : undefined
// Validate key to prevent prototype pollution
if (!isValidKey(key)) {
throw new Error('Invalid key detected');
}
// Validate objValue to prevent prototype pollution
if (!isObject(objValue)) {
throw new Error('Invalid object value detected');
}

newValue = customizer ? customizer(objValue, key, nested) : undefined;
if (newValue === undefined) {
newValue = isObject(objValue)
? objValue
Expand All @@ -45,4 +54,10 @@ function baseSet(object, path, value, customizer) {
return object
}

export default baseSet
// Function to validate keys to prevent prototype pollution
function isValidKey(key: string): boolean {
const disallowedKeys = ['__proto__', '__constructor__', '__prototype__'];
return !disallowedKeys.includes(key);
}

export default baseSet;
2 changes: 1 addition & 1 deletion src/.internal/isIndex.ts
Expand Up @@ -12,7 +12,7 @@ const reIsUint = /^(?:0|[1-9]\d*)$/
* @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
* @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
*/
function isIndex(value, length) {
function isIndex(value: any, length?: number | null): boolean {
const type = typeof value
length = length == null ? MAX_SAFE_INTEGER : length

Expand Down