Skip to content

fix(vulnerability): order advisories deterministically (0.7.z backport) - #2901

Open
ruromero wants to merge 1 commit into
guacsec:release/0.7.zfrom
ruromero:TC-7641-backport-0.7.z
Open

ruromero wants to merge 1 commit into
guacsec:release/0.7.zfrom
ruromero:TC-7641-backport-0.7.z

Conversation

@ruromero

@ruromero ruromero commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Backport of #2900 to release/0.7.z for RHTPA 3.2.0 / stream 3.2.x.

  • Order the advisories returned for a vulnerability by document_id, then advisory type (labels.type), then advisory UUID so GET /v3/vulnerability/{id} no longer depends on database row order.
  • Apply the equivalent SQL ordering to VulnerabilityContext::advisories in the ingestor so that path is stable too.
  • Add endpoint and service tests that ingest two advisories sharing a document_id (different types) plus an unrelated advisory, asserting the exact sequence for both forward and reverse ingestion order.

Notes

  • The endpoint response is sorted where the response DTOs are assembled (VulnerabilityAdvisorySummary::from_entities), which keeps the tie-break portable instead of relying on PostgreSQL JSON ordering. The ingestor query uses the same three keys as an ORDER BY.
  • Only difference from fix(vulnerability): order advisories deterministically #2900: AdvisoryInformation on this branch has no publisher_namespace field, so the new test helper does not set it.

Implements TC-7641

Summary by Sourcery

Return vulnerability advisories in a stable, deterministic order across API and service paths.

Bug Fixes:

  • Ensure vulnerability advisories are returned in a deterministic order by document ID, advisory type, and advisory UUID, independent of database row or ingestion order.

Tests:

  • Add endpoint and service coverage confirming advisory ordering for both forward and reverse ingestion sequences.

The vulnerability details response listed advisories in whatever order the
database returned them, so advisories sharing a document ID (for example a
CSAF VEX and a CVE record) could swap between requests. Order them by
document ID, then advisory type, then advisory UUID - the same tie-break
consumers such as trustify-ui apply client-side.

VulnerabilityContext::advisories gets the equivalent SQL ordering so the
ingestor path is stable too.

Implements TC-7641

Assisted-by: Claude Code
@sourcery-ai

sourcery-ai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Sort assembled vulnerability advisory summaries by document ID, advisory type, and UUID so the response order is stable.

  • modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs L450-461, L466-473

Apply the equivalent three-key ordering in the ingestor’s vulnerability-advisory query.

  • modules/ingestor/src/graph/vulnerability/mod.rs L11-14, L244-248, L263-265

Tests

Add endpoint and service tests that check the exact advisory order for forward and reverse ingestion sequences.

  • modules/fundamental/src/vulnerability/endpoints/test.rs L641-739
  • modules/fundamental/src/vulnerability/service/test.rs L26-30, L1313-1414 (+2 small edits)

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="modules/ingestor/src/graph/vulnerability/mod.rs" line_range="264" />
<code_context>
                 advisory_vulnerability::Column::VulnerabilityId.eq(self.vulnerability.id.clone()),
             )
