Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): prevent self-injection (#2400) #2424

Merged
merged 1 commit into from
Oct 20, 2020
Merged
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
15 changes: 15 additions & 0 deletions packages/runtime-core/__tests__/apiInject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,19 @@ describe('api: provide/inject', () => {
render(h(Provider), root)
expect(`injection "foo" not found.`).not.toHaveBeenWarned()
})

// #2400
it('should not self-inject', () => {
const Comp = {
setup() {
provide('foo', 'foo')
const injection = inject('foo', null)
return () => injection
}
}

const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(serialize(root)).toBe(`<div><!----></div>`)
})
})
11 changes: 9 additions & 2 deletions packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ export function inject(
// a functional component
const instance = currentInstance || currentRenderingInstance
if (instance) {
const provides = instance.provides
if ((key as string | symbol) in provides) {
// #2400
// to support `app.use` plugins,
// fallback to appContext's `provides` if the intance is at root
const provides =
instance.parent == null
? instance.vnode.appContext && instance.vnode.appContext.provides
: instance.parent.provides

if (provides && (key as string | symbol) in provides) {
// TS doesn't allow symbol as index type
return provides[key as string]
} else if (arguments.length > 1) {
Expand Down