-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into julien.doutre/go-support
- Loading branch information
Showing
18 changed files
with
2,252 additions
and
410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,108 @@ | ||
import os | ||
import pathlib | ||
from dataclasses import dataclass | ||
from typing import Optional, Iterable | ||
|
||
import yaml | ||
from yaml.loader import SafeLoader | ||
|
||
from guarddog.ecosystems import ECOSYSTEM | ||
|
||
current_dir = pathlib.Path(__file__).parent.resolve() | ||
rule_file_names = list( | ||
filter( | ||
lambda x: x.endswith('yml'), | ||
os.listdir(current_dir) | ||
) | ||
) | ||
|
||
SOURCECODE_RULES = { | ||
ECOSYSTEM.PYPI: list(), | ||
ECOSYSTEM.NPM: list(), | ||
ECOSYSTEM.GO: list(), | ||
} # type: dict[ECOSYSTEM, list[dict]] | ||
|
||
for file_name in rule_file_names: | ||
# These data class aim to reduce the spreading of the logic | ||
# Instead of using the a dict as a structure and parse it difffently depending on the type | ||
@dataclass | ||
class SourceCodeRule: | ||
""" | ||
Base class for source code rules | ||
""" | ||
id: str | ||
file: str | ||
|
||
|
||
@dataclass | ||
class YaraRule(SourceCodeRule): | ||
""" | ||
Yara rule just reimplements base | ||
""" | ||
pass | ||
|
||
|
||
@dataclass | ||
class SempgrepRule(SourceCodeRule): | ||
""" | ||
Semgrep rule are language specific | ||
Content of rule in yaml format is accessible through rule_content | ||
""" | ||
description: str | ||
ecosystem: ECOSYSTEM | ||
rule_content: dict | ||
|
||
|
||
def get_sourcecode_rules( | ||
ecosystem: ECOSYSTEM, kind: Optional[type] = None | ||
) -> Iterable[SourceCodeRule]: | ||
""" | ||
This function returns the source code rules for a given ecosystem and kind. | ||
Args: | ||
ecosystem: The ecosystem to filter for if rules are ecosystem specific | ||
kind: The kind of rule to filter for | ||
""" | ||
for rule in SOURCECODE_RULES: | ||
if kind and not isinstance(rule, kind): | ||
continue | ||
if not (getattr(rule, "ecosystem", ecosystem) == ecosystem): | ||
continue | ||
yield rule | ||
|
||
|
||
SOURCECODE_RULES: list[SourceCodeRule] = list() | ||
|
||
semgrep_rule_file_names = list( | ||
filter(lambda x: x.endswith("yml"), os.listdir(current_dir)) | ||
) | ||
# all yml files placed in the sourcecode directory are loaded as semgrep rules | ||
# refer to README.md for more information | ||
for file_name in semgrep_rule_file_names: | ||
with open(os.path.join(current_dir, file_name), "r") as fd: | ||
data = yaml.load(fd, Loader=SafeLoader) | ||
for rule in data["rules"]: | ||
for lang in rule["languages"]: | ||
ecosystem = None | ||
match lang: | ||
case "python": | ||
if rule not in SOURCECODE_RULES[ECOSYSTEM.PYPI]: | ||
SOURCECODE_RULES[ECOSYSTEM.PYPI].append(rule) | ||
ecosystem = ECOSYSTEM.PYPI | ||
case "javascript" | "typescript" | "json": | ||
if rule not in SOURCECODE_RULES[ECOSYSTEM.NPM]: | ||
SOURCECODE_RULES[ECOSYSTEM.NPM].append(rule) | ||
ecosystem = ECOSYSTEM.NPM | ||
case "go": | ||
if rule not in SOURCECODE_RULES[ECOSYSTEM.GO]: | ||
SOURCECODE_RULES[ECOSYSTEM.GO].append(rule) | ||
ecosystem = ECOSYSTEM.GO | ||
case _: | ||
continue | ||
|
||
# avoids duplicates when multiple languages are supported by a rule | ||
if not next( | ||
filter( | ||
lambda r: r.id == rule["id"], | ||
get_sourcecode_rules(ecosystem, SempgrepRule), | ||
), | ||
None, | ||
): | ||
SOURCECODE_RULES.append( | ||
SempgrepRule( | ||
id=rule["id"], | ||
ecosystem=ecosystem, | ||
description=rule.get("metadata", {}).get("description", ""), | ||
file=file_name, | ||
rule_content=rule, | ||
) | ||
) | ||
|
||
yara_rule_file_names = list( | ||
filter(lambda x: x.endswith("yar"), os.listdir(current_dir)) | ||
) | ||
# all yar files placed in the sourcecode directory are loaded as YARA rules | ||
# refer to README.md for more information | ||
for file_name in yara_rule_file_names: | ||
SOURCECODE_RULES.append(YaraRule(id=pathlib.Path(file_name).stem, file=file_name)) |
Oops, something went wrong.