-
Notifications
You must be signed in to change notification settings - Fork 358
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
tech story: [M3-8385] - Replace lodash set utility function to handle prototype pollution security threat #10814
Merged
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ce72f36
remove usage of omit that depends on lodash
coliu-akamai 630c709
it is not going
coliu-akamai d002981
need to figure out types
coliu-akamai 71b15b4
hopefully this is something
coliu-akamai 82f904d
todo: fix all bugs and write all the tests
coliu-akamai c0320a9
write tests - need to debug
coliu-akamai a252d41
determine if supposed index is actually a valid index
coliu-akamai 3d52159
some path indexing that seems difficult to pursue
coliu-akamai b9fe76b
Merge branch 'develop' into m3-8385
coliu-akamai 8ed1033
update naming, tests
coliu-akamai 46893df
remove set from package.json
coliu-akamai ce4bfbc
add back in some commented out tests
coliu-akamai 559e49d
some cleanup, need to look over everything
coliu-akamai be10ebe
update test cases and comments
coliu-akamai 4e0ef19
changeset + update tests
coliu-akamai 2ab0bf2
simplify set, remove separate set utility files
coliu-akamai fda3f56
Merge branch 'develop' into m3-8385
coliu-akamai 01c6e76
Update packages/manager/src/utilities/formikErrorUtils.ts
coliu-akamai b4c000e
update types + fix tests @abailly-akamai
coliu-akamai 991866c
Merge branch 'develop' into m3-8385
coliu-akamai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/manager/.changeset/pr-10814-tech-stories-1724434847753.md
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/manager": Tech Stories | ||
--- | ||
|
||
Replace lodash set utility function to handle security threat raised by Dependabot ([#10814](https://github.com/linode/manager/pull/10814)) |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import { | |
getFormikErrorsFromAPIErrors, | ||
handleAPIErrors, | ||
handleVPCAndSubnetErrors, | ||
set, | ||
} from './formikErrorUtils'; | ||
|
||
const errorWithoutField = [{ reason: 'Internal server error' }]; | ||
|
@@ -35,51 +36,51 @@ describe('handleAPIErrors', () => { | |
|
||
const subnetMultipleErrorsPerField = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are just eslint changes for this test (note - the specific method these tests are for may be able to be removed in favor of using |
||
{ | ||
reason: 'not expected error for label', | ||
field: 'subnets[0].label', | ||
reason: 'not expected error for label', | ||
}, | ||
{ | ||
reason: 'expected error for label', | ||
field: 'subnets[0].label', | ||
reason: 'expected error for label', | ||
}, | ||
{ | ||
reason: 'not expected error for label', | ||
field: 'subnets[3].label', | ||
reason: 'not expected error for label', | ||
}, | ||
{ | ||
reason: 'expected error for label', | ||
field: 'subnets[3].label', | ||
reason: 'expected error for label', | ||
}, | ||
{ | ||
reason: 'not expected error for ipv4', | ||
field: 'subnets[3].ipv4', | ||
reason: 'not expected error for ipv4', | ||
}, | ||
{ | ||
reason: 'expected error for ipv4', | ||
field: 'subnets[3].ipv4', | ||
reason: 'expected error for ipv4', | ||
}, | ||
]; | ||
|
||
const subnetErrors = [ | ||
{ | ||
reason: 'Label required', | ||
field: 'subnets[1].label', | ||
reason: 'Label required', | ||
}, | ||
{ | ||
reason: 'bad label', | ||
field: 'subnets[2].label', | ||
reason: 'bad label', | ||
}, | ||
{ | ||
reason: 'cidr ipv4', | ||
field: 'subnets[2].ipv4', | ||
reason: 'cidr ipv4', | ||
}, | ||
{ | ||
reason: 'needs an ip', | ||
field: 'subnets[4].ipv4', | ||
reason: 'needs an ip', | ||
}, | ||
{ | ||
reason: 'needs an ipv6', | ||
field: 'subnets[4].ipv6', | ||
reason: 'needs an ipv6', | ||
}, | ||
]; | ||
|
||
|
@@ -93,7 +94,7 @@ describe('handleVpcAndConvertSubnetErrors', () => { | |
expect(Object.keys(errors)).toHaveLength(3); | ||
expect(Object.keys(errors)).toEqual(['1', '2', '4']); | ||
expect(errors[1]).toEqual({ label: 'Label required' }); | ||
expect(errors[2]).toEqual({ label: 'bad label', ipv4: 'cidr ipv4' }); | ||
expect(errors[2]).toEqual({ ipv4: 'cidr ipv4', label: 'bad label' }); | ||
expect(errors[4]).toEqual({ ipv4: 'needs an ip', ipv6: 'needs an ipv6' }); | ||
}); | ||
|
||
|
@@ -106,8 +107,8 @@ describe('handleVpcAndConvertSubnetErrors', () => { | |
expect(Object.keys(errors)).toHaveLength(2); | ||
expect(errors[0]).toEqual({ label: 'expected error for label' }); | ||
expect(errors[3]).toEqual({ | ||
label: 'expected error for label', | ||
ipv4: 'expected error for ipv4', | ||
label: 'expected error for label', | ||
}); | ||
}); | ||
|
||
|
@@ -201,3 +202,195 @@ describe('getFormikErrorsFromAPIErrors', () => { | |
} | ||
}); | ||
}); | ||
|
||
describe('Tests for set', () => { | ||
it("returns the passed in 'object' as is if it's not actually a (non array) object", () => { | ||
expect(set([], 'path not needed', 3)).toEqual([]); | ||
}); | ||
|
||
describe('Correctly setting the value at the given path', () => { | ||
it('sets the value for a simple path', () => { | ||
const object = {}; | ||
let settedObject = set(object, 'test', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({ test: 1 }); | ||
|
||
settedObject = set(object, 'test2', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({ test: 1, test2: 1 }); | ||
}); | ||
|
||
it('sets the value for complex string paths (without indexes)', () => { | ||
const object = {}; | ||
|
||
set(object, 'a.b.c', 'c'); | ||
expect(object).toEqual({ a: { b: { c: 'c' } } }); | ||
|
||
set(object, 'a.b.d', 'd'); | ||
expect(object).toEqual({ | ||
a: { b: { c: 'c', d: 'd' } }, | ||
}); | ||
|
||
set(object, 'e[f][g]', 'g'); | ||
expect(object).toEqual({ | ||
a: { b: { c: 'c', d: 'd' } }, | ||
e: { f: { g: 'g' } }, | ||
}); | ||
}); | ||
|
||
it('sets the value for complex string paths (with indexes)', () => { | ||
const object = {}; | ||
|
||
set(object, 'a.b.1', 'b1'); | ||
expect(object).toEqual({ a: { b: [undefined, 'b1'] } }); | ||
set(object, 'a.b[0]', 5); | ||
expect(object).toEqual({ a: { b: [5, 'b1'] } }); | ||
|
||
set(object, 'a.b.2', 'b2'); | ||
expect(object).toEqual({ | ||
a: { b: [5, 'b1', 'b2'] }, | ||
}); | ||
|
||
set(object, 'a.b[3].c', 'c'); | ||
expect(object).toEqual({ | ||
a: { b: [5, 'b1', 'b2', { c: 'c' }] }, | ||
}); | ||
}); | ||
|
||
it('only considers 0 or positive integers for setting array values', () => { | ||
const object = {}; | ||
|
||
expect(set(object, 'test[-01].test1', 'test')).toEqual({ | ||
test: { '-01': { test1: 'test' } }, | ||
}); | ||
expect(set(object, 'test[-01][-02]', 'test2')).toEqual({ | ||
test: { '-01': { '-02': 'test2', test1: 'test' } }, | ||
}); | ||
expect(set(object, 'test[ 02]', 'test3')).toEqual({ | ||
test: { | ||
' 02': 'test3', | ||
'-01': { '-02': 'test2', test1: 'test' }, | ||
}, | ||
}); | ||
expect(set(object, 'test[0 0]', 'test4')).toEqual({ | ||
test: { | ||
' 02': 'test3', | ||
'-01': { '-02': 'test2', test1: 'test' }, | ||
'0 0': 'test4', | ||
}, | ||
}); | ||
}); | ||
|
||
it('considers numbers as keys if they are not followed by another number or if there is an already existing object', () => { | ||
const object = {}; | ||
set(object, '1', 'test'); | ||
expect(object).toEqual({ 1: 'test' }); | ||
|
||
set(object, '2', '2'); | ||
expect(object).toEqual({ 1: 'test', 2: '2' }); | ||
|
||
expect(set({ test: { test1: 'test' } }, 'test[1]', 'test2')).toEqual({ | ||
test: { '1': 'test2', test1: 'test' }, | ||
}); | ||
}); | ||
|
||
it('treats numbers as array indexes if they precede some previous key (if they are valid indexes)', () => { | ||
const obj1 = set({}, '1[1]', 'test'); | ||
expect(obj1).toEqual({ 1: [undefined, 'test'] }); | ||
|
||
const obj2 = set({}, '1.2', 'test'); | ||
expect(obj2).toEqual({ 1: [undefined, undefined, 'test'] }); | ||
}); | ||
|
||
it('can replace the value at an already existing key', () => { | ||
const alreadyExisting = { test: 'test' }; | ||
expect(set(alreadyExisting, 'test', 'changed')).toEqual({ | ||
test: 'changed', | ||
}); | ||
expect(set(alreadyExisting, 'test[test2][test3]', 'changed x4')).toEqual({ | ||
test: { test2: { test3: 'changed x4' } }, | ||
}); | ||
}); | ||
|
||
it('sets the value for nonstandard paths', () => { | ||
expect(set({}, 'test.[.test]', 'testing 2')).toEqual({ | ||
test: { test: 'testing 2' }, | ||
}); | ||
expect(set({}, 'test.[te[st]', 'testing 3')).toEqual({ | ||
test: { te: { st: 'testing 3' } }, | ||
}); | ||
expect(set({}, 'test.]test', 'testing 4')).toEqual({ | ||
test: { test: 'testing 4' }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Ensuring safety against prototype pollution and that the passed in and returned object are the same', () => { | ||
it('protects against the given string path matching a prototype pollution key', () => { | ||
const object = {}; | ||
// __proto__ | ||
let settedObject = set(object, '__proto__', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
|
||
// constructor | ||
settedObject = set(object, 'constructor', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
|
||
// prototype | ||
settedObject = set(object, 'prototype', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
}); | ||
|
||
it('protects against the given string path containing prototype pollution keys that are separated by path delimiters', () => { | ||
const object = {}; | ||
// prototype pollution key separated by . | ||
let settedObject = set(object, 'test.__proto__.test', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
|
||
settedObject = set(object, 'test.constructor.test', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
|
||
settedObject = set(object, 'test.prototype.test', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
|
||
// prototype pollution key separated by [] | ||
settedObject = set(object, 'test.test[__proto__]', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
|
||
settedObject = set(object, 'test.test[constructor]', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
|
||
settedObject = set(object, 'test.test[prototype]', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({}); | ||
}); | ||
|
||
it('is not considered prototype pollution if the string paths have a key not separated by delimiters', () => { | ||
const object = {}; | ||
// prototype pollution key separated by . | ||
let settedObject = set(object, 'test__proto__test', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({ test__proto__test: 1 }); | ||
|
||
settedObject = set(object, 'constructortest', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({ constructortest: 1, test__proto__test: 1 }); | ||
|
||
settedObject = set(object, 'testprototype', 1); | ||
expect(object).toBe(settedObject); | ||
expect(object).toEqual({ | ||
constructortest: 1, | ||
test__proto__test: 1, | ||
testprototype: 1, | ||
}); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
omitProps is a direct replacement for lodash's omit
should we rename it to omit as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes this change is fine. I just realized lodash is still in out bundle because of formik, but we don't import it directly (in other words this import would break the build if we were to get rid of formik for instance). I am going to add an eslint rule to prevent that from happening in the future