Skip to content

Commit

Permalink
mend
Browse files Browse the repository at this point in the history
  • Loading branch information
djfhe committed Nov 2, 2023
1 parent b66d1f0 commit 91f6f8f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
33 changes: 15 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,24 +279,21 @@ function setByPath<
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const parentObject = pathArray.reduce(
(current: any, pathPart) => {
if (typeof current !== 'object' || !hasOwnProperty.call(current, pathPart)) {
throw new Error(`Property ${pathPart} is undefined`)
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const next = current?.[pathPart]

if (next === undefined || next === null) {
throw new Error(`Property ${pathPart} is undefined`)
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return next
},
object,
)
const parentObject = pathArray.reduce((current: any, pathPart) => {
if (typeof current !== 'object' || !hasOwnProperty.call(current, pathPart)) {
throw new Error(`Property ${pathPart} is undefined`)
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const next = current?.[pathPart]

if (next === undefined || next === null) {
throw new Error(`Property ${pathPart} is undefined`)
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return next
}, object)

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
parentObject[lastKey] = value
Expand Down
22 changes: 14 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, it, } from 'vitest'
import { expect, it } from 'vitest'

import { getByPath, setByPath } from '../src'

Expand Down Expand Up @@ -165,16 +165,20 @@ it('Test for prototype pollution', () => {
const object = {}

// @ts-expect-error - this is not a valid path for the object
expect(() => { setByPath(object, '__proto__.polluted', true) }).toThrowError('__proto__');
expect(() => {
setByPath(object, '__proto__.polluted', true)

Check failure on line 169 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type 'string' is not assignable to parameter of type 'never'.
}).toThrowError('__proto__')

// @ts-expect-error - this is not a valid path for the object
expect(getByPath(object, '__proto__')).toBe(undefined)

// @ts-expect-error - this is not a valid path for the object

Check failure on line 175 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / tests

Unused '@ts-expect-error' directive.
expect(() => { setByPath(object, 'constructor.polluted', true) }).toThrowError('constructor');
expect(() => {
setByPath(object, 'constructor.polluted', true)

Check failure on line 177 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type 'string' is not assignable to parameter of type 'never'.
}).toThrowError('constructor')

// @ts-expect-error - this is not a valid path for the object
expect(getByPath(object, 'constructor')).toBe(undefined);
expect(getByPath(object, 'constructor')).toBe(undefined)

// @ts-expect-error - this is should not be defined on the object
expect(object.polluted).toBe(undefined)
Expand All @@ -187,7 +191,6 @@ it('Test for prototype pollution', () => {

expect(object2.constructor.prototype.polluted).toBe(false)


// eslint-disable-next-line @typescript-eslint/no-extraneous-class
const testClass = class TestClass {
// eslint-disable-next-line @typescript-eslint/no-useless-constructor, @typescript-eslint/no-empty-function
Expand All @@ -203,9 +206,12 @@ it('Test for prototype pollution', () => {
expect(getByPath(object3, 'constructor')).toBe(undefined)

// @ts-expect-error - this is not a valid path for the object

Check failure on line 208 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / tests

Unused '@ts-expect-error' directive.
expect(() => { setByPath(object3, 'constructor.polluted', true) }).toThrowError('constructor');
expect(() => {
setByPath(object3, 'constructor.polluted', true)

Check failure on line 210 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type 'string' is not assignable to parameter of type 'never'.
}).toThrowError('constructor')

// @ts-expect-error - this is not a valid path for the object

Check failure on line 213 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / tests

Unused '@ts-expect-error' directive.
expect(() => { setByPath(object3, '__proto__.polluted', true) }).toThrowError('__proto__');

expect(() => {
setByPath(object3, '__proto__.polluted', true)

Check failure on line 215 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type 'string' is not assignable to parameter of type 'never'.
}).toThrowError('__proto__')
})

0 comments on commit 91f6f8f

Please sign in to comment.