2022-06-11 13:45:31 +00:00
|
|
|
'use strict';
|
|
|
|
|
2022-06-17 17:12:42 +00:00
|
|
|
const CallExpression = (fnName) => `CallExpression[callee.name=${fnName}]`;
|
2022-10-21 03:12:03 +00:00
|
|
|
const AnyFunction = 'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression';
|
2022-06-17 17:12:42 +00:00
|
|
|
|
2022-06-11 13:45:31 +00:00
|
|
|
function checkProperties(context, node) {
|
|
|
|
if (
|
|
|
|
node.type === 'CallExpression' &&
|
|
|
|
node.callee.name === 'ObjectGetOwnPropertyDescriptors'
|
|
|
|
) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message:
|
|
|
|
'Property descriptors inherits from the Object prototype, therefore are subject to prototype pollution',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (node.type !== 'ObjectExpression') return;
|
|
|
|
for (const { key, value } of node.properties) {
|
|
|
|
if (
|
|
|
|
key != null && value != null &&
|
|
|
|
!(key.type === 'Identifier' && key.name === '__proto__') &&
|
|
|
|
!(key.type === 'Literal' && key.value === '__proto__')
|
|
|
|
) {
|
|
|
|
checkPropertyDescriptor(context, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 03:12:03 +00:00
|
|
|
function isNullPrototypeObjectExpression(node) {
|
|
|
|
if (node.type !== 'ObjectExpression') return;
|
|
|
|
|
|
|
|
for (const { key, value } of node.properties) {
|
|
|
|
if (
|
|
|
|
key != null && value != null &&
|
|
|
|
((key.type === 'Identifier' && key.name === '__proto__') ||
|
|
|
|
(key.type === 'Literal' && key.value === '__proto__')) &&
|
|
|
|
value.type === 'Literal' && value.value === null
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-11 13:45:31 +00:00
|
|
|
function checkPropertyDescriptor(context, node) {
|
|
|
|
if (
|
|
|
|
node.type === 'CallExpression' &&
|
|
|
|
(node.callee.name === 'ObjectGetOwnPropertyDescriptor' ||
|
|
|
|
node.callee.name === 'ReflectGetOwnPropertyDescriptor')
|
|
|
|
) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message:
|
|
|
|
'Property descriptors inherits from the Object prototype, therefore are subject to prototype pollution',
|
|
|
|
suggest: [{
|
|
|
|
desc: 'Wrap the property descriptor in a null-prototype object',
|
|
|
|
fix(fixer) {
|
|
|
|
return [
|
|
|
|
fixer.insertTextBefore(node, '{ __proto__: null,...'),
|
|
|
|
fixer.insertTextAfter(node, ' }'),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
}
|
2022-10-21 03:12:03 +00:00
|
|
|
if (isNullPrototypeObjectExpression(node) === false) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message: 'Must use null-prototype object for property descriptors',
|
|
|
|
});
|
2022-06-11 13:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 22:21:57 +00:00
|
|
|
function createUnsafeStringMethodReport(context, name, lookedUpProperty) {
|
2022-06-17 17:12:42 +00:00
|
|
|
const lastDotPosition = '$String.prototype.'.length;
|
|
|
|
const unsafePrimordialName = `StringPrototype${name.charAt(lastDotPosition).toUpperCase()}${name.slice(lastDotPosition + 1, -1)}`;
|
2022-06-15 22:21:57 +00:00
|
|
|
return {
|
2022-06-17 17:12:42 +00:00
|
|
|
[CallExpression(unsafePrimordialName)](node) {
|
2022-06-15 22:21:57 +00:00
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message: `${name} looks up the ${lookedUpProperty} property on the first argument`,
|
|
|
|
});
|
2022-12-18 16:39:39 +00:00
|
|
|
},
|
2022-06-15 22:21:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-17 17:12:42 +00:00
|
|
|
function createUnsafeStringMethodOnRegexReport(context, name, lookedUpProperty) {
|
|
|
|
const dotPosition = 'Symbol.'.length;
|
|
|
|
const safePrimordialName = `RegExpPrototypeSymbol${lookedUpProperty.charAt(dotPosition).toUpperCase()}${lookedUpProperty.slice(dotPosition + 1)}`;
|
|
|
|
const lastDotPosition = '$String.prototype.'.length;
|
|
|
|
const unsafePrimordialName = `StringPrototype${name.charAt(lastDotPosition).toUpperCase()}${name.slice(lastDotPosition + 1, -1)}`;
|
|
|
|
return {
|
|
|
|
[[
|
|
|
|
`${CallExpression(unsafePrimordialName)}[arguments.1.type=Literal][arguments.1.regex]`,
|
|
|
|
`${CallExpression(unsafePrimordialName)}[arguments.1.type=NewExpression][arguments.1.callee.name=RegExp]`,
|
|
|
|
].join(',')](node) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message: `${name} looks up the ${lookedUpProperty} property of the passed regex, use ${safePrimordialName} directly`,
|
|
|
|
});
|
2022-12-18 16:39:39 +00:00
|
|
|
},
|
2022-06-17 17:12:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-11 13:45:31 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: { hasSuggestions: true },
|
|
|
|
create(context) {
|
|
|
|
return {
|
2022-06-17 17:12:42 +00:00
|
|
|
[CallExpression(/^(Object|Reflect)DefinePropert(ies|y)$/)](node) {
|
|
|
|
switch (node.callee.name) {
|
2022-06-11 13:45:31 +00:00
|
|
|
case 'ObjectDefineProperties':
|
2022-06-17 17:12:42 +00:00
|
|
|
checkProperties(context, node.arguments[1]);
|
2022-06-11 13:45:31 +00:00
|
|
|
break;
|
|
|
|
case 'ReflectDefineProperty':
|
|
|
|
case 'ObjectDefineProperty':
|
2022-06-17 17:12:42 +00:00
|
|
|
checkPropertyDescriptor(context, node.arguments[2]);
|
2022-06-11 13:45:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Unreachable');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-06-17 17:12:42 +00:00
|
|
|
[`${CallExpression('ObjectCreate')}[arguments.length=2]`](node) {
|
|
|
|
checkProperties(context, node.arguments[1]);
|
2022-06-11 13:45:31 +00:00
|
|
|
},
|
2022-10-21 03:12:03 +00:00
|
|
|
|
|
|
|
[`:matches(${AnyFunction})[async=true]>BlockStatement ReturnStatement>ObjectExpression, :matches(${AnyFunction})[async=true]>ObjectExpression`](node) {
|
|
|
|
if (node.parent.type === 'ReturnStatement') {
|
|
|
|
let { parent } = node;
|
|
|
|
do {
|
|
|
|
({ parent } = parent);
|
|
|
|
} while (!parent.type.includes('Function'));
|
|
|
|
|
|
|
|
if (!parent.async) return;
|
|
|
|
}
|
|
|
|
if (isNullPrototypeObjectExpression(node) === false) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message: 'Use null-prototype when returning from async function',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-06-17 17:12:42 +00:00
|
|
|
[CallExpression('RegExpPrototypeTest')](node) {
|
2022-06-15 22:21:57 +00:00
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message: '%RegExp.prototype.test% looks up the "exec" property of `this` value',
|
|
|
|
suggest: [{
|
|
|
|
desc: 'Use RegexpPrototypeExec instead',
|
|
|
|
fix(fixer) {
|
2024-05-23 19:45:18 +00:00
|
|
|
const testRange = [ ...node.range ];
|
|
|
|
testRange[0] = testRange[0] + 'RegexpPrototype'.length;
|
|
|
|
testRange[1] = testRange[0] + 'Test'.length;
|
2022-06-15 22:21:57 +00:00
|
|
|
return [
|
2022-07-26 22:38:24 +00:00
|
|
|
fixer.replaceTextRange(testRange, 'Exec'),
|
2022-06-15 22:21:57 +00:00
|
|
|
fixer.insertTextAfter(node, ' !== null'),
|
|
|
|
];
|
2022-12-18 16:39:39 +00:00
|
|
|
},
|
2022-07-26 22:38:24 +00:00
|
|
|
}],
|
2022-06-15 22:21:57 +00:00
|
|
|
});
|
|
|
|
},
|
2022-10-19 02:26:16 +00:00
|
|
|
[CallExpression(/^RegExpPrototypeSymbol(Match|MatchAll)$/)](node) {
|
2022-06-15 22:21:57 +00:00
|
|
|
context.report({
|
|
|
|
node,
|
2022-06-17 17:12:42 +00:00
|
|
|
message: node.callee.name + ' looks up the "exec" property of `this` value',
|
2022-06-15 22:21:57 +00:00
|
|
|
});
|
|
|
|
},
|
2022-10-19 02:26:16 +00:00
|
|
|
[CallExpression(/^(RegExpPrototypeSymbol|StringPrototype)Search$/)](node) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message: node.callee.name + ' is unsafe, use SafeStringPrototypeSearch instead',
|
|
|
|
});
|
|
|
|
},
|
2022-06-17 17:12:42 +00:00
|
|
|
...createUnsafeStringMethodReport(context, '%String.prototype.match%', 'Symbol.match'),
|
|
|
|
...createUnsafeStringMethodReport(context, '%String.prototype.matchAll%', 'Symbol.matchAll'),
|
|
|
|
...createUnsafeStringMethodOnRegexReport(context, '%String.prototype.replace%', 'Symbol.replace'),
|
|
|
|
...createUnsafeStringMethodOnRegexReport(context, '%String.prototype.replaceAll%', 'Symbol.replace'),
|
|
|
|
...createUnsafeStringMethodOnRegexReport(context, '%String.prototype.split%', 'Symbol.split'),
|
2022-06-12 11:52:43 +00:00
|
|
|
|
|
|
|
'NewExpression[callee.name="Proxy"][arguments.1.type="ObjectExpression"]'(node) {
|
|
|
|
for (const { key, value } of node.arguments[1].properties) {
|
|
|
|
if (
|
|
|
|
key != null && value != null &&
|
|
|
|
((key.type === 'Identifier' && key.name === '__proto__') ||
|
|
|
|
(key.type === 'Literal' && key.value === '__proto__')) &&
|
|
|
|
value.type === 'Literal' && value.value === null
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.report({
|
|
|
|
node,
|
2022-07-26 22:38:24 +00:00
|
|
|
message: 'Proxy handler must be a null-prototype object',
|
2022-06-12 11:52:43 +00:00
|
|
|
});
|
2022-07-26 22:38:24 +00:00
|
|
|
},
|
|
|
|
|
2022-10-27 20:09:07 +00:00
|
|
|
[`ExpressionStatement>AwaitExpression>${CallExpression(/^(Safe)?PromiseAll(Settled)?$/)}`](node) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message: `Use ${node.callee.name}ReturnVoid`,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-06-17 17:12:42 +00:00
|
|
|
[CallExpression('PromisePrototypeCatch')](node) {
|
2022-07-26 22:38:24 +00:00
|
|
|
context.report({
|
|
|
|
node,
|
2022-08-30 12:18:29 +00:00
|
|
|
message: '%Promise.prototype.catch% looks up the `then` property of ' +
|
2022-07-26 22:38:24 +00:00
|
|
|
'the `this` argument, use PromisePrototypeThen instead',
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-06-17 17:12:42 +00:00
|
|
|
[CallExpression('PromisePrototypeFinally')](node) {
|
2022-07-26 22:38:24 +00:00
|
|
|
context.report({
|
|
|
|
node,
|
2022-08-30 12:18:29 +00:00
|
|
|
message: '%Promise.prototype.finally% looks up the `then` property of ' +
|
2022-07-26 22:38:24 +00:00
|
|
|
'the `this` argument, use SafePromisePrototypeFinally or ' +
|
|
|
|
'try/finally instead',
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-06-17 17:12:42 +00:00
|
|
|
[CallExpression(/^Promise(All(Settled)?|Any|Race)/)](node) {
|
2022-07-26 22:38:24 +00:00
|
|
|
context.report({
|
|
|
|
node,
|
2022-06-17 17:12:42 +00:00
|
|
|
message: `Use Safe${node.callee.name} instead of ${node.callee.name}`,
|
2022-07-26 22:38:24 +00:00
|
|
|
});
|
|
|
|
},
|
2022-12-17 19:15:24 +00:00
|
|
|
|
|
|
|
[CallExpression('ArrayPrototypeConcat')](node) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message: '%Array.prototype.concat% looks up `@@isConcatSpreadable` ' +
|
|
|
|
'which can be subject to prototype pollution',
|
|
|
|
});
|
|
|
|
},
|
2022-06-11 13:45:31 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|