Repository navigation
Expand file tree
/
Copy pathPythonAnalyzerToolImplementation.cpp
More file actions
155 lines (143 loc) · 5.85 KB
/
Copy pathPythonAnalyzerToolImplementation.cpp
File metadata and controls
155 lines (143 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SPDX-License-Identifier: Apache-2.0
#include "Process/Tools/AnalysisTools.hpp"
#include "Process/Tools/InputPaths.hpp"
#include "Process/Tools/Sarif.hpp"
#include <coretrace/logger.hpp>
#include <array>
#include <map>
namespace ctrace
{
namespace
{
namespace fs = std::filesystem;
constexpr const char* kToolName = "coretrace-python-analyzer";
/// The analyzer's exit codes that mean it analyzed the project: 0 clean, 1 findings.
/// 2 is an analysis error and is reported as a failed run.
constexpr std::initializer_list<int> kCompletedExitCodes = {0, 1};
constexpr std::array<const char*, 4> kProjectMarkers = {"pyproject.toml", "setup.py",
"setup.cfg", ".git"};
} // namespace
std::vector<std::string>
PythonAnalyzerToolImplementation::buildArguments(const ctrace::ProgramConfig& config,
const std::string& projectRoot)
{
// Always SARIF: CoreTrace renders the text itself and merges SARIF across tools.
std::vector<std::string> args = {"--check", "--format", "sarif"};
appendToolArguments(args, config, kToolName);
args.push_back(projectRoot);
return args;
}
fs::path PythonAnalyzerToolImplementation::projectRoot(const std::string& file)
{
const fs::path directory = InputPaths::identityOf(file).parent_path();
for (fs::path candidate = directory; !candidate.empty();
candidate = candidate.parent_path())
{
for (const char* marker : kProjectMarkers)
{
std::error_code err;
if (fs::exists(candidate / marker, err))
{
return candidate;
}
}
if (candidate == candidate.root_path())
{
break;
}
}
return directory;
}
std::optional<std::vector<Diagnostic>> PythonAnalyzerToolImplementation::parseDiagnostics(
const std::string& output, const fs::path& root, const std::vector<std::string>& inputs)
{
auto diagnostics = diagnosticsFromSarifText(output, kToolName);
if (!diagnostics)
{
return diagnostics;
}
const InputPaths paths(inputs);
// The analyzer locates findings, and the places they involve, relative to the project
// root it was given.
const auto locate = [&root](const std::string& file)
{ return fs::path(file).is_relative() ? root / file : fs::path(file); };
std::vector<Diagnostic> kept;
for (Diagnostic& diagnostic : *diagnostics)
{
if (diagnostic.file.empty())
{
continue;
}
for (RelatedLocation& related : diagnostic.relatedLocations)
{
if (!related.file.empty())
{
related.file = paths.display(locate(related.file));
}
}
const fs::path located = locate(diagnostic.file);
if (auto input = paths.asInput(located))
{
diagnostic.file = *input;
kept.push_back(std::move(diagnostic));
}
else if (located.extension() != ".py")
{
// Not in a Python source: a finding about the project itself, such as a
// vulnerable pin in requirements.txt. It concerns every input of the project.
diagnostic.file = paths.display(located);
kept.push_back(std::move(diagnostic));
}
}
return kept;
}
void PythonAnalyzerToolImplementation::executeBatch(const std::vector<std::string>& files,
const ctrace::ProgramConfig& config,
ToolOutput& output) const
{
std::map<fs::path, std::vector<std::string>> inputsByProject;
for (const std::string& file : files)
{
inputsByProject[projectRoot(file)].push_back(file);
}
for (const auto& [root, inputs] : inputsByProject)
{
coretrace::log(coretrace::Level::Info, "Running {} on {} for {} input file(s)\n",
kToolName, root.string(), inputs.size());
const auto run = runExternalTool(config, *this, buildArguments(config, root.string()),
output, kCompletedExitCodes);
if (!run)
{
continue;
}
const auto diagnostics = parseDiagnostics(run->output, root, inputs);
if (!diagnostics)
{
// The analyzer always reports in SARIF when it ran. Without a report, its exit
// code means nothing: a system that cannot load the executable exits 1, which
// would otherwise read as "findings".
output.error(name() + " did not produce a SARIF report (exit code " +
std::to_string(run->exitCode) + "); the analysis is incomplete");
if (config.output.sarif_format)
{
output.record("stdout", run->output);
}
else
{
output.result(run->output);
}
continue;
}
// In SARIF mode the merged document is the output; text lines would pollute it.
if (!config.output.sarif_format && !diagnostics->empty())
{
output.result(renderLines(*diagnostics));
}
output.diagnostics(*diagnostics);
}
}
std::string PythonAnalyzerToolImplementation::name() const
{
return kToolName;
}
} // namespace ctrace