Skip to content

Commit

Permalink
Merge pull request #15 from cyqsimon/ban-abs
Browse files Browse the repository at this point in the history
Explicitly disallow absolute paths
  • Loading branch information
SOF3 committed Apr 5, 2023
2 parents 363fd4f + 8ed5d34 commit 4d79700
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use syn::{Error, LitByteStr, LitStr, Result};
/// `deflate_file!("file")` is equivalent to `include_bytes!("file.gz")`.
///
/// # Parameters
/// This macro accepts exactly one literal parameter that refers to a path, either absolute or relative to `CARGO_MANIFEST_DIR`.
/// This macro accepts exactly one literal parameter that refers to a path relative to
/// `CARGO_MANIFEST_DIR`. Absolute paths are not supported.
///
/// Note that **this is distinct from the behaviour of the builtin `include_bytes!`/`include_str!` macros** —
/// `includle_bytes!`/`include_str!` paths are relative to the current source file, while `deflate_file!` paths are relative to
Expand Down Expand Up @@ -66,21 +67,21 @@ pub fn deflate_utf8_file(ts: TokenStream) -> TokenStream {
}

fn inner(ts: TokenStream, utf8: bool) -> Result<impl Into<TokenStream>> {
fn emap<E: std::fmt::Display>(error: E) -> Error {
Error::new(Span::call_site(), error)
}

let dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());

let lit = syn::parse::<LitStr>(ts)?;
let path = PathBuf::from(lit.value());

let target = if path.is_relative() {
dir.join(path)
} else {
path
};

fn emap<E: std::fmt::Display>(error: E) -> Error {
Error::new(Span::call_site(), error)
if path.is_absolute() {
Err(emap("absolute paths are not supported"))?;
}

let target = dir.join(path);

let mut file = File::open(target).map_err(emap)?;

let mut encoder = Encoder::new(Vec::<u8>::new());
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ pub use lazy_static::lazy_static;
/// - `$name` is the name of the static variable..
/// - `$type` can be either `[u8]` or `str`. However, the actual type created would dereference
/// into `Vec<u8>` and `String` (although they are `AsRef<[u8]>` and `AsRef<str>`) respectively.
/// - `$file` is either an absolute path or a path relative to the current
/// [`CARGO_MANIFEST_DIR`][4]. Note that **this is distinct from the behaviour of the builtin
/// `include_bytes!`/`include_str!` macros** &mdash; `includle_bytes!`/`include_str!` paths are
/// relative to the current source file, while `flate!` paths are relative to `CARGO_MANIFEST_DIR`.
/// - `$file` is a path relative to the current [`CARGO_MANIFEST_DIR`][4]. Absolute paths are not supported.
/// Note that **this is distinct from the behaviour of the builtin `include_bytes!`/`include_str!`
/// macros** &mdash; `includle_bytes!`/`include_str!` paths are relative to the current source file,
/// while `flate!` paths are relative to `CARGO_MANIFEST_DIR`.
///
/// # Returns
/// The macro expands to a [`lazy_static`][3] call, which lazily inflates the compressed bytes.
Expand Down

0 comments on commit 4d79700

Please sign in to comment.