Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
feat: allow blocking content types in config
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed Aug 14, 2024
1 parent d4f4f72 commit e62b517
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "autumn"
version = "1.1.10"
version = "1.1.11"
authors = ["Paul Makles <[email protected]>"]
edition = "2018"

Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ pub enum ServeConfig {
PNG,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct FilterConfig {
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub content_types: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub tags: HashMap<String, Tag>,
pub serve: ServeConfig,
pub jpeg_quality: u8,
#[serde(default)]
pub filter: FilterConfig,
}

static INSTANCE: OnceCell<Config> = OnceCell::new();
Expand Down
7 changes: 6 additions & 1 deletion src/routes/download.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::get_tag;
use crate::config::{get_tag, Config};
use crate::db::find_file;
use crate::util::result::Error;

Expand All @@ -16,6 +16,11 @@ pub async fn get(req: HttpRequest) -> Result<HttpResponse, Error> {
return Err(Error::NotFound);
}

let config = Config::global();
if config.filter.content_types.contains(&file.content_type) {
return Err(Error::ContentTypeNotAllowed);
}

let (contents, _) = fetch_file(id, &tag.0, file.metadata, None).await?;

Ok(HttpResponse::Ok()
Expand Down
5 changes: 5 additions & 0 deletions src/routes/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ pub async fn get(req: HttpRequest, resize: Query<Resize>) -> Result<HttpResponse
return Err(Error::NotFound);
}

let config = Config::global();
if config.filter.content_types.contains(&file.content_type) {
return Err(Error::ContentTypeNotAllowed);
}

let (contents, content_type) = fetch_file(id, &tag.0, file.metadata, Some(resize.0)).await?;
let content_type = content_type.unwrap_or(file.content_type);

Expand Down
5 changes: 5 additions & 0 deletions src/routes/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ pub async fn post(req: HttpRequest, mut payload: Multipart) -> Result<HttpRespon
}
}

// Check if content type is blocked
if config.filter.content_types.contains(&content_type) {
return Err(Error::ContentTypeNotAllowed);
}

let s = &content_type[..];

let metadata = match s {
Expand Down
2 changes: 2 additions & 0 deletions src/util/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::fmt::Display;
#[serde(tag = "type")]
pub enum Error {
FileTooLarge { max_size: usize },
ContentTypeNotAllowed,
FileTypeNotAllowed,
FailedToReceive,
BlockingError,
Expand All @@ -32,6 +33,7 @@ impl ResponseError for Error {
fn status_code(&self) -> StatusCode {
match &self {
Error::FileTooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE,
Error::ContentTypeNotAllowed => StatusCode::BAD_REQUEST,
Error::FileTypeNotAllowed => StatusCode::BAD_REQUEST,
Error::FailedToReceive => StatusCode::BAD_REQUEST,
Error::DatabaseError => StatusCode::INTERNAL_SERVER_ERROR,
Expand Down

0 comments on commit e62b517

Please sign in to comment.