-
Notifications
You must be signed in to change notification settings - Fork 730
feat(security-contacts): implement tier D #4373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a5bcd8
feat: add golang.org security contact fallback for go packages
mbani01 5268362
feat: add tier D last-resort security contacts (committers + owner)
mbani01 0fa7a8e
fix: code review fixes
mbani01 07c1154
Merge branch 'main' into feat/security_contacts_tier_d
mbani01 f6a517d
fix: 202 status for contributors endpoint
mbani01 729f6a5
fix: exclude AI-agent committer accounts from tier D security contacts
mbani01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
services/apps/packages_worker/src/security-contacts/extractors/registry/go.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { ExtractorResult, ProvenanceEntry, RawContact } from '../../types' | ||
|
|
||
| const SOURCE = 'go.dev/security-policy' | ||
| const POLICY_URL = 'https://go.dev/doc/security/policy#reporting-a-security-bug' | ||
|
|
||
| export async function fetchGo(): Promise<ExtractorResult> { | ||
| const fetchedAt = new Date().toISOString() | ||
| const prov = (): ProvenanceEntry[] => [ | ||
| { source: SOURCE, sourceTier: 'D', path: POLICY_URL, fetchedAt }, | ||
| ] | ||
| const contacts: RawContact[] = [ | ||
| { | ||
| channel: 'email', | ||
| value: 'security@golang.org', | ||
| role: 'security-team', | ||
| tier: 'D', | ||
| provenance: prov(), | ||
| }, | ||
| { | ||
| channel: 'web-form', | ||
| value: 'https://g.co/vulnz', | ||
| role: 'security-team', | ||
| tier: 'D', | ||
| provenance: prov(), | ||
| }, | ||
| ] | ||
|
mbani01 marked this conversation as resolved.
|
||
| return { contacts, policies: {} } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
services/apps/packages_worker/src/security-contacts/extractors/repoOwner.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { getServiceChildLogger } from '@crowd/logging' | ||
|
|
||
| import { parseGithubUrl } from '../../enricher/fetchLightRepo' | ||
| import { ExtractorDeps, ProvenanceEntry, RawContact, RepoTarget } from '../types' | ||
|
|
||
| import { isEmail } from './http' | ||
|
|
||
| const log = getServiceChildLogger('security-contacts:repo-owner') | ||
|
|
||
| const SOURCE = 'github-repo-owner' | ||
|
|
||
| export function buildOwnerHandleContact(owner: string, fetchedAt: string, url: string): RawContact { | ||
| return { | ||
| channel: 'github-handle', | ||
| value: owner, | ||
| handle: owner, | ||
| role: 'org-owner', | ||
| tier: 'D', | ||
| provenance: [{ source: SOURCE, sourceTier: 'D', path: url, fetchedAt }], | ||
| } | ||
| } | ||
|
|
||
| export async function fetchRepoOwner( | ||
| target: RepoTarget, | ||
| deps: Pick<ExtractorDeps, 'githubGet'>, | ||
| ): Promise<RawContact[]> { | ||
| let owner: string | ||
| try { | ||
| ;({ owner } = parseGithubUrl(target.url)) | ||
| } catch { | ||
| return [] | ||
| } | ||
|
|
||
| const fetchedAt = new Date().toISOString() | ||
| const contacts: RawContact[] = [buildOwnerHandleContact(owner, fetchedAt, target.url)] | ||
|
|
||
| try { | ||
| const { text } = await deps.githubGet(`/users/${owner}`) | ||
| const email = (text ? (JSON.parse(text) as { email?: unknown }) : null)?.email | ||
| if (typeof email === 'string' && isEmail(email)) { | ||
| const provenance: ProvenanceEntry[] = [ | ||
| { | ||
| source: SOURCE, | ||
| sourceTier: 'D', | ||
| path: `https://api.github.com/users/${owner}`, | ||
| fetchedAt, | ||
| }, | ||
| ] | ||
| contacts.push({ | ||
| channel: 'email', | ||
| value: email, | ||
| handle: owner, | ||
| role: 'org-owner', | ||
| tier: 'D', | ||
| provenance, | ||
| }) | ||
| } | ||
| } catch (err) { | ||
| log.warn( | ||
| { repoId: target.repoId, owner, errMsg: (err as Error).message }, | ||
| 'Owner email lookup failed', | ||
| ) | ||
| } | ||
|
|
||
| return contacts | ||
| } |
204 changes: 204 additions & 0 deletions
204
services/apps/packages_worker/src/security-contacts/extractors/topCommitters.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { getServiceChildLogger } from '@crowd/logging' | ||
|
|
||
| import { parseGithubUrl } from '../../enricher/fetchLightRepo' | ||
| import { ExtractorDeps, ProvenanceEntry, RawContact, RepoTarget } from '../types' | ||
|
|
||
| import { isEmail } from './http' | ||
|
|
||
| const log = getServiceChildLogger('security-contacts:top-committers') | ||
|
|
||
| const SOURCE = 'github-commits' | ||
| const SINCE_DAYS = 90 | ||
| const TOP_N = 3 | ||
| // GitHub computes /stats/contributors asynchronously and returns 202 while it's not ready yet — | ||
| // poll a few times with a short backoff before giving up for this pass. | ||
| const STATS_POLL_ATTEMPTS = 3 | ||
| const STATS_POLL_DELAY_MS = 2000 | ||
|
|
||
| interface WeekStat { | ||
| w?: unknown | ||
| c?: unknown | ||
| } | ||
|
|
||
| interface ContributorStat { | ||
| author?: { login?: unknown; type?: unknown } | null | ||
| weeks?: WeekStat[] | ||
| } | ||
|
|
||
| interface AggregatedAuthor { | ||
| login: string | ||
| count: number | ||
| } | ||
|
|
||
| const BOT_TOKENS = [ | ||
| 'dependabot', | ||
| 'renovate', | ||
| 'github-actions', | ||
| 'claude', | ||
| 'anthropic', | ||
| 'copilot', | ||
| 'chatgpt', | ||
| 'openai', | ||
| ] | ||
|
|
||
| function matchesBotToken(value: string): boolean { | ||
| const lower = value.toLowerCase() | ||
| return BOT_TOKENS.some((token) => lower.includes(token)) | ||
| } | ||
|
|
||
| function isBotLogin(login: string): boolean { | ||
| const lower = login.toLowerCase() | ||
| if (lower.endsWith('[bot]')) return true | ||
| return matchesBotToken(lower) | ||
| } | ||
|
|
||
| export function aggregateTopCommitters( | ||
| stats: ContributorStat[], | ||
| sinceUnixSeconds: number, | ||
| topN: number = TOP_N, | ||
| ): AggregatedAuthor[] { | ||
| const authors: AggregatedAuthor[] = [] | ||
|
|
||
| for (const stat of stats) { | ||
| if (stat.author?.type === 'Bot') continue | ||
| const login = typeof stat.author?.login === 'string' ? stat.author.login : undefined | ||
| if (!login || isBotLogin(login)) continue | ||
|
|
||
| const count = (stat.weeks ?? []).reduce((sum, week) => { | ||
| const w = typeof week.w === 'number' ? week.w : 0 | ||
| const c = typeof week.c === 'number' ? week.c : 0 | ||
| return w >= sinceUnixSeconds ? sum + c : sum | ||
|
Comment on lines
+67
to
+70
|
||
| }, 0) | ||
| if (count > 0) authors.push({ login, count }) | ||
| } | ||
|
|
||
| return authors.sort((a, b) => b.count - a.count || a.login.localeCompare(b.login)).slice(0, topN) | ||
| } | ||
|
|
||
| async function resolvePublicEmail( | ||
| login: string, | ||
| githubGet: ExtractorDeps['githubGet'], | ||
| ): Promise<string | null> { | ||
| try { | ||
| const { text } = await githubGet(`/users/${login}`) | ||
| const email = (text ? (JSON.parse(text) as { email?: unknown }) : null)?.email | ||
| if (typeof email !== 'string' || !isEmail(email) || matchesBotToken(email)) return null | ||
| return email | ||
| } catch (err) { | ||
| log.warn({ login, errMsg: (err as Error).message }, 'Committer email resolution failed') | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| export async function mapCommittersToContacts( | ||
| authors: AggregatedAuthor[], | ||
| fetchedAt: string, | ||
| apiPath: string, | ||
| githubGet: ExtractorDeps['githubGet'], | ||
| ): Promise<RawContact[]> { | ||
| const contacts: RawContact[] = [] | ||
|
|
||
| for (const author of authors) { | ||
| const provenance: ProvenanceEntry[] = [ | ||
| { source: SOURCE, sourceTier: 'D', path: apiPath, fetchedAt }, | ||
| ] | ||
|
|
||
| contacts.push({ | ||
| channel: 'github-handle', | ||
| value: author.login, | ||
| handle: author.login, | ||
| role: 'committer', | ||
| tier: 'D', | ||
| provenance, | ||
| }) | ||
|
|
||
| const email = await resolvePublicEmail(author.login, githubGet) | ||
| if (email) { | ||
| contacts.push({ | ||
| channel: 'email', | ||
| value: email, | ||
| handle: author.login, | ||
| role: 'committer', | ||
| tier: 'D', | ||
| provenance, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return contacts | ||
| } | ||
|
|
||
| async function fetchContributorStats( | ||
| path: string, | ||
| target: RepoTarget, | ||
| owner: string, | ||
| name: string, | ||
| githubGet: ExtractorDeps['githubGet'], | ||
| sleep: (ms: number) => Promise<void>, | ||
| ): Promise<ContributorStat[] | null> { | ||
| for (let attempt = 1; attempt <= STATS_POLL_ATTEMPTS; attempt++) { | ||
| let status: number | ||
| let text: string | null | ||
| try { | ||
| ;({ status, text } = await githubGet(path, { extraOkStatuses: [202] })) | ||
| } catch (err) { | ||
| log.warn( | ||
| { repoId: target.repoId, owner, name, errMsg: (err as Error).message }, | ||
| 'Contributor stats lookup failed', | ||
| ) | ||
| return null | ||
| } | ||
|
|
||
| if (status === 202) { | ||
| if (attempt < STATS_POLL_ATTEMPTS) await sleep(STATS_POLL_DELAY_MS) | ||
| continue | ||
| } | ||
|
|
||
| if (!text) return null | ||
| try { | ||
| const parsed = JSON.parse(text) | ||
| return Array.isArray(parsed) ? (parsed as ContributorStat[]) : null | ||
| } catch (err) { | ||
| log.warn( | ||
| { repoId: target.repoId, owner, name, errMsg: (err as Error).message }, | ||
| 'Contributor stats lookup failed', | ||
| ) | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| log.info( | ||
| { repoId: target.repoId, owner, name }, | ||
| 'Contributor stats still computing after polling — skipping this pass', | ||
| ) | ||
| return null | ||
| } | ||
|
|
||
| export async function fetchTopCommitters( | ||
| target: RepoTarget, | ||
| deps: Pick<ExtractorDeps, 'githubGet'>, | ||
| now: () => Date = () => new Date(), | ||
| sleep: (ms: number) => Promise<void> = (ms) => new Promise((r) => setTimeout(r, ms)), | ||
| ): Promise<RawContact[]> { | ||
| let owner: string | ||
| let name: string | ||
| try { | ||
| ;({ owner, name } = parseGithubUrl(target.url)) | ||
| } catch { | ||
| return [] | ||
| } | ||
|
|
||
| const path = `/repos/${owner}/${name}/stats/contributors` | ||
| const stats = await fetchContributorStats(path, target, owner, name, deps.githubGet, sleep) | ||
|
Comment on lines
+191
to
+192
|
||
| if (!stats) return [] | ||
|
|
||
| const sinceUnixSeconds = Math.floor(now().getTime() / 1000) - SINCE_DAYS * 24 * 60 * 60 | ||
| const authors = aggregateTopCommitters(stats, sinceUnixSeconds) | ||
| const fetchedAt = new Date().toISOString() | ||
| return mapCommittersToContacts( | ||
| authors, | ||
| fetchedAt, | ||
| `https://api.github.com${path}`, | ||
| deps.githubGet, | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.