Skip to content

Commit e65b8e2

Browse files
committed
Update function docs
Add resource filter based on language we support Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent 07ed82f commit e65b8e2

2 files changed

Lines changed: 29 additions & 33 deletions

File tree

scanpipe/pipelines/analyze_symbols_reachability.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,24 @@
2222

2323
from scanpipe.pipelines import Pipeline
2424
from scanpipe.pipes import reachability
25+
from scanpipe.pipes.symbols import TS_QUERIES
2526

2627

2728
class SymbolReachability(Pipeline):
2829
"""
29-
Patch reachability analysis for given vulnerability patches.
30+
Determine the reachability of vulnerabilities identified in the project.
3031
31-
For every patch we clone the git repository and extract
32-
the vulnerable and fixed symbols from the patch commit.
32+
Note: You must run `find_vulnerabilities` pipeline before running this pipeline.
3333
34-
These symbols are then matched against the project's codebase resources to
35-
determine if the vulnerable code is actually present and reachable.
34+
For every patch the git repository is cloned and extract the vulnerable and fixed
35+
symbols from the patch commit. These symbols are then matched against
36+
the project's codebase resources to determine if the vulnerable code
37+
is actually present and reachable.
3638
37-
The analysis checks if vulnerable symbols are defined, imported, called, or
38-
exactly match a fingerprint within the project files. The results, including
39-
tool_details and a reachability status (REACHABLE, UNKNOWN, or
40-
NOT_REACHABLE), are stored in the `extra_data` of the matching resources
41-
under the `symbols_reachability` key.
39+
The analysis checks if vulnerable symbols are defined, imported, called,
40+
or exactly match a code within the project files. The results, including
41+
tool_details and a reachability status (yes, unknown, or no), are stored
42+
in the `extra_data` of the matching resources under the `symbols_reachability` key.
4243
4344
Finally, a summary report is generated for each vulnerability
4445
advisory and saved as a JSON output file.
@@ -66,24 +67,22 @@ def get_vulnerabilities_patches(self):
6667
)
6768

6869
def collect_resource_index(self):
69-
"""Collect resources symbols for each resource exactly once."""
70+
"""Collect resources symbols for each resource"""
7071
self.candidate_resources = self.project.codebaseresources.files().filter(
71-
is_binary=False, is_archive=False, is_media=False
72+
is_binary=False, is_archive=False, is_media=False,
73+
programming_language__in=TS_QUERIES.keys()
7274
)
7375
self.resource_indexes = reachability.collect_resource_index(
7476
candidate_resources=self.candidate_resources
7577
)
7678

7779
def collect_patch_symbols(self):
78-
"""
79-
Collect patch symbols for all related commits,
80-
then remove the local clone before moving on
81-
"""
80+
"""Collect patch symbols for all related commits."""
8281
self.patch_symbols = reachability.collect_patch_symbols(patches=self.patches)
8382

8483
def collect_and_match_resources(self):
8584
"""Match resource symbols against patch symbols."""
86-
reachability.collect_and_match_resources(
85+
reachability.match_patches_to_resources(
8786
patches=self.patches,
8887
patch_symbols=self.patch_symbols,
8988
resource_indexes=self.resource_indexes,

scanpipe/pipes/reachability.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
from git import Repo
3131
from git.diff import NULL_TREE
32-
from scancode.api import get_file_info
32+
from typecode import get_type
3333

3434
from scanpipe.pipes.symbols import TS_QUERIES
3535
from scanpipe.pipes.symbols import SymbolExtractor
@@ -55,10 +55,7 @@ def normalize_text(content):
5555

5656

5757
def detect_language_with_scancode(file_path, content):
58-
"""
59-
Detect the programming language of the text
60-
content using get_file_info function
61-
"""
58+
"""Detect the programming language of the text"""
6259
content = normalize_text(content)
6360

6461
if not content:
@@ -67,11 +64,11 @@ def detect_language_with_scancode(file_path, content):
6764
tmp_dir = tempfile.mkdtemp(prefix="patch-lang-")
6865

6966
try:
70-
target = Path(tmp_dir) / Path(file_path).name
71-
target.write_text(content, encoding="utf-8", errors="replace")
67+
location = Path(tmp_dir) / Path(file_path).name
68+
location.write_text(content, encoding="utf-8", errors="replace")
7269

73-
info = get_file_info(location=str(target)) or {}
74-
return info.get("programming_language") or None
70+
info = get_type(location)
71+
return info.programming_language or None
7572

7673
finally:
7774
shutil.rmtree(tmp_dir, ignore_errors=True)
@@ -652,7 +649,7 @@ def get_vulnerabilities_patches(package_vulnerabilities, dependency_vulnerabilit
652649

653650

654651
def collect_resource_index(candidate_resources):
655-
"""Collect resources symbols for each resource exactly once."""
652+
"""Collect resources symbols for each resource"""
656653
resource_indexes = {}
657654
for resource in candidate_resources:
658655
resource_language = resource.programming_language
@@ -674,8 +671,8 @@ def collect_resource_index(candidate_resources):
674671

675672
def collect_patch_symbols(patches):
676673
"""
677-
For each unique repo: clone it once, collect patch symbols for all
678-
related commits, then remove the local clone before moving on
674+
For each unique repo clone it once,
675+
collect patch symbols for all related commits
679676
"""
680677
patch_symbols = {}
681678
patches_by_repo = {}
@@ -704,7 +701,7 @@ def collect_patch_symbols(patches):
704701
return patch_symbols
705702

706703

707-
def collect_and_match_resources(
704+
def match_patches_to_resources(
708705
patches, patch_symbols, candidate_resources, resource_indexes
709706
):
710707
"""Match resource symbols against patch symbols."""
@@ -760,18 +757,18 @@ def collect_and_match_resources(
760757

761758
def generate_advisory_reachability_report(project, patches, candidate_resources):
762759
"""
763-
Generate reachability report keyed by advisory_uid.
760+
Generate a reachability report summarizing status by advisory.
764761
765762
Each advisory contains its overall reachability status
766763
and the reachability results collected from all resources
767764
and associated patches.
768765
769766
The overall reachability status is determined using the following
770-
priority order: REACHABLE > UNKNOWN > NOT_REACHABLE
767+
priority order: REACHABLE: "yes" > UNKNOWN: "unknown" > NOT_REACHABLE: "no"
771768
772769
This means that an advisory is considered reachable if it is reachable
773770
through at least one resource or patch. If no reachable result exists,
774-
but at least one result is unknown, the advisory status is UNKNOWN.
771+
but at least one result is UNKNOWN, the advisory status is UNKNOWN.
775772
Otherwise, it is NOT_REACHABLE.
776773
"""
777774
status_priority = {

0 commit comments

Comments
 (0)