Skip to content
Closed
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
22 changes: 3 additions & 19 deletions VotingPlugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,24 +225,8 @@
<exclude>META-INF/versions/**</exclude>
</excludes>
</filter>
<filter>
<artifact>org.xerial:sqlite-jdbc</artifact>
<excludes>
<!-- Keep the common Linux x86_64 native for offline startup. Other
targets are fetched with a pinned digest only when SQLite is selected. -->
<exclude>org/sqlite/native/FreeBSD/**</exclude>
<exclude>org/sqlite/native/Linux-Musl/**</exclude>
<exclude>org/sqlite/native/Linux/aarch64/**</exclude>
<exclude>org/sqlite/native/Linux/arm/**</exclude>
<exclude>org/sqlite/native/Linux/armv6/**</exclude>
<exclude>org/sqlite/native/Linux/armv7/**</exclude>
<exclude>org/sqlite/native/Linux/ppc64/**</exclude>
<exclude>org/sqlite/native/Linux/riscv64/**</exclude>
<exclude>org/sqlite/native/Linux/x86/**</exclude>
<exclude>org/sqlite/native/Mac/**</exclude>
<exclude>org/sqlite/native/Windows/**</exclude>
</excludes>
</filter>
<!-- Retain every official SQLite native for offline drop-in upgrades.
PackagedArtifactTest checks every target against the pinned driver. -->
<filter>
<artifact>*:*</artifact>
<excludes>
Expand Down Expand Up @@ -396,7 +380,7 @@
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.53.4.0</version>
<version>3.54.1.0</version>
<exclusions>
<exclusion>
<groupId>org.checkerframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,77 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.sqlite.util.OSInfo;

/** Prepares the current Xerial SQLite native without embedding every target in the plugin JAR. */
/** Verifies the embedded Xerial SQLite native, with a pinned-driver fallback for custom packaging. */
public final class SqliteNativeLibrary {
static final String DRIVER_FILE = "sqlite-jdbc-3.53.4.0.jar";
static final String DRIVER_SHA256 = "bcb1f51e36f940867e83342f9efbf5968ac44a6bef4d397bb4af7b17b45cd2fb";
static final String DRIVER_FILE = "sqlite-jdbc-3.54.1.0.jar";
static final String DRIVER_SHA256 = "f8dce420a6016278497f4f0560f65602b9db858de141b541cf91a4644ebe4639";
private static final URI DRIVER_URI = URI.create("https://maven-central.storage-download.googleapis.com/maven2/"
+ "org/xerial/sqlite-jdbc/3.53.4.0/" + DRIVER_FILE);
+ "org/xerial/sqlite-jdbc/3.54.1.0/" + DRIVER_FILE);
private static final long MAX_DRIVER_BYTES = 16L * 1024L * 1024L;
private static final int MAX_NATIVE_BYTES = 2 * 1024 * 1024;
// Digests from the official artifact identified by DRIVER_SHA256. Never trust a resource
// supplied by a different driver or classloader merely because its path matches.
private static final Map<String, String> NATIVE_SHA256 = Map.ofEntries(
Map.entry("org/sqlite/native/FreeBSD/aarch64/libsqlitejdbc.so",
"d450f90bfaf858c2e52505b4370c99ede31c07430c66c3231132a6920b26ed5c"),
Map.entry("org/sqlite/native/FreeBSD/x86/libsqlitejdbc.so",
"61f86fe5144d8965a3e64bfa48af874f6901167a8b3e4e6a370500640ac8ac1b"),
Map.entry("org/sqlite/native/FreeBSD/x86_64/libsqlitejdbc.so",
"04584ec9a87bc2d59e707949f2c9db3e206108dd8fce1cf1797f13d7e0760f30"),
Map.entry("org/sqlite/native/Linux/aarch64/libsqlitejdbc.so",
"2deb508458a39049204b0e92db5d82e53598eb4e2d34dff3ee29cc47a38f83ee"),
Map.entry("org/sqlite/native/Linux/arm/libsqlitejdbc.so",
"9b50eb59ae3a2d2a956287c53b3c6cf87cd5ffff58c00889fbd5a7a9ef220482"),
Map.entry("org/sqlite/native/Linux/armv6/libsqlitejdbc.so",
"3601ac7a21215b9dc928db447a63a28383dad419e579d77168ab6c91acbbbf3b"),
Map.entry("org/sqlite/native/Linux/armv7/libsqlitejdbc.so",
"43f0c78a6de21ce39539a0df93edf19bcef79c95358467128459d551666f098c"),
Map.entry("org/sqlite/native/Linux/ppc64/libsqlitejdbc.so",
"1a2f43080088c6230aa1554e1fc198e1961f8ef8f5df94656a612fee97ada41a"),
Map.entry("org/sqlite/native/Linux/riscv64/libsqlitejdbc.so",
"cbfb0bed9e5feca3dcec0fcd95d9e9c21f90382adc62be64fcd3300772d7c74d"),
Map.entry("org/sqlite/native/Linux/x86/libsqlitejdbc.so",
"7440830f45d7b2e32f16c27cc6d61e65070f9a7ea6ceff731a141943faf97512"),
Map.entry("org/sqlite/native/Linux/x86_64/libsqlitejdbc.so",
"9149f234a03da04caba72ce534dfa0f0e5b999ae8d03d630034be65f24c7b44c"),
Map.entry("org/sqlite/native/Linux-Musl/aarch64/libsqlitejdbc.so",
"126521645e72d05b57442583ab33b2c8494fd04d60d1be442a15536723e26861"),
Map.entry("org/sqlite/native/Linux-Musl/x86/libsqlitejdbc.so",
"cf9e993ea2a052f8ec3931a202f4fea0049e7f0276b3726c1ac87d52064c45c0"),
Map.entry("org/sqlite/native/Linux-Musl/x86_64/libsqlitejdbc.so",
"b21aac0da3a80a0c4cee035d5919021c991ee17c8284a76b38ab7b62e68bab52"),
Map.entry("org/sqlite/native/Mac/aarch64/libsqlitejdbc.dylib",
"47fa68b43f34552fbfedeed25e82875a42cfd53e81c33c7cee20ac3f729c060e"),
Map.entry("org/sqlite/native/Mac/x86_64/libsqlitejdbc.dylib",
"51d30fe476f0170cd42102baabb82c4b39d43996c9209cdc60876a4b928d85f7"),
Map.entry("org/sqlite/native/Windows/aarch64/sqlitejdbc.dll",
"59ec28822968199a7a94feb62a44357ab201c7bc3c55ebdf0e3e5ece840a45b6"),
Map.entry("org/sqlite/native/Windows/armv7/sqlitejdbc.dll",
"175095c594d0fa73e819d7b1066a4d8a19265d89bbe4bf0c629a29dc233b6e04"),
Map.entry("org/sqlite/native/Windows/x86/sqlitejdbc.dll",
"d03f852a3350e65f6ed8244e6f348d154aca10c89820e1387885dd95b4fa0445"),
Map.entry("org/sqlite/native/Windows/x86_64/sqlitejdbc.dll",
"4896f8ba8824a85f10f31383e38b9542b6043886a0c834e0d6c8606b02f82308"));
private static final long STALE_NATIVE_MILLIS = 24L * 60L * 60L * 1000L;

