Skip to content

fix(vulnerability): order advisories deterministically - #2900

Open
ruromero wants to merge 1 commit into
guacsec:mainfrom
ruromero:TC-7641
Open

ruromero wants to merge 1 commit into
guacsec:mainfrom
ruromero:TC-7641

Conversation

@ruromero

@ruromero ruromero commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • 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.
  • Backend counterpart to the client-side ordering introduced in fix(advisories): tie-break document_id sort deterministically trustify-ui#1442; no trustify-ui change is required, the UI memo can stay as harmless redundancy.

Implements TC-7641

Summary by Sourcery

Make vulnerability advisory ordering deterministic across all retrieval paths.

Bug Fixes:

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

Enhancements:

  • Apply consistent advisory ordering across the vulnerability endpoint, service layer, and ingestor query.

Tests:

  • Add endpoint and service coverage confirming identical advisory ordering for 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 vulnerability advisory summaries by document ID, advisory type, and UUID, and apply the same ordering in the ingestor query.

  • modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs L518-529, L534-541
  • modules/ingestor/src/graph/vulnerability/mod.rs L11-14, L244-248, L263-265

Tests

Test that endpoint and service responses keep the expected advisory order across forward and reverse ingestion orders.

  • modules/fundamental/src/vulnerability/endpoints/test.rs L642-741
  • modules/fundamental/src/vulnerability/service/test.rs L26-30, L1311-1413 (+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>
**Advisories appear in different orders**

When advisories sharing a document ID include one without a `type` label and another with a nonempty type, the SQL ordering treats the missing type as `NULL`, which PostgreSQL places after non-NULL values in ascending order, while `advisory_type` maps it to `""` and sorts it before nonempty types. The ingestor and endpoint therefore return different advisory sequences.

Use `COALESCE` in the SQL ordering expression so a missing type sorts like `""` and the UUID tie-break applies consistently.

Also at `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:526`, `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:535`, `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:539-540`.
</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 has a locale-sensitive default collation and linked advisories have text keys ordered differently by that collation than by Rust string comparison, postgreSQL orders these text keys using the database collation, while the endpoint uses Rust string ordering. The ingestor result can therefore disagree with the endpoint for document IDs or type labels whose locale order differs from byte order.

Apply `COLLATE "C"` to the SQL text sort keys so their order matches the endpoint.

Also at `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:521-527`.
</issue_to_address>

### Comment 3
<location path="modules/fundamental/src/vulnerability/service/test.rs" line_range="1391-1398" />
<code_context>
+        .iter()
+        .map(|advisory| {
+            (
+                advisory.head.head.document_id.clone(),
+                advisory
+                    .head
+                    .head
+                    .labels
+                    .get("type")
+                    .cloned()
+                    .unwrap_or_default(),
+            )
+        })
</code_context>
<issue_to_address>
**UUID ordering regressions go undetected**

When the UUID tie-break is removed or implemented incorrectly, the ordering tests compare only document ID and type, so both tests still pass when advisories with matching keys are returned in the wrong UUID order.

Include UUIDs in both test projections and assert their ordering.

Also at `modules/fundamental/src/vulnerability/endpoints/test.rs:718-728`.
</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/service/test.rs:1398


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 · Advisories appear in different orders

When advisories sharing a document ID include one without a type label and another with a nonempty type, the SQL ordering treats the missing type as NULL, which PostgreSQL places after non-NULL values in ascending order, while advisory_type maps it to "" and sorts it before nonempty types. The ingestor and endpoint therefore return different advisory sequences.

Use COALESCE in the SQL ordering expression so a missing type sorts like "" and the UUID tie-break applies consistently.

Also at modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:526, modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:535, modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:539-540.

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

**Advisories appear in different orders**

When advisories sharing a document ID include one without a `type` label and another with a nonempty type, the SQL ordering treats the missing type as `NULL`, which PostgreSQL places after non-NULL values in ascending order, while `advisory_type` maps it to `""` and sorts it before nonempty types. The ingestor and endpoint therefore return different advisory sequences.

Use `COALESCE` in the SQL ordering expression so a missing type sorts like `""` and the UUID tie-break applies consistently.

Also at `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:526`, `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:535`, `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:539-540`.

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 has a locale-sensitive default collation and linked advisories have text keys ordered differently by that collation than by Rust string comparison, postgreSQL orders these text keys using the database collation, while the endpoint uses Rust string ordering. The ingestor result can therefore disagree with the endpoint for document IDs or type labels whose locale order differs from byte order.

Apply COLLATE "C" to the SQL text sort keys so their order matches the endpoint.

Also at modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:521-527.

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

**Locale changes advisory order**

When the database has a locale-sensitive default collation and linked advisories have text keys ordered differently by that collation than by Rust string comparison, postgreSQL orders these text keys using the database collation, while the endpoint uses Rust string ordering. The ingestor result can therefore disagree with the endpoint for document IDs or type labels whose locale order differs from byte order.

Apply `COLLATE "C"` to the SQL text sort keys so their order matches the endpoint.

Also at `modules/fundamental/src/vulnerability/model/details/vulnerability_advisory.rs:521-527`.

Comment on lines +1391 to +1398
advisory.head.head.document_id.clone(),
advisory
.head
.head
.labels
.get("type")
.cloned()
.unwrap_or_default(),

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 ordering regressions go undetected

When the UUID tie-break is removed or implemented incorrectly, the ordering tests compare only document ID and type, so both tests still pass when advisories with matching keys are returned in the wrong UUID order.

Include UUIDs in both test projections and assert their ordering.

Also at modules/fundamental/src/vulnerability/endpoints/test.rs:718-728.

Prompt for AI agents
In `modules/fundamental/src/vulnerability/service/test.rs` at lines 1391-1398:

**UUID ordering regressions go undetected**

When the UUID tie-break is removed or implemented incorrectly, the ordering tests compare only document ID and type, so both tests still pass when advisories with matching keys are returned in the wrong UUID order.

Include UUIDs in both test projections and assert their ordering.

Also at `modules/fundamental/src/vulnerability/endpoints/test.rs:718-728`.

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.

1 participant