+            .order_by_asc(advisory::Column::DocumentId)
+            .order_by_asc(Expr::cust(r#""advisory"."labels" ->> 'type'"#))
+            .order_by_asc(advisory::Column::Id)
             .all(connection)
</code_context>
<issue_to_address>
**Advisory sequences differ by path**

When linked advisories share a document ID and one lacks `labels.type` while another has a non-empty type, or an empty type that sorts before it by UUID, `VulnerabilityContext::advisories` orders SQL `NULL` after non-NULL values, while `advisory_type` maps a missing type to `""` and the response sort uses UUID for ties. The ingestor and API therefore return different advisory sequences.

Coalesce the SQL type key to `''` so both paths sort missing and empty types identically before the UUID tie-break.

Also at `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:453`, `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:458`, `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:467-472`.
</issue_to_address>

### Comment 2
<location path="modules/ingestor/src/graph/vulnerability/mod.rs" line_range="263-264" />
<code_context>
             .filter(
                 advisory_vulnerability::Column::VulnerabilityId.eq(self.vulnerability.id.clone()),
             )
+            .order_by_asc(advisory::Column::DocumentId)
+            .order_by_asc(Expr::cust(r#""advisory"."labels" ->> 'type'"#))
+            .order_by_asc(advisory::Column::Id)
             .all(connection)
</code_context>
<issue_to_address>
**Locale changes advisory order**

When the database uses a locale-aware collation and linked advisories have document IDs or type labels whose locale order differs from Rust's bytewise order, postgreSQL applies the database collation to these text sort keys, while the response sorter uses Rust string ordering. Under a locale-aware collation, advisories with document IDs or types whose ordering differs from bytewise order appear in a different sequence in the ingestor path than in the API response.

Apply `COLLATE "C"` to the SQL text sort keys so their ordering matches Rust string comparison.
</issue_to_address>

### Comment 3
<location path="modules/fundamental/src/vulnerability/endpoints/test.rs" line_range="716-726" />
<code_context>
+        .expect("advisories must be an array")
+        .iter()
+        .map(|advisory| {
+            (
+                advisory["document_id"]
+                    .as_str()
+                    .unwrap_or_default()
+                    .to_string(),
+                advisory["labels"]["type"]
+                    .as_str()
+                    .unwrap_or_default()
+                    .to_string(),
+            )
+        })
+        .collect();
+
</code_context>
<issue_to_address>
**UUID tie-break tests miss duplicates**

When two linked advisories have the same document ID and type, the endpoint and service tests compare only document ID and type, and their fixtures do not contain a repeated pair. Removing the UUID tie-break therefore still passes the tests, leaving callers without the tested ordering guarantee.

Add advisories with the same document ID and type to the fixtures, and include UUIDs in the collected values and assertions.

Also at `modules/fundamental/src/vulnerability/service/test.rs:1386-1411`.
</issue_to_address>

Sourcery assessment

Approval pending. 3 findings to address first.

Blocking findings: modules/ingestor/src/graph/vulnerability/mod.rs:264, modules/ingestor/src/graph/vulnerability/mod.rs:264, modules/fundamental/src/vulnerability/endpoints/test.rs:726


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

advisory_vulnerability::Column::VulnerabilityId.eq(self.vulnerability.id.clone()),
)
.order_by_asc(advisory::Column::DocumentId)
.order_by_asc(Expr::cust(r#""advisory"."labels" ->> 'type'"#))

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.

🟡 Medium · Advisory sequences differ by path

When linked advisories share a document ID and one lacks labels.type while another has a non-empty type, or an empty type that sorts before it by UUID, VulnerabilityContext::advisories orders SQL NULL after non-NULL values, while advisory_type maps a missing type to "" and the response sort uses UUID for ties. The ingestor and API therefore return different advisory sequences.

Coalesce the SQL type key to '' so both paths sort missing and empty types identically before the UUID tie-break.

Also at modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:453, modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:458, modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:467-472.

Prompt for AI agents
In `modules/ingestor/src/graph/vulnerability/mod.rs` at line 264:

**Advisory sequences differ by path**

When linked advisories share a document ID and one lacks `labels.type` while another has a non-empty type, or an empty type that sorts before it by UUID, `VulnerabilityContext::advisories` orders SQL `NULL` after non-NULL values, while `advisory_type` maps a missing type to `""` and the response sort uses UUID for ties. The ingestor and API therefore return different advisory sequences.

Coalesce the SQL type key to `''` so both paths sort missing and empty types identically before the UUID tie-break.

Also at `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:453`, `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:458`, `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:467-472`.

Comment on lines +263 to +264
.order_by_asc(advisory::Column::DocumentId)
.order_by_asc(Expr::cust(r#""advisory"."labels" ->> 'type'"#))

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.

🟡 Medium · Locale changes advisory order

When the database uses a locale-aware collation and linked advisories have document IDs or type labels whose locale order differs from Rust's bytewise order, postgreSQL applies the database collation to these text sort keys, while the response sorter uses Rust string ordering. Under a locale-aware collation, advisories with document IDs or types whose ordering differs from bytewise order appear in a different sequence in the ingestor path than in the API response.

Apply COLLATE "C" to the SQL text sort keys so their ordering matches Rust string comparison.

Prompt for AI agents
In `modules/ingestor/src/graph/vulnerability/mod.rs` at lines 263-264:

**Locale changes advisory order**

When the database uses a locale-aware collation and linked advisories have document IDs or type labels whose locale order differs from Rust's bytewise order, postgreSQL applies the database collation to these text sort keys, while the response sorter uses Rust string ordering. Under a locale-aware collation, advisories with document IDs or types whose ordering differs from bytewise order appear in a different sequence in the ingestor path than in the API response.

Apply `COLLATE "C"` to the SQL text sort keys so their ordering matches Rust string comparison.

Comment on lines +716 to +726
(
advisory["document_id"]
.as_str()
.unwrap_or_default()
.to_string(),
advisory["labels"]["type"]
.as_str()
.unwrap_or_default()
.to_string(),
)
})

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.

🟡 Medium · UUID tie-break tests miss duplicates

When two linked advisories have the same document ID and type, the endpoint and service tests compare only document ID and type, and their fixtures do not contain a repeated pair. Removing the UUID tie-break therefore still passes the tests, leaving callers without the tested ordering guarantee.

Add advisories with the same document ID and type to the fixtures, and include UUIDs in the collected values and assertions.

Also at modules/fundamental/src/vulnerability/service/test.rs:1386-1411.

Prompt for AI agents
In `modules/fundamental/src/vulnerability/endpoints/test.rs` at lines 716-726:

**UUID tie-break tests miss duplicates**

When two linked advisories have the same document ID and type, the endpoint and service tests compare only document ID and type, and their fixtures do not contain a repeated pair. Removing the UUID tie-break therefore still passes the tests, leaving callers without the tested ordering guarantee.

Add advisories with the same document ID and type to the fixtures, and include UUIDs in the collected values and assertions.

Also at `modules/fundamental/src/vulnerability/service/test.rs:1386-1411`.

@gildub

gildub commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@ruromero, I'm not sure why this one was created prior the fix on the main branch ?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants