-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add trust level computation core module #138
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
Open
dreyfus92
wants to merge
2
commits into
e18e:main
Choose a base branch
from
dreyfus92:unify-logic-001
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
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,5 @@ | ||
| /** | ||
| * Core modules containing pure, reusable logic that can be imported | ||
| * by external tools like the e18e GitHub Action. | ||
| */ | ||
| export * from './trust.js'; | ||
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,161 @@ | ||
| /** | ||
| * Trust level computation for npm packages based on provenance attestations. | ||
| * | ||
| * This module provides pure functions for computing trust levels without | ||
| * making network calls. The actual package metadata must be fetched separately. | ||
| */ | ||
|
|
||
| /** | ||
| * The provenance status of a package, indicating its level of trust. | ||
| * | ||
| * - `trusted-with-provenance`: Published by a trusted publisher with provenance attestations | ||
| * - `provenance`: Has provenance attestations but not from a trusted publisher | ||
| * - `none`: No provenance attestations | ||
| */ | ||
| export type ProvenanceStatus = | ||
| | 'trusted-with-provenance' | ||
| | 'provenance' | ||
| | 'none'; | ||
|
|
||
| /** | ||
| * Metadata about an npm package version, containing provenance information. | ||
| * This is a subset of the full npm registry metadata. | ||
| */ | ||
| export interface PackageProvenanceMetadata { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. presumably there's a util which fetches the metadata and uses this type too? do we want to include that in here? where does the action have it today? |
||
| name: string; | ||
| version: string; | ||
| dist?: { | ||
| attestations?: { | ||
| url: string; | ||
| provenance?: unknown; | ||
| }; | ||
| }; | ||
| _npmUser?: { | ||
| name: string; | ||
| email: string; | ||
| trustedPublisher?: unknown; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Result of computing the minimum trust level across a set of packages. | ||
| */ | ||
| export interface MinTrustLevelResult { | ||
| level: number; | ||
| status: ProvenanceStatus; | ||
| } | ||
|
|
||
| /** | ||
| * Summary of trust levels across a set of dependencies. | ||
| */ | ||
| export interface TrustSummary { | ||
| trusted: number; | ||
| provenance: number; | ||
| untrusted: number; | ||
| total: number; | ||
| } | ||
|
|
||
| /** | ||
| * Determines the provenance status of a package from its metadata. | ||
| * | ||
| * @param meta - Package metadata from the npm registry | ||
| * @returns The provenance status of the package | ||
| */ | ||
| export function getProvenance( | ||
| meta: PackageProvenanceMetadata | ||
| ): ProvenanceStatus { | ||
| if (meta._npmUser?.trustedPublisher) { | ||
| return 'trusted-with-provenance'; | ||
| } | ||
| if (meta.dist?.attestations?.provenance) { | ||
| return 'provenance'; | ||
| } | ||
| return 'none'; | ||
| } | ||
|
|
||
| /** | ||
| * Converts a provenance status to a numeric trust level. | ||
| * | ||
| * Higher numbers indicate higher trust: | ||
| * - 2: trusted-with-provenance | ||
| * - 1: provenance | ||
| * - 0: none | ||
| * | ||
| * @param status - The provenance status | ||
| * @returns The numeric trust level (0-2) | ||
| */ | ||
| export function getTrustLevel(status: ProvenanceStatus): number { | ||
| switch (status) { | ||
| case 'trusted-with-provenance': | ||
| return 2; | ||
| case 'provenance': | ||
| return 1; | ||
| case 'none': | ||
| return 0; | ||
| default: | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Finds the minimum trust level across a set of provenance statuses. | ||
| * | ||
| * This is useful for determining the overall trust level of a dependency tree, | ||
| * where the weakest link determines the overall security. | ||
| * | ||
| * @param statuses - An iterable of provenance statuses | ||
| * @returns The minimum trust level and its corresponding status | ||
| */ | ||
| export function getMinTrustLevel( | ||
| statuses: Iterable<ProvenanceStatus> | ||
| ): MinTrustLevelResult { | ||
| let result: MinTrustLevelResult | null = null; | ||
|
|
||
| for (const status of statuses) { | ||
| const level = getTrustLevel(status); | ||
| if (result === null || level < result.level) { | ||
| result = {level, status}; | ||
| } | ||
| } | ||
|
|
||
| if (!result) { | ||
| return {level: 0, status: 'none'}; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Computes a summary of trust levels from a collection of provenance statuses. | ||
| * | ||
| * @param statuses - An iterable of provenance statuses | ||
| * @returns A summary with counts for each trust category | ||
| */ | ||
| export function computeTrustSummary( | ||
| statuses: Iterable<ProvenanceStatus> | ||
| ): TrustSummary { | ||
| let trusted = 0; | ||
| let provenance = 0; | ||
| let untrusted = 0; | ||
|
|
||
| for (const status of statuses) { | ||
| switch (status) { | ||
| case 'trusted-with-provenance': | ||
| trusted++; | ||
| break; | ||
| case 'provenance': | ||
| provenance++; | ||
| break; | ||
| case 'none': | ||
| untrusted++; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| trusted, | ||
| provenance, | ||
| untrusted, | ||
| total: trusted + provenance + untrusted | ||
| }; | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if we should drop this barrel file as we export them in the root anyway
instead of this re-exporting everything, and the root re-exporting them again. we can just put this in the root