Skip to content

Commit 99b0ad8

Browse files
committed
fix: close XMLStreamReader in JavaMavenProvider and make readLicenseFromManifest abstract
1 parent 178570d commit 99b0ad8

6 files changed

Lines changed: 55 additions & 29 deletions

File tree

src/main/java/io/github/guacsec/trustifyda/Provider.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,14 @@ protected Provider(Ecosystem.Type ecosystem, Path manifest) {
7373
public abstract Content provideComponent() throws IOException;
7474

7575
/**
76-
* Read the project license from the manifest file. Providers that support manifest-level license
77-
* declarations (e.g., pom.xml {@code <licenses>}, package.json {@code license}, Cargo.toml {@code
78-
* license}) should override this method.
76+
* Read the project license from the manifest file. Each provider must decide how to extract the
77+
* license from its manifest (e.g., pom.xml {@code <licenses>}, package.json {@code license},
78+
* Cargo.toml {@code license}). Providers without a manifest-level license field should fall back
79+
* to {@link LicenseUtils#readLicenseFile(Path)}.
7980
*
8081
* @return SPDX identifier or license name from the manifest, or null if not available
8182
*/
82-
public String readLicenseFromManifest() {
83-
// Default: no manifest license field. Falls back to LICENSE file detection.
84-
return LicenseUtils.readLicenseFile(manifest);
85-
}
83+
public abstract String readLicenseFromManifest();
8684

8785
/**
8886
* If a package manager requires having a lock file it must exist in the provided path

src/main/java/io/github/guacsec/trustifyda/providers/CargoProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,15 @@ public CargoProvider(Path manifest) {
612612

613613
@Override
614614
public String readLicenseFromManifest() {
615-
return readLicenseFromToml(null);
615+
String manifestLicense = readLicenseFromToml(null);
616+
return LicenseUtils.getLicense(manifestLicense, manifest);
616617
}
617618

618619
private String readLicenseFromToml(TomlParseResult existingResult) {
619620
try {
620621
TomlParseResult tomlResult = existingResult != null ? existingResult : Toml.parse(manifest);
621622
if (tomlResult.hasErrors()) {
622-
return LicenseUtils.getLicense(null, manifest);
623+
return null;
623624
}
624625
String license = tomlResult.getString("package.license");
625626
return LicenseUtils.getLicense(license, manifest);

src/main/java/io/github/guacsec/trustifyda/providers/GoModulesProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.github.packageurl.PackageURL;
2323
import io.github.guacsec.trustifyda.Api;
2424
import io.github.guacsec.trustifyda.Provider;
25+
import io.github.guacsec.trustifyda.license.LicenseUtils;
2526
import io.github.guacsec.trustifyda.logging.LoggersFactory;
2627
import io.github.guacsec.trustifyda.sbom.Sbom;
2728
import io.github.guacsec.trustifyda.sbom.SbomFactory;
@@ -70,6 +71,11 @@ public GoModulesProvider(Path manifest) {
7071
this.mainModuleVersion = getDefaultMainModuleVersion();
7172
}
7273

74+
@Override
75+
public String readLicenseFromManifest() {
76+
return LicenseUtils.readLicenseFile(manifest);
77+
}
78+
7379
@Override
7480
public Content provideStack() throws IOException {
7581
// check for custom executable

src/main/java/io/github/guacsec/trustifyda/providers/GradleProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.github.packageurl.PackageURL;
2323
import io.github.guacsec.trustifyda.Api;
2424
import io.github.guacsec.trustifyda.Provider;
25+
import io.github.guacsec.trustifyda.license.LicenseUtils;
2526
import io.github.guacsec.trustifyda.logging.LoggersFactory;
2627
import io.github.guacsec.trustifyda.sbom.Sbom;
2728
import io.github.guacsec.trustifyda.sbom.SbomFactory;
@@ -64,6 +65,11 @@ public GradleProvider(Path manifest) {
6465
super(Type.GRADLE, manifest);
6566
}
6667

68+
@Override
69+
public String readLicenseFromManifest() {
70+
return LicenseUtils.readLicenseFile(manifest);
71+
}
72+
6773
@Override
6874
public Content provideStack() throws IOException {
6975
Path tempFile = getDependencies(manifest);

src/main/java/io/github/guacsec/trustifyda/providers/JavaMavenProvider.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,37 @@ private String readLicenseFromPom(Path pomPath) {
8484
XMLInputFactory factory = XMLInputFactory.newInstance();
8585
try (InputStream is = Files.newInputStream(pomPath)) {
8686
XMLStreamReader reader = factory.createXMLStreamReader(is);
87-
boolean insideLicenses = false;
88-
boolean insideLicense = false;
89-
while (reader.hasNext()) {
90-
int event = reader.next();
91-
if (event == XMLStreamConstants.START_ELEMENT) {
92-
String name = reader.getLocalName();
93-
if ("licenses".equals(name)) {
94-
insideLicenses = true;
95-
} else if (insideLicenses && "license".equals(name)) {
96-
insideLicense = true;
97-
} else if (insideLicense && "name".equals(name)) {
98-
String license = reader.getElementText();
99-
if (license != null && !license.isBlank()) {
100-
return license.trim();
87+
try {
88+
boolean insideLicenses = false;
89+
boolean insideLicense = false;
90+
while (reader.hasNext()) {
91+
int event = reader.next();
92+
if (event == XMLStreamConstants.START_ELEMENT) {
93+
String name = reader.getLocalName();
94+
if ("licenses".equals(name)) {
95+
insideLicenses = true;
96+
} else if (insideLicenses && "license".equals(name)) {
97+
insideLicense = true;
98+
} else if (insideLicense && "name".equals(name)) {
99+
String license = reader.getElementText();
100+
if (license != null && !license.isBlank()) {
101+
return license.trim();
102+
}
103+
}
104+
} else if (event == XMLStreamConstants.END_ELEMENT) {
105+
String name = reader.getLocalName();
106+
if ("license".equals(name)) {
107+
insideLicense = false;
108+
} else if ("licenses".equals(name)) {
109+
break;
101110
}
102111
}
103-
} else if (event == XMLStreamConstants.END_ELEMENT) {
104-
String name = reader.getLocalName();
105-
if ("license".equals(name)) {
106-
insideLicense = false;
107-
} else if ("licenses".equals(name)) {
108-
break;
112+
}
113+
} finally {
114+
if (!Objects.isNull(reader)) {
115+
try {
116+
reader.close();
117+
} catch (XMLStreamException e) {
109118
}
110119
}
111120
}

src/main/java/io/github/guacsec/trustifyda/providers/PythonPipProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.github.packageurl.PackageURL;
2424
import io.github.guacsec.trustifyda.Api;
2525
import io.github.guacsec.trustifyda.Provider;
26+
import io.github.guacsec.trustifyda.license.LicenseUtils;
2627
import io.github.guacsec.trustifyda.logging.LoggersFactory;
2728
import io.github.guacsec.trustifyda.sbom.Sbom;
2829
import io.github.guacsec.trustifyda.sbom.SbomFactory;
@@ -60,6 +61,11 @@ public PythonPipProvider(Path manifest) {
6061
super(Ecosystem.Type.PYTHON, manifest);
6162
}
6263

64+
@Override
65+
public String readLicenseFromManifest() {
66+
return LicenseUtils.readLicenseFile(manifest);
67+
}
68+
6369
@Override
6470
public Content provideStack() throws IOException {
6571
PythonControllerBase pythonController = getPythonController();

0 commit comments

Comments
 (0)