-
Notifications
You must be signed in to change notification settings - Fork 38
fix: sort CVE records correctly #2020
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
Open
lcarva
wants to merge
1
commit into
guacsec:main
Choose a base branch
from
lcarva:fix-cve-ordering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,61 @@ | ||
| use sea_orm_migration::prelude::*; | ||
|
|
||
| #[derive(DeriveMigrationName)] | ||
| pub struct Migration; | ||
|
|
||
| #[async_trait::async_trait] | ||
| #[allow(deprecated)] | ||
| impl MigrationTrait for Migration { | ||
| async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
| // Add a generated column that stores the normalized sort key | ||
| // This pads numeric segments with zeros to achieve numeric sorting: | ||
| // 1. First, prepend 19 zeros to each numeric segment | ||
| // 2. Then, keep only the 19 right-most digits for each numeric segment | ||
| // The number 19 is used as that is the largest segment defined in the CVE ID spec. | ||
| // | ||
| // Using a STORED generated column means: | ||
| // - The value is computed once when the row is inserted/updated | ||
| // - It's physically stored in the table | ||
| // - Indexes on this column work like regular column indexes | ||
| // - PostgreSQL automatically computes values for all existing rows during migration | ||
| // (this may take some time on large tables, but is a one-time operation) | ||
| manager | ||
| .get_connection() | ||
| .execute_unprepared( | ||
| "ALTER TABLE vulnerability | ||
| ADD COLUMN id_sort_key TEXT GENERATED ALWAYS AS ( | ||
| REGEXP_REPLACE( | ||
| REGEXP_REPLACE(id, '\\y([0-9]+)\\y', '0000000000000000000\\1', 'g'), | ||
| '\\y([0-9]+)([0-9]{19})\\y', | ||
| '\\2', | ||
| 'g' | ||
| ) | ||
| ) STORED", | ||
| ) | ||
| .await?; | ||
|
|
||
| // Create an index on the generated column for efficient sorting | ||
| manager | ||
| .get_connection() | ||
| .execute_unprepared( | ||
| "CREATE INDEX vulnerability_id_sort_key_idx ON vulnerability (id_sort_key)", | ||
| ) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
| manager | ||
| .get_connection() | ||
| .execute_unprepared("DROP INDEX IF EXISTS vulnerability_id_sort_key_idx") | ||
| .await?; | ||
|
|
||
| manager | ||
| .get_connection() | ||
| .execute_unprepared("ALTER TABLE vulnerability DROP COLUMN IF EXISTS id_sort_key") | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
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
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 |
|---|---|---|
|
|
@@ -555,6 +555,82 @@ async fn vulnerability_queries(ctx: &TrustifyContext) -> Result<(), anyhow::Erro | |
| Ok(()) | ||
| } | ||
|
|
||
| #[test_context(TrustifyContext)] | ||
| #[test(tokio::test)] | ||
| async fn vulnerability_numeric_sorting(ctx: &TrustifyContext) -> Result<(), anyhow::Error> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good to have that. I'm not happy about the repetition. I think we could rewrite this as: for id in [
"ID1",
"ID2",
] {
ctx.graph.ingest_vulnerability(id, (), &ctx.db).await?;
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| let service = VulnerabilityService::new(); | ||
|
|
||
| // Test various OSV ID formats, including edge cases, to ensure generic numeric sorting works | ||
| for id in [ | ||
| "CVE-2024-40000", | ||
| "CVE-2024-10288000", | ||
| "GHSA-r9p9-mrjm-926w", | ||
| "GHSA-vp9c-fpxx-744v", | ||
| "GO-2024-268", | ||
| "GO-2024-1234", | ||
| "RUSTSEC-2019-0033", | ||
| "RUSTSEC-2024-0001", | ||
| "ALPINE-12345", | ||
| "ALPINE-6789", | ||
| "PYSEC-2021-1234", | ||
| "PYSEC-2024-5678", | ||
| "OSV-2020-111", | ||
| "OSV-2020-58", | ||
| "ABC-xxxx-yyyy", | ||
| "12345", | ||
| "NOPE", | ||
| ] { | ||
| ctx.graph.ingest_vulnerability(id, (), &ctx.db).await?; | ||
| } | ||
|
|
||
| const EXPECTED_ASC: &[&str] = &[ | ||
| "12345", | ||
| "ABC-xxxx-yyyy", | ||
| "ALPINE-6789", | ||
| "ALPINE-12345", | ||
| "CVE-2024-40000", | ||
| "CVE-2024-10288000", | ||
| "GHSA-r9p9-mrjm-926w", | ||
| "GHSA-vp9c-fpxx-744v", | ||
| "GO-2024-268", | ||
| "GO-2024-1234", | ||
| "NOPE", | ||
| "OSV-2020-58", | ||
| "OSV-2020-111", | ||
| "PYSEC-2021-1234", | ||
| "PYSEC-2024-5678", | ||
| "RUSTSEC-2019-0033", | ||
| "RUSTSEC-2024-0001", | ||
| ]; | ||
|
|
||
| // Test ascending sort | ||
| let vulns = service | ||
| .fetch_vulnerabilities( | ||
| q("").sort("id:asc"), | ||
| Paginated::default(), | ||
| Default::default(), | ||
| &ctx.db, | ||
| ) | ||
| .await?; | ||
| let ids: Vec<_> = vulns.items.iter().map(|v| v.head.identifier.as_str()).collect(); | ||
| assert_eq!(EXPECTED_ASC, ids); | ||
|
|
||
| // Test descending sort | ||
| let vulns = service | ||
| .fetch_vulnerabilities( | ||
| q("").sort("id:desc"), | ||
| Paginated::default(), | ||
| Default::default(), | ||
| &ctx.db, | ||
| ) | ||
| .await?; | ||
| let ids: Vec<_> = vulns.items.iter().map(|v| v.head.identifier.as_str()).collect(); | ||
| let expected_desc: Vec<_> = EXPECTED_ASC.iter().rev().copied().collect(); | ||
| assert_eq!(expected_desc, ids); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test_context(TrustifyContext)] | ||
| #[test(tokio::test)] | ||
| async fn analyze_purls(ctx: &TrustifyContext) -> Result<(), anyhow::Error> { | ||
|
|
||
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
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.