Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(app): resolve every Windows DLL load from System32 only (#9495)

Thinh Le4e7bed73c930
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index 6d45db017..be17ad05e 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -278,7 +278,7 @@ gtk = { version = "0.18", optional = true }
[target.'cfg(windows)'.dependencies]
libc = "0.2.172"
-windows-sys = { version = "0.60.2", features = ["Win32_Storage_FileSystem"] }
+windows-sys = { version = "0.60.2", features = ["Win32_Storage_FileSystem", "Win32_System_LibraryLoader"] }
# MLX runs only on Apple Silicon, so the plugin is built solely for macOS.
[target.'cfg(target_os = "macos")'.dependencies]
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index a5f24db3c..132e14dd7 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -1,7 +1,28 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
+/// Windows: from here on, every DLL the process loads is resolved from System32 only,
+/// including the dependencies that System32 DLLs load lazily (e.g. `windows.storage.dll`
+/// pulling in `profapi.dll` the first time a shell folder is queried). Without it those
+/// loads search the install folder first, so a DLL planted there runs inside Jan.
+/// The `/DEPENDENTLOADFLAG` link arg in build.rs only covers the exe's own static
+/// imports, which are resolved before `main`. The setting is per process and is not
+/// inherited by the engine worker or other helpers Jan spawns.
+#[cfg(windows)]
+fn restrict_dll_search_to_system32() {
+ use windows_sys::Win32::System::LibraryLoader::{
+ SetDefaultDllDirectories, LOAD_LIBRARY_SEARCH_SYSTEM32,
+ };
+ // SAFETY: plain Win32 call with a constant flag and no pointers. It only fails on
+ // Windows builds that predate the API, where there is nothing to restrict.
+ let _ = unsafe { SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32) };
+}
+
fn main() {
+ // Must stay first: nothing below may load a DLL before the search path is restricted.
+ #[cfg(windows)]
+ restrict_dll_search_to_system32();
+
// Fix PATH before anything that may spawn subprocesses, so the engine
// worker (and any other child) inherits directories added by fix_path_env.
let _ = fix_path_env::fix();