private SqliteNativeLibrary() {
}

/** Ensures Xerial can load the native for this operating system and architecture. */
public static synchronized void ensureAvailable(Path directory) throws IOException {
if (System.getProperty("org.sqlite.lib.path") != null) {
String configuredName = System.getProperty("org.sqlite.lib.name");
// A custom name alone selects a library through Xerial/java.library.path.
// The standard name still uses our verified extraction, including on noexec temp mounts.
if (System.getProperty("org.sqlite.lib.path") != null
|| (configuredName != null && !configuredName.equals(System.mapLibraryName("sqlitejdbc")))) {
try {
if (!org.sqlite.core.NativeDB.load()) throw new IOException("Configured SQLite native library did not load");
return;
Expand All @@ -53,10 +102,21 @@ static Path prepareNative(Path directory, String folder, ClassLoader resourceLoa
throws IOException {
String libraryName = nativeLibraryName(folder);
String resource = "org/sqlite/native/" + folder + "/" + libraryName;
if (resourceLoader.getResource(resource) != null) return null;
byte[] embeddedBytes = null;
try (InputStream embedded = resourceLoader.getResourceAsStream(resource)) {
if (embedded != null) {
String expected = NATIVE_SHA256.get(resource);
byte[] bytes = embedded.readNBytes(MAX_NATIVE_BYTES + 1);
if (expected == null || bytes.length == 0 || bytes.length > MAX_NATIVE_BYTES
|| !expected.equals(sha256(bytes))) {
throw new IOException("Embedded SQLite native failed SHA-256 verification: " + resource);
}
embeddedBytes = bytes;
}
}
Files.createDirectories(directory);
Path driver = directory.resolve(DRIVER_FILE);
if (!hasExpectedDigest(driver, DRIVER_SHA256)) {
if (embeddedBytes == null && !hasExpectedDigest(driver, DRIVER_SHA256)) {
try {
downloadVerified(driver, fetcher);
} catch (IOException failure) {
Expand All @@ -70,7 +130,18 @@ static Path prepareNative(Path directory, String folder, ClassLoader resourceLoa
Path nativeDirectory = platformDirectory.resolve("load-" + UUID.randomUUID());
Files.createDirectories(nativeDirectory);
Path nativeLibrary = nativeDirectory.resolve(libraryName);
extractVerifiedEntry(driver, resource, nativeLibrary);
if (embeddedBytes != null) {
// Keep loading in the plugin directory, including when the JVM temp directory
// is read-only or mounted noexec on a previously fallback-backed platform.
Path temporary = Files.createTempFile(nativeDirectory, libraryName + ".", ".extract");
setPrivatePermissions(temporary);
try {
Files.write(temporary, embeddedBytes);
moveReplacing(temporary, nativeLibrary);
} finally { Files.deleteIfExists(temporary); }
} else {
extractVerifiedEntry(driver, resource, nativeLibrary);
}
nativeLibrary.toFile().deleteOnExit();
nativeDirectory.toFile().deleteOnExit();
return nativeLibrary;
Expand Down Expand Up @@ -187,6 +258,14 @@ private static void moveReplacing(Path source, Path target) throws IOException {
}
}

private static String sha256(byte[] bytes) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
} catch (NoSuchAlgorithmException impossible) {
throw new IllegalStateException("SHA-256 is unavailable", impossible);
}
}

private static boolean hasExpectedDigest(Path file, String expected) throws IOException {
if (!Files.isRegularFile(file)) return false;
try (InputStream input = Files.newInputStream(file)) {
Expand Down
Loading
Loading