Skip to content

Commit b44e9c0

Browse files
feat(bundler): redownload outdated/mis-hashed files (#8431)
* feat(bundler): redownload outdata/mis-hashed files * change import location * Update util.rs * Update util.rs * Update bundler-mishashed-files.md * Update bundler-mishashed-files.md * rename fn --------- Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent b2f83f0 commit b44e9c0

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri-bundler': 'patch:enhance'
3+
---
4+
5+
Check if required files/tools for bundling are outdated or mis-hashed and redownload them.

tooling/bundler/src/bundle/windows/nsis.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::{
99
common::CommandExt,
1010
windows::util::{
1111
download, download_and_verify, download_webview2_bootstrapper,
12-
download_webview2_offline_installer, extract_zip, HashAlgorithm, NSIS_OUTPUT_FOLDER_NAME,
13-
NSIS_UPDATER_OUTPUT_FOLDER_NAME,
12+
download_webview2_offline_installer, extract_zip, verify_file_hash, HashAlgorithm,
13+
NSIS_OUTPUT_FOLDER_NAME, NSIS_UPDATER_OUTPUT_FOLDER_NAME,
1414
},
1515
},
1616
Settings,
@@ -36,7 +36,7 @@ const NSIS_URL: &str =
3636
#[cfg(target_os = "windows")]
3737
const NSIS_SHA1: &str = "057e83c7d82462ec394af76c87d06733605543d4";
3838
const NSIS_APPLICATIONID_URL: &str = "https://github.com/tauri-apps/binary-releases/releases/download/nsis-plugins-v0/NSIS-ApplicationID.zip";
39-
const NSIS_TAURI_UTILS: &str =
39+
const NSIS_TAURI_UTILS_URL: &str =
4040
"https://github.com/tauri-apps/nsis-tauri-utils/releases/download/nsis_tauri_utils-v0.2.2/nsis_tauri_utils.dll";
4141
const NSIS_TAURI_UTILS_SHA1: &str = "16DF1D1A5B4D5DF3859447279C55BE36D4109DFB";
4242

@@ -60,6 +60,13 @@ const NSIS_REQUIRED_FILES: &[&str] = &[
6060
"Plugins/x86-unicode/nsis_tauri_utils.dll",
6161
];
6262

63+
const NSIS_REQUIRED_FILES_HASH: &[(&str, &str, &str, HashAlgorithm)] = &[(
64+
"Plugins/x86-unicode/nsis_tauri_utils.dll",
65+
NSIS_TAURI_UTILS_URL,
66+
NSIS_TAURI_UTILS_SHA1,
67+
HashAlgorithm::Sha1,
68+
)];
69+
6370
/// Runs all of the commands to build the NSIS installer.
6471
/// Returns a vector of PathBuf that shows where the NSIS installer was created.
6572
pub fn bundle_project(settings: &Settings, updater: bool) -> crate::Result<Vec<PathBuf>> {
@@ -75,6 +82,21 @@ pub fn bundle_project(settings: &Settings, updater: bool) -> crate::Result<Vec<P
7582
warn!("NSIS directory is missing some files. Recreating it.");
7683
std::fs::remove_dir_all(&nsis_toolset_path)?;
7784
get_and_extract_nsis(&nsis_toolset_path, &tauri_tools_path)?;
85+
} else {
86+
let mismatched = NSIS_REQUIRED_FILES_HASH
87+
.iter()
88+
.filter(|(p, _, hash, hash_algorithm)| {
89+
verify_file_hash(nsis_toolset_path.join(p), hash, *hash_algorithm).is_err()
90+
})
91+
.collect::<Vec<_>>();
92+
93+
if !mismatched.is_empty() {
94+
warn!("NSIS directory contains mis-hashed files. Redownloading them.");
95+
for (path, url, hash, hash_algorithim) in mismatched {
96+
let data = download_and_verify(url, hash, *hash_algorithim)?;
97+
write(nsis_toolset_path.join(path), data)?;
98+
}
99+
}
78100
}
79101

80102
build_nsis_app_installer(settings, &nsis_toolset_path, &tauri_tools_path, updater)
@@ -107,7 +129,11 @@ fn get_and_extract_nsis(nsis_toolset_path: &Path, _tauri_tools_path: &Path) -> c
107129
nsis_plugins.join("x86-unicode").join("ApplicationID.dll"),
108130
)?;
109131

110-
let data = download_and_verify(NSIS_TAURI_UTILS, NSIS_TAURI_UTILS_SHA1, HashAlgorithm::Sha1)?;
132+
let data = download_and_verify(
133+
NSIS_TAURI_UTILS_URL,
134+
NSIS_TAURI_UTILS_SHA1,
135+
HashAlgorithm::Sha1,
136+
)?;
111137
write(
112138
nsis_plugins
113139
.join("x86-unicode")

tooling/bundler/src/bundle/windows/util.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub fn download(url: &str) -> crate::Result<Vec<u8>> {
7878
Ok(bytes)
7979
}
8080

81+
#[derive(Clone, Copy)]
8182
pub enum HashAlgorithm {
8283
#[cfg(target_os = "windows")]
8384
Sha256,
@@ -92,23 +93,25 @@ pub fn download_and_verify(
9293
) -> crate::Result<Vec<u8>> {
9394
let data = download(url)?;
9495
info!("validating hash");
96+
verify_hash(&data, hash, hash_algorithm)?;
97+
Ok(data)
98+
}
9599

100+
pub fn verify_hash(data: &[u8], hash: &str, hash_algorithm: HashAlgorithm) -> crate::Result<()> {
96101
match hash_algorithm {
97102
#[cfg(target_os = "windows")]
98103
HashAlgorithm::Sha256 => {
99104
let hasher = sha2::Sha256::new();
100-
verify(&data, hash, hasher)?;
105+
verify_data_with_hasher(data, hash, hasher)
101106
}
102107
HashAlgorithm::Sha1 => {
103108
let hasher = sha1::Sha1::new();
104-
verify(&data, hash, hasher)?;
109+
verify_data_with_hasher(data, hash, hasher)
105110
}
106111
}
107-
108-
Ok(data)
109112
}
110113

111-
fn verify(data: &Vec<u8>, hash: &str, mut hasher: impl Digest) -> crate::Result<()> {
114+
fn verify_data_with_hasher(data: &[u8], hash: &str, mut hasher: impl Digest) -> crate::Result<()> {
112115
hasher.update(data);
113116

114117
let url_hash = hasher.finalize().to_vec();
@@ -120,6 +123,15 @@ fn verify(data: &Vec<u8>, hash: &str, mut hasher: impl Digest) -> crate::Result<
120123
}
121124
}
122125

126+
pub fn verify_file_hash<P: AsRef<Path>>(
127+
path: P,
128+
hash: &str,
129+
hash_algorithm: HashAlgorithm,
130+
) -> crate::Result<()> {
131+
let data = std::fs::read(path)?;
132+
verify_hash(&data, hash, hash_algorithm)
133+
}
134+
123135
/// Extracts the zips from memory into a useable path.
124136
pub fn extract_zip(data: &[u8], path: &Path) -> crate::Result<()> {
125137
let cursor = Cursor::new(data);

0 commit comments

Comments
 (0)