Unverified Commit 95781016 authored by Josh Story's avatar Josh Story Committed by GitHub

[dynamicIO] Do not warn on arbitrary param access in browser (#71437)

Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 1749d432
......@@ -2,7 +2,7 @@ import type { Params } from './params'
import { ReflectAdapter } from '../web/spec-extension/adapters/reflect'
import { InvariantError } from '../../shared/lib/invariant-error'
import { describeStringPropertyAccess } from './utils'
import { describeStringPropertyAccess, wellKnownProperties } from './utils'
export function createRenderParamsFromClient(underlyingParams: Params) {
if (process.env.NODE_ENV === 'development') {
......@@ -24,29 +24,12 @@ function makeUntrackedExoticParams(underlyingParams: Params): Promise<Params> {
const promise = Promise.resolve(underlyingParams)
CachedParams.set(underlyingParams, promise)
Object.defineProperties(promise, {
status: {
value: 'fulfilled',
writable: true,
},
value: {
value: underlyingParams,
writable: true,
},
})
Object.keys(underlyingParams).forEach((prop) => {
switch (prop) {
case 'then':
case 'value':
case 'status': {
// These properties cannot be shadowed with a search param because they
// are necessary for ReactPromise's to work correctly with `use`
break
}
default: {
;(promise as any)[prop] = underlyingParams[prop]
}
if (wellKnownProperties.has(prop)) {
// These properties cannot be shadowed because they need to be the
// true underlying value for Promises to work correctly at runtime
} else {
;(promise as any)[prop] = underlyingParams[prop]
}
})
......@@ -61,36 +44,21 @@ function makeDynamicallyTrackedExoticParamsWithDevWarnings(
return cachedParams
}
// We don't use makeResolvedReactPromise here because params
// supports copying with spread and we don't want to unnecessarily
// instrument the promise with spreadable properties of ReactPromise.
const promise = Promise.resolve(underlyingParams)
Object.defineProperties(promise, {
status: {
value: 'fulfilled',
writable: true,
},
value: {
value: underlyingParams,
writable: true,
},
})
const proxiedProperties = new Set<string>()
const unproxiedProperties: Array<string> = []
Object.keys(underlyingParams).forEach((prop) => {
switch (prop) {
case 'then':
case 'value':
case 'status': {
// These properties cannot be shadowed with a search param because they
// are necessary for ReactPromise's to work correctly with `use`
unproxiedProperties.push(prop)
break
}
default: {
proxiedProperties.add(prop)
;(promise as any)[prop] = underlyingParams[prop]
}
if (wellKnownProperties.has(prop)) {
// These properties cannot be shadowed because they need to be the
// true underlying value for Promises to work correctly at runtime
} else {
proxiedProperties.add(prop)
;(promise as any)[prop] = underlyingParams[prop]
}
})
......@@ -99,9 +67,7 @@ function makeDynamicallyTrackedExoticParamsWithDevWarnings(
if (typeof prop === 'string') {
if (
// We are accessing a property that was proxied to the promise instance
proxiedProperties.has(prop) ||
// We are accessing a property that doesn't exist on the promise nor the underlying
Reflect.has(target, prop) === false
proxiedProperties.has(prop)
) {
const expression = describeStringPropertyAccess('params', prop)
warnForSyncAccess(expression)
......@@ -109,6 +75,12 @@ function makeDynamicallyTrackedExoticParamsWithDevWarnings(
}
return ReflectAdapter.get(target, prop, receiver)
},
set(target, prop, value, receiver) {
if (typeof prop === 'string') {
proxiedProperties.delete(prop)
}
return ReflectAdapter.set(target, prop, value, receiver)
},
ownKeys(target) {
warnForEnumeration(unproxiedProperties)
return Reflect.ownKeys(target)
......
This diff is collapsed.
......@@ -4,6 +4,7 @@ import { ReflectAdapter } from '../web/spec-extension/adapters/reflect'
import {
describeStringPropertyAccess,
describeHasCheckingStringProperty,
wellKnownProperties,
} from './utils'
export function createRenderSearchParamsFromClient(
......@@ -29,56 +30,61 @@ function makeUntrackedExoticSearchParamsWithDevWarnings(
return cachedSearchParams
}
const proxiedProperties = new Set<string>()
const unproxiedProperties: Array<string> = []
const promise = Promise.resolve(underlyingSearchParams)
Object.defineProperties(promise, {
status: {
value: 'fulfilled',
},
value: {
value: underlyingSearchParams,
},
})
Object.keys(underlyingSearchParams).forEach((prop) => {
if (Reflect.has(promise, prop)) {
// We can't assign a value over a property on the promise. The only way to
// access this is if you await the promise and recover the underlying searchParams object.
if (wellKnownProperties.has(prop)) {
// These properties cannot be shadowed because they need to be the
// true underlying value for Promises to work correctly at runtime
unproxiedProperties.push(prop)
} else {
Object.defineProperty(promise, prop, {
value: underlyingSearchParams[prop],
writable: false,
enumerable: true,
})
proxiedProperties.add(prop)
;(promise as any)[prop] = underlyingSearchParams[prop]
}
})
const proxiedPromise = new Proxy(promise, {
get(target, prop, receiver) {
if (Reflect.has(target, prop)) {
return ReflectAdapter.get(target, prop, receiver)
} else if (typeof prop === 'symbol') {
return undefined
} else {
const expression = describeStringPropertyAccess('searchParams', prop)
warnForSyncAccess(expression)
return underlyingSearchParams[prop]
if (typeof prop === 'string') {
if (
!wellKnownProperties.has(prop) &&
(proxiedProperties.has(prop) ||
// We are accessing a property that doesn't exist on the promise nor
// the underlying searchParams.
Reflect.has(target, prop) === false)
) {
const expression = describeStringPropertyAccess('searchParams', prop)
warnForSyncAccess(expression)
}
}
return ReflectAdapter.get(target, prop, receiver)
},
set(target, prop, value, receiver) {
if (typeof prop === 'string') {
proxiedProperties.delete(prop)
}
return Reflect.set(target, prop, value, receiver)
},
has(target, prop) {
if (Reflect.has(target, prop)) {
return true
} else if (typeof prop === 'symbol') {
// searchParams never has symbol properties containing searchParam data
// and we didn't match above so we just return false here.
return false
} else {
const expression = describeHasCheckingStringProperty(
'searchParams',
prop
)
warnForSyncAccess(expression)
return Reflect.has(underlyingSearchParams, prop)
if (typeof prop === 'string') {
if (
!wellKnownProperties.has(prop) &&
(proxiedProperties.has(prop) ||
// We are accessing a property that doesn't exist on the promise nor
// the underlying searchParams.
Reflect.has(target, prop) === false)
) {
const expression = describeHasCheckingStringProperty(
'searchParams',
prop
)
warnForSyncAccess(expression)
}
}
return Reflect.has(target, prop)
},
ownKeys(target) {
warnForSyncSpread()
......@@ -93,26 +99,23 @@ function makeUntrackedExoticSearchParamsWithDevWarnings(
function makeUntrackedExoticSearchParams(
underlyingSearchParams: SearchParams
): Promise<SearchParams> {
const cachedSearchParams = CachedSearchParams.get(underlyingSearchParams)
if (cachedSearchParams) {
return cachedSearchParams
}
// We don't use makeResolvedReactPromise here because searchParams
// supports copying with spread and we don't want to unnecessarily
// instrument the promise with spreadable properties of ReactPromise.
const promise = Promise.resolve(underlyingSearchParams)
Object.defineProperties(promise, {
status: {
value: 'fulfilled',
},
value: {
value: underlyingSearchParams,
},
})
CachedSearchParams.set(underlyingSearchParams, promise)
Object.keys(underlyingSearchParams).forEach((prop) => {
if (Reflect.has(promise, prop)) {
// We can't assign a value over a property on the promise. The only way to
// access this is if you await the promise and recover the underlying searchParams object.
if (wellKnownProperties.has(prop)) {
// These properties cannot be shadowed because they need to be the
// true underlying value for Promises to work correctly at runtime
} else {
Object.defineProperty(promise, prop, {
value: underlyingSearchParams[prop],
writable: false,
enumerable: true,
})
;(promise as any)[prop] = underlyingSearchParams[prop]
}
})
......
......@@ -639,11 +639,19 @@ function makeDynamicallyTrackedExoticSearchParamsWithDevWarnings(
},
has(target, prop) {
if (typeof prop === 'string') {
const expression = describeHasCheckingStringProperty(
'searchParams',
prop
)
warnForSyncAccess(store.route, expression)
if (
!wellKnownProperties.has(prop) &&
(proxiedProperties.has(prop) ||
// We are accessing a property that doesn't exist on the promise nor
// the underlying searchParams.
Reflect.has(target, prop) === false)
) {
const expression = describeHasCheckingStringProperty(
'searchParams',
prop
)
warnForSyncAccess(store.route, expression)
}
}
return Reflect.has(target, prop)
},
......
'use client'
export default function Page() {
return typeof window === 'undefined' ? 'server' : 'client'
}
'use client'
export default function Page() {
return 'normal'
}
......@@ -229,4 +229,40 @@ describe('dynamic-requests warnings', () => {
],
})
})
describe('no warnings', () => {
it('should have no warnings on normal rsc page without accessing params', async () => {
const browser = await next.browser('/no-access/normal')
const browserLogItems = await browser.log()
const browserConsoleErrors = browserLogItems
.filter((log) => log.source === 'error')
.map((log) => log.message)
expect(browserConsoleErrors.length).toBe(0)
})
it('should only have hydration warnings on hydration mismatch page without accessing params', async () => {
const browser = await next.browser('/no-access/mismatch')
const browserLogItems = await browser.log()
console.log('browserLogItems', browserLogItems)
const browserConsoleErrors = browserLogItems
.filter((log) => log.source === 'error')
.map((log) => log.message)
// We assert there are zero logged errors but first we assert specific strings b/c we want a better
// failure message if these do appear
expect(browserConsoleErrors).toEqual(
expect.not.arrayContaining([
expect.stringContaining('param property was accessed directly with'),
expect.stringContaining(
'searchParam property was accessed directly with'
),
])
)
// Even though there is a hydration error it does show up in the logs list b/c it is an
// uncaught Error not a console.error. We expect there to be no logged errors
expect(browserConsoleErrors.length).toBe(0)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment