Skip to content

Commit

Permalink
fix jsonl schema checking
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Double <pat@patdouble.com>
  • Loading branch information
double16 committed Jul 25, 2024
1 parent 0a48501 commit e49d0ed
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions shadycompass/facts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ def check_file_signature(file_path: str, *signatures) -> bool:
elif isinstance(sig, dict):
# jsonl
lines = content.split('\n')
while len(lines) > 0 and len(lines[-1]) == 0:
lines.pop(-1)
if len(lines) == 0:
return False
try:
# ignore the last line, it's likely cut off
for line in lines[0:-1]:
if len(content) > 4000:
lines_to_check = lines[0:-1]
else:
lines_to_check = lines
for line in lines_to_check:
jsonschema.validate(instance=json.loads(line), schema=sig)
except (ValueError, jsonschema.exceptions.ValidationError):
return False
Expand Down Expand Up @@ -1110,10 +1116,10 @@ def parse_cpe(value: str) -> Union[Fact, None]:
assert cpe_parts.pop(0) == 'cpe'
cpe_type = cpe_parts.pop(0) # [/a,/o,/h] or float version
try:
cpe_version = float(cpe_type)
float(cpe_type) # check if version is a valid float
cpe_type = cpe_parts.pop(0)
except ValueError:
cpe_version = 1.0
pass

kwargs['vendor'] = cpe_parts.pop(0)
kwargs['product'] = cpe_parts.pop(0)
Expand Down
4 changes: 4 additions & 0 deletions tests/facts/http_spider/test_facts_gospider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ def test_facts(self):
assertFactIn(VirtualHostname(hostname='ir.shadycompass.test'), facts)
assertFactIn(VirtualHostname(hostname='footer-ir.shadycompass.test'), facts)
assertFactIn(VirtualHostname(hostname='footer-store.shadycompass.test'), facts)

def test_facts_not_gospider_output(self):
facts = self.reader.read_facts('tests/fixtures/shadycompass.ini')
self.assertEqual(0, len(facts))
4 changes: 4 additions & 0 deletions tests/facts/http_spider/test_facts_katana.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ def test_facts_passive(self):
assertFactIn(VirtualHostname(hostname='www.vpn.shadycompass.test', port=443, secure=True), facts)
assertFactIn(VirtualHostname(hostname='click.e.shadycompass.test', port=443, secure=True), facts)
assertFactIn(VirtualHostname(hostname='techinfo.shadycompass.test', port=443, secure=True), facts)

def test_facts_not_katana_output(self):
facts = self.reader.read_facts('tests/fixtures/shadycompass.ini')
self.assertEqual(0, len(facts))

0 comments on commit e49d0ed

Please sign in to comment.