Skip to content

Commit

Permalink
fix: avoid infinite loops parsing Maven poms with syntax errors (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Apr 27, 2023
1 parent 8bc880e commit c988b29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/lockfile/fixtures/maven/invalid-syntax.xml
@@ -0,0 +1,13 @@
<project>
<properties>
<${Id}.version>${project.version}</${Id}.version>
</properties>

<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.42.Final</version>
</dependency>
</dependencies>
</project>
6 changes: 5 additions & 1 deletion pkg/lockfile/parse-maven-lock.go
Expand Up @@ -72,7 +72,11 @@ func (p *MavenLockProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
p.m = map[string]string{}

for {
t, _ := d.Token()
t, err := d.Token()

if err != nil {
return fmt.Errorf("%w", err)
}

switch tt := t.(type) {
case xml.StartElement:
Expand Down
9 changes: 9 additions & 0 deletions pkg/lockfile/parse-maven-lock_test.go
Expand Up @@ -23,6 +23,15 @@ func TestParseMavenLock_Invalid(t *testing.T) {
expectPackages(t, packages, []lockfile.PackageDetails{})
}

func TestParseMavenLock_InvalidSyntax(t *testing.T) {
t.Parallel()

packages, err := lockfile.ParseMavenLock("fixtures/maven/invalid-syntax.xml")

expectErrContaining(t, err, "XML syntax error")
expectPackages(t, packages, []lockfile.PackageDetails{})
}

func TestParseMavenLock_NoPackages(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit c988b29

Please sign in to comment.