Skip to content

ci: add CodeQL + SonarQube Cloud security scanning - #1

Merged
jl-0 merged 3 commits into
mainfrom
feature/sonarcloud-security-scan
Oct 9, 2026
Merged

jl-0 merged 3 commits into
mainfrom
feature/sonarcloud-security-scan

Conversation

@jl-0

@jl-0 jl-0 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds the AMMOS code vulnerability scanning pipeline:

CodeQL -> nasa-scrub translate_results -> fix_sarif_paths.py -> SonarQube Cloud

Following the AMMOS scanning guide.

File Purpose
.github/workflows/security-scan.yml The pipeline
.github/codeql/codeql-config.yml CodeQL query suite and analysis scope
.github/scripts/fix_sarif_paths.py Repairs the SARIF that nasa-scrub degrades
.github/scripts/test_fix_sarif_paths.py 24 unit tests for the above
sonar-project.properties SonarQube Cloud project settings and scope

Why the SARIF repair step exists

nasa-scrub's translate_results is the step the AMMOS guide prescribes, and it is kept for compliance, but it is lossy. It 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 imports as MEDIUM regardless of its true severity.

fix_sarif_paths.py restores both from the pre-scrub report. It is shared verbatim across astria_ui, mis_rest_service and tile_service — fix it in one, copy to all.

Triggers

  • push to main — analyzed as main
  • pull request into main, from any source branch — analyzed as a SonarQube pull request, which expires with the PR and never becomes a long-lived branch in the project
  • workflow_dispatch — pins -Dsonar.branch.name=main so validating the pipeline by hand cannot leave a stray branch behind

PRs from forks are skipped: they run without repository secrets, so SONAR_TOKEN would be empty and the scan would fail red for a reason the contributor cannot fix.

A concurrency group per ref supersedes in-progress PR runs when new commits arrive, and never cancels a run on main.

Build mode is chosen per run

Each run first asks Maven to resolve every dependency in pom.xml (mvn -B -q dependency:resolve):

resolves  -> CodeQL build-mode: manual, mvn -DskipTests compile, SonarQube gets target/classes
fails     -> CodeQL build-mode: none,   notice only (check stays green), SonarQube runs on source alone

While a dependency is not yet publicly available, scans run source-only with reduced precision on anything needing resolved types. Once every dependency is published, scans switch to the full build on their own; nothing in the workflow or sonar-project.properties needs to change. After that, a genuine compile failure turns the check red. Any resolution failure (including a network error or Maven Central rate limit) also falls back to source-only.

SONAR_TOKEN never shares a job with Maven

The workflow is two jobs, so PR-controlled build code (plugins, dependencies) never runs alongside the token:

  • codeql (no secrets): probe, optional build, CodeQL, nasa-scrub, fix_sarif_paths.py; uploads the processed SARIF and, if built, target/classes as artifacts.
  • sonarqube (needs: codeql): checks out, reads the version from pom.xml with sed, downloads the artifacts, runs the scanner. Never runs Maven.

Same structure as tile_service#1; the workflow differs only in repo-name comments.

Verified

A scan of this branch pinned to main ran green and imported cleanly:

CodeQL findings imported 58
Rule namespace external_CodeQL (not external_java) — driver.name restored
Severities 35 CRITICAL, 23 MAJOR — real severities, not a wall of MEDIUM
Unlocated findings 0 — paths restored
SonarQube branches after the run main only

The two-job workflow (755c585) also ran green on this PR in source-only mode.

Link to Devin session: https://nasa-jpl-demo.devinenterprise.com/sessions/86093dc594e44c788862ddb10afd3903
Open in Devin Desktop: https://nasa-jpl-demo.devinenterprise.com/desktop/session/86093dc594e44c788862ddb10afd3903?variant=devin
Requested by: @jl-0


Devin Review

Adds the AMMOS scanning pipeline already running on astria_ui:

  CodeQL -> nasa-scrub translate_results -> fix_sarif_paths.py -> SonarQube Cloud

