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

tools: add eslint rule for __proto__: null in object #48646

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ module.exports = {
'node-core/no-unescaped-regexp-dot': 'error',
'node-core/no-duplicate-requires': 'error',
'node-core/prefer-proto': 'error',
'node-core/set-proto-to-null-in-object': 'error',
},
globals: {
ByteLengthQueuingStrategy: 'readable',
Expand Down
31 changes: 31 additions & 0 deletions tools/eslint-rules/set-proto-to-null-in-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

module.exports = {
meta: {
messages: {
error: 'Add `__proto__: null` to object',
},
},
create: function(context) {
return {
ObjectExpression(node) {
const properties = node.properties;
let hasProto = false;

for (const property of properties) {
if (property.key && property.key.name === '__proto__') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we verify that the value is null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, and I'm gonna make it fixable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasProto = true;
break;
}
}

if (!hasProto) {
context.report({
node,
message: 'Every object must have __proto__: null',
});
}
},
};
},
};