detect-non-literal-require
Ast Rule: assignment
detect-non-literal-require
function visit(node, filename, code) {
if (node?.right?.functionName?.value === "require") {
if (node.right?.arguments?.values?.length) {
const argument = node.right?.arguments?.values[0];
if (
argument.value.astType === "string" &&
(
argument.value.value.startsWith(`\"`) &&
argument.value.value.endsWith(`"`) ||
argument.value.value.startsWith(`'`) &&
argument.value.value.endsWith(`'`)
)
) {
return;
}
addError(buildError(
argument.value.start.line,
argument.value.start.col,
argument.value.end.line,
argument.value.end.col,
`Found non-literal argument in require`,
"WARNING",
"SECURITY",
));
}
}
}
good.js
Expected test result: no error
bad.js
Expected test result: no error