Skip to content
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

bug: idle static folder #692

Merged
merged 3 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 37 additions & 9 deletions resources/static-folder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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::fs::create_dir_all;
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 +17,7 @@ pub struct StaticFolder<'a> {
pub enum Error {
AbsolutePath,
TransversedUp,
Copy(fs_extra::error::Error),
}

impl<'a> StaticFolder<'a> {
Expand All @@ -40,34 +41,61 @@ 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);

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

create_dir_all(&output_dir)?;

Ok(output_dir)
let copy_options = CopyOptions::new().overwrite(true);
match copy(&input_dir, &output_dir, &copy_options) {
Ok(_) => Ok(output_dir),
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