Skip to content

Commit 2884831

Browse files
committed
fix(runtime-core): instance should not expose non-declared props
1 parent e43f593 commit 2884831

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

packages/runtime-core/__tests__/componentProxy.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ describe('component: proxy', () => {
7676
expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
7777
})
7878

79+
test('should not expose non-declared props', () => {
80+
let instanceProxy: any
81+
const Comp = {
82+
setup() {
83+
return () => null
84+
},
85+
mounted() {
86+
instanceProxy = this
87+
}
88+
}
89+
render(h(Comp, { count: 1 }), nodeOps.createElement('div'))
90+
expect('count' in instanceProxy).toBe(false)
91+
})
92+
7993
test('public properties', () => {
8094
let instance: ComponentInternalInstance
8195
let instanceProxy: any

packages/runtime-core/src/componentProxy.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ const publicPropertiesMap: Record<
6969
const enum AccessTypes {
7070
DATA,
7171
CONTEXT,
72-
PROPS
72+
PROPS,
73+
OTHER
7374
}
7475

7576
export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
@@ -104,20 +105,24 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
104105
return renderContext[key]
105106
case AccessTypes.PROPS:
106107
return propsProxy![key]
108+
// default: just fallthrough
107109
}
108110
} else if (data !== EMPTY_OBJ && hasOwn(data, key)) {
109111
accessCache![key] = AccessTypes.DATA
110112
return data[key]
111113
} else if (hasOwn(renderContext, key)) {
112114
accessCache![key] = AccessTypes.CONTEXT
113115
return renderContext[key]
114-
} else if (hasOwn(props, key)) {
115-
// only cache props access if component has declared (thus stable) props
116-
if (type.props != null) {
116+
} else if (type.props != null) {
117+
// only cache other properties when instance has declared (this stable)
118+
// props
119+
if (hasOwn(props, key)) {
117120
accessCache![key] = AccessTypes.PROPS
121+
// return the value from propsProxy for ref unwrapping and readonly
122+
return propsProxy![key]
123+
} else {
124+
accessCache![key] = AccessTypes.OTHER
118125
}
119-
// return the value from propsProxy for ref unwrapping and readonly
120-
return propsProxy![key]
121126
}
122127
}
123128

0 commit comments

Comments
 (0)