Skip to content

Commit 84129bb

Browse files
authored
Merge pull request #11 from OPPIDA/fix/various
2 parents 662baa8 + 1660284 commit 84129bb

18 files changed

Lines changed: 65 additions & 63 deletions

File tree

codesectools/cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ def get_downloadable() -> dict[str, DownloadableRequirement | Dataset]:
160160

161161

162162
@cli.command(hidden=download_hidden)
163-
def download(name: download_arg_type = download_arg_value, test: bool = False) -> None:
163+
def download(
164+
name: download_arg_type = download_arg_value,
165+
test: Annotated[bool, typer.Option(hidden=True)] = False,
166+
) -> None:
164167
"""Download and install any missing resources that are available for download."""
165168
if name is None:
166169
print("All downloadable resources have been retrieved.")

codesectools/datasets/BenchmarkJava/dataset.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@ class TestCode(File):
2222
Inherits from the base `File` class and adds a `vuln_type` attribute
2323
specific to this dataset.
2424
25-
Attributes:
26-
vuln_type (str): The type of vulnerability present in the file.
27-
2825
"""
2926

3027
def __init__(
3128
self,
3229
filepath: Path,
3330
content: str | bytes,
3431
cwes: list[CWE],
35-
vuln_type: str,
3632
has_vuln: bool,
3733
) -> None:
3834
"""Initialize a TestCode instance.
@@ -41,16 +37,13 @@ def __init__(
4137
filepath: The path to the file.
4238
content: The content of the file, as a string or bytes.
4339
cwes: A list of CWEs associated with the file.
44-
vuln_type: The type of vulnerability.
4540
has_vuln: A boolean indicating if the vulnerability is real or a false positive test case.
4641
4742
"""
4843
super().__init__(
4944
filepath=filepath, content=content, cwes=cwes, has_vuln=has_vuln
5045
)
5146

52-
self.vuln_type = vuln_type
53-
5447

5548
class BenchmarkJava(PrebuiltFileDataset):
5649
"""Represents the BenchmarkJava dataset.
@@ -151,14 +144,12 @@ def load_dataset(self) -> list[TestCode]:
151144
filepath = testcode_dir / filename
152145
content = filepath.read_text()
153146
cwes = [CWEs.from_id(int(row[3]))]
154-
vuln_type = row[1]
155147
has_vuln = True if row[2] == "true" else False
156148
files.append(
157149
TestCode(
158150
filepath.relative_to(self.directory),
159151
content,
160152
cwes,
161-
vuln_type,
162153
has_vuln,
163154
)
164155
)

codesectools/sasts/core/sast/__init__.py

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
USER_OUTPUT_DIR,
3636
MissingFile,
3737
NonZeroExit,
38+
render_command,
3839
run_command,
3940
)
4041

