Skip to content

Commit 7ea9933

Browse files
fix: prevent crash when query params have conflicting dot-notation keys (#2958)
fix: return undefined from expand() when a path segment conflicts with a primitive Co-authored-by: Michael Solomon <github@tltv.co.il>
1 parent d00d371 commit 7ea9933

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ const expand = input => {
682682
} else {
683683
resultPtr[part] = {}
684684
}
685+
} else if (typeof resultPtr[part] !== 'object') {
686+
return undefined
685687
}
686688
resultPtr = resultPtr[part]
687689
}

tests/got/test_query_complex.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ describe('`query()` complex encoding', () => {
111111
scope.done()
112112
})
113113

114+
it('does not throw on unbalanced dot-notation query params', async () => {
115+
const scope = nock('http://example.test')
116+
.get('/path')
117+
.query({ parent: 'value', 'parent.1': 'first' })
118+
.reply(200)
119+
await got('http://example.test/path?parent=value&parent.1=first')
120+
scope.done()
121+
})
122+
114123
it('query with array and regexp', async () => {
115124
// In Node 10.x this can be updated:
116125
// const exampleQuery = new URLSearchParams([

tests/test_common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,4 +629,8 @@ describe('`expand()`', () => {
629629
expect(original).deep.equal({ 'foo[bar][0]': 'baz' })
630630
expect(original).not.equal(result)
631631
})
632+
633+
it('returns undefined when a key conflicts with an already-set primitive value', () => {
634+
expect(expand({ parent: 'value', 'parent.1': 'first' })).equal(undefined)
635+
})
632636
})

0 commit comments

Comments
 (0)