-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add Hex advisory source #54
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
dl-alexandre
wants to merge
4
commits into
mirego:main
Choose a base branch
from
dl-alexandre:feat/hex-advisory-source
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| defmodule MixAudit.HexAdvisories do | ||
| def advisories(config \\ :hex_core.default_config()) do | ||
| with {:ok, {200, _headers, %{packages: packages}}} <- :hex_repo.get_versions(config) do | ||
| packages | ||
| |> Enum.filter(&has_advisories?/1) | ||
| |> Enum.flat_map(&package_advisories(config, field(&1, :name))) | ||
| else | ||
| _ -> [] | ||
| end | ||
| end | ||
|
|
||
| def map_package_advisories(package, releases, advisories) do | ||
| advisories | ||
| |> Enum.with_index() | ||
| |> advisory_groups() | ||
| |> Enum.map(fn %{advisory: advisory, indexes: indexes} -> | ||
| affected_versions = affected_versions(releases, indexes) | ||
|
|
||
| %MixAudit.Advisory{ | ||
| id: advisory_id(advisory), | ||
| package: package, | ||
| disclosure_date: nil, | ||
| url: field(advisory, :html_url) || field(advisory, :api_url), | ||
| title: field(advisory, :summary) || advisory_id(advisory), | ||
| description: field(advisory, :summary) || "", | ||
| vulnerable_version_ranges: Enum.map(affected_versions, &"= #{&1}"), | ||
| first_patched_versions: first_patched_versions(releases, indexes, affected_versions), | ||
| severity: severity(advisory) | ||
| } | ||
| end) | ||
| |> Enum.reject(&Enum.empty?(&1.vulnerable_version_ranges)) | ||
| end | ||
|
|
||
| defp advisory_groups(indexed_advisories) do | ||
| Enum.reduce(indexed_advisories, [], fn {advisory, index}, groups -> | ||
| identifiers = identifiers(advisory) | ||
|
|
||
| case Enum.find_index(groups, &same_advisory?(&1.identifiers, identifiers)) do | ||
| nil -> | ||
| [ | ||
| %{ | ||
| advisory: advisory, | ||
| identifiers: identifiers, | ||
| indexes: [index] | ||
| } | ||
| | groups | ||
| ] | ||
|
|
||
| group_index -> | ||
| List.update_at(groups, group_index, fn group -> | ||
| %{ | ||
| group | ||
| | advisory: preferred_advisory(group.advisory, advisory), | ||
| identifiers: MapSet.union(group.identifiers, identifiers), | ||
| indexes: [index | group.indexes] | ||
| } | ||
| end) | ||
| end | ||
| end) | ||
| |> Enum.reverse() | ||
| end | ||
|
|
||
| defp package_advisories(config, package) do | ||
| case :hex_repo.get_package(config, package) do | ||
| {:ok, {200, _headers, %{releases: releases, advisories: advisories}}} -> | ||
| map_package_advisories(package, releases, advisories) | ||
|
|
||
| _ -> | ||
| [] | ||
| end | ||
| end | ||
|
|
||
| defp has_advisories?(package) do | ||
| package | ||
| |> field(:with_advisories) | ||
| |> empty?() | ||
| |> Kernel.not() | ||
| end | ||
|
|
||
| defp same_advisory?(left, right), do: not MapSet.disjoint?(left, right) | ||
|
|
||
| defp identifiers(advisory) do | ||
| advisory | ||
| |> aliases() | ||
| |> MapSet.new() | ||
| end | ||
|
|
||
| defp aliases(advisory) do | ||
| [field(advisory, :id) | field(advisory, :aliases) || []] | ||
| |> Enum.reject(&is_nil/1) | ||
| end | ||
|
|
||
| defp preferred_advisory(left, right) do | ||
| cond do | ||
| ghsa_id?(field(left, :id)) -> left | ||
| ghsa_id?(field(right, :id)) -> right | ||
| true -> left | ||
| end | ||
| end | ||
|
|
||
| defp ghsa_id?(id) when is_binary(id), do: String.starts_with?(id, "GHSA-") | ||
| defp ghsa_id?(_id), do: false | ||
|
|
||
| defp affected_versions(releases, advisory_indexes) do | ||
| releases | ||
| |> Enum.filter(&has_advisory_index?(&1, advisory_indexes)) | ||
| |> Enum.map(&field(&1, :version)) | ||
| end | ||
|
|
||
| defp first_patched_versions(_releases, _advisory_indexes, []), do: [] | ||
|
|
||
| defp first_patched_versions(releases, advisory_indexes, affected_versions) do | ||
| highest_affected_version = Enum.max(affected_versions, Version) | ||
|
|
||
| releases | ||
| |> Enum.reject(&has_advisory_index?(&1, advisory_indexes)) | ||
| |> Enum.map(&field(&1, :version)) | ||
| |> Enum.filter(&(Version.compare(&1, highest_affected_version) == :gt)) | ||
| |> Enum.sort(Version) | ||
| |> Enum.take(1) | ||
| end | ||
|
|
||
| defp has_advisory_index?(release, advisory_indexes) do | ||
| advisory_indexes = MapSet.new(advisory_indexes) | ||
|
|
||
| release | ||
| |> field(:advisory_indexes) | ||
| |> MapSet.new() | ||
| |> MapSet.disjoint?(advisory_indexes) | ||
| |> Kernel.not() | ||
| end | ||
|
|
||
| defp advisory_id(advisory) do | ||
| aliases(advisory) | ||
| |> Enum.find(&ghsa_id?/1) | ||
| |> Kernel.||(field(advisory, :id) || List.first(field(advisory, :aliases) || [])) | ||
| end | ||
|
|
||
| defp severity(advisory) do | ||
| case field(advisory, :severity) do | ||
| nil -> | ||
| nil | ||
|
|
||
| severity when is_atom(severity) -> | ||
| severity | ||
| |> Atom.to_string() | ||
| |> String.replace_prefix("SEVERITY_", "") | ||
| |> String.downcase() | ||
|
|
||
| severity -> | ||
| to_string(severity) | ||
| end | ||
| end | ||
|
|
||
| defp empty?(nil), do: true | ||
| defp empty?(collection), do: Enum.empty?(collection) | ||
|
|
||
| defp field(map, key) when is_map(map) do | ||
| Map.get(map, key) || Map.get(map, to_string(key)) | ||
| end | ||
| end | ||
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
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,83 @@ | ||
| defmodule MixAudit.HexAdvisoriesTest do | ||
| use ExUnit.Case | ||
|
|
||
| alias MixAudit.HexAdvisories | ||
|
|
||
| test "map_package_advisories/3 maps Hex advisory indexes to exact vulnerable versions" do | ||
| releases = [ | ||
| %{version: "0.1.0", advisory_indexes: [0]}, | ||
| %{version: "0.2.0", advisory_indexes: [0, 1]}, | ||
| %{version: "1.0.0", advisory_indexes: []} | ||
| ] | ||
|
|
||
| advisories = [ | ||
| %{ | ||
| id: "GHSA-1234", | ||
| severity: :SEVERITY_HIGH, | ||
| html_url: "https://osv.dev/vulnerability/GHSA-1234", | ||
| summary: "Vulnerable challenge handling" | ||
| }, | ||
| %{ | ||
| id: "GHSA-5678", | ||
| severity: "medium", | ||
| api_url: "https://api.osv.dev/v1/vulns/GHSA-5678", | ||
| summary: "Replay vulnerability" | ||
| } | ||
| ] | ||
|
|
||
| assert [ | ||
| %MixAudit.Advisory{ | ||
| id: "GHSA-1234", | ||
| package: "altcha", | ||
| severity: "high", | ||
| url: "https://osv.dev/vulnerability/GHSA-1234", | ||
| title: "Vulnerable challenge handling", | ||
| description: "Vulnerable challenge handling", | ||
| vulnerable_version_ranges: ["= 0.1.0", "= 0.2.0"], | ||
| first_patched_versions: ["1.0.0"] | ||
| }, | ||
| %MixAudit.Advisory{ | ||
| id: "GHSA-5678", | ||
| package: "altcha", | ||
| severity: "medium", | ||
| url: "https://api.osv.dev/v1/vulns/GHSA-5678", | ||
| title: "Replay vulnerability", | ||
| vulnerable_version_ranges: ["= 0.2.0"], | ||
| first_patched_versions: ["1.0.0"] | ||
| } | ||
| ] = HexAdvisories.map_package_advisories("altcha", releases, advisories) | ||
| end | ||
|
|
||
| test "map_package_advisories/3 collapses aliased OSV records" do | ||
| releases = [ | ||
| %{version: "1.0.0", advisory_indexes: [0]}, | ||
| %{version: "1.1.0", advisory_indexes: [0, 1]}, | ||
| %{version: "1.2.0", advisory_indexes: []} | ||
| ] | ||
|
|
||
| advisories = [ | ||
| %{ | ||
| id: "EEF-CVE-2026-39803", | ||
| aliases: ["CVE-2026-39803", "GHSA-9q9q-324x-93r2"], | ||
| severity: :SEVERITY_HIGH, | ||
| summary: "EEF record" | ||
| }, | ||
| %{ | ||
| id: "GHSA-9q9q-324x-93r2", | ||
| aliases: ["CVE-2026-39803", "EEF-CVE-2026-39803"], | ||
| severity: :SEVERITY_HIGH, | ||
| summary: "GHSA record" | ||
| } | ||
| ] | ||
|
|
||
| assert [ | ||
| %MixAudit.Advisory{ | ||
| id: "GHSA-9q9q-324x-93r2", | ||
| package: "bandit", | ||
| title: "GHSA record", | ||
| vulnerable_version_ranges: ["= 1.0.0", "= 1.1.0"], | ||
| first_patched_versions: ["1.2.0"] | ||
| } | ||
| ] = HexAdvisories.map_package_advisories("bandit", releases, advisories) | ||
| end | ||
| end |
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.