Skip to content

Commit

Permalink
chore: implement OSV api & format
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Nov 28, 2023
1 parent 6ec82b9 commit d7991ee
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as osv from "./osv.js";
53 changes: 53 additions & 0 deletions src/database/osv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Import Third-Party Dependencies
import * as httpie from "@myunisoft/httpie";

// Import Internal Dependencies
import { OSV } from "../formats/osv";

// CONSTANTS
const kOsvRootApi = "https://api.osv.dev";

export type OSVApiParameter = {
version?: string;
package: {
name: string;
/**
* @default npm
*/
ecosystem?: string;
};
}

export async function findOne(
parameters: OSVApiParameter
): Promise<OSV[]> {
if (!parameters.package.ecosystem) {
parameters.package.ecosystem = "npm";
}

const { data } = await httpie.post<{ vulns: OSV[] }>(
new URL("v1/query", kOsvRootApi),
{
body: parameters
}
);

return data.vulns;
}

export function findOneBySpec(
spec: string

Check warning on line 39 in src/database/osv.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

'spec' is defined but never used

Check warning on line 39 in src/database/osv.ts

View workflow job for this annotation

GitHub Actions / lint

'spec' is defined but never used

Check warning on line 39 in src/database/osv.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

'spec' is defined but never used
) {
// TODO: parse spec

return findOne({
version: "",
package: {
name: "foo"
}
});
}

export async function findMany() {
throw new Error("not implemented yet");
}
Empty file removed src/formats/osv/.gitkeep
Empty file.
80 changes: 80 additions & 0 deletions src/formats/osv/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

/**
* @see https://ossf.github.io/osv-schema/
*/
export interface OSV {
schema_version: string;
id: string;
modified: string;
published: string;
withdraw: string;
aliases: string[];
related: string[];
summary: string;
details: string;
severity: OSVSeverity[];
affected: OSVAffected[];
references: {
type: OSVReferenceType;
url: string;
}[];
credits: {
name: string;
contact: string[];
type: OSVCreditType;
}[];
database_specific: Record<string, any>;
}

export type OSVReferenceType = "ADVISORY" |
"ARTICLE" |
"DETECTION" |
"DISCUSSION" |
"REPORT" |
"FIX" |
"GIT" |
"INTRODUCED" |
"PACKAGE" |
"EVIDENCE" |
"WEB";

export type OSVCreditType = "FINDER" |
"REPORTER" |
"ANALYST" |
"COORDINATOR" |
"REMEDIATION_DEVELOPER" |
"REMEDIATION_REVIEWER" |
"REMEDIATION_VERIFIER" |
"TOOL" |
"SPONSOR" |
"OTHER";

export interface OSVAffected {
package: {
ecosystem: "npm",
name: string;
purl: string;
};
severity: OSVSeverity[];
ranges: OSVRange[];
versions: string[];
ecosystem_specific: Record<string, any>;
database_specific: Record<string, any>;
}

export interface OSVRange {
type: string;
repo: string;
events: {
introduced?: string;
fixed?: string;
last_affected?: string;
limit?: string;
}[];
database_specific: Record<string, any>;
}

export interface OSVSeverity {
type: string;
score: string;
}
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import {
import type {
StandardVulnerability, Severity, StandardPatch
} from "./formats/standard/index.js";
import type {
OSV
} from "./formats/osv/index.js";

import type {
Dependencies, ScannerVersionDescriptor
Expand All @@ -43,6 +46,8 @@ import type {
HydratePayloadDepsOptions
} from "./strategies/types/api.js";

export * as Database from "./database/index.js";

export type AllStrategy = {
"none": NoneStrategyDefinition;
"github-advisory": GithubAdvisoryStrategyDefinition;
Expand Down Expand Up @@ -110,5 +115,7 @@ export {
NpmAuditAdvisory,
PnpmAuditAdvisory,
SnykVulnerability,
SonatypeVulnerability
SonatypeVulnerability,

OSV
};

0 comments on commit d7991ee

Please sign in to comment.