Skip to content

Commit 7209fdf

Browse files
committed
refactor(core): load APPIMAGE and APPDIR env vars on startup [TRI-007] [TRI-041]
1 parent 4de285c commit 7209fdf

16 files changed

Lines changed: 193 additions & 100 deletions

File tree

.changes/core-env.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
The `process`, `path` and `updater` APIs now takes a `tauri::Env` argument, used to force environment variables load on startup to prevent env var update attacks.

core/tauri-utils/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ impl PackageInfo {
3737
}
3838
}
3939

40+
/// Information about environment variables.
41+
#[derive(Debug, Clone)]
42+
pub struct Env {
43+
/// The APPIMAGE environment variable.
44+
#[cfg(target_os = "linux")]
45+
pub appimage: Option<std::ffi::OsString>,
46+
/// The APPDIR environment variable.
47+
#[cfg(target_os = "linux")]
48+
pub appdir: Option<std::ffi::OsString>,
49+
}
50+
51+
impl Default for Env {
52+
fn default() -> Self {
53+
Self {
54+
#[cfg(target_os = "linux")]
55+
appimage: std::env::var_os("APPIMAGE"),
56+
#[cfg(target_os = "linux")]
57+
appdir: std::env::var_os("APPDIR"),
58+
}
59+
}
60+
}
61+
4062
/// The result type of `tauri-utils`.
4163
pub type Result<T> = std::result::Result<T, Error>;
4264

core/tauri-utils/src/platform.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
//! Platform helper functions.
66
7-
use std::{
8-
env,
9-
path::{PathBuf, MAIN_SEPARATOR},
10-
};
7+
use std::path::{PathBuf, MAIN_SEPARATOR};
118

12-
use crate::PackageInfo;
9+
use crate::{Env, PackageInfo};
1310

