rust-embed v8.14.0
git clone https://git.pyrossh.dev/rust-embed
rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.
61bc8e6
— Peter John
2026-11-11T11:25:10+05:30
Canonicalize folder path at runtime in debug mode
- changelog.md +4 -0
- impl/src/lib.rs +7 -12
changelog.md
CHANGED
|
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
|
7
7
|
|
|
8
8
|
Thanks to [Mark Drobnak](https://github.com/AzureMarker) for the changelog.
|
|
9
9
|
|
|
10
|
+
## [8.14.0] - 2026-11-11
|
|
11
|
+
|
|
12
|
+
- Fix `get()` returning `None` for every file in debug builds (without `debug-embed`) when the binary runs in a different environment than it was compiled in, e.g. cross-compiled for Windows and run under Wine. The folder path is now canonicalized at runtime instead of in the macro. Thanks to Maya Janzen.
|
|
13
|
+
|
|
10
14
|
## [8.13.0] - 2026-10-07
|
|
11
15
|
|
|
12
16
|
- Add `DynRustEmbed`, a dyn-compatible counterpart to `RustEmbed`, blanket-implemented for all embed types so they can be stored behind `Box<dyn DynRustEmbed>` / `&dyn DynRustEmbed`. Thanks to pyrossh.
|
impl/src/lib.rs
CHANGED
|
@@ -10,7 +10,6 @@ use rust_embed_utils::PathMatcher;
|
|
|
10
10
|
use std::{
|
|
11
11
|
collections::BTreeMap,
|
|
12
12
|
env,
|
|
13
|
-
io::ErrorKind,
|
|
14
13
|
iter::FromIterator,
|
|
15
14
|
path::{Path, PathBuf},
|
|
16
15
|
};
|
|
@@ -196,15 +195,6 @@ fn dynamic(
|
|
|
196
195
|
});
|
|
197
196
|
|
|
198
197
|
let methods = if let Some(ref folder_path) = folder_path {
|
|
199
|
-
let non_canonical_folder_path = Path::new(&folder_path);
|
|
200
|
-
let canonical_folder_path = non_canonical_folder_path
|
|
201
|
-
.canonicalize()
|
|
202
|
-
.or_else(|err| match err {
|
|
203
|
-
err if err.kind() == ErrorKind::NotFound => Ok(non_canonical_folder_path.to_owned()),
|
|
204
|
-
err => Err(err),
|
|
205
|
-
})
|
|
206
|
-
.expect("folder path must resolve to an absolute path");
|
|
207
|
-
let canonical_folder_path = canonical_folder_path.to_str().expect("absolute folder path must be valid unicode");
|
|
208
198
|
quote! {
|
|
209
199
|
/// Get an embedded file and its metadata.
|
|
210
200
|
pub fn get(file_path: &str) -> ::std::option::Option<#crate_path::EmbeddedFile> {
|
|
@@ -213,9 +203,14 @@ fn dynamic(
|
|
|
213
203
|
let rel_file_path = file_path.replace("\\", "/");
|
|
214
204
|
let file_path = ::std::path::Path::new(#folder_path).join(&rel_file_path);
|
|
215
205
|
|
|
216
|
-
// Make sure the path requested does not escape the folder path
|
|
206
|
+
// Make sure the path requested does not escape the folder path.
|
|
207
|
+
// The folder path is canonicalized here at runtime rather than in the macro,
|
|
208
|
+
// because the two can differ when the binary runs in another environment
|
|
209
|
+
// than the one it was compiled in (e.g. cross-compiled and run under Wine,
|
|
210
|
+
// where `/home/..` canonicalizes to `\\?\Z:\home\..`).
|
|
217
211
|
let canonical_file_path = file_path.canonicalize().ok()?;
|
|
212
|
+
let canonical_folder_path = ::std::path::Path::new(#folder_path).canonicalize().ok()?;
|
|
218
|
-
if !canonical_file_path.starts_with(
|
|
213
|
+
if !canonical_file_path.starts_with(&canonical_folder_path) {
|
|
219
214
|
// Tried to request a path that is not in the embedded folder
|
|
220
215
|
|
|
221
216
|
// TODO: Currently it allows "path_traversal_attack" for the symlink files
|