Skip to content

Commit fc53815

Browse files
authored
Merge pull request #20 from advanced-security/copilot/update-malware-advisory-interface
Refactor type assertions in malwareMatcher.ts with proper type aliases
2 parents 5e9b55f + 56b5698 commit fc53815

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

package-lock.json

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/malwareMatcher.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import path from "path";
66
import zlib from "zlib";
77
import * as semver from "semver";
88

9+
// Type alias to improve readability for complex package shape
10+
type PkgLike = {
11+
purl?: string;
12+
externalRefs?: Array<{ referenceType?: string; referenceLocator?: string }>;
13+
name?: string;
14+
version?: string;
15+
versionInfo?: string;
16+
};
17+
918
const our_tool_name = "SBOM Toolkit";
1019
const our_tool_url = "https://github.com/advanced-security/github-sbom-toolkit";
1120

@@ -23,6 +32,12 @@ export interface MalwareMatch {
2332
reason: string;
2433
}
2534

35+
// Type alias for packages used in enumeratePackages
36+
export type EnumeratedPackage = SbomPackage & {
37+
externalRefs?: { referenceType?: string; referenceLocator?: string }[];
38+
versionInfo?: string;
39+
};
40+
2641
// Map GitHub ecosystem enums to purl types
2742
const ecosystemToPurlType: Record<string, string> = {
2843
ACTIONS: "githubactions",
@@ -112,11 +127,11 @@ export function matchMalware(advisories: MalwareAdvisoryNode[], sboms: Repositor
112127
const index = new Map<string, MalwareAdvisoryNode[]>();
113128
for (const adv of advisories) {
114129
// Ignore advisories that have been withdrawn
115-
if ((adv as unknown as { withdrawnAt?: string | null }).withdrawnAt) continue;
130+
if (adv.withdrawnAt) continue;
116131
// Ignore advisories older than cutoff (must be before cutoff in BOTH publishedAt & updatedAt to be excluded)
117132
if (cutoffDate) {
118-
const pub = new Date((adv as unknown as { publishedAt?: string }).publishedAt || 0);
119-
const upd = new Date((adv as unknown as { updatedAt?: string }).updatedAt || 0);
133+
const pub = new Date(adv.publishedAt || 0);
134+
const upd = new Date(adv.updatedAt || 0);
120135
if (pub < cutoffDate && upd < cutoffDate) continue;
121136
}
122137
for (const vuln of adv.vulnerabilities) {
@@ -131,12 +146,12 @@ export function matchMalware(advisories: MalwareAdvisoryNode[], sboms: Repositor
131146
}
132147

133148
// Helper to enumerate packages with fallback to raw SPDX packages inside repoSbom.sbom if flattened list empty
134-
const enumeratePackages = (repo: RepositorySbom): Array<SbomPackage & { externalRefs?: { referenceType?: string; referenceLocator?: string }[]; versionInfo?: string }> => {
149+
const enumeratePackages = (repo: RepositorySbom): EnumeratedPackage[] => {
135150
const explicit: SbomPackage[] = Array.isArray(repo.packages) ? repo.packages : [];
136-
if (explicit.length > 0) return explicit as Array<SbomPackage & { externalRefs?: { referenceType?: string; referenceLocator?: string }[]; versionInfo?: string }>;
151+
if (explicit.length > 0) return explicit as EnumeratedPackage[];
137152
const rawMaybe: unknown = repo.sbom?.packages;
138153
if (Array.isArray(rawMaybe)) {
139-
return rawMaybe as Array<SbomPackage & { externalRefs?: { referenceType?: string; referenceLocator?: string }[]; versionInfo?: string }>;
154+
return rawMaybe as EnumeratedPackage[];
140155
}
141156
return [];
142157
};
@@ -146,7 +161,7 @@ export function matchMalware(advisories: MalwareAdvisoryNode[], sboms: Repositor
146161
if (!pkgs.length) continue;
147162

148163
for (const pkg of pkgs) {
149-
const pkgAny = pkg as unknown as { purl?: string; externalRefs?: Array<{ referenceType?: string; referenceLocator?: string }>; name?: string; version?: string; versionInfo?: string };
164+
const pkgAny = pkg as unknown as PkgLike;
150165
const candidatePurls = new Set<string>();
151166
if (pkgAny.purl) candidatePurls.add(pkgAny.purl);
152167
if (Array.isArray(pkgAny.externalRefs)) {

0 commit comments

Comments
 (0)