-
-
Notifications
You must be signed in to change notification settings - Fork 923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: throw error when asset is empty #3379
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b6b9694
fix: throw error when asset is empty
jkelleyrtp 38f1754
Use std::path::absolute to fix windows unc problems
jkelleyrtp d4bf1d0
fix: don't use canonicalize at all on windows
jkelleyrtp 39deb42
remove unnecessary check
jkelleyrtp b29a401
fix comment
jkelleyrtp 6cb2cd7
fix error not actually being thrown
jkelleyrtp b223317
remove test cases from example
jkelleyrtp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,12 @@ use syn::{ | |
}; | ||
|
||
fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> { | ||
if raw.is_empty() { | ||
return Err(AssetParseError::InvalidPath { | ||
path: PathBuf::new(), | ||
}); | ||
} | ||
|
||
// Get the location of the root of the crate which is where all assets are relative to | ||
// | ||
// IE | ||
|
@@ -19,19 +25,28 @@ fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> { | |
// /users/dioxus/dev/app/assets/blah.css | ||
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR") | ||
.map(PathBuf::from) | ||
.map(std::path::absolute) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
// 1. the input file should be a pathbuf | ||
let input = PathBuf::from(raw); | ||
|
||
// 2. absolute path to the asset | ||
manifest_dir | ||
.join(raw.trim_start_matches('/')) | ||
.canonicalize() | ||
.map_err(|err| AssetParseError::AssetDoesntExist { | ||
err, | ||
path: input.clone(), | ||
}) | ||
let path = | ||
std::path::absolute(manifest_dir.join(raw.trim_start_matches('/'))).map_err(|err| { | ||
AssetParseError::AssetDoesntExist { | ||
err, | ||
path: input.clone(), | ||
} | ||
})?; | ||
|
||
// 3. Ensure the path is not the current dir or exist outside the current dir | ||
if path == manifest_dir || !path.starts_with(manifest_dir) || !path.exists() { | ||
return Err(AssetParseError::InvalidPath { path }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead, this case triggers which gives the wrong error message |
||
} | ||
|
||
Ok(path) | ||
} | ||
|
||
fn hash_file_contents(file_path: &Path) -> Result<u64, AssetParseError> { | ||
|
@@ -82,6 +97,9 @@ pub(crate) enum AssetParseError { | |
err: std::io::Error, | ||
path: std::path::PathBuf, | ||
}, | ||
InvalidPath { | ||
path: std::path::PathBuf, | ||
}, | ||
FailedToReadAsset(std::io::Error), | ||
} | ||
|
||
|
@@ -91,6 +109,13 @@ impl std::fmt::Display for AssetParseError { | |
AssetParseError::AssetDoesntExist { err, path } => { | ||
write!(f, "Asset at {} doesn't exist: {}", path.display(), err) | ||
} | ||
AssetParseError::InvalidPath { path } => { | ||
write!( | ||
f, | ||
"Asset path {} is invalid. Make sure the asset exists within this crate.", | ||
path.display() | ||
) | ||
} | ||
AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err), | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::path::absolute
does not check if the path exists (unlike canonicalize) so this error does not trigger when the path doesn't exist