From e88cef4fc871f95bf3516c18c275f3e504a6ebda Mon Sep 17 00:00:00 2001 From: Jeff Leach Date: Fri, 4 Sep 2026 07:37:07 -0500 Subject: [PATCH 1/5] ci: add CodeQL + SonarQube Cloud security scanning Adds a manually-dispatched Security-Scan workflow following the AMMOS guide, "Code Vulnerability Scanning Set Up for AMMOS Open Source Software" (wiki pageId 1029935039): CodeQL -> nasa-scrub -> fix_sarif_paths.py -> sonarqube-scan-action The scrub step is the one the guide prescribes. It is lossy, so a repair step is chained after it. Measured against nasa-scrub 3.0.2 with a CodeQL-shaped report, translate_results: - rewrites CodeQL's workspace-relative URIs to absolute paths and puts the absolute source root in uriBaseId, where SARIF expects a symbolic key such as %SRCROOT%. SonarQube then cannot map findings to files. - deletes tool.extensions -- where CodeQL keeps its real query metadata -- and rebuilds tool.driver.rules as id-only stubs, dropping every severity, so all findings import as MEDIUM. - sets driver.name to the input filename rather than CodeQL, changing the external rule namespace to external_javascript:*. This is not an ordering problem: scrub rebuilds driver.rules from scratch even when handed a report whose driver already carries rich rules, so running the repair first does not help. fix_sarif_paths.py instead takes the pre-scrub report via --reference and restores rule metadata, tool name and severities, and relativizes paths. Covered by 24 tests. Residual, documented in the script: scrub also discards relatedLocations and codeFlows, which are not recoverable by this route. Scope is limited to first-party code. Excluded are dependencies and build output, static assets under public/ (including the vendored minified pdf.js worker we only host), docs/, fonts and images, and tests. src/externals/ is deliberately kept: those vendored React components are compiled into our bundle and absent from package.json, so Dependabot does not track them and nothing else would cover them. Their own bundled dependencies and test suite are excluded. sonar.branch.name is intentionally not set for the first runs, so a manual dispatch is attributed to the project's main branch instead of creating a second branch in SonarQube Cloud. Claude-Session: https://claude.ai/code/session_01KKwLzyhbDQ2MyUuofXE25y --- .github/codeql/codeql-config.yml | 50 ++++ .github/scripts/fix_sarif_paths.py | 372 ++++++++++++++++++++++++ .github/scripts/test_fix_sarif_paths.py | 295 +++++++++++++++++++ .github/workflows/security-scan.yml | 149 ++++++++++ sonar-project.properties | 97 ++++++ 5 files changed, 963 insertions(+) create mode 100644 .github/codeql/codeql-config.yml create mode 100755 .github/scripts/fix_sarif_paths.py create mode 100644 .github/scripts/test_fix_sarif_paths.py create mode 100644 .github/workflows/security-scan.yml create mode 100644 sonar-project.properties diff --git a/.github/codeql/codeql-config.yml b/.github/codeql/codeql-config.yml new file mode 100644 index 0000000..96bbbf8 --- /dev/null +++ b/.github/codeql/codeql-config.yml @@ -0,0 +1,50 @@ +name: "CodeQL Security Scan Configuration" + +# Keep the default query suite and add the extended security queries. +disable-default-queries: false + +queries: + - uses: security-extended + +# Scope note: astria_ui is a React frontend plus a small Express proxy. What is +# worth analyzing is our own application code. Everything below is either not +# ours, not code, or not shipped. +# +# src/externals/ is deliberately NOT excluded. Those are vendored third-party +# React components (react-json-inspector, react-splitter-layout) that are +# compiled into our bundle and are not declared in package.json -- so Dependabot +# does not see them and nothing else would cover them. Their own bundled +# dependencies and tests are excluded just below. +paths-ignore: + # Dependencies and build output -- covered by Dependabot, not our source. + - '**/node_modules/**' + - '**/dist/**' + - '**/build/**' + - '**/coverage/**' + + # Third-party code we only host or vendor wholesale. + - 'public/**' # static assets + pdf.worker.min.mjs + - 'src/externals/**/node_modules/**' # bundled deps of a vendored component + - 'src/externals/**/test/**' # a vendored component's own test suite + - '**/*.min.js' + - '**/*.min.mjs' + - '**/*.min.css' + + # Not code. + - 'docs/**' + - 'src/fonts/**' + - 'src/images/**' + - 'src/styles/**' + + # Tests. + - 'src/test/**' + - '**/*.test.js' + - '**/*.test.jsx' + - '**/*.spec.js' + - '**/*.spec.jsx' + +query-filters: + # Style and maintainability suggestions add volume without adding security + # signal; SonarQube already covers that ground. + - exclude: + problem.severity: recommendation diff --git a/.github/scripts/fix_sarif_paths.py b/.github/scripts/fix_sarif_paths.py new file mode 100755 index 0000000..3b4074e --- /dev/null +++ b/.github/scripts/fix_sarif_paths.py @@ -0,0 +1,372 @@ +#!/usr/bin/env python3 +""" +Repair a CodeQL SARIF report so SonarQube Cloud can import it correctly. + +This runs *after* nasa-scrub's ``translate_results``, which the AMMOS scanning +guide prescribes: + + https://wiki.jpl.nasa.gov/display/AmmosArch/Code+Vulnerability+Scanning+Set+Up+for+AMMOS+Open+Source+Software + +scrub is kept in the pipeline for compliance with that guide, but it is +lossy. Measured against a CodeQL-shaped report with nasa-scrub 3.0.2 (the +current release), ``translate_results``: + +1. Rewrites workspace-relative URIs to **absolute** filesystem paths, and + writes the absolute source root into ``uriBaseId`` -- where SARIF 2.1.0 + expects a symbolic key into ``originalUriBaseIds`` such as ``%SRCROOT%``. + SonarQube cannot map issues to files with either, so findings collapse to + project-level "Unable to resolve issue location" warnings. + +2. Deletes ``runs[].tool.extensions`` and rebuilds ``runs[].tool.driver.rules`` + as id-only stubs:: + + {"id": "js/redos", "shortDescription": {"text": "js/redos"}} + + CodeQL publishes its real query metadata -- descriptions, ``help``, + ``defaultConfiguration.level`` and ``properties.security-severity`` -- under + ``tool.extensions[].rules``, leaving ``driver.rules`` empty. SonarQube reads + only ``driver.rules``. So scrub is the only thing populating the field + SonarQube reads, but it populates it with metadata-free stubs, and every + finding imports as MEDIUM regardless of real severity. + + This is not an ordering problem: scrub rebuilds ``driver.rules`` from + scratch even when handed a report whose driver already carries rich rules, + so it cannot be fixed by running this script first. + +3. Sets ``driver.name`` to the input *filename* (e.g. ``javascript``) rather + than ``CodeQL``, which is what SonarQube uses to namespace external rule + keys (``external_javascript:js/redos`` instead of ``external_CodeQL:...``). + +Passing ``--reference`` with the original, pre-scrub CodeQL SARIF lets this +script restore (2) and (3) from the authoritative source, and it always fixes +(1). The result is a report that satisfies the AMMOS guide's pipeline while +still importing with correct paths, rule names, descriptions and severities. + +Known residual loss: scrub also discards ``relatedLocations`` and +``codeFlows``. Those are not restored here -- re-associating them would mean +matching results across two reports by heuristic, which is fragile. If the +security office is open to it, dropping the scrub step entirely (feeding this +script the raw CodeQL SARIF, where ``--reference`` is unnecessary because the +extensions are still present) preserves them. + +Usage: + fix_sarif_paths.py + [--reference ] +""" +import json +import os +import sys + +# The valid SARIF 2.1.0 schema URL. Some tools emit the old master-branch URL +# (which 404s); rewrite it. Mirrors nasa/scrub PR #121. +GOOD_SCHEMA_URL = ( + 'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/' + 'sarif-2.1/schema/sarif-schema-2.1.0.json' +) +BAD_SCHEMA_URLS = ( + 'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/' + 'Schemata/sarif-schema-2.1.0.json', +) + + +def collect_rules(run): + """Gather every rule a run publishes, richest-first, de-duplicated by id. + + CodeQL puts its query metadata under ``tool.extensions[].rules`` (one tool + component per query pack) and leaves ``tool.driver.rules`` empty, so both + locations have to be consulted. Driver rules win on conflict. + """ + tool = run.get('tool') + if not isinstance(tool, dict): + return [] + + rules = [] + seen = set() + driver = tool.get('driver') + sources = [] + if isinstance(driver, dict): + sources.append(driver.get('rules') or []) + for extension in tool.get('extensions') or []: + if isinstance(extension, dict): + sources.append(extension.get('rules') or []) + + for source in sources: + for rule in source: + if not isinstance(rule, dict): + continue + rule_id = rule.get('id') + if rule_id is None or rule_id in seen: + continue + # An id-only stub carries no metadata worth keeping; let a richer + # definition of the same id from a later source replace it. + seen.add(rule_id) + rules.append(rule) + return rules + + +def is_stub(rule): + """True if a rule carries no metadata beyond its id. + + scrub emits ``{"id": X, "shortDescription": {"text": X}}`` -- a description + identical to the id and nothing else. Treated as empty so a reference + report can replace it. + """ + if not isinstance(rule, dict): + return True + rule_id = rule.get('id') + has_severity = bool( + (rule.get('defaultConfiguration') or {}).get('level') + or (rule.get('properties') or {}).get('security-severity') + ) + short = (rule.get('shortDescription') or {}).get('text') + has_text = bool(short and short != rule_id) or bool(rule.get('fullDescription')) + return not (has_severity or has_text) + + +def apply_rules(run, rules, tool_name=None, force_level=False): + """Install ``rules`` on the run's driver and re-point every result at them. + + Results are matched by ``ruleId``; ``ruleIndex`` and ``result.rule`` are + rewritten to index into the driver's array, since any previous index + referred to an extension that may no longer exist. ``result.level`` is made + explicit from the rule's ``defaultConfiguration`` so SonarQube's importer + cannot miss it and fall back to MEDIUM. + + ``force_level`` overwrites a level that is already present. It is set when + restoring from a reference report, because scrub materializes a flat + ``"warning"`` onto every result -- SARIF says an absent level is inherited + from the rule's ``defaultConfiguration``, and scrub writes that inherited + value out after having already discarded the real one. Left off, a genuine + per-result level from the tool is preserved. + """ + if not rules: + return False + + tool = run.setdefault('tool', {}) + driver = tool.setdefault('driver', {}) + driver['rules'] = rules + if tool_name: + driver['name'] = tool_name + + index_of = {rule.get('id'): i for i, rule in enumerate(rules)} + + for result in run.get('results') or []: + rule_id = result.get('ruleId') or (result.get('rule') or {}).get('id') + index = index_of.get(rule_id) + if index is None: + continue + result['ruleId'] = rule_id + result['ruleIndex'] = index + result['rule'] = {'id': rule_id, 'index': index} + if force_level or 'level' not in result: + level = (rules[index].get('defaultConfiguration') or {}).get('level') + if level: + result['level'] = level + + return True + + +def restore_rules(run, reference_run): + """Replace scrub's stub rules with the real metadata from the pre-scrub report. + + No-op when the run already carries non-stub rules, so a report that was + never degraded is left alone. + """ + # Only the driver's own rules decide whether the report is already good -- + # SonarQube reads nothing else. Rules sitting in tool.extensions[] (where + # CodeQL actually puts them) still need promoting onto the driver. + driver_rules = ((run.get('tool') or {}).get('driver') or {}).get('rules') or [] + if driver_rules and not all(is_stub(rule) for rule in driver_rules): + return False + + existing = collect_rules(run) + if reference_run is None: + # Nothing authoritative to restore from; fall back to whatever the run + # itself publishes (this is the no-scrub path, where CodeQL's + # extensions are still intact). + return apply_rules(run, existing) + + rules = collect_rules(reference_run) + if not rules: + return apply_rules(run, existing) + + reference_name = ( + (reference_run.get('tool') or {}).get('driver') or {} + ).get('name') + return apply_rules(run, rules, tool_name=reference_name, force_level=True) + + +def harden_attribution(sarif): + """Normalize the document so SonarQube can attribute imported issues. + + SonarQube reads the tool name from ``runs[].tool.driver.name`` (mandatory) + and rule metadata from ``runs[].tool.driver.rules[]``. Anything under + ``runs[].tool.rules`` is ignored on import. + """ + modified = False + + if sarif.get('$schema') in BAD_SCHEMA_URLS: + sarif['$schema'] = GOOD_SCHEMA_URL + modified = True + + for run in sarif.get('runs') or []: + tool = run.get('tool') + if not isinstance(tool, dict): + continue + driver = tool.setdefault('driver', {}) + if not isinstance(driver, dict): + continue + + # Move misplaced rules onto the driver, without clobbering richer ones. + if 'rules' in tool: + if not driver.get('rules'): + driver['rules'] = tool['rules'] + del tool['rules'] + modified = True + + # SonarQube drops the whole report if driver.name is missing. + if not driver.get('name'): + driver['name'] = 'CodeQL' + modified = True + + return modified + + +def strip_absolute_uri_base_id(artifact_loc): + """Drop a ``uriBaseId`` holding a filesystem path rather than a symbolic name. + + SARIF 2.1.0 defines ``uriBaseId`` as a key into ``run.originalUriBaseIds`` + (e.g. ``%SRCROOT%``). scrub writes the absolute source root, which + SonarQube may prepend to an already-relative ``uri``. Symbolic ids are left + untouched. + """ + base_id = artifact_loc.get('uriBaseId') + if isinstance(base_id, str) and (base_id.startswith('/') or base_id.startswith('file://')): + del artifact_loc['uriBaseId'] + return True + return False + + +def relativize(artifact_loc, workspace_path): + """Make a single artifactLocation's URI relative to the workspace root.""" + modified = False + uri = artifact_loc.get('uri') + if isinstance(uri, str): + if uri.startswith('file://'): + uri = uri[len('file://'):] + if uri.startswith(workspace_path + '/'): + uri = uri[len(workspace_path) + 1:] + if uri != artifact_loc.get('uri'): + artifact_loc['uri'] = uri + modified = True + if strip_absolute_uri_base_id(artifact_loc): + modified = True + return modified + + +def iter_artifact_locations(run): + """Yield every artifactLocation in a run, including nested flow locations. + + scrub currently discards relatedLocations and codeFlows, but the raw + CodeQL report carries both, so this also covers the no-scrub pipeline. + """ + for artifact in run.get('artifacts') or []: + location = artifact.get('location') + if isinstance(location, dict): + yield location + + def from_location(location): + physical = (location or {}).get('physicalLocation') or {} + artifact_loc = physical.get('artifactLocation') + if isinstance(artifact_loc, dict): + yield artifact_loc + + for result in run.get('results') or []: + for key in ('locations', 'relatedLocations'): + for location in result.get(key) or []: + yield from from_location(location) + for flow in result.get('codeFlows') or []: + for thread_flow in flow.get('threadFlows') or []: + for entry in thread_flow.get('locations') or []: + yield from from_location(entry.get('location')) + + +def fix_sarif(sarif_file, output_file, workspace_path, reference_file=None): + """Repair paths and rule attribution, writing the result to ``output_file``.""" + try: + with open(sarif_file, 'r', encoding='utf-8') as handle: + sarif = json.load(handle) + + reference = None + if reference_file: + try: + with open(reference_file, 'r', encoding='utf-8') as handle: + reference = json.load(handle) + except (OSError, ValueError) as exc: + print( + f"WARNING: could not read reference {reference_file}: {exc}; " + "continuing without rule restoration", + file=sys.stderr, + ) + + reference_runs = (reference or {}).get('runs') or [] + modified = False + + for index, run in enumerate(sarif.get('runs') or []): + reference_run = None + if index < len(reference_runs): + reference_run = reference_runs[index] + elif len(reference_runs) == 1: + reference_run = reference_runs[0] + + if restore_rules(run, reference_run): + modified = True + + if harden_attribution(sarif): + modified = True + + workspace_path = workspace_path.rstrip('/') + for run in sarif.get('runs') or []: + for artifact_loc in iter_artifact_locations(run): + if relativize(artifact_loc, workspace_path): + modified = True + + with open(output_file, 'w', encoding='utf-8') as handle: + json.dump(sarif, handle, ensure_ascii=False, indent=2) + + name = os.path.basename(output_file) + if modified: + print(f"[ok] Normalized paths/attribution in {name}") + else: + print(f"[info] No changes needed for {name}") + return 0 + + except Exception as exc: # noqa: BLE001 - surface any failure to the runner + print(f"ERROR processing {sarif_file}: {exc}", file=sys.stderr) + return 1 + + +def main(argv): + args = list(argv) + reference_file = None + if '--reference' in args: + position = args.index('--reference') + if position + 1 >= len(args): + print("ERROR: --reference requires a file path", file=sys.stderr) + return 2 + reference_file = args[position + 1] + del args[position:position + 2] + + if len(args) != 3: + print( + "Usage: fix_sarif_paths.py " + " [--reference ]", + file=sys.stderr, + ) + return 2 + + return fix_sarif(args[0], args[1], args[2], reference_file) + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/.github/scripts/test_fix_sarif_paths.py b/.github/scripts/test_fix_sarif_paths.py new file mode 100644 index 0000000..cf28161 --- /dev/null +++ b/.github/scripts/test_fix_sarif_paths.py @@ -0,0 +1,295 @@ +#!/usr/bin/env python3 +"""Tests for fix_sarif_paths.py. + +Run with: python3 .github/scripts/test_fix_sarif_paths.py +""" +import copy +import json +import os +import sys +import tempfile +import unittest + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +from fix_sarif_paths import ( # noqa: E402 + GOOD_SCHEMA_URL, + collect_rules, + fix_sarif, + is_stub, +) + +WORKSPACE = '/home/runner/work/astria_ui/astria_ui' + +# A CodeQL report as github/codeql-action actually emits it: rules live under +# tool.extensions[], driver.rules is empty, URIs are relative with a symbolic +# %SRCROOT% uriBaseId, and results carry no explicit level. +RAW_CODEQL = { + "$schema": ("https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/" + "Schemata/sarif-schema-2.1.0.json"), + "version": "2.1.0", + "runs": [{ + "tool": { + "driver": {"name": "CodeQL", "rules": []}, + "extensions": [{ + "name": "codeql/javascript-queries", + "rules": [ + {"id": "js/redos", + "shortDescription": {"text": "Inefficient regular expression"}, + "fullDescription": {"text": "Exponential backtracking."}, + "defaultConfiguration": {"level": "error"}, + "properties": {"security-severity": "7.5"}}, + {"id": "js/unused-local-variable", + "shortDescription": {"text": "Unused variable"}, + "defaultConfiguration": {"level": "note"}}, + ], + }], + }, + "originalUriBaseIds": {"%SRCROOT%": {"uri": "file://" + WORKSPACE + "/"}}, + "artifacts": [{"location": {"uri": "src/app.js", "uriBaseId": "%SRCROOT%"}}], + "results": [{ + "ruleId": "js/redos", + "rule": {"id": "js/redos", "index": 0, "toolComponent": {"index": 0}}, + "message": {"text": "Vulnerable regex."}, + "locations": [{"physicalLocation": { + "artifactLocation": {"uri": "src/app.js", "uriBaseId": "%SRCROOT%"}, + "region": {"startLine": 1}}}], + "relatedLocations": [{"id": 1, "physicalLocation": { + "artifactLocation": {"uri": "src/util.js", "uriBaseId": "%SRCROOT%"}, + "region": {"startLine": 9}}}], + "codeFlows": [{"threadFlows": [{"locations": [{"location": { + "physicalLocation": { + "artifactLocation": {"uri": "src/flow.js", "uriBaseId": "%SRCROOT%"}, + "region": {"startLine": 3}}}}]}]}], + }], + }], +} + +# The same report after nasa-scrub's translate_results: absolute URIs, an +# absolute uriBaseId, extensions deleted, driver.rules rebuilt as id-only +# stubs, driver.name set to the filename, and a flat "warning" level. +SCRUBBED = { + "$schema": "https://json.schemastore.org/sarif-2.1.0.json", + "version": "2.1.0", + "runs": [{ + "tool": {"driver": { + "name": "javascript", + "rules": [{"id": "js/redos", "shortDescription": {"text": "js/redos"}}], + }}, + "artifacts": [{"location": {"uri": WORKSPACE + "/src/app.js"}}], + "results": [{ + "ruleId": "js/redos", + "level": "warning", + "message": {"text": "Vulnerable regex."}, + "locations": [{"physicalLocation": { + "artifactLocation": {"uri": WORKSPACE + "/src/app.js", + "uriBaseId": WORKSPACE}, + "region": {"startLine": 1}}}], + }], + }], +} + + +def run_fix(document, reference=None, workspace=WORKSPACE): + """Run fix_sarif on an in-memory document, returning the parsed output.""" + with tempfile.TemporaryDirectory() as tmp: + src = os.path.join(tmp, 'in.sarif') + dst = os.path.join(tmp, 'out.sarif') + with open(src, 'w', encoding='utf-8') as handle: + json.dump(document, handle) + + ref_path = None + if reference is not None: + ref_path = os.path.join(tmp, 'ref.sarif') + with open(ref_path, 'w', encoding='utf-8') as handle: + json.dump(reference, handle) + + code = fix_sarif(src, dst, workspace, ref_path) + assert code == 0, f"fix_sarif returned {code}" + with open(dst, encoding='utf-8') as handle: + return json.load(handle) + + +def first_location(result): + return result['locations'][0]['physicalLocation']['artifactLocation'] + + +class TestScrubRepair(unittest.TestCase): + """The wiki-prescribed pipeline: CodeQL -> scrub -> this script.""" + + def setUp(self): + self.out = run_fix(copy.deepcopy(SCRUBBED), reference=copy.deepcopy(RAW_CODEQL)) + self.run = self.out['runs'][0] + + def test_rule_metadata_restored_from_reference(self): + rules = self.run['tool']['driver']['rules'] + self.assertEqual(len(rules), 2) + redos = rules[0] + self.assertEqual(redos['shortDescription']['text'], + 'Inefficient regular expression') + self.assertEqual(redos['defaultConfiguration']['level'], 'error') + self.assertEqual(redos['properties']['security-severity'], '7.5') + + def test_tool_name_restored(self): + # Drives SonarQube's external rule namespace: external_CodeQL:js/redos + # rather than external_javascript:js/redos. + self.assertEqual(self.run['tool']['driver']['name'], 'CodeQL') + + def test_scrub_flat_warning_is_overridden(self): + # scrub flattens every finding to "warning"; the real rule is "error". + self.assertEqual(self.run['results'][0]['level'], 'error') + + def test_paths_relativized(self): + self.assertEqual(first_location(self.run['results'][0])['uri'], 'src/app.js') + self.assertEqual(self.run['artifacts'][0]['location']['uri'], 'src/app.js') + + def test_absolute_uri_base_id_removed(self): + self.assertNotIn('uriBaseId', first_location(self.run['results'][0])) + + def test_results_repointed_at_driver_rules(self): + result = self.run['results'][0] + self.assertEqual(result['ruleIndex'], 0) + self.assertEqual(result['rule'], {'id': 'js/redos', 'index': 0}) + + +class TestRawCodeQL(unittest.TestCase): + """The no-scrub pipeline: CodeQL -> this script.""" + + def setUp(self): + self.out = run_fix(copy.deepcopy(RAW_CODEQL)) + self.run = self.out['runs'][0] + + def test_extension_rules_promoted_to_driver(self): + rules = self.run['tool']['driver']['rules'] + self.assertEqual([r['id'] for r in rules], + ['js/redos', 'js/unused-local-variable']) + self.assertEqual(rules[0]['properties']['security-severity'], '7.5') + + def test_level_derived_from_rule(self): + self.assertEqual(self.run['results'][0]['level'], 'error') + + def test_symbolic_uri_base_id_preserved(self): + # %SRCROOT% is valid SARIF and must survive; only filesystem paths go. + self.assertEqual(first_location(self.run['results'][0])['uriBaseId'], + '%SRCROOT%') + + def test_relative_paths_untouched(self): + self.assertEqual(first_location(self.run['results'][0])['uri'], 'src/app.js') + + def test_related_locations_and_code_flows_preserved(self): + result = self.run['results'][0] + self.assertEqual(len(result['relatedLocations']), 1) + self.assertEqual(len(result['codeFlows']), 1) + + def test_broken_schema_url_corrected(self): + self.assertEqual(self.out['$schema'], GOOD_SCHEMA_URL) + + +class TestPathHandling(unittest.TestCase): + def test_absolute_paths_in_nested_locations_relativized(self): + doc = copy.deepcopy(RAW_CODEQL) + result = doc['runs'][0]['results'][0] + result['relatedLocations'][0]['physicalLocation']['artifactLocation'] = { + 'uri': 'file://' + WORKSPACE + '/src/util.js'} + flow = result['codeFlows'][0]['threadFlows'][0]['locations'][0] + flow['location']['physicalLocation']['artifactLocation'] = { + 'uri': WORKSPACE + '/src/flow.js'} + + out = run_fix(doc)['runs'][0]['results'][0] + self.assertEqual( + out['relatedLocations'][0]['physicalLocation']['artifactLocation']['uri'], + 'src/util.js') + self.assertEqual( + out['codeFlows'][0]['threadFlows'][0]['locations'][0]['location'] + ['physicalLocation']['artifactLocation']['uri'], + 'src/flow.js') + + def test_trailing_slash_workspace_is_tolerated(self): + out = run_fix(copy.deepcopy(SCRUBBED), reference=copy.deepcopy(RAW_CODEQL), + workspace=WORKSPACE + '/') + self.assertEqual(first_location(out['runs'][0]['results'][0])['uri'], + 'src/app.js') + + def test_paths_outside_workspace_left_alone(self): + doc = copy.deepcopy(SCRUBBED) + first_location(doc['runs'][0]['results'][0])['uri'] = '/usr/lib/node/thing.js' + out = run_fix(doc, reference=copy.deepcopy(RAW_CODEQL)) + self.assertEqual(first_location(out['runs'][0]['results'][0])['uri'], + '/usr/lib/node/thing.js') + + +class TestAttributionHardening(unittest.TestCase): + def test_misplaced_tool_rules_moved_to_driver(self): + doc = copy.deepcopy(SCRUBBED) + tool = doc['runs'][0]['tool'] + tool['driver'] = {'name': 'javascript'} + tool['rules'] = [{'id': 'js/redos', + 'shortDescription': {'text': 'Inefficient regex'}, + 'defaultConfiguration': {'level': 'error'}}] + out = run_fix(doc)['runs'][0]['tool'] + self.assertNotIn('rules', out) + self.assertEqual(out['driver']['rules'][0]['id'], 'js/redos') + + def test_missing_driver_name_defaults(self): + doc = copy.deepcopy(SCRUBBED) + del doc['runs'][0]['tool']['driver']['name'] + out = run_fix(doc) + self.assertEqual(out['runs'][0]['tool']['driver']['name'], 'CodeQL') + + def test_rich_rules_not_clobbered_by_reference(self): + # A report that was never degraded must be left alone. + doc = copy.deepcopy(SCRUBBED) + doc['runs'][0]['tool']['driver']['rules'] = [{ + 'id': 'js/redos', + 'shortDescription': {'text': 'Real description'}, + 'defaultConfiguration': {'level': 'note'}}] + out = run_fix(doc, reference=copy.deepcopy(RAW_CODEQL)) + rules = out['runs'][0]['tool']['driver']['rules'] + self.assertEqual(len(rules), 1) + self.assertEqual(rules[0]['shortDescription']['text'], 'Real description') + # And a genuine per-result level survives. + self.assertEqual(out['runs'][0]['results'][0]['level'], 'warning') + + +class TestRobustness(unittest.TestCase): + def test_missing_reference_file_does_not_fail_the_build(self): + with tempfile.TemporaryDirectory() as tmp: + src = os.path.join(tmp, 'in.sarif') + dst = os.path.join(tmp, 'out.sarif') + with open(src, 'w', encoding='utf-8') as handle: + json.dump(SCRUBBED, handle) + code = fix_sarif(src, dst, WORKSPACE, os.path.join(tmp, 'nope.sarif')) + self.assertEqual(code, 0) + + def test_malformed_input_reports_error(self): + with tempfile.TemporaryDirectory() as tmp: + src = os.path.join(tmp, 'in.sarif') + with open(src, 'w', encoding='utf-8') as handle: + handle.write('{not json') + code = fix_sarif(src, os.path.join(tmp, 'out.sarif'), WORKSPACE) + self.assertEqual(code, 1) + + def test_empty_runs_is_a_no_op(self): + out = run_fix({"version": "2.1.0", "runs": []}) + self.assertEqual(out['runs'], []) + + +class TestHelpers(unittest.TestCase): + def test_is_stub_detects_scrub_output(self): + self.assertTrue(is_stub({'id': 'js/redos', + 'shortDescription': {'text': 'js/redos'}})) + + def test_is_stub_rejects_real_rule(self): + self.assertFalse(is_stub({'id': 'js/redos', + 'shortDescription': {'text': 'Inefficient regex'}})) + self.assertFalse(is_stub({'id': 'js/redos', + 'defaultConfiguration': {'level': 'error'}})) + + def test_collect_rules_merges_driver_and_extensions(self): + rules = collect_rules(RAW_CODEQL['runs'][0]) + self.assertEqual([r['id'] for r in rules], + ['js/redos', 'js/unused-local-variable']) + + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml new file mode 100644 index 0000000..31499fb --- /dev/null +++ b/.github/workflows/security-scan.yml @@ -0,0 +1,149 @@ +name: Security-Scan + +# Static analysis for astria_ui, following the AMMOS scanning guide: +# "Code Vulnerability Scanning Set Up for AMMOS Open Source Software" +# https://wiki.jpl.nasa.gov/display/AmmosArch/Code+Vulnerability+Scanning+Set+Up+for+AMMOS+Open+Source+Software +# +# Pipeline: +# CodeQL scan +# -> nasa-scrub translate_results (the step the AMMOS guide prescribes) +# -> fix_sarif_paths.py (repairs what scrub degrades) +# -> sonarqube-scan-action (imports into SonarQube Cloud) +# +# Why the extra repair step: scrub rewrites CodeQL's workspace-relative URIs to +# absolute paths and writes the absolute source root into uriBaseId, which stops +# SonarQube mapping findings to files. It also deletes tool.extensions -- where +# CodeQL keeps its real rule metadata -- and rebuilds tool.driver.rules as +# id-only stubs, so every finding would otherwise import as MEDIUM regardless of +# its true severity. fix_sarif_paths.py restores both from the pre-scrub report. +# See .github/scripts/fix_sarif_paths.py for the measurements. +# +# Manual-first: dispatch this workflow to validate a change to the scan +# pipeline. workflow_dispatch checks out the ref you select, so it scans that +# branch's code. + +on: + workflow_dispatch: + + # Enable once the manual runs look right. Until then this stays off so scans + # are only ever triggered deliberately. + # push: + # branches: + # - main + +permissions: + contents: read + security-events: write + +jobs: + sonarqube: + name: SonarQube + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + # Shallow clones give SonarQube poorer relevancy on issue dating. + fetch-depth: 0 + + - name: Extract version from package.json + run: | + VERSION=$(node -p "require('./package.json').version") + echo "VERSION=$VERSION" >> $GITHUB_ENV + echo "Extracted version: $VERSION" + + # Pinned interpreter so `pip install nasa-scrub` lands in a clean, + # non-externally-managed environment on the runner. + - name: Set up Python + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: '3.12' + + - name: Initialize CodeQL + uses: github/codeql-action/init@78ed0c7291d93e40c51b085850dc669a4c3ab73b # v3 + with: + languages: javascript-typescript + build-mode: none + config-file: ./.github/codeql/codeql-config.yml + + - name: CodeQL Scan + uses: github/codeql-action/analyze@78ed0c7291d93e40c51b085850dc669a4c3ab73b # v3 + with: + category: /language:javascript-typescript + + - name: Post-Process Output + run: | + set -euo pipefail + python3 -m pip install --disable-pip-version-check nasa-scrub==3.0.2 + + results_dir=$(realpath "${{ github.workspace }}/../results") + workspace_path="${{ github.workspace }}" + + echo "SARIF files directory: $results_dir" + echo "Workspace path: $workspace_path" + echo "Available SARIF files:" + ls -lh "$results_dir"/*.sarif + + # Captured before the loop so the files written below are not picked + # up as inputs on a later iteration. + sarif_files=$(find "$results_dir" -name '*.sarif') + + for sarif_file in $sarif_files + do + base=$(basename "$sarif_file" .sarif) + stripped_file="$results_dir/${base}_stripped.sarif" + processed_file="$results_dir/${base}_processed.sarif" + echo "Processing: $(basename "$sarif_file")" + + # The AMMOS guide's step: reformat CodeQL output for SonarQube. + python3 -m scrub.tools.parsers.translate_results \ + "$sarif_file" "$stripped_file" "$workspace_path" sarifv2.1.0 + + # Repair the paths and rule metadata scrub degrades, using the + # original CodeQL report as the source of truth. + python3 .github/scripts/fix_sarif_paths.py \ + "$stripped_file" "$processed_file" "$workspace_path" \ + --reference "$sarif_file" + done + + sarif_list=$(find "$results_dir" -name '*_processed.sarif' | paste -sd, -) + + echo "RESULTS_DIR=$results_dir" >> $GITHUB_ENV + echo "SARIF_FILES=$sarif_list" >> $GITHUB_ENV + + echo "" + echo "SARIF files for SonarQube:" + echo "$sarif_list" + + # Makes the raw, scrubbed and repaired reports downloadable, which is how + # you check attribution after a run without re-running the scan. + - name: Upload Artifacts + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: codeql-artifacts + path: ${{ env.RESULTS_DIR }} + if-no-files-found: warn + retention-days: 14 + + # sonar.branch.name is deliberately NOT set for these first runs. Without + # it the scanner publishes to the project's main branch, so a manual + # dispatch from this feature branch is attributed to main rather than + # creating a second branch in SonarQube Cloud -- which is what we want + # while the branch carries the same code as main. + # + # Once the results look right, add: + # -Dsonar.branch.name=${{ github.ref_name }} + # so future scans are attributed to the branch actually being analyzed. + # Note that any dispatch from a branch with real code changes will + # otherwise overwrite main's analysis. + - name: SonarQube Scan + uses: SonarSource/sonarqube-scan-action@22918119ff8e1ca75a623e15c8296b6ea4fbe28f # v8.2.1 + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + with: + projectBaseDir: . + args: > + -Dsonar.sarifReportPaths=${{ env.SARIF_FILES }} + -Dsonar.projectVersion=${{ env.VERSION }} diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..03381ce --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,97 @@ +# SonarQube Cloud configuration for astria_ui +# +# Set up per the AMMOS scanning guide: +# https://wiki.jpl.nasa.gov/display/AmmosArch/Code+Vulnerability+Scanning+Set+Up+for+AMMOS+Open+Source+Software +# +# NOTE: confirm projectKey against the value shown under Information on the +# project's SonarQube Cloud Overview page before the first run -- +# https://sonarcloud.io/organizations/nasa-ammos/projects +sonar.projectKey=NASA-AMMOS_astria_ui +sonar.organization=nasa-ammos + +# Project metadata. projectVersion is overridden from package.json by the +# Security-Scan workflow. +sonar.projectName=astria_ui +sonar.projectVersion=8.7.0 +sonar.projectDescription=ASTRIA UI - React frontend for browsing, searching and analyzing planetary mission imagery. + +# Project links - shown in the SonarQube Cloud UI +sonar.links.scm=https://github.com/NASA-AMMOS/astria_ui +sonar.links.issue=https://github.com/NASA-AMMOS/astria_ui/issues +sonar.links.ci=https://github.com/NASA-AMMOS/astria_ui/actions + +# ========================================== +# Analysis scope +# ========================================== +# Our own code only: the React app (src/), the Express proxy (server.js) and +# the pages it serves, the build tooling, and the container definitions. +# +# Deliberately not listed: public/ (static assets and a vendored minified +# pdf.js worker we only host) and docs/. +sonar.sources=src,server.js,server,.dev,docker + +# Test files are intermixed with source, so they are named via +# sonar.test.exclusions below rather than sonar.tests. + +# Exclusions. +# +# src/externals/ is intentionally NOT excluded: it holds vendored third-party +# React components that are bundled into our application and are absent from +# package.json, so Dependabot does not track them. If we don't scan them, +# nothing does. Their own bundled dependencies and tests are excluded. +sonar.exclusions=\ + **/node_modules/**,\ + **/dist/**,\ + **/build/**,\ + **/coverage/**,\ + src/externals/**/node_modules/**,\ + src/externals/**/test/**,\ + **/*.min.js,\ + **/*.min.mjs,\ + **/*.min.css,\ + src/fonts/**,\ + src/images/**,\ + **/*.svg,\ + **/*.png,\ + **/*.ico,\ + **/*.woff,\ + **/*.woff2,\ + **/*.ttf + +# Test exclusions +sonar.test.exclusions=\ + src/test/**,\ + **/*.test.js,\ + **/*.test.jsx,\ + **/*.spec.js,\ + **/*.spec.jsx + +# Coverage exclusions - no coverage is currently published, but these should +# never count against it when it is. +sonar.coverage.exclusions=\ + src/test/**,\ + **/*.test.js,\ + **/*.test.jsx,\ + **/*.spec.js,\ + **/*.spec.jsx,\ + **/*.config.js,\ + .dev/**,\ + docker/** + +# Duplication exclusions - vendored code is not ours to de-duplicate. +sonar.cpd.exclusions=\ + src/externals/**,\ + **/*.min.js,\ + **/*.min.mjs + +# Language settings +sonar.javascript.file.suffixes=.js,.jsx,.mjs +sonar.sourceEncoding=UTF-8 + +sonar.tags=planetary,nasa,imagery,react,frontend,vite + +# New Code: anything added in the last 30 days counts as new. +sonar.leak.period=30 + +# sonar.sarifReportPaths is supplied by the Security-Scan workflow, which +# passes the repaired CodeQL reports on the scanner command line. From 3af1e899431066315ce429a4f25df96d8d6aa7cc Mon Sep 17 00:00:00 2001 From: Jeff Leach Date: Wed, 9 Sep 2026 11:46:58 -0700 Subject: [PATCH 2/5] ci: trigger the first scan from this branch workflow_dispatch alone cannot start this workflow yet. GitHub only exposes the "Run workflow" button for workflows that exist on the repository's default branch, and main has no workflows at all -- the Actions API reports zero workflows for the repo. So there is nothing to dispatch until this merges. Adding this branch to the push trigger runs the workflow from the branch's own copy, which is how the first scan can happen before the file reaches main. Mirrors the sonarcloud_action branch trigger in MMGIS. The entry comes out once this is on main. Claude-Session: https://claude.ai/code/session_01KKwLzyhbDQ2MyUuofXE25y --- .github/workflows/security-scan.yml | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml index 31499fb..af78279 100644 --- a/.github/workflows/security-scan.yml +++ b/.github/workflows/security-scan.yml @@ -25,11 +25,20 @@ name: Security-Scan on: workflow_dispatch: - # Enable once the manual runs look right. Until then this stays off so scans - # are only ever triggered deliberately. - # push: - # branches: - # - main + push: + branches: + # Bootstrap trigger. GitHub only offers a "Run workflow" button for + # workflow_dispatch once this file exists on the repository's default + # branch, and main currently has no workflows at all -- so until this + # merges, a dispatch cannot be started at all. Pushing this branch runs + # the workflow from the branch's own copy instead, which is how the + # first scan gets done before anything lands on main. + # + # Remove this entry once the workflow is on main. + - feature/sonarcloud-security-scan + + # Enable once the first runs look right, so merges to main are scanned. + # - main permissions: contents: read From 5debe52bdb26d346491b31949889f57721c1f683 Mon Sep 17 00:00:00 2001 From: Jeff Leach Date: Wed, 9 Sep 2026 11:56:01 -0700 Subject: [PATCH 3/5] ci: pin analysis to the main branch while validating Sets -Dsonar.branch.name=main so runs from this branch publish into main's analysis instead of creating a second branch in SonarQube Cloud. The branch carries the same code as main, so that is where the numbers belong. Leaving the property unset would likely behave the same way -- an unset branch name published to the main analysis on MMGIS in this org -- but that relies on the scanner's CI branch auto-detection staying inactive. Pinning does not rely on it: the failure mode becomes a failed step rather than an unwanted branch appearing in the project. Switch to ${{ github.ref_name }} once the pipeline is trusted. Claude-Session: https://claude.ai/code/session_01KKwLzyhbDQ2MyUuofXE25y --- .github/workflows/security-scan.yml | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml index af78279..2bcc90d 100644 --- a/.github/workflows/security-scan.yml +++ b/.github/workflows/security-scan.yml @@ -136,17 +136,22 @@ jobs: if-no-files-found: warn retention-days: 14 - # sonar.branch.name is deliberately NOT set for these first runs. Without - # it the scanner publishes to the project's main branch, so a manual - # dispatch from this feature branch is attributed to main rather than - # creating a second branch in SonarQube Cloud -- which is what we want - # while the branch carries the same code as main. + # sonar.branch.name is pinned to main on purpose while we validate the + # pipeline. This branch carries the same code as main, so the results + # belong on main's analysis -- and pinning guarantees a test run cannot + # create a second branch in SonarQube Cloud. # - # Once the results look right, add: + # Leaving it unset would probably do the same thing (an unset branch + # name published to the main analysis on MMGIS), but it depends on the + # scanner's CI branch auto-detection staying inactive. Setting it + # explicitly does not: the worst case is a failed step rather than a + # stray branch appearing. + # + # Once the results look right, change this to: # -Dsonar.branch.name=${{ github.ref_name }} - # so future scans are attributed to the branch actually being analyzed. - # Note that any dispatch from a branch with real code changes will - # otherwise overwrite main's analysis. + # so scans are attributed to the branch actually being analyzed. Until + # then, be aware that a run from a branch with real code changes will + # publish those numbers onto main. - name: SonarQube Scan uses: SonarSource/sonarqube-scan-action@22918119ff8e1ca75a623e15c8296b6ea4fbe28f # v8.2.1 env: @@ -156,3 +161,4 @@ jobs: args: > -Dsonar.sarifReportPaths=${{ env.SARIF_FILES }} -Dsonar.projectVersion=${{ env.VERSION }} + -Dsonar.branch.name=main From 597719489163f7cda8575714e2cef8394cfda940 Mon Sep 17 00:00:00 2001 From: Jeff Leach Date: Wed, 9 Sep 2026 14:53:48 -0700 Subject: [PATCH 4/5] ci: move actions off Node 20 and CodeQL Action v3 The first run warned on both counts: - actions/setup-python, actions/upload-artifact and both codeql-action steps target Node 20, which the runners now force onto Node 24. https://github.blog/changelog/2025-09-19-deprecation-of-node-20-on-github-actions-runners/ - CodeQL Action v3 is deprecated in December 2026. https://github.blog/changelog/2025-10-28-upcoming-deprecation-of-codeql-action-v3/ Bumped, keeping the SHA pinning: actions/setup-python v5.6.0 -> v7.0.0 actions/upload-artifact v4.6.2 -> v7.0.1 github/codeql-action v3 -> v4.38.0 (init and analyze) Each SHA was resolved from its release tag and the action manifest checked to confirm runs.using is node24. actions/checkout was already on v7.0.1/node24 and was not in the warning list, so it is unchanged. Checked for breakage rather than assuming: codeql-action v4.38.0 still defaults analyze `output` to ../results, which the Post-Process Output step depends on, and upload-artifact v7 still accepts name, path, if-no-files-found and retention-days. Claude-Session: https://claude.ai/code/session_01KKwLzyhbDQ2MyUuofXE25y --- .github/workflows/security-scan.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml index 2bcc90d..fb1b0ac 100644 --- a/.github/workflows/security-scan.yml +++ b/.github/workflows/security-scan.yml @@ -65,19 +65,19 @@ jobs: # Pinned interpreter so `pip install nasa-scrub` lands in a clean, # non-externally-managed environment on the runner. - name: Set up Python - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: '3.12' - name: Initialize CodeQL - uses: github/codeql-action/init@78ed0c7291d93e40c51b085850dc669a4c3ab73b # v3 + uses: github/codeql-action/init@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0 with: languages: javascript-typescript build-mode: none config-file: ./.github/codeql/codeql-config.yml - name: CodeQL Scan - uses: github/codeql-action/analyze@78ed0c7291d93e40c51b085850dc669a4c3ab73b # v3 + uses: github/codeql-action/analyze@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0 with: category: /language:javascript-typescript @@ -129,7 +129,7 @@ jobs: # you check attribution after a run without re-running the scan. - name: Upload Artifacts if: always() - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: name: codeql-artifacts path: ${{ env.RESULTS_DIR }} From f3b5353cf7a412766b18a236301607e4209a74d5 Mon Sep 17 00:00:00 2001 From: Jeff Leach Date: Fri, 11 Sep 2026 07:04:04 -0700 Subject: [PATCH 5/5] ci: scan pushes to main and every PR into main Brings this workflow in line with the MMGIS security-scan setup and retires the bootstrap triggers used to validate the pipeline: - push now triggers on main only, not on the feature branch - pull_request triggers on any PR targeting main, from any source branch - concurrency group per ref, cancelling superseded in-progress PR runs but never cancelling a run on main - the job is skipped for PRs from forks, which have no SONAR_TOKEN and would otherwise fail red for a reason the contributor cannot fix Branch attribution is now left to the scanner's GitHub Actions auto-detection: a push to main analyzes as main, and a PR analyzes as a SonarQube pull request, which expires with the PR rather than becoming a long-lived branch in the project. Only workflow_dispatch still pins -Dsonar.branch.name=main, so validating the pipeline by hand cannot leave a stray branch behind. Claude-Session: https://claude.ai/code/session_011WeGYwtRQbXf1wJE7YzvYW --- .github/workflows/security-scan.yml | 80 +++++++++++++++++------------ 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml index fb1b0ac..eab015a 100644 --- a/.github/workflows/security-scan.yml +++ b/.github/workflows/security-scan.yml @@ -16,29 +16,34 @@ name: Security-Scan # CodeQL keeps its real rule metadata -- and rebuilds tool.driver.rules as # id-only stubs, so every finding would otherwise import as MEDIUM regardless of # its true severity. fix_sarif_paths.py restores both from the pre-scrub report. -# See .github/scripts/fix_sarif_paths.py for the measurements. +# See .github/scripts/fix_sarif_paths.py for the measurements. The script is +# shared verbatim with mis_rest_service and tile_service -- fix it in one, copy +# to all. # -# Manual-first: dispatch this workflow to validate a change to the scan -# pipeline. workflow_dispatch checks out the ref you select, so it scans that -# branch's code. +# To validate a change to the scan pipeline, dispatch this workflow against the +# ref you want; see the branch-attribution note on the SonarQube Scan step for +# what that does to the published results. + +# One run per ref. A PR that gets new pushes supersedes its own in-progress +# run; pushes to main are never cancelled, so main's analysis history stays +# complete. +concurrency: + group: security-scan-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} on: workflow_dispatch: push: branches: - # Bootstrap trigger. GitHub only offers a "Run workflow" button for - # workflow_dispatch once this file exists on the repository's default - # branch, and main currently has no workflows at all -- so until this - # merges, a dispatch cannot be started at all. Pushing this branch runs - # the workflow from the branch's own copy instead, which is how the - # first scan gets done before anything lands on main. - # - # Remove this entry once the workflow is on main. - - feature/sonarcloud-security-scan - - # Enable once the first runs look right, so merges to main are scanned. - # - main + - main + + # Any PR targeting main, whatever the source branch is called. SonarQube Cloud + # records these as pull request analyses, which expire with the PR and never + # become long-lived branches in the project. + pull_request: + branches: + - main permissions: contents: read @@ -48,6 +53,12 @@ jobs: sonarqube: name: SonarQube runs-on: ubuntu-latest + # A PR from a fork runs without repository secrets, so SONAR_TOKEN would be + # empty and the scan would fail red for a reason the contributor cannot fix. + # Skip instead; maintainers still get a scan when the change lands on main. + if: >- + github.event_name != 'pull_request' || + github.event.pull_request.head.repo.full_name == github.repository steps: - name: Checkout repository @@ -136,22 +147,25 @@ jobs: if-no-files-found: warn retention-days: 14 - # sonar.branch.name is pinned to main on purpose while we validate the - # pipeline. This branch carries the same code as main, so the results - # belong on main's analysis -- and pinning guarantees a test run cannot - # create a second branch in SonarQube Cloud. - # - # Leaving it unset would probably do the same thing (an unset branch - # name published to the main analysis on MMGIS), but it depends on the - # scanner's CI branch auto-detection staying inactive. Setting it - # explicitly does not: the worst case is a failed step rather than a - # stray branch appearing. - # - # Once the results look right, change this to: - # -Dsonar.branch.name=${{ github.ref_name }} - # so scans are attributed to the branch actually being analyzed. Until - # then, be aware that a run from a branch with real code changes will - # publish those numbers onto main. + # Branch attribution. The scanner auto-detects this from the GitHub Actions + # environment in every case except a manual dispatch: + # push to main -> analyzed as main + # pull_request -> analyzed as a pull request; no branch is created + # workflow_dispatch -> pinned to main below, so validating a change to + # the pipeline from a feature branch cannot leave a + # stray branch behind in the SonarQube project. The + # tradeoff: dispatching from a branch that carries + # real code changes publishes those numbers onto + # main. Dispatch from a branch that matches main, or + # open a PR instead. + - name: Determine SonarQube branch argument + run: | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then + echo "SONAR_BRANCH_ARG=-Dsonar.branch.name=main" >> "$GITHUB_ENV" + else + echo "SONAR_BRANCH_ARG=" >> "$GITHUB_ENV" + fi + - name: SonarQube Scan uses: SonarSource/sonarqube-scan-action@22918119ff8e1ca75a623e15c8296b6ea4fbe28f # v8.2.1 env: @@ -161,4 +175,4 @@ jobs: args: > -Dsonar.sarifReportPaths=${{ env.SARIF_FILES }} -Dsonar.projectVersion=${{ env.VERSION }} - -Dsonar.branch.name=main + ${{ env.SONAR_BRANCH_ARG }}