Skip to content

Commit 733c1be

Browse files
privatenumberyyx990803
authored andcommitted
fix(inject): use hasOwn instead of 'in' for provideKey check (#7460)
fix #7284 * fix(Injection with Symbol polyfill): hasOwn instead of 'in' Symbol polyfill adds a setter on the Object prototype so the 'in' check evaluated to true on every object * test(Injected properties): Ensures prototype properties aren't injected Prototype properties were being injected, so injecting 'constructor' would have hit the first provide-layer and not yield expected results.
1 parent e98d12d commit 733c1be

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/core/instance/inject.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { warn } from '../util/index'
44
import { hasSymbol } from 'core/util/env'
55
import { defineReactive, observerState } from '../observer/index'
6+
import { hasOwn } from 'shared/util'
67

78
export function initProvide (vm: Component) {
89
const provide = vm.$options.provide
@@ -52,7 +53,7 @@ export function resolveInject (inject: any, vm: Component): ?Object {
5253
const provideKey = inject[key].from
5354
let source = vm
5455
while (source) {
55-
if (source._provided && provideKey in source._provided) {
56+
if (source._provided && hasOwn(source._provided, provideKey)) {
5657
result[key] = source._provided[provideKey]
5758
break
5859
}

test/unit/features/options/inject.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -635,4 +635,16 @@ describe('Options provide/inject', () => {
635635

636636
expect(injected).toEqual('foo')
637637
})
638+
639+
// #7284
640+
it('should not inject prototype properties', () => {
641+
const vm = new Vue({
642+
provide: {}
643+
})
644+
new Vue({
645+
parent: vm,
646+
inject: ['constructor']
647+
})
648+
expect(`Injection "constructor" not found`).toHaveBeenWarned()
649+
})
638650
})

0 commit comments

Comments
 (0)