save progress
This commit is contained in:
@@ -269,16 +269,18 @@ public sealed class PostgresAdvisoryStore : IPostgresAdvisoryStore, AdvisoryCont
|
||||
}
|
||||
}
|
||||
|
||||
var normalizedVersions = BuildNormalizedVersions(versionRanges);
|
||||
var (platform, normalizedVersions) = ReadDatabaseSpecific(a.DatabaseSpecific);
|
||||
var effectivePlatform = platform ?? ResolvePlatformFromRanges(versionRanges);
|
||||
var resolvedNormalizedVersions = normalizedVersions ?? BuildNormalizedVersions(versionRanges);
|
||||
|
||||
return new AffectedPackage(
|
||||
MapEcosystemToType(a.Ecosystem),
|
||||
a.PackageName,
|
||||
null,
|
||||
effectivePlatform,
|
||||
versionRanges,
|
||||
Array.Empty<AffectedPackageStatus>(),
|
||||
Array.Empty<AdvisoryProvenance>(),
|
||||
normalizedVersions);
|
||||
resolvedNormalizedVersions);
|
||||
}).ToArray();
|
||||
|
||||
// Parse provenance if available
|
||||
@@ -391,7 +393,7 @@ public sealed class PostgresAdvisoryStore : IPostgresAdvisoryStore, AdvisoryCont
|
||||
"pub" => "semver",
|
||||
"rpm" => "rpm",
|
||||
"deb" => "deb",
|
||||
"apk" => "semver",
|
||||
"apk" => "apk",
|
||||
"cpe" => "cpe",
|
||||
"vendor" => "vendor",
|
||||
"ics" => "ics-vendor",
|
||||
@@ -399,4 +401,75 @@ public sealed class PostgresAdvisoryStore : IPostgresAdvisoryStore, AdvisoryCont
|
||||
_ => "semver"
|
||||
};
|
||||
}
|
||||
|
||||
private static (string? Platform, IReadOnlyList<NormalizedVersionRule>? NormalizedVersions) ReadDatabaseSpecific(string? databaseSpecific)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(databaseSpecific) || databaseSpecific == "{}")
|
||||
{
|
||||
return (null, null);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var document = JsonDocument.Parse(databaseSpecific);
|
||||
var root = document.RootElement;
|
||||
|
||||
string? platform = null;
|
||||
if (root.TryGetProperty("platform", out var platformValue) && platformValue.ValueKind == JsonValueKind.String)
|
||||
{
|
||||
platform = platformValue.GetString();
|
||||
}
|
||||
|
||||
IReadOnlyList<NormalizedVersionRule>? normalizedVersions = null;
|
||||
if (root.TryGetProperty("normalizedVersions", out var normalizedValue) && normalizedValue.ValueKind == JsonValueKind.Array)
|
||||
{
|
||||
normalizedVersions = JsonSerializer.Deserialize<NormalizedVersionRule[]>(normalizedValue.GetRawText(), JsonOptions);
|
||||
}
|
||||
|
||||
return (platform, normalizedVersions);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return (null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static string? ResolvePlatformFromRanges(IEnumerable<AffectedVersionRange> ranges)
|
||||
{
|
||||
foreach (var range in ranges)
|
||||
{
|
||||
var extensions = range.Primitives?.VendorExtensions;
|
||||
if (extensions is null || extensions.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (extensions.TryGetValue("debian.release", out var debRelease) && !string.IsNullOrWhiteSpace(debRelease))
|
||||
{
|
||||
return debRelease;
|
||||
}
|
||||
|
||||
if (extensions.TryGetValue("ubuntu.release", out var ubuntuRelease) && !string.IsNullOrWhiteSpace(ubuntuRelease))
|
||||
{
|
||||
return ubuntuRelease;
|
||||
}
|
||||
|
||||
if (extensions.TryGetValue("alpine.distroversion", out var alpineRelease) && !string.IsNullOrWhiteSpace(alpineRelease))
|
||||
{
|
||||
if (extensions.TryGetValue("alpine.repo", out var alpineRepo) && !string.IsNullOrWhiteSpace(alpineRepo))
|
||||
{
|
||||
return $"{alpineRelease}/{alpineRepo}";
|
||||
}
|
||||
|
||||
return alpineRelease;
|
||||
}
|
||||
|
||||
if (extensions.TryGetValue("suse.platform", out var susePlatform) && !string.IsNullOrWhiteSpace(susePlatform))
|
||||
{
|
||||
return susePlatform;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +99,7 @@ public sealed class AdvisoryConverter
|
||||
{
|
||||
var ecosystem = MapTypeToEcosystem(pkg.Type);
|
||||
var versionRangeJson = JsonSerializer.Serialize(pkg.VersionRanges, JsonOptions);
|
||||
var databaseSpecificJson = BuildDatabaseSpecific(pkg);
|
||||
|
||||
affectedEntities.Add(new AdvisoryAffectedEntity
|
||||
{
|
||||
@@ -110,7 +111,7 @@ public sealed class AdvisoryConverter
|
||||
VersionRange = versionRangeJson,
|
||||
VersionsAffected = null,
|
||||
VersionsFixed = ExtractFixedVersions(pkg.VersionRanges),
|
||||
DatabaseSpecific = null,
|
||||
DatabaseSpecific = databaseSpecificJson,
|
||||
CreatedAt = now
|
||||
});
|
||||
}
|
||||
@@ -245,6 +246,29 @@ public sealed class AdvisoryConverter
|
||||
_ => null
|
||||
};
|
||||
|
||||
private static string? BuildDatabaseSpecific(AffectedPackage package)
|
||||
{
|
||||
if (package is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var payload = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
if (!string.IsNullOrWhiteSpace(package.Platform))
|
||||
{
|
||||
payload["platform"] = package.Platform;
|
||||
}
|
||||
|
||||
if (!package.NormalizedVersions.IsEmpty)
|
||||
{
|
||||
payload["normalizedVersions"] = package.NormalizedVersions;
|
||||
}
|
||||
|
||||
return payload.Count == 0
|
||||
? null
|
||||
: JsonSerializer.Serialize(payload, JsonOptions);
|
||||
}
|
||||
|
||||
private static string[]? ExtractFixedVersions(IEnumerable<AffectedVersionRange> ranges)
|
||||
{
|
||||
var fixedVersions = ranges
|
||||
|
||||
Reference in New Issue
Block a user