1411
/// Try to determine the current target triple.
1512
///
@@ -76,7 +73,7 @@ pub fn target_triple() -> crate::Result<String> {
7673
/// `${exe_dir}/../lib/${exe_name}`.
7774
///
7875
/// On MacOS, it's `${exe_dir}../Resources` (inside .app).
79-
pub fn resource_dir(package_info: &PackageInfo) -> crate::Result<PathBuf> {
76+
pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> crate::Result<PathBuf> {
8077
let exe = std::env::current_exe()?;
8178
let exe_dir = exe.parent().expect("failed to get exe directory");
8279
let curr_dir = exe_dir.display().to_string();
@@ -93,10 +90,11 @@ pub fn resource_dir(package_info: &PackageInfo) -> crate::Result<PathBuf> {
9390
if curr_dir.ends_with("/data/usr/bin") {
9491
// running from the deb bundle dir
9592
Ok(exe_dir.join(format!("../lib/{}", package_info.package_name())))
96-
} else if let Ok(appdir) = env::var("APPDIR") {
93+
} else if let Some(appdir) = &env.appdir {
94+
let appdir: &std::path::Path = appdir.as_ref();
9795
Ok(PathBuf::from(format!(
9896
"{}/usr/lib/{}",
99-
appdir,
97+
appdir.display(),
10098
package_info.package_name()
10199
)))
102100
} else {

core/tauri/src/api/path.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
path::{Component, Path, PathBuf},
1010
};
1111

12-
use crate::{Config, PackageInfo};
12+
use crate::{Config, Env, PackageInfo};
1313

1414
use serde_repr::{Deserialize_repr, Serialize_repr};
1515

@@ -83,6 +83,7 @@ pub enum BaseDirectory {
8383
/// authors: "tauri",
8484
/// description: "a tauri test",
8585
/// },
86+
/// &Default::default(),
8687
/// "path/to/something",
8788
/// Some(BaseDirectory::Config)
8889
/// ).expect("failed to resolve path");
@@ -91,6 +92,7 @@ pub enum BaseDirectory {
9192
pub fn resolve_path<P: AsRef<Path>>(
9293
config: &Config,
9394
package_info: &PackageInfo,
95+
env: &Env,
9496
path: P,
9597
dir: Option<BaseDirectory>,
9698
) -> crate::api::Result<PathBuf> {
@@ -113,7 +115,7 @@ pub fn resolve_path<P: AsRef<Path>>(
113115
BaseDirectory::Runtime => runtime_dir(),
114116
BaseDirectory::Template => template_dir(),
115117
BaseDirectory::Video => video_dir(),
116-
BaseDirectory::Resource => resource_dir(package_info),
118+
BaseDirectory::Resource => resource_dir(package_info, env),
117119
BaseDirectory::App => app_dir(config),
118120
BaseDirectory::Current => Some(env::current_dir()?),
119121
BaseDirectory::Log => log_dir(config),
@@ -229,8 +231,8 @@ pub fn video_dir() -> Option<PathBuf> {
229231
}
230232

231233
/// Returns the path to the resource directory of this app.
232-
pub fn resource_dir(package_info: &PackageInfo) -> Option<PathBuf> {
233-
crate::utils::platform::resource_dir(package_info).ok()
234+
pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> Option<PathBuf> {
235+
crate::utils::platform::resource_dir(package_info, env).ok()
234236
}
235237

236238
/// Returns the path to the suggested directory for your app config files.

core/tauri/src/api/process.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
//! Types and functions related to child processes management.
66
7+
use crate::Env;
8+
79
use std::{
810
env,
911
path::PathBuf,
@@ -16,12 +18,13 @@ mod command;
1618
pub use command::*;
1719

1820
/// Gets the current binary.
19-
pub fn current_binary() -> Option<PathBuf> {
21+
#[allow(unused_variables)]
22+
pub fn current_binary(env: &Env) -> Option<PathBuf> {
2023
let mut current_binary = None;
2124

2225
// if we are running with an APP Image, we should return the app image path
2326
#[cfg(target_os = "linux")]
24-
if let Some(app_image_path) = env::var_os("APPIMAGE") {
27+
if let Some(app_image_path) = &env.appimage {
2528
current_binary = Some(PathBuf::from(app_image_path));
2629
}
2730

@@ -37,8 +40,8 @@ pub fn current_binary() -> Option<PathBuf> {
3740
}
3841

3942
/// Restarts the process.
40-
pub fn restart() {
41-
if let Some(path) = current_binary() {
43+
pub fn restart(env: &Env) {
44+
if let Some(path) = current_binary(env) {
4245
StdCommand::new(path)
4346
.spawn()
4447
.expect("application failed to start");

core/tauri/src/app.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::{
1919
Dispatch, ExitRequestedEventAction, RunEvent, Runtime,
2020
},
2121
sealed::{ManagerBase, RuntimeOrDispatch},
22-
utils::assets::Assets,
2322
utils::config::{Config, WindowUrl},
23+
utils::{assets::Assets, Env},
2424
Context, Invoke, InvokeError, InvokeResponse, Manager, StateManager, Window,
2525
};
2626

@@ -150,14 +150,15 @@ impl<R: Runtime> GlobalWindowEvent<R> {
150150
/// The path resolver is a helper for the application-specific [`crate::api::path`] APIs.
151151
#[derive(Debug, Clone)]
152152
pub struct PathResolver {
153+
env: Env,
153154
config: Arc<Config>,
154155
package_info: PackageInfo,
155156
}
156157

157158
impl PathResolver {
158159
/// Returns the path to the resource directory of this app.
159160
pub fn resource_dir(&self) -> Option<PathBuf> {
160-
crate::api::path::resource_dir(&self.package_info)
161+
crate::api::path::resource_dir(&self.package_info, &self.env)
161162
}
162163

163164
/// Returns the path to the suggested directory for your app config files.
@@ -407,6 +408,7 @@ macro_rules! shared_app_impl {
407408
/// The path resolver for the application.
408409
pub fn path_resolver(&self) -> PathResolver {
409410
PathResolver {
411+
env: self.state::<Env>().inner().clone(),
410412
config: self.manager.config(),
411413
package_info: self.manager.package_info().clone(),
412414
}
@@ -432,6 +434,11 @@ macro_rules! shared_app_impl {
432434
self.manager.package_info()
433435
}
434436

437+
/// Gets the managed [`Env`].
438+
pub fn env(&self) -> Env {
439+
self.state::<Env>().inner().clone()
440+
}
441+
435442
/// The application's asset resolver.
436443
pub fn asset_resolver(&self) -> AssetResolver<R> {
437444
AssetResolver {
@@ -990,6 +997,8 @@ impl<R: Runtime> Builder<R> {
990997
},
991998
};
992999

1000+
app.manage(Env::default());
1001+
9931002
#[cfg(feature = "system-tray")]
9941003
if let Some(system_tray) = self.system_tray {
9951004
let mut ids = HashMap::new();

core/tauri/src/endpoints.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,21 @@ impl Module {
7575
.and_then(|r| r.json)
7676
.map_err(InvokeError::from)
7777
}),
78-
Self::Process(cmd) => resolver
79-
.respond_async(async move { cmd.run().and_then(|r| r.json).map_err(InvokeError::from) }),
78+
Self::Process(cmd) => resolver.respond_async(async move {
79+
cmd
80+
.run(window)
81+
.and_then(|r| r.json)
82+
.map_err(InvokeError::from)
83+
}),
8084
Self::Fs(cmd) => resolver.respond_async(async move {
8185
cmd
82-
.run(config, &package_info)
86+
.run(window, config, &package_info)
8387
.and_then(|r| r.json)
8488
.map_err(InvokeError::from)
8589
}),
8690
Self::Path(cmd) => resolver.respond_async(async move {
8791
cmd
88-
.run(config, &package_info)
92+
.run(window, config, &package_info)
8993
.and_then(|r| r.json)
9094
.map_err(InvokeError::from)
9195
}),

0 commit comments

Comments
 (0)