-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add no-prototype-builtins and use NAME_REF for Expr
- Loading branch information
1 parent
3e9d238
commit 76093da
Showing
62 changed files
with
481 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<!-- | ||
generated docs file, do not edit by hand, see xtask/docgen | ||
--> | ||
# no-prototype-builtins | ||
|
||
Disallow direct use of `Object.prototype` builtins directly. | ||
|
||
ES 5.1 added `Object.create` which allows creation of object with a custom prototype. This | ||
pattern is frequently used for objects used as Maps. However this pattern can lead to errors | ||
if something else relies on prototype properties/methods. | ||
|
||
Moreover, the methods could be shadowed, this can lead to random bugs and denial of service | ||
vulnerabilities. For example, calling `hasOwnProperty` directly on parsed json could lead to vulnerabilities. | ||
Instead, you should use get the method directly from the object using `Object.prototype.prop.call(item, args)`. | ||
|
||
## Invalid Code Examples | ||
|
||
```js | ||
var bar = foo.hasOwnProperty("bar"); | ||
|
||
var bar = foo.isPrototypeOf(bar); | ||
|
||
var bar = foo.propertyIsEnumerable("bar"); | ||
``` | ||
|
||
## Correct Code Examples | ||
|
||
```js | ||
var bar = Object.prototype.hasOwnProperty.call(foo, "bar"); | ||
|
||
var bar = Object.prototype.isPrototypeOf.call(foo, bar); | ||
|
||
var bar = Object.propertyIsEnumerable.call(foo, "bar"); | ||
``` | ||
|
||
<details> | ||
<summary> More incorrect examples </summary> | ||
|
||
```js | ||
foo.hasOwnProperty("bar"); | ||
``` | ||
|
||
```js | ||
foo.isPrototypeOf("bar"); | ||
``` | ||
|
||
```js | ||
foo.propertyIsEnumberable("bar"); | ||
``` | ||
|
||
```js | ||
foo.bar.baz.hasOwnProperty("bar"); | ||
``` | ||
</details><br> | ||
<details> | ||
<summary> More correct examples </summary> | ||
|
||
```js | ||
Object.prototype.hasOwnProperty.call(foo, 'bar'); | ||
``` | ||
|
||
```js | ||
Object.prototype.isPrototypeOf.call(foo, 'bar'); | ||
``` | ||
|
||
```js | ||
Object.prototype.propertyIsEnumberable.call(foo, 'bar'); | ||
``` | ||
|
||
```js | ||
Object.prototype.hasOwnProperty.apply(foo, ['bar']); | ||
``` | ||
|
||
```js | ||
Object.prototype.isPrototypeOf.apply(foo, ['bar']); | ||
``` | ||
|
||
```js | ||
Object.prototype.propertyIsEnumberable.apply(foo, ['bar']); | ||
``` | ||
|
||
```js | ||
hasOwnProperty(foo, 'bar'); | ||
``` | ||
|
||
```js | ||
isPrototypeOf(foo, 'bar'); | ||
``` | ||
|
||
```js | ||
propertyIsEnumberable(foo, 'bar'); | ||
``` | ||
|
||
```js | ||
({}.hasOwnProperty.call(foo, 'bar')); | ||
``` | ||
|
||
```js | ||
({}.isPrototypeOf.call(foo, 'bar')); | ||
``` | ||
|
||
```js | ||
({}.propertyIsEnumberable.call(foo, 'bar')); | ||
``` | ||
|
||
```js | ||
({}.hasOwnProperty.apply(foo, ['bar'])); | ||
``` | ||
|
||
```js | ||
({}.isPrototypeOf.apply(foo, ['bar'])); | ||
``` | ||
|
||
```js | ||
({}.propertyIsEnumberable.apply(foo, ['bar'])); | ||
``` | ||
</details> | ||
|
||
[Source](../../../rslint_core/src/groups/errors/no_prototype_builtins.rs) |
Oops, something went wrong.