Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-areas-shine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/router-core': patch
---

Prevent constructor fields in search parameters from crashing router initialization or client navigation, including null values and objects that shadow hasOwnProperty.
4 changes: 2 additions & 2 deletions packages/router-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,13 @@ export function isPlainObject(o: any) {
}

// If has modified prototype
const prot = ctor.prototype
const prot = ctor?.prototype
if (!hasObjectPrototype(prot)) {
return false
}

// If constructor does not have an Object-specific method
if (!prot.hasOwnProperty('isPrototypeOf')) {
if (!hasOwn.call(prot, 'isPrototypeOf')) {
return false
}

Expand Down
62 changes: 62 additions & 0 deletions packages/router-core/tests/search-constructor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, test, vi } from 'vitest'
import { createMemoryHistory } from '@tanstack/history'
import { BaseRootRoute, BaseRoute } from '../src'
import { createTestRouter } from './routerTestUtils'

describe('search parameters with constructor data', () => {
test.each([
['null', null],
['zero', 0],
['string', 'model'],
['object', { prototype: { hasOwnProperty: null } }],
])('initializes and loads with a %s constructor field', async (_, value) => {
const rootRoute = new BaseRootRoute({})
const loader = vi.fn(() => 'loaded')
const validateSearch = vi.fn(() => ({}))
const route = new BaseRoute({
getParentRoute: () => rootRoute,
path: '/',
validateSearch,
loader,
})
const history = createMemoryHistory({
initialEntries: [
`/?constructor=${encodeURIComponent(JSON.stringify(value))}`,
],
})
const router = createTestRouter({
routeTree: rootRoute.addChildren([route]),
history,
isServer: false,
search: { strict: true },
})

await router.load()

expect(loader).toHaveBeenCalledOnce()
expect(validateSearch).toHaveBeenCalled()
expect(router.state.matches.at(-1)?.loaderData).toBe('loaded')
})

test('preserves constructor data during client navigation', async () => {
const rootRoute = new BaseRootRoute({})
const route = new BaseRoute({
getParentRoute: () => rootRoute,
path: '/',
validateSearch: (search: Record<string, unknown>) => search,
})
const router = createTestRouter({
routeTree: rootRoute.addChildren([route]),
history: createMemoryHistory(),
isServer: false,
})
await router.load()

await router.navigate({ to: '/', search: { constructor: null } })

expect(router.state.location.search.constructor).toBeNull()
expect(router.state.location.href).toBe('/?constructor=null')
await router.navigate({ to: '/', search: { constructor: 'model' } })
expect(router.state.location.search.constructor).toBe('model')
})
})