From f1d21e4a438edba5972040ea5f0a5cbac4b947ff Mon Sep 17 00:00:00 2001 From: Michael Hohn Date: Wed, 8 Dec 2021 16:02:31 -0800 Subject: [PATCH] Fix missing 'region' key in relatedLocations: use whole-file output The goal is fixed-structure output formatting, so whole-file output uses -1,-1,-1,-1 for line, column information. --- bin/sarif-results-summary | 9 ++++++++- sarif_cli/__init__.py | 6 ++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bin/sarif-results-summary b/bin/sarif-results-summary index 127e751..5f7a668 100755 --- a/bin/sarif-results-summary +++ b/bin/sarif-results-summary @@ -71,6 +71,10 @@ for runi in S.indices(sarif_struct, 'runs'): else: S.msg("REFERENCE: %s: %s\n\n" % ("", message)) else: + if region == S.WholeFile: + l1, c1, l2, c2 = -1, -1, -1, -1 + else: + l1, c1, l2, c2 = S.lineinfo(region) l1, c1, l2, c2 = S.lineinfo(region) filepath = "%s:%d:%d:%d:%d" % (artifact['uri'], l1, c1, l2, c2) if args.csv: @@ -97,7 +101,10 @@ for runi in S.indices(sarif_struct, 'runs'): else: S.msg("FLOW STEP %d: %s: %s\n\n" % (loci, "", message)) else: - l1, c1, l2, c2 = S.lineinfo(region) + if region == S.WholeFile: + l1, c1, l2, c2 = -1, -1, -1, -1 + else: + l1, c1, l2, c2 = S.lineinfo(region) filepath = "%s:%d:%d:%d:%d" % (artifact['uri'], l1, c1, l2, c2) if args.csv: S.write_csv(cw, "flow_step", loci, artifact['uri'], l1, c1, l2, c2, message) diff --git a/sarif_cli/__init__.py b/sarif_cli/__init__.py index b31d887..51722bd 100644 --- a/sarif_cli/__init__.py +++ b/sarif_cli/__init__.py @@ -45,11 +45,13 @@ def get_relatedlocation_message_info(related_location): Returns: (message, artifact, region) by default For an empty 'physicalLocation' key, returns (message, sarif_cli.NoFile, sarif_cli.NoFile) + For an empty 'region' key, returns (message, artifact, sarif_cli.WholeFile) """ message = get(related_location, 'message', 'text') if 'physicalLocation' in related_location: - artifact = get(related_location, 'physicalLocation', 'artifactLocation') - region = get(related_location, 'physicalLocation', 'region') + ploc = get(related_location, 'physicalLocation') + artifact = ploc.get('artifactLocation') + region = ploc.get('region', WholeFile) else: artifact, region = NoFile, NoFile return message, artifact, region