Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Sideload updater: only the update's own file, only with its SHA-256

Duarte Santosd7152f87a7ba
diff --git a/frontend/android/app/src/sideload/java/ch/duartesantos/opengym/InstallPlugin.java b/frontend/android/app/src/sideload/java/ch/duartesantos/opengym/InstallPlugin.java
index 244fbd107..bb9f0deac 100644
--- a/frontend/android/app/src/sideload/java/ch/duartesantos/opengym/InstallPlugin.java
+++ b/frontend/android/app/src/sideload/java/ch/duartesantos/opengym/InstallPlugin.java
@@ -21,8 +21,10 @@ import java.security.MessageDigest;
* const Install = registerPlugin('Install');
* await Install.installApk({ fileName: 'opengym-update.apk', sha256: '<64 hex>' });
*
- * With `sha256` the file is streamed through SHA-256 first and a mismatch is refused (and the
- * file deleted) before the installer opens: the APK is ~300 MB, too big to hash in the WebView.
+ * The file is streamed through SHA-256 first and a mismatch is refused (and the file deleted)
+ * before the installer opens: the APK is ~300 MB, too big to hash in the WebView. Both arguments
+ * are required and checked (UpdateFile): only the update's own name in the cache directory, and
+ * only with a hash. A call without one used to install unchecked.
*/
@CapacitorPlugin(name = "Install")
public class InstallPlugin extends Plugin {
@@ -30,8 +32,13 @@ public class InstallPlugin extends Plugin {
@PluginMethod
public void installApk(PluginCall call) {
String fileName = call.getString("fileName");
- if (fileName == null || fileName.isEmpty()) {
- call.reject("fileName is required");
+ if (!UpdateFile.nameOk(fileName)) {
+ call.reject("not an update file name");
+ return;
+ }
+ String expected = call.getString("sha256");
+ if (!UpdateFile.hashOk(expected)) {
+ call.reject("sha256 is required");
return;
}
@@ -41,18 +48,15 @@ public class InstallPlugin extends Plugin {
return;
}
- String expected = call.getString("sha256");
- if (expected != null && !expected.isEmpty()) {
- try {
- if (!expected.equalsIgnoreCase(sha256(file))) {
- file.delete();
- call.reject("SHA-256 mismatch: the download may be corrupted or tampered with");
- return;
- }
- } catch (Exception e) {
- call.reject("Could not check the download: " + e.getMessage());
+ try {
+ if (!expected.equalsIgnoreCase(sha256(file))) {
+ file.delete();
+ call.reject("SHA-256 mismatch: the download may be corrupted or tampered with");
return;
}
+ } catch (Exception e) {
+ call.reject("Could not check the download: " + e.getMessage());
+ return;
}
Uri uri = FileProvider.getUriForFile(
diff --git a/frontend/android/app/src/sideload/java/ch/duartesantos/opengym/UpdateFile.java b/frontend/android/app/src/sideload/java/ch/duartesantos/opengym/UpdateFile.java
new file mode 100644
index 000000000..d1fcef15e
--- /dev/null
+++ b/frontend/android/app/src/sideload/java/ch/duartesantos/opengym/UpdateFile.java
@@ -0,0 +1,26 @@
+package ch.duartesantos.opengym;
+
+import java.util.regex.Pattern;
+
+/**
+ * What InstallPlugin agrees to hand the package installer: only the update the app itself
+ * downloaded into its cache (lib/update.js names it opengym-update.apk; a per-size variant may
+ * carry a short suffix), and only with the SHA-256 the release published next to it. Plain Java,
+ * no Android types, so the rules are unit-tested on their own (src/testSideload).
+ */
+final class UpdateFile {
+ private static final Pattern NAME = Pattern.compile("opengym-update(-[a-z0-9]{1,16})?\\.apk");
+ private static final Pattern SHA256 = Pattern.compile("[0-9a-fA-F]{64}");
+
+ private UpdateFile() {}
+
+ /** A file name the updater writes: no directory part, no other file of the app's. */
+ static boolean nameOk(String fileName) {
+ return fileName != null && NAME.matcher(fileName).matches();
+ }
+
+ /** A SHA-256 in hex. Without one nothing is installed: a missing hash is not a pass. */
+ static boolean hashOk(String sha256) {
+ return sha256 != null && SHA256.matcher(sha256).matches();
+ }
+}
diff --git a/frontend/android/app/src/testSideload/java/ch/duartesantos/opengym/UpdateFileTest.java b/frontend/android/app/src/testSideload/java/ch/duartesantos/opengym/UpdateFileTest.java
new file mode 100644
index 000000000..ef69f8f38
--- /dev/null
+++ b/frontend/android/app/src/testSideload/java/ch/duartesantos/opengym/UpdateFileTest.java
@@ -0,0 +1,33 @@
+package ch.duartesantos.opengym;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class UpdateFileTest {
+ @Test
+ public void onlyTheUpdatersOwnFileName() {
+ assertTrue(UpdateFile.nameOk("opengym-update.apk"));
+ assertTrue(UpdateFile.nameOk("opengym-update-360.apk"));
+ assertFalse(UpdateFile.nameOk(null));
+ assertFalse(UpdateFile.nameOk(""));
+ assertFalse(UpdateFile.nameOk("../files/opengym-update.apk"));
+ assertFalse(UpdateFile.nameOk("sub/opengym-update.apk"));
+ assertFalse(UpdateFile.nameOk("opengym-update.apk/"));
+ assertFalse(UpdateFile.nameOk("other.apk"));
+ assertFalse(UpdateFile.nameOk("opengym-update.apk.sha256"));
+ assertFalse(UpdateFile.nameOk("opengym-update-../x.apk"));
+ assertFalse(UpdateFile.nameOk("opengym-update.apk\n"));
+ }
+
+ @Test
+ public void aHashIsRequired() {
+ assertTrue(UpdateFile.hashOk("0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789abcdef"));
+ assertFalse(UpdateFile.hashOk(null));
+ assertFalse(UpdateFile.hashOk(""));
+ assertFalse(UpdateFile.hashOk("abc"));
+ assertFalse(UpdateFile.hashOk("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdeg"));
+ assertFalse(UpdateFile.hashOk("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0"));
+ }
+}