Skip to content

fix: guard against null version and change property name to avoid conflicts #2116

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 4 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package com.amazonaws.encryptionsdk.internal;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

/** This class specifies the versioning system for the AWS KMS encryption client. */
Expand Down Expand Up @@ -43,8 +45,27 @@ public static String versionNumber() {
try {
final Properties properties = new Properties();
final ClassLoader loader = VersionInfo.class.getClassLoader();
properties.load(loader.getResourceAsStream("project.properties"));
return properties.getProperty("version");
// Other JARs on the classpath may also define project.properties
// Enumerate through and find the one for the ESDK
Enumeration<URL> urls = loader.getResources("project.properties");
if (urls == null) {
return UNKNOWN_VERSION;
}
while (urls.hasMoreElements()) {
URL thisURL = urls.nextElement();
if (thisURL.getPath().contains("aws-encryption-sdk-java")) {
properties.load(thisURL.openStream());
break;
}
}
String maybeVersion = properties.getProperty("esdkVersion");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the real problem is in line 47: the class loader will load only the first resource matching the name project.properties. If some other JAR file on the classpath contains such a file and appears before this library on the classpath, it will be loaded instead of this library's project.properties file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, this just prevents the program from crashing. I'm going to explore ways to load the specific project.properties file next.

if (maybeVersion == null) {
// This should never happen in practice,
// but is included for robustness.
return UNKNOWN_VERSION;
} else {
return maybeVersion;
}
} catch (final IOException ex) {
return UNKNOWN_VERSION;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/project.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=${project.version}
esdkVersion=${project.version}