Skip to content

Commit

Permalink
fix(runtime-core): instance should not expose non-declared props
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 27, 2020
1 parent e43f593 commit 2884831
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 14 additions & 0 deletions packages/runtime-core/__tests__/componentProxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ describe('component: proxy', () => {
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', () => {
let instance: ComponentInternalInstance
let instanceProxy: any
Expand Down
17 changes: 11 additions & 6 deletions packages/runtime-core/src/componentProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ const publicPropertiesMap: Record<
const enum AccessTypes {
DATA,
CONTEXT,
PROPS
PROPS,
OTHER
}

export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
Expand Down Expand Up @@ -104,20 +105,24 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
return renderContext[key]
case AccessTypes.PROPS:
return propsProxy![key]
// default: just fallthrough
}
} else if (data !== EMPTY_OBJ && hasOwn(data, key)) {
accessCache![key] = AccessTypes.DATA
return data[key]
} else if (hasOwn(renderContext, key)) {
accessCache![key] = AccessTypes.CONTEXT
return renderContext[key]
} else if (hasOwn(props, key)) {
// only cache props access if component has declared (thus stable) props
if (type.props != null) {
} else if (type.props != null) {
// only cache other properties when instance has declared (this stable)
// props
if (hasOwn(props, key)) {
accessCache![key] = AccessTypes.PROPS
// return the value from propsProxy for ref unwrapping and readonly
return propsProxy![key]
} else {
accessCache![key] = AccessTypes.OTHER
}
// return the value from propsProxy for ref unwrapping and readonly
return propsProxy![key]
}
}

Expand Down

0 comments on commit 2884831

Please sign in to comment.