Skip to content

Commit 1f31183

Browse files
enhance(cli): add context to public/secret key decoding errors (#11405)
* enhance(cli): add context to public/secret key decoding errors closes #10488 * Update .changes/cli-updater-errorr.md --------- Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app>
1 parent e0d1307 commit 1f31183

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

.changes/cli-updater-errorr.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-cli": "patch:enhance"
3+
"@tauri-apps/cli": "patch:enhance"
4+
---
5+
6+
Add more context for errors when decoding secret and public keys for signing updater artifacts.
7+

crates/tauri-cli/src/bundle.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{
99
};
1010

1111
use anyhow::Context;
12-
use base64::Engine;
1312
use clap::{builder::PossibleValue, ArgAction, Parser, ValueEnum};
1413
use tauri_bundler::PackageType;
1514
use tauri_utils::platform::Target;
@@ -257,15 +256,14 @@ fn sign_updaters(
257256
// check if private_key points to a file...
258257
let maybe_path = Path::new(&private_key);
259258
let private_key = if maybe_path.exists() {
260-
std::fs::read_to_string(maybe_path)?
259+
std::fs::read_to_string(maybe_path)
260+
.with_context(|| format!("faild to read {}", maybe_path.display()))?
261261
} else {
262262
private_key
263263
};
264-
let secret_key = updater_signature::secret_key(private_key, password)?;
265-
266-
let pubkey = base64::engine::general_purpose::STANDARD.decode(pubkey)?;
267-
let pub_key_decoded = String::from_utf8_lossy(&pubkey);
268-
let public_key = minisign::PublicKeyBox::from_string(&pub_key_decoded)?.into_public_key()?;
264+
let secret_key =
265+
updater_signature::secret_key(private_key, password).context("failed to decode secret key")?;
266+
let public_key = updater_signature::pub_key(pubkey).context("failed to decode pubkey")?;
269267

270268
let mut signed_paths = Vec::new();
271269
for bundle in update_enabled_bundles {

crates/tauri-cli/src/helpers/updater_signature.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use anyhow::Context;
66
use base64::Engine;
7-
use minisign::{sign, KeyPair as KP, SecretKey, SecretKeyBox, SignatureBox};
7+
use minisign::{
8+
sign, KeyPair as KP, PublicKey, PublicKeyBox, SecretKey, SecretKeyBox, SignatureBox,
9+
};
810
use std::{
911
fs::{self, File, OpenOptions},
1012
io::{BufReader, BufWriter, Write},
@@ -132,15 +134,24 @@ pub fn secret_key<S: AsRef<[u8]>>(
132134
private_key: S,
133135
password: Option<String>,
134136
) -> crate::Result<SecretKey> {
135-
let decoded_secret = decode_key(private_key)?;
136-
let sk_box = SecretKeyBox::from_string(&decoded_secret)
137-
.with_context(|| "failed to load updater private key")?;
137+
let decoded_secret = decode_key(private_key).context("failed to decode base64 secret key")?;
138+
let sk_box =
139+
SecretKeyBox::from_string(&decoded_secret).context("failed to load updater private key")?;
138140
let sk = sk_box
139141
.into_secret_key(password)
140-
.with_context(|| "incorrect updater private key password")?;
142+
.context("incorrect updater private key password")?;
141143
Ok(sk)
142144
}
143145

146+
/// Gets the updater secret key from the given private key and password.
147+
pub fn pub_key<S: AsRef<[u8]>>(public_key: S) -> crate::Result<PublicKey> {
148+
let decoded_publick = decode_key(public_key).context("failed to decode base64 pubkey")?;
149+
let pk_box =
150+
PublicKeyBox::from_string(&decoded_publick).context("failed to load updater pubkey")?;
151+
let pk = pk_box.into_public_key()?;
152+
Ok(pk)
153+
}
154+
144155
fn unix_timestamp() -> u64 {
145156
let start = SystemTime::now();
146157
let since_the_epoch = start

0 commit comments

Comments
 (0)