-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from CybercentreCanada/bugfix/regression
Fix bug with generating metadata for all rules within rules file
- Loading branch information
Showing
4 changed files
with
116 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: tests | ||
|
||
trigger: ["*"] | ||
pr: ["*"] | ||
|
||
pool: | ||
vmImage: "ubuntu-20.04" | ||
|
||
jobs: | ||
- job: run_test | ||
strategy: | ||
matrix: | ||
Python3_8: | ||
python.version: "3.8" | ||
Python3_9: | ||
python.version: "3.9" | ||
Python3_10: | ||
python.version: "3.10" | ||
Python3_11: | ||
python.version: "3.11" | ||
Python3_12: | ||
python.version: "3.12" | ||
|
||
timeoutInMinutes: 10 | ||
|
||
steps: | ||
- task: UsePythonVersion@0 | ||
displayName: Set python version | ||
inputs: | ||
versionSpec: "$(python.version)" | ||
- script: | | ||
[ ! -d "$(pwd)/tests" ] && echo "No tests found" && exit | ||
[ -f $(pwd)/requirements.txt ] && sudo env "PATH=$PATH" python -m pip install -U --no-cache-dir -r $(pwd)/requirements.txt | ||
[ -f $(pwd)/tests/requirements.txt ] && sudo env "PATH=$PATH" python -m pip install -U --no-cache-dir -r $(pwd)/tests/requirements.txt | ||
sudo rm -rf /tmp/* /var/lib/apt/lists/* ~/.cache/pip | ||
displayName: Setup environment | ||
- script: | | ||
[ ! -d "$(pwd)/tests" ] && echo "No tests found" && exit | ||
export REPO_NAME=${BUILD_REPOSITORY_NAME##*/} | ||
python -m pytest -p no:cacheprovider --durations=10 -rsx -vv | ||
displayName: Test |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from tempfile import NamedTemporaryFile | ||
|
||
from yara_validator.validator import run_yara_validator | ||
|
||
RULES = b""" | ||
rule x | ||
{ | ||
meta: | ||
version = "1.0" | ||
score = "0" | ||
minimum_yara = "3.5" | ||
date = "2024-05-07" | ||
modified = "2024-05-07" | ||
status = "RELEASED" | ||
sharing = "TLP:CLEAR" | ||
author = "CCCS" | ||
description = "Fake rule for testing" | ||
category = "TOOL" | ||
tool = "exemplar" | ||
source = "CCCS" | ||
strings: | ||
$ = "x" | ||
condition: | ||
all of them | ||
} | ||
rule y | ||
{ | ||
meta: | ||
version = "1.0" | ||
score = "0" | ||
minimum_yara = "3.5" | ||
date = "2024-05-07" | ||
modified = "2024-05-07" | ||
status = "RELEASED" | ||
sharing = "TLP:CLEAR" | ||
author = "CCCS" | ||
description = "Fake rule for testing" | ||
category = "TOOL" | ||
tool = "exemplar" | ||
source = "CCCS" | ||
strings: | ||
$ = "y" | ||
condition: | ||
all of them | ||
} | ||
""" | ||
|
||
def test_required_fields(): | ||
# Bug: Metadata generation only worked on the first rule within a ruleset | ||
with NamedTemporaryFile() as tf: | ||
tf.write(RULES) | ||
tf.seek(0) | ||
|
||
for rule in run_yara_validator(tf.name, generate_values=True).yara_rules: | ||
fingerprint, id = None, None | ||
for m in rule.rule_plyara['metadata']: | ||
if 'id' in m: | ||
id = m['id'] | ||
elif 'fingerprint' in m: | ||
fingerprint = m['fingerprint'] | ||
|
||
# Ensure the fingerprint and the id metadata fields were generated for all rules | ||
assert fingerprint and id |
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