fix(runtime-core): instance should not expose non-declared props
This commit is contained in:
parent
e43f5935b5
commit
2884831065
@ -76,6 +76,20 @@ describe('component: proxy', () => {
|
|||||||
expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
|
expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('should not expose non-declared props', () => {
|
||||||
|
let instanceProxy: any
|
||||||
|
const Comp = {
|
||||||
|
setup() {
|
||||||
|
return () => null
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
instanceProxy = this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
render(h(Comp, { count: 1 }), nodeOps.createElement('div'))
|
||||||
|
expect('count' in instanceProxy).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
test('public properties', () => {
|
test('public properties', () => {
|
||||||
let instance: ComponentInternalInstance
|
let instance: ComponentInternalInstance
|
||||||
let instanceProxy: any
|
let instanceProxy: any
|
||||||
|
@ -69,7 +69,8 @@ const publicPropertiesMap: Record<
|
|||||||
const enum AccessTypes {
|
const enum AccessTypes {
|
||||||
DATA,
|
DATA,
|
||||||
CONTEXT,
|
CONTEXT,
|
||||||
PROPS
|
PROPS,
|
||||||
|
OTHER
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
||||||
@ -104,6 +105,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
|||||||
return renderContext[key]
|
return renderContext[key]
|
||||||
case AccessTypes.PROPS:
|
case AccessTypes.PROPS:
|
||||||
return propsProxy![key]
|
return propsProxy![key]
|
||||||
|
// default: just fallthrough
|
||||||
}
|
}
|
||||||
} else if (data !== EMPTY_OBJ && hasOwn(data, key)) {
|
} else if (data !== EMPTY_OBJ && hasOwn(data, key)) {
|
||||||
accessCache![key] = AccessTypes.DATA
|
accessCache![key] = AccessTypes.DATA
|
||||||
@ -111,13 +113,16 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
|||||||
} else if (hasOwn(renderContext, key)) {
|
} else if (hasOwn(renderContext, key)) {
|
||||||
accessCache![key] = AccessTypes.CONTEXT
|
accessCache![key] = AccessTypes.CONTEXT
|
||||||
return renderContext[key]
|
return renderContext[key]
|
||||||
} else if (hasOwn(props, key)) {
|
} else if (type.props != null) {
|
||||||
// only cache props access if component has declared (thus stable) props
|
// only cache other properties when instance has declared (this stable)
|
||||||
if (type.props != null) {
|
// props
|
||||||
|
if (hasOwn(props, key)) {
|
||||||
accessCache![key] = AccessTypes.PROPS
|
accessCache![key] = AccessTypes.PROPS
|
||||||
}
|
|
||||||
// return the value from propsProxy for ref unwrapping and readonly
|
// return the value from propsProxy for ref unwrapping and readonly
|
||||||
return propsProxy![key]
|
return propsProxy![key]
|
||||||
|
} else {
|
||||||
|
accessCache![key] = AccessTypes.OTHER
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user