-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
213 lines (189 loc) · 5.77 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Import Third-party Dependencies
import { get } from "@myunisoft/httpie";
import { packument } from "@nodesecure/npm-registry-sdk";
// Import Internal Dependencies
import { repositoryFromUrl } from "./utils/repositoryFromUrl.js";
import type { GitHubRepository, GitLabProject } from "./types.js";
// CONSTANTS
const kDefaultPlatform = "github.com";
const kGitHubApiUrl = "https://api.github.com";
const kGitHubRequestOptions = {
authorization: process.env.GITHUB_TOKEN ?? ""
};
const kGitLabApiUrl = "https://gitlab.com";
export const API_URL = "https://api.securityscorecards.dev";
export type ScorecardCheck = {
name: string;
score: number;
reason: string;
details: null | string[];
documentation: {
short: string;
url: string;
};
};
export type ScorecardResult = {
date: string;
metadata: string;
repo: {
name: string;
commit: string;
};
scorecard: {
version: string;
commit: string;
};
score: number;
checks: ScorecardCheck[];
};
export interface IResultOptions {
/**
* @description VCS platform. eg. github.com
* @default github.com
*/
platform?: "github.com" | "github.corp.com" | "gitlab.com";
/**
* @description Try to resolve the given repository on the NPM registry if its not found on the given platform.
* @default true
*/
resolveOnNpmRegistry?: boolean;
/**
* @description Try to resolve the given repository on the given platform. This can be useful when the given repository
* is not exactly the same as the one on the given platform (case sensitive).
* @default true
*/
resolveOnVersionControl?: boolean;
/**
* @description The version of the npm package (when `resolveOnNpmRegistry` only) to retrieve the scorecard for.
* @default "latest"
*/
npmPackageVersion?: string;
}
async function getNpmRepository(
repository: string,
version: string
): Promise<string> {
const data = await packument(repository);
const latestVersion = data["dist-tags"].latest!;
const packageVersion = data.versions[version === "latest" ? latestVersion : version];
if (!packageVersion) {
throw new Error(`Cannot find the version '${version}' of the given repository`);
}
const homepage = packageVersion.homepage || null;
const repo = packageVersion.repository;
const repoUrl = typeof repo === "string" ? repo : repo?.url;
return repositoryFromUrl(repoUrl ?? homepage ?? "");
}
async function retrieveRepositoryOnGithub(
owner: string,
repo: string
): Promise<string> {
const { data } = await get<GitHubRepository>(
new URL(`/repos/${owner}/${repo}`, kGitHubApiUrl),
kGitHubRequestOptions
);
return data.full_name;
}
async function retrieveRepositoryOnGitLab(
owner: string,
repo: string
): Promise<string> {
const { data } = await get<GitLabProject>(
new URL(`/api/v4/projects/${owner}%2F${repo}`, kGitLabApiUrl)
);
return data.path_with_namespace;
}
/**
* @description Get a repository's ScorecardResult
* @see https://api.securityscorecards.dev/#/results/getResult
*/
export async function result(
repository: string,
options: IResultOptions = {}
): Promise<ScorecardResult> {
let formattedRepository = repository;
const {
platform = kDefaultPlatform,
resolveOnNpmRegistry = true,
resolveOnVersionControl = true,
npmPackageVersion = "latest"
} = options;
const [owner, repo] = repository.replace("@", "").split("/");
// We don't try to resolve repository via GitHub or NPM API when platform is GHES
if (platform !== "github.corp.com" && resolveOnVersionControl) {
try {
// We try to retrieve the repository with the GitHub or GitLab API because
// Scorecard API is case sensitive, i.e if we pass "nodesecure/cli",
// we must reformat it to "NodeSecure/cli"
if (platform === "github.com") {
formattedRepository = await retrieveRepositoryOnGithub(owner, repo);
}
else if (platform === "gitlab.com") {
formattedRepository = await retrieveRepositoryOnGitLab(owner, repo);
}
}
catch (error) {
const platformName = platform.split(".")[0];
if (!resolveOnNpmRegistry) {
throw new Error(`Invalid repository, cannot find it on ${platformName}`, {
cause: error
});
}
try {
formattedRepository = await getNpmRepository(repository, npmPackageVersion);
}
catch (error) {
throw new Error(`Invalid repository, cannot find it on ${platformName} or NPM registry`, {
cause: error
});
}
}
}
else if (resolveOnNpmRegistry && !resolveOnVersionControl) {
try {
formattedRepository = await getNpmRepository(repository, npmPackageVersion);
}
catch (error) {
throw new Error(`Invalid repository, cannot find it on NPM registry`, {
cause: error
});
}
}
const { data } = await get<ScorecardResult>(
new URL(`/projects/${platform}/${formattedRepository}`, API_URL)
);
return data;
}
export interface IBadgeOptions extends IResultOptions {
/**
* Style to render the badge
*
* @default flat
*/
style?: "plastic" | "flat" | "flat-square" | "for-the-badge" | "social";
}
export interface BadgeResult {
image: string;
svg: string;
}
/**
* @description Get a repository's Scorecard badge URL
* @see https://api.securityscorecards.dev/#/badge/getBadge
*/
export async function badge(
repository: string,
options: IBadgeOptions = {}
): Promise<BadgeResult> {
const { platform = kDefaultPlatform, style = "flat" } = options;
const apiUrl = new URL(`/projects/${platform}/${repository}/badge`, API_URL);
apiUrl.searchParams.set("style", style);
const response = await fetch(apiUrl);
const svg = await response.text();
if (svg.includes("invalid repo path")) {
throw new Error("Invalid repo path");
}
return {
image: response.url,
svg
};
}