@@ -52,7 +53,7 @@ class SAST(ABC):
5253
supported_datasets (list[Dataset]): A list of supported dataset classes.
5354
properties (SASTProperties): The properties of the SAST tool.
5455
requirements (SASTRequirements): The requirements for the SAST tool.
55-
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendred and executed.
56+
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendered and executed.
5657
valid_codes (list[int]): A list of exit codes indicating that the command did not fail.
5758
environ (dict[str, str]): Environment variables to set for commands.
5859
output_files (list[tuple[Path, bool]]): Expected output files and
@@ -89,46 +90,15 @@ def __init__(self) -> None:
8990
Set up supported datasets, the output directory, and requirement status.
9091
"""
9192
self.supported_datasets = [
92-
DATASETS_ALL[d] for d in self.supported_dataset_names
93+
DATASETS_ALL[d]
94+
for d in self.supported_dataset_names
95+
if DATASETS_ALL[d].is_cached()
9396
]
9497
self.output_dir = USER_OUTPUT_DIR / self.name
9598
self.requirements.name = self.name
9699
self.status = self.requirements.get_status()
97100
self.missing = self.requirements.get_missing()
98101

99-
def render_command(self, command: list[str], map: dict[str, str]) -> list[str]:
100-
"""Render a command template by replacing placeholders with values.
101-
102-
Args:
103-
command: The command template as a list of strings.
104-
map: A dictionary of placeholders to their replacement values.
105-
106-
Returns:
107-
The rendered command as a list of strings.
108-
109-
"""
110-
_command = command.copy()
111-
for pattern, value in map.items():
112-
for i, arg in enumerate(_command):
113-
# Check if optional argument can be used
114-
if isinstance(arg, tuple):
115-
default_arg, optional_arg = arg
116-
if pattern in optional_arg:
117-
_command[i] = arg.replace(pattern, value)
118-
else:
119-
_command[i] = default_arg
120-
else:
121-
if pattern in arg:
122-
_command[i] = arg.replace(pattern, value)
123-
124-
# Remove not rendered part of the command:
125-
__command = []
126-
for part in _command:
127-
if not ("{" in part and "}" in part):
128-
__command.append(part)
129-
130-
return __command
131-
132102
def run_analysis(
133103
self, lang: str, project_dir: Path, output_dir: Path, **kwargs: Any
134104
) -> None:
@@ -165,7 +135,7 @@ def run_analysis(
165135
command_output = ""
166136
start = time.time()
167137
for command in self.commands:
168-
rendered_command = self.render_command(command, render_variables)
138+
rendered_command = render_command(command, render_variables)
169139
retcode, out = run_command(rendered_command, project_dir, self.environ)
170140
command_output += out
171141
if retcode not in self.valid_codes:

codesectools/sasts/tools/Bearer/sast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BearerSAST(BuildlessSAST):
2626
supported_dataset_names (list[str]): A list of names of compatible datasets.
2727
properties (SASTProperties): The properties of the SAST tool.
2828
requirements (SASTRequirements): The requirements for the SAST tool.
29-
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendred and executed.
29+
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendered and executed.
3030
valid_codes (list[int]): A list of exit codes indicating that the command did not fail.
3131
output_files (list[tuple[Path, bool]]): A list of expected output files and
3232
whether they are required.

codesectools/sasts/tools/Coverity/sast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CoveritySAST(BuildlessSAST):
2525
supported_dataset_names (list[str]): A list of names of compatible datasets.
2626
properties (SASTProperties): The properties of the SAST tool.
2727
requirements (SASTRequirements): The requirements for the SAST tool.
28-
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendred and executed.
28+
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendered and executed.
2929
valid_codes (list[int]): A list of exit codes indicating that the command did not fail.
3030
output_files (list[tuple[Path, bool]]): A list of expected output files and
3131
whether they are required.

codesectools/sasts/tools/Cppcheck/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, output_dir: Path, xml_tree: ElementTree, cmdout: dict) -> Non
7979
filepath=Path(error.xpath("location")[0].get("file")),
8080
checker=error.get("id"),
8181
category=category,
82-
cwe=CWEs.from_id(error.get("cwe", -1)),
82+
cwe=CWEs.from_id(int(error.get("cwe", -1))),
8383
message=error.get("msg"),
8484
lines=[
8585
int(location.get("line"))

codesectools/sasts/tools/Cppcheck/sast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CppcheckSAST(PrebuiltBuildlessSAST):
2424
supported_dataset_names (list[str]): A list of names of compatible datasets.
2525
properties (SASTProperties): The properties of the SAST tool.
2626
requirements (SASTRequirements): The requirements for the SAST tool.
27-
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendred and executed.
27+
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendered and executed.
2828
valid_codes (list[int]): A list of exit codes indicating that the command did not fail.
2929
output_files (list[tuple[Path, bool]]): A list of expected output files and
3030
whether they are required.

codesectools/sasts/tools/SemgrepCE/sast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SemgrepCESAST(BuildlessSAST):
2626
supported_dataset_names (list[str]): A list of names of compatible datasets.
2727
properties (SASTProperties): The properties of the SAST tool.
2828
requirements (SASTRequirements): The requirements for the SAST tool.
29-
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendred and executed.
29+
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendered and executed.
3030
valid_codes (list[int]): A list of exit codes indicating that the command did not fail.
3131
output_files (list[tuple[Path, bool]]): A list of expected output files and
3232
whether they are required.

codesectools/sasts/tools/SnykCode/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SnykCodeAnalysisResult(AnalysisResult):
5454
metadata, including timings, file lists, and defects.
5555
"""
5656

57-
normalize_lang_names = {"cpp": ["c", "cpp"]}
57+
normalize_lang_names = {"java": ["java"], "cpp": ["c", "cpp"]}
5858

5959
def __init__(self, output_dir: Path, result_data: dict, cmdout: dict) -> None:
6060
"""Initialize a SnykCodeAnalysisResult instance.
@@ -83,7 +83,7 @@ def __init__(self, output_dir: Path, result_data: dict, cmdout: dict) -> None:
8383
for result in run["results"]:
8484
rule_index = result["ruleIndex"]
8585
lang, *_, checker = result["ruleId"].split("/")
86-
if self.lang not in self.normalize_lang_names[lang]:
86+
if self.lang not in self.normalize_lang_names.get(lang, []):
8787
continue
8888

8989
start = (

codesectools/sasts/tools/SnykCode/sast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class SnykCodeSAST(BuildlessSAST):
2222
supported_dataset_names (list[str]): A list of names of compatible datasets.
2323
properties (SASTProperties): The properties of the SAST tool.
2424
requirements (SASTRequirements): The requirements for the SAST tool.
25-
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendred and executed.
25+
commands (list[list[Union[str, tuple[str]]]]): The list of commands templates to be rendered and executed.
2626
valid_codes (list[int]): A list of exit codes indicating that the command did not fail.
2727
output_files (list[tuple[Path, bool]]): A list of expected output files and
2828
whether they are required.

0 commit comments

Comments
 (0)