-
Notifications
You must be signed in to change notification settings - Fork 2
[PPSC-878] feat(supply-chain): add Java (Maven/Gradle) package release-age enforcement #211
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
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
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
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
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
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,79 @@ | ||
| package check | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ParseGradleLockfile parses a Gradle lockfile (gradle.lockfile). | ||
| // Format: one dependency per line as "group:artifact:version=configurations" | ||
| // after a header, where the suffix after "=" is a comma-separated list of the | ||
| // configurations that resolved the dependency (e.g. "compileClasspath,runtimeClasspath"). | ||
| // The parser treats everything after "=" as metadata and ignores it. | ||
| // armis:ignore cwe:22 cwe:23 cwe:73 reason:local CLI reading the user's own lockfile; path is from local detection or an explicit --lockfile flag, not untrusted input crossing a trust boundary | ||
| func ParseGradleLockfile(path string) ([]PackageEntry, error) { | ||
| // armis:ignore cwe:22 cwe:23 cwe:73 reason:local CLI reading the user's own lockfile; path is from local detection or an explicit --lockfile flag, not untrusted input crossing a trust boundary | ||
| data, err := readLockfile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| scanner := bufio.NewScanner(bytes.NewReader(data)) | ||
| // Gradle lockfile lines can carry a long, comma-separated configuration list | ||
| // after "=", so raise the scanner's per-line cap. data is already size-bounded | ||
| // by readLockfile. | ||
| scanner.Buffer(make([]byte, 0, bufio.MaxScanTokenSize), maxLockfileSize) | ||
|
|
||
| var entries []PackageEntry | ||
| headerPassed := false | ||
|
|
||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
|
|
||
| if line == "" || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
|
|
||
| // The header line "empty=" signals end of preamble in some formats | ||
| if !headerPassed { | ||
| if strings.Contains(line, "=") && !strings.Contains(line, ":") { | ||
| // Metadata line like "empty=" | ||
| continue | ||
| } | ||
| headerPassed = true | ||
| } | ||
|
|
||
| // Expected: group:artifact:version=configurations | ||
| eqIdx := strings.Index(line, "=") | ||
| gav := line | ||
| if eqIdx > 0 { | ||
| gav = line[:eqIdx] | ||
| } | ||
|
|
||
| parts := strings.Split(gav, ":") | ||
| if len(parts) < 3 { | ||
| continue | ||
| } | ||
|
|
||
| group := parts[0] | ||
| artifact := parts[1] | ||
| version := parts[2] | ||
|
|
||
| if group == "" || artifact == "" || version == "" { | ||
| continue | ||
| } | ||
|
|
||
| entries = append(entries, PackageEntry{ | ||
| Name: group + ":" + artifact, | ||
| Version: version, | ||
| }) | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return nil, fmt.Errorf("scanning gradle lockfile: %w", err) | ||
| } | ||
|
|
||
| return entries, nil | ||
| } | ||
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,44 @@ | ||
| package check | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "sort" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseGradleLockfile(t *testing.T) { | ||
| t.Run("valid gradle lockfile", func(t *testing.T) { | ||
| entries, err := ParseGradleLockfile(filepath.Join("testdata", "gradle.lockfile")) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| sort.Slice(entries, func(i, j int) bool { | ||
| return entries[i].Name < entries[j].Name | ||
| }) | ||
|
|
||
| expected := []PackageEntry{ | ||
| {Name: "com.fasterxml.jackson.core:jackson-core", Version: "2.16.0"}, | ||
| {Name: "com.google.guava:guava", Version: "32.1.3-jre"}, | ||
| {Name: "org.slf4j:slf4j-api", Version: "2.0.9"}, | ||
| {Name: "org.springframework:spring-core", Version: "6.1.2"}, | ||
| } | ||
|
|
||
| if len(entries) != len(expected) { | ||
| t.Fatalf("expected %d entries, got %d: %+v", len(expected), len(entries), entries) | ||
| } | ||
|
|
||
| for i, e := range entries { | ||
| if e.Name != expected[i].Name || e.Version != expected[i].Version { | ||
| t.Errorf("entry %d: expected %s@%s, got %s@%s", i, expected[i].Name, expected[i].Version, e.Name, e.Version) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| t.Run("file not found", func(t *testing.T) { | ||
| _, err := ParseGradleLockfile("testdata/nonexistent.lockfile") | ||
| if err == nil { | ||
| t.Fatal("expected error for nonexistent file") | ||
| } | ||
| }) | ||
| } |
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,99 @@ | ||
| package check | ||
|
|
||
| import ( | ||
| "encoding/xml" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| type pomProject struct { | ||
| XMLName xml.Name `xml:"project"` | ||
| Dependencies pomDeps `xml:"dependencies"` | ||
| DepMgmt pomDepMgmt `xml:"dependencyManagement"` | ||
| } | ||
|
|
||
| type pomDepMgmt struct { | ||
| Dependencies pomDeps `xml:"dependencies"` | ||
| } | ||
|
|
||
| type pomDeps struct { | ||
| Dependency []pomDependency `xml:"dependency"` | ||
| } | ||
|
|
||
| type pomDependency struct { | ||
| GroupID string `xml:"groupId"` | ||
| ArtifactID string `xml:"artifactId"` | ||
| Version string `xml:"version"` | ||
| Scope string `xml:"scope"` | ||
| } | ||
|
|
||
| // ParseMavenDeps parses a pom.xml file for direct dependencies with explicit versions. | ||
| // Only direct dependencies are covered; transitive dependencies resolved by Maven | ||
| // at build time are not present in pom.xml. Entries under <dependencyManagement> | ||
| // are used only as a fallback version source for dependencies declared in | ||
| // <dependencies> that omit their own <version>; managed entries are not treated | ||
| // as dependencies themselves, since declaring a managed version does not pull a | ||
| // package into the build. | ||
| // armis:ignore cwe:22 cwe:23 cwe:73 reason:local CLI reading the user's own lockfile; path is from local detection or an explicit --lockfile flag, not untrusted input crossing a trust boundary | ||
| func ParseMavenDeps(path string) ([]PackageEntry, error) { | ||
| // armis:ignore cwe:22 cwe:23 cwe:73 reason:local CLI reading the user's own lockfile; path is from local detection or an explicit --lockfile flag, not untrusted input crossing a trust boundary | ||
| data, err := readLockfile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var project pomProject | ||
| // armis:ignore cwe:502 cwe:770 reason:xml.Unmarshal into a typed struct does not execute code; data is size-bounded by readLockfile and is the user's own lockfile, not untrusted data | ||
| if err := xml.Unmarshal(data, &project); err != nil { | ||
| return nil, fmt.Errorf("parsing pom.xml: %w", err) | ||
| } | ||
|
|
||
| // Build a groupId:artifactId -> version index from <dependencyManagement> so | ||
| // dependencies that omit their <version> can inherit the managed value. | ||
| managedVersions := make(map[string]string) | ||
| for _, dep := range project.DepMgmt.Dependencies.Dependency { | ||
| if dep.GroupID == "" || dep.ArtifactID == "" || dep.Version == "" { | ||
| continue | ||
| } | ||
| managedVersions[dep.GroupID+":"+dep.ArtifactID] = dep.Version | ||
| } | ||
|
|
||
| var entries []PackageEntry | ||
| seen := make(map[string]bool) | ||
|
|
||
| for _, dep := range project.Dependencies.Dependency { | ||
| // Backfill a missing version from <dependencyManagement> before converting. | ||
| if dep.Version == "" { | ||
| dep.Version = managedVersions[dep.GroupID+":"+dep.ArtifactID] | ||
| } | ||
| entry := mavenDepToEntry(dep) | ||
| if entry != nil && !seen[entry.Name+"@"+entry.Version] { | ||
| seen[entry.Name+"@"+entry.Version] = true | ||
| entries = append(entries, *entry) | ||
| } | ||
| } | ||
|
|
||
| return entries, nil | ||
| } | ||
|
|
||
| func mavenDepToEntry(dep pomDependency) *PackageEntry { | ||
| if dep.GroupID == "" || dep.ArtifactID == "" || dep.Version == "" { | ||
| return nil | ||
| } | ||
|
|
||
| // Skip property references that can't be resolved | ||
| if strings.Contains(dep.Version, "${") { | ||
| return nil | ||
| } | ||
|
|
||
| // Skip test and provided scope | ||
| scope := strings.ToLower(dep.Scope) | ||
| if scope == "test" || scope == "provided" { | ||
| return nil | ||
| } | ||
|
|
||
| return &PackageEntry{ | ||
| Name: dep.GroupID + ":" + dep.ArtifactID, | ||
| Version: dep.Version, | ||
| } | ||
| } |
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.