fix_sarif_paths.py is copied verbatim from astria_ui; it repairs the file
paths and rule metadata that nasa-scrub degrades, without which every
finding imports unlocated and at MEDIUM severity.

CodeQL runs with build-mode: none and SonarQube's Java analyzer runs
without bytecode, because this project's gov.nasa.jpl.ammos.ids
dependencies live in JPL Artifactory and cannot be resolved from a public
runner. Both still produce findings, with reduced precision on anything
needing resolved types.

While the pipeline is validated, the workflow triggers on a push to this
branch and pins -Dsonar.branch.name=main, so test runs land on main's
analysis instead of creating a second branch in the SonarQube project.
Both are flagged in-file for removal once the results look right.

Claude-Session: https://claude.ai/code/session_011WeGYwtRQbXf1wJE7YzvYW
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

@martyvona martyvona left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the comments I left for NASA-AMMOS/tile_service#1 also apply here

…t of the Maven job

Ports tile_service#1's Security-Scan changes:
- Probe pom.xml dependencies against public repositories. If they all
  resolve, CodeQL runs with build-mode manual and the project is
  compiled; otherwise fall back to build-mode none as before.
- Split into a codeql job (probe, build, analyze, repair SARIF; no
  secrets) and a sonarqube job that only downloads the SARIF and any
  compiled classes and runs the scanner.
- Drop the JPL Artifactory references and stop naming a specific
  dependency as the build blocker.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 potential issues.

2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +161 to +164
if force_level or 'level' not in result:
level = (rules[index].get('defaultConfiguration') or {}).get('level')
if level:
result['level'] = level

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Explicit finding severity lost during repair

When a CodeQL finding overrides its rule's default, apply_rules assigns the default instead of the finding's original level. The finding imports with the wrong severity.

Learn more

A SARIF result can set its own level, independently of its rule's defaultConfiguration.level. Scrub flattens levels in the processed report, so restoring the rule default fixes only results that originally inherited it. The original result levels remain available in the reference report loaded by fix_sarif, but restore_rules passes only its rules to apply_rules. An explicitly overridden result therefore loses its severity.

Example: The reference rule defaults to warning, but its result explicitly says error. Scrub writes warning; repair writes warning again instead of restoring error.

Recommended fix: Match each processed result to its original result using a reliable stable identifier or preserved ordering, and restore an explicit reference level when present. Apply the rule default only when the reference result has no explicit level; add a test covering both cases.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferring this one, same as on tile_service#1. nasa-scrub writes the same flat level for every result, so force_level=True puts back the rule default deliberately. To also keep per-result overrides, each scrubbed result has to be matched to its original reliably. This script is shared verbatim with astria_ui and tile_service, so that fix (with tests) should go into all three repos together rather than only this PR.

Written by Devin

Comment on lines +78 to +80
if: >-
github.event_name != 'pull_request' ||
github.event.pull_request.head.repo.full_name == github.repository

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟨 Fork contributions bypass pre-merge scanning

For fork pull requests, codeql skips despite needing no secrets. sonarqube depends on that job, so neither scan checks those changes before merge.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct: fork PRs skip both jobs, and the split doesn't change that. codeql doesn't need secrets, but a fork PR's GITHUB_TOKEN is read-only. That means security-events: write isn't granted, so CodeQL's upload to code scanning would fail. Without SONAR_TOKEN, sonarqube can't run either. Fork changes are still scanned once merged, on the push to main.

Scanning forks before merge would mean running codeql with upload: never for forks and keeping the SARIF as an artifact only. tile_service#1 merged with the same behavior, so if we want that, it should be a follow-up across the repos.

Written by Devin

@sonarqubecloud

sonarqubecloud Bot commented Oct 9, 2026

Copy link
Copy Markdown

@jl-0
jl-0 merged commit 36f9d9e into main Oct 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants