Skip to content

Commit 0d4df38

Browse files
authored
fix: handle prototype-colliding segment names in segment explorer trie (#95403)
Fixes: #95395 ```console let x = {} x['constructor']; // function x['toString']; // function x = Object.create(null); x['constructor']; // undefined x['toString'] // undefined ```
1 parent c099530 commit 0d4df38

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

‎packages/next/src/next-devtools/dev-overlay/segment-explorer-trie.test.tsx‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,85 @@ describe('Segment Explorer', () => {
105105
})
106106
})
107107

108+
test('handle segments that collide with Object.prototype members', () => {
109+
insertSegmentNode(
110+
createSegmentNode({ pagePath: '/constructor/page.js', type: 'page' })
111+
)
112+
insertSegmentNode(
113+
createSegmentNode({ pagePath: '/toString/page.js', type: 'page' })
114+
)
115+
116+
const { result } = renderHook(useSegmentTree)
117+
118+
expect(result.current).toEqual({
119+
children: {
120+
'': {
121+
children: {
122+
constructor: {
123+
children: {
124+
'page.js': {
125+
children: {},
126+
value: {
127+
pagePath: '/constructor/page.js',
128+
type: 'page',
129+
boundaryType: null,
130+
setBoundaryType: expect.anything(),
131+
},
132+
},
133+
},
134+
value: undefined,
135+
},
136+
toString: {
137+
children: {
138+
'page.js': {
139+
children: {},
140+
value: {
141+
pagePath: '/toString/page.js',
142+
type: 'page',
143+
boundaryType: null,
144+
setBoundaryType: expect.anything(),
145+
},
146+
},
147+
},
148+
value: undefined,
149+
},
150+
},
151+
value: undefined,
152+
},
153+
},
154+
value: undefined,
155+
})
156+
157+
removeSegmentNode(
158+
createSegmentNode({ pagePath: '/constructor/page.js', type: 'page' })
159+
)
160+
161+
expect(result.current).toEqual({
162+
children: {
163+
'': {
164+
children: {
165+
toString: {
166+
children: {
167+
'page.js': {
168+
children: {},
169+
value: {
170+
pagePath: '/toString/page.js',
171+
type: 'page',
172+
boundaryType: null,
173+
setBoundaryType: expect.anything(),
174+
},
175+
},
176+
},
177+
value: undefined,
178+
},
179+
},
180+
value: undefined,
181+
},
182+
},
183+
value: undefined,
184+
})
185+
})
186+
108187
test('remove node in the middle', () => {
109188
insertSegmentNode(
110189
createSegmentNode({ pagePath: '/a/b/@sidebar/page.js', type: 'page' })

‎packages/next/src/next-devtools/dev-overlay/segment-explorer-trie.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ function createTrie<Value = string>({
6262
getCharacters?: (item: Value) => string[]
6363
compare?: (a: Value | undefined, b: Value | undefined) => boolean
6464
}): Trie<Value> {
65+
// Null-prototype children so segment names that collide with
66+
// Object.prototype members (e.g. "constructor", "toString") work correctly.
6567
let root: TrieNode<Value> = {
6668
value: undefined,
67-
children: {},
69+
children: Object.create(null),
6870
}
6971

7072
function markUpdated() {
@@ -82,7 +84,7 @@ function createTrie<Value = string>({
8284
currentNode.children[segment] = {
8385
value: undefined,
8486
// Skip value for intermediate nodes
85-
children: {},
87+
children: Object.create(null),
8688
}
8789
}
8890
currentNode = currentNode.children[segment]

0 commit comments

Comments
 (0)