Skip to content

Commit

Permalink
Check downloaded jar with hardcoded hash
Browse files Browse the repository at this point in the history
  • Loading branch information
TechnicJelle committed Aug 28, 2024
1 parent d95f9bc commit b0f50bb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import "path_picker_button.dart";
import "prefs.dart";
import "tech_app.dart";

const String commit = String.fromEnvironment("commit", defaultValue: "development");

// == Hardcoded BlueMap CLI JAR download URL and hash ==
const blueMapCliJarUrl = "https://github.com/BlueMap-Minecraft/BlueMap/releases/download/"
"v5.3/BlueMap-5.3-cli.jar";
const blueMapCliJarHash =
"a0be9a36325f3caabc6306e9c6dd306aeec464b8abe907e93b6589659c8751f5"; //SHA256

String get blueMapCliJarName => blueMapCliJarUrl.split("/").last;

const String commit = String.fromEnvironment("commit", defaultValue: "development");

final projectDirectoryProvider = Provider<Directory?>((ref) {
final String? bluemapJarPath = Prefs.instance.projectPath;
if (bluemapJarPath == null) {
Expand Down
39 changes: 39 additions & 0 deletions lib/path_picker_button.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import "dart:convert";
import "dart:io";

import "package:crypto/crypto.dart";
import "package:file_picker/file_picker.dart";
import "package:flutter/material.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:path/path.dart" as p;
import "package:url_launcher/url_launcher_string.dart";

import "main.dart";
import "prefs.dart";
Expand All @@ -14,6 +16,8 @@ enum _PickingState {
picking,
scanning,
downloading,
hashing,
wrongHash,
running,
}

Expand Down Expand Up @@ -69,6 +73,14 @@ class _PathPickerButtonState extends ConsumerState<PathPickerButton> {
client.close();
}

// == Verify BlueMap CLI JAR hash ==
setState(() => _pickingState = _PickingState.hashing);
final String hash = await bluemapJar.openRead().transform(sha256).join();
if (hash != blueMapCliJarHash) {
setState(() => _pickingState = _PickingState.wrongHash);
return;
}

// == Run BlueMap CLI JAR to generate default configs ==
setState(() => _pickingState = _PickingState.running);
ProcessResult run = await Process.run(
Expand Down Expand Up @@ -121,6 +133,33 @@ class _PathPickerButtonState extends ConsumerState<PathPickerButton> {
),
],
),
_PickingState.hashing => Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text("Verifying BlueMap CLI JAR hash..."),
const SizedBox(height: 8),
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 300),
child: const LinearProgressIndicator(),
),
],
),
_PickingState.wrongHash => Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
"Downloaded failed! Invalid hash!",
style: TextStyle(color: Colors.red),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () => launchUrlString(
"https://github.com/TechnicJelle/BlueMapGUI/issues/new",
),
child: const Text("Contact developer"),
),
],
),
_PickingState.running => Column(
mainAxisSize: MainAxisSize.min,
children: [
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ packages:
source: hosted
version: "0.3.4+2"
crypto:
dependency: transitive
dependency: "direct main"
description:
name: crypto
sha256: ec30d999af904f33454ba22ed9a86162b35e52b44ac4807d1d93c288041d7d27
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ environment:
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
crypto: ^3.0.5
file_picker: ^8.1.2
flutter:
sdk: flutter
Expand Down

0 comments on commit b0f50bb

Please sign in to comment.