Commit 8ba5d765ad
Unsigned
Layout: unified · split
api/src/app.ts +2
| @@ -21,6 +21,7 @@ import android from '@/routes/private/android' | |||
| 21 | import discord from '@/routes/private/discord' | 21 | import discord from '@/routes/private/discord' |
| 22 | import reddit from '@/routes/private/reddit' | 22 | import reddit from '@/routes/private/reddit' |
| 23 | import website from '@/routes/private/website' | 23 | import website from '@/routes/private/website' |
| 24 | import security from '@/routes/private/security' | ||
| 24 | 25 | ||
| 25 | import mcp from '@/routes/mcp' | 26 | import mcp from '@/routes/mcp' |
| 26 | 27 | ||
| @@ -51,6 +52,7 @@ const buildPrivate = () => { | |||
| 51 | priv.route('/', discord) | 52 | priv.route('/', discord) |
| 52 | priv.route('/', reddit) | 53 | priv.route('/', reddit) |
| 53 | priv.route('/', website) | 54 | priv.route('/', website) |
| 55 | priv.route('/', security) | ||
| 54 | return priv | 56 | return priv |
| 55 | } | 57 | } |
| 56 | 58 | ||
api/src/lib/fetch.ts +1
| @@ -16,6 +16,7 @@ const ALLOWED_HOSTS = new Set([ | |||
| 16 | 'www.reddit.com', | 16 | 'www.reddit.com', |
| 17 | 'endpoint.apivoid.com', | 17 | 'endpoint.apivoid.com', |
| 18 | 'raw.githubusercontent.com', | 18 | 'raw.githubusercontent.com', |
| 19 | 'api.deps.dev', | ||
| 19 | ]) | 20 | ]) |
| 20 | 21 | ||
| 21 | export interface FetchOpts { | 22 | export interface FetchOpts { |
api/src/lib/github.ts added +9
| @@ -0,0 +1,9 @@ | |||
| 1 | // Shared GitHub REST request headers, adds auth when a token is present | ||
| 2 | export const githubHeaders = (token?: string): Record<string, string> => { | ||
| 3 | const headers: Record<string, string> = { | ||
| 4 | 'User-Agent': 'awesome-privacy-api', | ||
| 5 | Accept: 'application/vnd.github+json', | ||
| 6 | } | ||
| 7 | if (token) headers.Authorization = `token ${token}` | ||
| 8 | return headers | ||
| 9 | } | ||
api/src/routes/private/github.ts +13 −12
| @@ -1,6 +1,7 @@ | |||
| 1 | // GitHub repo info, aggregates info, languages, tags, contributors, commits | 1 | // GitHub repo info, aggregates info, languages, tags, contributors, commits |
| 2 | import { createRoute, z } from '@hono/zod-openapi' | 2 | import { createRoute, z } from '@hono/zod-openapi' |
| 3 | import { fetchJson } from '@/lib/fetch' | 3 | import { fetchJson } from '@/lib/fetch' |
| 4 | import { githubHeaders } from '@/lib/github' | ||
| 4 | import { newApp } from '@/lib/openapi' | 5 | import { newApp } from '@/lib/openapi' |
| 5 | import { ErrorResponse, GithubRepoSchema, Ok } from '@/schemas' | 6 | import { ErrorResponse, GithubRepoSchema, Ok } from '@/schemas' |
| 6 | 7 | ||
| @@ -16,15 +17,18 @@ interface RepoInfo { | |||
| 16 | homepage?: string | 17 | homepage?: string |
| 17 | language?: string | 18 | language?: string |
| 18 | topics?: string[] | 19 | topics?: string[] |
| 19 | license?: { spdx_id?: string } | 20 | license?: { spdx_id?: string; name?: string } |
| 20 | fork?: boolean | 21 | fork?: boolean |
| 21 | archived?: boolean | 22 | archived?: boolean |
| 23 | parent?: { full_name?: string } | ||
| 22 | created_at?: string | 24 | created_at?: string |
| 23 | updated_at?: string | 25 | updated_at?: string |
| 26 | pushed_at?: string | ||
| 24 | size?: number | 27 | size?: number |
| 25 | stargazers_count?: number | 28 | stargazers_count?: number |
| 26 | forks_count?: number | 29 | forks_count?: number |
| 27 | watchers_count?: number | 30 | watchers_count?: number |
| 31 | open_issues_count?: number | ||
| 28 | } | 32 | } |
| 29 | 33 | ||
| 30 | interface Tag { | 34 | interface Tag { |
| @@ -37,6 +41,7 @@ interface Tag { | |||
| 37 | interface Contributor { | 41 | interface Contributor { |
| 38 | login?: string | 42 | login?: string |
| 39 | avatar_url?: string | 43 | avatar_url?: string |
| 44 | html_url?: string | ||
| 40 | contributions?: number | 45 | contributions?: number |
| 41 | } | 46 | } |
| 42 | 47 | ||
| @@ -60,14 +65,18 @@ const mapInfo = (info: RepoInfo) => ({ | |||
| 60 | language: info.language ?? '', | 65 | language: info.language ?? '', |
| 61 | topics: info.topics ?? [], | 66 | topics: info.topics ?? [], |
| 62 | license: info.license?.spdx_id ?? '', | 67 | license: info.license?.spdx_id ?? '', |
| 68 | licenseName: info.license?.name ?? '', | ||
| 63 | isFork: info.fork ?? false, | 69 | isFork: info.fork ?? false, |
| 64 | isArchived: info.archived ?? false, | 70 | isArchived: info.archived ?? false, |
| 71 | forkParent: info.parent?.full_name ?? '', | ||
| 65 | createdAt: info.created_at ?? '', | 72 | createdAt: info.created_at ?? '', |
| 66 | updatedAt: info.updated_at ?? '', | 73 | updatedAt: info.updated_at ?? '', |
| 74 | pushedAt: info.pushed_at ?? '', | ||
| 67 | size: info.size ?? 0, | 75 | size: info.size ?? 0, |
| 68 | starCount: info.stargazers_count ?? 0, | 76 | starCount: info.stargazers_count ?? 0, |
| 69 | forksCount: info.forks_count ?? 0, | 77 | forksCount: info.forks_count ?? 0, |
| 70 | watchersCount: info.watchers_count ?? 0, | 78 | watchersCount: info.watchers_count ?? 0, |
| 79 | openIssues: info.open_issues_count ?? 0, | ||
| 71 | }) | 80 | }) |
| 72 | 81 | ||
| 73 | const mapVersion = (tag: Tag) => ({ | 82 | const mapVersion = (tag: Tag) => ({ |
| @@ -80,6 +89,7 @@ const mapVersion = (tag: Tag) => ({ | |||
| 80 | const mapContributor = (person: Contributor) => ({ | 89 | const mapContributor = (person: Contributor) => ({ |
| 81 | username: person.login ?? '', | 90 | username: person.login ?? '', |
| 82 | avatar: person.avatar_url ?? '', | 91 | avatar: person.avatar_url ?? '', |
| 92 | url: person.html_url ?? '', | ||
| 83 | contributions: person.contributions ?? 0, | 93 | contributions: person.contributions ?? 0, |
| 84 | }) | 94 | }) |
| 85 | 95 | ||
| @@ -92,15 +102,6 @@ const mapCommit = (commit: Commit) => ({ | |||
| 92 | authorAvatar: commit.author?.avatar_url ?? '', | 102 | authorAvatar: commit.author?.avatar_url ?? '', |
| 93 | }) | 103 | }) |
| 94 | 104 | ||
| 95 | const buildHeaders = (token?: string) => { | ||
| 96 | const headers: Record<string, string> = { | ||
| 97 | 'User-Agent': 'awesome-privacy-api', | ||
| 98 | Accept: 'application/vnd.github.v3+json', | ||
| 99 | } | ||
| 100 | if (token) headers.Authorization = `token ${token}` | ||
| 101 | return headers | ||
| 102 | } | ||
| 103 | |||
| 104 | const route = createRoute({ | 105 | const route = createRoute({ |
| 105 | method: 'get', | 106 | method: 'get', |
| 106 | path: '/enrich/github/{owner}/{repo}', | 107 | path: '/enrich/github/{owner}/{repo}', |
| @@ -119,7 +120,7 @@ const route = createRoute({ | |||
| 119 | app.openapi(route, async (c) => { | 120 | app.openapi(route, async (c) => { |
| 120 | const { owner, repo } = c.req.valid('param') | 121 | const { owner, repo } = c.req.valid('param') |
| 121 | const slug = `${owner}/${repo}` | 122 | const slug = `${owner}/${repo}` |
| 122 | const headers = buildHeaders(c.env.GITHUB_TOKEN) | 123 | const headers = githubHeaders(c.env.GITHUB_TOKEN) |
| 123 | const data = await c.var.storage.fetch( | 124 | const data = await c.var.storage.fetch( |
| 124 | `gh:${slug}`, | 125 | `gh:${slug}`, |
| 125 | SUCCESS_TTL, | 126 | SUCCESS_TTL, |
| @@ -130,7 +131,7 @@ app.openapi(route, async (c) => { | |||
| 130 | fetchJson<RepoInfo>(base, { headers }), | 131 | fetchJson<RepoInfo>(base, { headers }), |
| 131 | fetchJson<Record<string, number>>(`${base}/languages`, { headers }), | 132 | fetchJson<Record<string, number>>(`${base}/languages`, { headers }), |
| 132 | fetchJson<Tag[]>(`${base}/tags`, { headers }), | 133 | fetchJson<Tag[]>(`${base}/tags`, { headers }), |
| 133 | fetchJson<Contributor[]>(`${base}/contributors`, { headers }), | 134 | fetchJson<Contributor[]>(`${base}/contributors?per_page=100`, { headers }), |
| 134 | fetchJson<Commit[]>(`${base}/commits`, { headers }), | 135 | fetchJson<Commit[]>(`${base}/commits`, { headers }), |
| 135 | ]) | 136 | ]) |
| 136 | return { | 137 | return { |
api/src/routes/private/security.ts added +151
| @@ -0,0 +1,151 @@ | |||
| 1 | // Repo security report: deps.dev scorecard + OSS-Fuzz + GitHub advisories | ||
| 2 | import { createRoute, z } from '@hono/zod-openapi' | ||
| 3 | import { fetchJson } from '@/lib/fetch' | ||
| 4 | import { githubHeaders } from '@/lib/github' | ||
| 5 | import { newApp } from '@/lib/openapi' | ||
| 6 | import { ErrorResponse, Ok, SecurityReportSchema } from '@/schemas' | ||
| 7 | |||
| 8 | const app = newApp() | ||
| 9 | |||
| 10 | const SUCCESS_TTL = 7 * 24 * 60 * 60 | ||
| 11 | const NEGATIVE_TTL = 60 * 60 | ||
| 12 | |||
| 13 | interface Check { | ||
| 14 | name?: string | ||
| 15 | score?: number | ||
| 16 | reason?: string | ||
| 17 | documentation?: { url?: string } | ||
| 18 | } | ||
| 19 | |||
| 20 | interface Project { | ||
| 21 | scorecard?: { date?: string; overallScore?: number; checks?: Check[] } | ||
| 22 | ossFuzz?: unknown | ||
| 23 | } | ||
| 24 | |||
| 25 | interface Vuln { | ||
| 26 | vulnerable_version_range?: string | null | ||
| 27 | first_patched_version?: { identifier?: string } | null | ||
| 28 | } | ||
| 29 | |||
| 30 | interface Advisory { | ||
| 31 | ghsa_id?: string | ||
| 32 | cve_id?: string | null | ||
| 33 | summary?: string | ||
| 34 | severity?: string | ||
| 35 | cvss?: { score?: number | null } | ||
| 36 | published_at?: string | ||
| 37 | withdrawn_at?: string | null | ||
| 38 | state?: string | ||
| 39 | html_url?: string | ||
| 40 | vulnerabilities?: Vuln[] | null | ||
| 41 | } | ||
| 42 | |||
| 43 | const mapCheck = (check: Check) => ({ | ||
| 44 | name: check.name ?? '', | ||
| 45 | score: check.score ?? -1, | ||
| 46 | reason: check.reason ?? '', | ||
| 47 | url: check.documentation?.url ?? '', | ||
| 48 | }) | ||
| 49 | |||
| 50 | // A lone strict "< x" range names the fix version, otherwise unknown | ||
| 51 | const deriveFix = (ranges: string[]) => { | ||
| 52 | const match = ranges.length === 1 ? ranges[0].match(/^<\s*([\w.+-]+)$/) : null | ||
| 53 | return match ? match[1] : null | ||
| 54 | } | ||
| 55 | |||
| 56 | const mapAdvisory = (a: Advisory) => { | ||
| 57 | const vulns = a.vulnerabilities ?? [] | ||
| 58 | const ranges = [ | ||
| 59 | ...new Set( | ||
| 60 | vulns | ||
| 61 | .map((v) => v.vulnerable_version_range?.trim()) | ||
| 62 | .filter((r): r is string => !!r), | ||
| 63 | ), | ||
| 64 | ] | ||
| 65 | const vulnerableRange = ranges.join('; ') | ||
| 66 | const explicit = vulns.map((v) => v.first_patched_version?.identifier).find(Boolean) | ||
| 67 | const firstPatchedVersion = explicit ?? deriveFix(ranges) | ||
| 68 | return { | ||
| 69 | ghsaId: a.ghsa_id ?? '', | ||
| 70 | cveId: a.cve_id ?? null, | ||
| 71 | summary: a.summary ?? '', | ||
| 72 | severity: a.severity ?? '', | ||
| 73 | cvssScore: a.cvss?.score ?? null, | ||
| 74 | publishedAt: a.published_at ?? '', | ||
| 75 | url: a.html_url ?? '', | ||
| 76 | firstPatchedVersion, | ||
| 77 | vulnerableRange, | ||
| 78 | // an upper bound means later versions are safe, so a fix exists | ||
| 79 | isPatched: Boolean(firstPatchedVersion) || vulnerableRange.includes('<'), | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | // OpenSSF Scorecard + fuzzing signal from deps.dev, degrades to empty | ||
| 84 | const getPosture = async (slug: string) => { | ||
| 85 | try { | ||
| 86 | const key = encodeURIComponent(`github.com/${slug}`) | ||
| 87 | const project = await fetchJson<Project>(`https://api.deps.dev/v3/projects/${key}`) | ||
| 88 | const card = project.scorecard | ||
| 89 | return { | ||
| 90 | scorecard: { | ||
| 91 | available: Boolean(card), | ||
| 92 | overallScore: card?.overallScore ?? null, | ||
| 93 | generatedAt: card?.date ?? '', | ||
| 94 | checks: (card?.checks ?? []).map(mapCheck), | ||
| 95 | }, | ||
| 96 | fuzzed: Boolean(project.ossFuzz), | ||
| 97 | } | ||
| 98 | } catch { | ||
| 99 | return { | ||
| 100 | scorecard: { available: false, overallScore: null, generatedAt: '', checks: [] }, | ||
| 101 | fuzzed: false, | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | // Maintainer-published GitHub security advisories, degrades to empty | ||
| 107 | const getAdvisories = async (slug: string, token?: string) => { | ||
| 108 | try { | ||
| 109 | const list = await fetchJson<Advisory[]>( | ||
| 110 | `https://api.github.com/repos/${slug}/security-advisories?per_page=100&state=published`, | ||
| 111 | { headers: githubHeaders(token) }, | ||
| 112 | ) | ||
| 113 | const items = list.filter((a) => !a.withdrawn_at).map(mapAdvisory) | ||
| 114 | return { count: items.length, items } | ||
| 115 | } catch { | ||
| 116 | return { count: 0, items: [] } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | const route = createRoute({ | ||
| 121 | method: 'get', | ||
| 122 | path: '/enrich/security/{owner}/{repo}', | ||
| 123 | tags: ['Enrichment'], | ||
| 124 | summary: 'Repo security', | ||
| 125 | request: { params: z.object({ owner: z.string(), repo: z.string() }) }, | ||
| 126 | responses: { | ||
| 127 | 200: Ok(SecurityReportSchema), | ||
| 128 | 401: ErrorResponse, | ||
| 129 | 500: ErrorResponse, | ||
| 130 | }, | ||
| 131 | }) | ||
| 132 | |||
| 133 | app.openapi(route, async (c) => { | ||
| 134 | const { owner, repo } = c.req.valid('param') | ||
| 135 | const slug = `${owner}/${repo}` | ||
| 136 | const data = await c.var.storage.fetch( | ||
| 137 | `sec:${slug}`, | ||
| 138 | SUCCESS_TTL, | ||
| 139 | NEGATIVE_TTL, | ||
| 140 | async () => { | ||
| 141 | const [posture, advisories] = await Promise.all([ | ||
| 142 | getPosture(slug), | ||
| 143 | getAdvisories(slug, c.env.GITHUB_TOKEN), | ||
| 144 | ]) | ||
| 145 | return { repo: slug, ...posture, advisories } | ||
| 146 | }, | ||
| 147 | ) | ||
| 148 | return c.json(data, 200) | ||
| 149 | }) | ||
| 150 | |||
| 151 | export default app | ||
api/src/schemas/index.ts +43
| @@ -155,14 +155,18 @@ export const GithubRepoSchema = z | |||
| 155 | language: z.string(), | 155 | language: z.string(), |
| 156 | topics: z.array(z.string()), | 156 | topics: z.array(z.string()), |
| 157 | license: z.string(), | 157 | license: z.string(), |
| 158 | licenseName: z.string(), | ||
| 158 | isFork: z.boolean(), | 159 | isFork: z.boolean(), |
| 159 | isArchived: z.boolean(), | 160 | isArchived: z.boolean(), |
| 161 | forkParent: z.string(), | ||
| 160 | createdAt: z.string(), | 162 | createdAt: z.string(), |
| 161 | updatedAt: z.string(), | 163 | updatedAt: z.string(), |
| 164 | pushedAt: z.string(), | ||
| 162 | size: z.number(), | 165 | size: z.number(), |
| 163 | starCount: z.number(), | 166 | starCount: z.number(), |
| 164 | forksCount: z.number(), | 167 | forksCount: z.number(), |
| 165 | watchersCount: z.number(), | 168 | watchersCount: z.number(), |
| 169 | openIssues: z.number(), | ||
| 166 | }), | 170 | }), |
| 167 | languages: z.record(z.number()), | 171 | languages: z.record(z.number()), |
| 168 | versions: z.array( | 172 | versions: z.array( |
| @@ -177,6 +181,7 @@ export const GithubRepoSchema = z | |||
| 177 | z.object({ | 181 | z.object({ |
| 178 | username: z.string(), | 182 | username: z.string(), |
| 179 | avatar: z.string(), | 183 | avatar: z.string(), |
| 184 | url: z.string(), | ||
| 180 | contributions: z.number(), | 185 | contributions: z.number(), |
| 181 | }), | 186 | }), |
| 182 | ), | 187 | ), |
| @@ -193,6 +198,44 @@ export const GithubRepoSchema = z | |||
| 193 | }) | 198 | }) |
| 194 | .openapi('GithubRepo') | 199 | .openapi('GithubRepo') |
| 195 | 200 | ||
| 201 | // Repo security report, merges deps.dev scorecard with GitHub advisories | ||
| 202 | export const SecurityReportSchema = z | ||
| 203 | .object({ | ||
| 204 | repo: z.string(), | ||
| 205 | scorecard: z.object({ | ||
| 206 | available: z.boolean(), | ||
| 207 | overallScore: z.number().nullable(), | ||
| 208 | generatedAt: z.string(), | ||
| 209 | checks: z.array( | ||
| 210 | z.object({ | ||
| 211 | name: z.string(), | ||
| 212 | score: z.number(), | ||
| 213 | reason: z.string(), | ||
| 214 | url: z.string(), | ||
| 215 | }), | ||
| 216 | ), | ||
| 217 | }), | ||
| 218 | advisories: z.object({ | ||
| 219 | count: z.number(), | ||
| 220 | items: z.array( | ||
| 221 | z.object({ | ||
| 222 | ghsaId: z.string(), | ||
| 223 | cveId: z.string().nullable(), | ||
| 224 | summary: z.string(), | ||
| 225 | severity: z.string(), | ||
| 226 | cvssScore: z.number().nullable(), | ||
| 227 | publishedAt: z.string(), | ||
| 228 | url: z.string(), | ||
| 229 | firstPatchedVersion: z.string().nullable(), | ||
| 230 | vulnerableRange: z.string(), | ||
| 231 | isPatched: z.boolean(), | ||
| 232 | }), | ||
| 233 | ), | ||
| 234 | }), | ||
| 235 | fuzzed: z.boolean(), | ||
| 236 | }) | ||
| 237 | .openapi('SecurityReport') | ||
| 238 | |||
| 196 | export const DiscordInviteSchema = z | 239 | export const DiscordInviteSchema = z |
| 197 | .object({ | 240 | .object({ |
| 198 | inviteCode: z.string().nullable(), | 241 | inviteCode: z.string().nullable(), |
api/tests/app.test.ts +5
| @@ -67,6 +67,11 @@ describe('auth', () => { | |||
| 67 | }) | 67 | }) |
| 68 | expect(res.status).toBe(401) | 68 | expect(res.status).toBe(401) |
| 69 | }) | 69 | }) |
| 70 | |||
| 71 | it('security enrich route requires auth', async () => { | ||
| 72 | const res = await hit('/v1/enrich/security/jquery/jquery') | ||
| 73 | expect(res.status).toBe(401) | ||
| 74 | }) | ||
| 70 | }) | 75 | }) |
| 71 | 76 | ||
| 72 | describe('mcp', () => { | 77 | describe('mcp', () => { |