send-file
Ast Rule: function call
send-file
function visit(node, filename, code) {
if (!node.functionName || node.functionName.value !== "send_file") {
return;
}
const useLibraryAndFunction = node.context.imports.filter(i => {
return (i.astType === "fromstatement" && i.pkg.value === "flask" &&
i.elements &&
i.elements.filter(e => e.name && e.name.value === "send_file").length > 0);
}).length > 0;
if (!useLibraryAndFunction) {
return;
}
const hasArgument = (arguments, name) => {
return arguments.filter(a => a.name && a.name.value === name).length > 0;
};
if (node.arguments && node.arguments.values) {
const hasMimetype = hasArgument(node.arguments.values, "mimetype");
const hasAttachmentFilename = hasArgument(node.arguments.values, "attachment_filename");
if (!hasMimetype && !hasAttachmentFilename) {
const error = buildError(node.functionName.start.line, node.functionName.start.col,
node.functionName.end.line, node.functionName.end.col,
"function send_file needs to have mimetype or attachment_filename", "CRITICAL", "SECURITY");
const edit1 = buildEditAdd(node.end.line, node.end.col - 1, ', mimetype="text/html"');
const fix1 = buildFix(`add mimetype argument`, [edit1]);
const edit2 = buildEditAdd(node.end.line, node.end.col - 1, ', attachment_filename="myfile.ext"');
const fix2 = buildFix(`add attachment_filename argument`, [edit2]);
addError(error.addFix(fix1).addFix(fix2));
}
}
}
correct.py
Expected test result: no error
missing.py
Expected test result: no error