Skip to content

Commit

Permalink
bug: idle static folder (#692)
Browse files Browse the repository at this point in the history
* misc: add more logs

* bug: copy static folder

* bug: extra subdirectory
  • Loading branch information
chesedo authored Mar 8, 2023
1 parent 67cf6bd commit 9fa862e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
2 changes: 2 additions & 0 deletions resources/static-folder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ keywords = ["shuttle-service", "static-folder"]

[dependencies]
async-trait = "0.1.56"
fs_extra = "1.3.0"
shuttle-service = { path = "../../service", version = "0.11.0", default-features = false }
tokio = { version = "1.19.2", features = ["rt"] }
tracing = "0.1.37"

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
45 changes: 35 additions & 10 deletions resources/static-folder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use async_trait::async_trait;
use fs_extra::dir::{copy, CopyOptions};
use shuttle_service::{
error::{CustomError, Error as ShuttleError},
Factory, ResourceBuilder,
};
use std::{
fs::rename,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};
use tokio::runtime::Runtime;
use tracing::{error, trace};

pub struct StaticFolder<'a> {
/// The folder to reach at runtime. Defaults to `static`
Expand All @@ -17,6 +16,7 @@ pub struct StaticFolder<'a> {
pub enum Error {
AbsolutePath,
TransversedUp,
Copy(fs_extra::error::Error),
}

impl<'a> StaticFolder<'a> {
Expand All @@ -40,34 +40,59 @@ impl<'a> ResourceBuilder<PathBuf> for StaticFolder<'a> {
) -> Result<PathBuf, shuttle_service::Error> {
let folder = Path::new(self.folder);

trace!(?folder, "building static folder");

// Prevent users from users from reading anything outside of their crate's build folder
if folder.is_absolute() {
error!("the static folder cannot be an absolute path");
return Err(Error::AbsolutePath)?;
}

let input_dir = factory.get_build_path()?.join(self.folder);

trace!(input_directory = ?input_dir, "got input directory");

match input_dir.canonicalize() {
Ok(canonical_path) if canonical_path != input_dir => return Err(Error::TransversedUp)?,
Ok(_) => {
// The path did not change to outside the crate's build folder
}
Err(err) => return Err(err)?,
Err(err) => {
error!(
error = &err as &dyn std::error::Error,
"failed to get static folder"
);
return Err(err)?;
}
}

let output_dir = factory.get_storage_path()?.join(self.folder);
let output_dir = factory.get_storage_path()?;

rename(input_dir, output_dir.clone())?;
trace!(output_directory = ?output_dir, "got output directory");

Ok(output_dir)
let copy_options = CopyOptions::new().overwrite(true);
match copy(&input_dir, &output_dir, &copy_options) {
Ok(_) => Ok(output_dir.join(self.folder)),
Err(error) => {
error!(
error = &error as &dyn std::error::Error,
"failed to copy static folder"
);

Err(Error::Copy(error))?
}
}
}
}

impl From<Error> for shuttle_service::Error {
fn from(error: Error) -> Self {
let msg = match error {
Error::AbsolutePath => "Cannot use an absolute path for a static folder",
Error::TransversedUp => "Cannot transverse out of crate for a static folder",
Error::AbsolutePath => "Cannot use an absolute path for a static folder".to_string(),
Error::TransversedUp => {
"Cannot transverse out of crate for a static folder".to_string()
}
Error::Copy(error) => format!("Cannot copy static folder: {}", error),
};

ShuttleError::Custom(CustomError::msg(msg))
Expand Down

0 comments on commit 9fa862e

Please sign in to comment.