clear-credentials
Ast Rule: function call
clear-credentials
function visit(node, filename, code) {
function isString(value) {
if (/^".+"$/.test(value)) {
return true;
}
if (/^'.+'$/.test(value)) {
return true;
}
return false;
}
if((node.arguments && node.arguments.values.length === 1) &&
(node.functionName.astType === "functioncall") &&
(node.functionName.functionName.astType === "string") &&
(node.functionName.functionName.value === "require") &&
(node.functionName.arguments) &&
(node.functionName.arguments.values.length === 1) &&
(node.functionName.arguments.values[0].value) &&
(node.functionName.arguments.values[0].value.astType === "string") &&
(node.functionName.arguments.values[0].value.value === "'knex'")
) {
const firstArgument = node.arguments.values[0].value;
if(firstArgument.astType !== "object") {
return;
}
const connections = firstArgument.elements.filter(e => e.name && e.name.value && e.name.value === "connection");
if(connections.length == 0){
return;
}
const connection = connections[0].value;
if(connection.astType === "object") {
const passwords = connection.elements.filter(e => e.name && e.name.value && e.name.value === "password");
if(passwords.length == 0) {
return;
}
const password = passwords[0].value;
if (password.value && isString(password.value)) {
const error = buildError(password.start.line,
password.start.col,
password.end.line,
password.end.col,
"use of hardcoded credentials", "WARNING", "SECURITY");
addError(error);
}
}
if(connection.astType === "string") {
if(isString(connection.value)) {
const error = buildError(connection.start.line,
connection.start.col,
connection.end.line,
connection.end.col,
"use of hardcoded credentials", "WARNING", "SECURITY");
addError(error);
}
}
}
}
connection-string-error.js
Expected test result: has error
connection-string.js
Expected test result: no error
password-variable.js
Expected test result: no error
credentials-in-variable.js
Expected test result: has error
clear-credentials.js
Expected test result: has error