-
Notifications
You must be signed in to change notification settings - Fork 0
Fix Scorecard SARIF upload warnings #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ce1df3f
Fix Scorecard SARIF upload warnings
seonghobae 7094fce
Harden Scorecard SARIF normalizer
seonghobae cbf63b0
Ensure Scorecard SARIF region lines
seonghobae d1cd795
Validate Scorecard SARIF start lines
seonghobae f54a3cb
Guard Scorecard SARIF containers
seonghobae 64cf557
Normalize Scorecard SARIF properties
seonghobae 9d46a73
Harden Scorecard SARIF upload guard
seonghobae bd80834
Parse Scorecard SARIF upload steps
seonghobae d644fb2
Harden Scorecard SARIF upload matching
seonghobae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,86 @@ | ||
| """Normalize OSSF Scorecard SARIF so GitHub can ingest repository findings.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| SCORECARD_REPOSITORY_PLACEHOLDER_URI = "no file associated with this alert" | ||
| SCORECARD_WORKFLOW_URI = ".github/workflows/ossf-scorecard.yml" | ||
|
|
||
|
|
||
| def normalize_scorecard_sarif(source: Path, target: Path) -> int: | ||
| """Rewrite repository-level Scorecard placeholder URIs and return change count.""" | ||
| sarif = json.loads(source.read_text(encoding="utf-8")) | ||
| rewritten = 0 | ||
|
|
||
| runs = sarif.get("runs", []) if isinstance(sarif, dict) else [] | ||
| if not isinstance(runs, list): | ||
| runs = [] | ||
| for run in runs: | ||
| if not isinstance(run, dict): | ||
| continue | ||
| results = run.get("results", []) | ||
| if not isinstance(results, list): | ||
| continue | ||
| for result in results: | ||
| if not isinstance(result, dict): | ||
| continue | ||
| locations = result.get("locations", []) | ||
| if not isinstance(locations, list): | ||
| continue | ||
| for location in locations: | ||
| if not isinstance(location, dict): | ||
| continue | ||
| physical_location = location.get("physicalLocation") | ||
| if not isinstance(physical_location, dict): | ||
| continue | ||
| artifact_location = physical_location.get("artifactLocation") | ||
| if not isinstance(artifact_location, dict): | ||
| continue | ||
| if artifact_location.get("uri") != SCORECARD_REPOSITORY_PLACEHOLDER_URI: | ||
| continue | ||
| artifact_location["uri"] = SCORECARD_WORKFLOW_URI | ||
| region = physical_location.get("region") | ||
| if not isinstance(region, dict): | ||
| region = {} | ||
| physical_location["region"] = region | ||
| start_line = region.get("startLine") | ||
| if type(start_line) is not int or start_line < 1: | ||
| region["startLine"] = 1 | ||
| properties = physical_location.get("properties") | ||
| if not isinstance(properties, dict): | ||
| properties = {} | ||
| physical_location["properties"] = properties | ||
| properties["bandscopeOriginalUri"] = ( | ||
| SCORECARD_REPOSITORY_PLACEHOLDER_URI | ||
| ) | ||
| properties["bandscopeRepositoryLevelFinding"] = True | ||
| rewritten += 1 | ||
|
|
||
| target.write_text( | ||
| json.dumps(sarif, indent=2, sort_keys=True) + "\n", encoding="utf-8" | ||
| ) | ||
| return rewritten | ||
|
|
||
|
|
||
| def parse_args() -> argparse.Namespace: | ||
| """Parse command-line arguments.""" | ||
| parser = argparse.ArgumentParser( | ||
| description="Normalize OSSF Scorecard SARIF repository-level locations." | ||
| ) | ||
| parser.add_argument("source", type=Path, help="Path to the Scorecard SARIF file") | ||
| parser.add_argument("target", type=Path, help="Path to write normalized SARIF") | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """Normalize a Scorecard SARIF file from the command line.""" | ||
| args = parse_args() | ||
| rewritten = normalize_scorecard_sarif(args.source, args.target) | ||
| print(f"Normalized {rewritten} OSSF Scorecard repository-level SARIF locations") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.