Skip to content

Limit the size of served files #834

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

Merged
merged 14 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/db/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use std::path::{Path, PathBuf};

pub(crate) use crate::storage::Blob;

pub(crate) fn get_path(conn: &Connection, path: &str) -> Result<Blob> {
Storage::new(conn).get(path)
pub(crate) fn get_path(conn: &Connection, path: &str, max_size: usize) -> Result<Blob> {
Storage::new(conn).get(path, max_size)
}

/// Store all files in a directory and return [[mimetype, filename]] as Json
Expand Down
8 changes: 4 additions & 4 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'a> Storage<'a> {
DatabaseBackend::new(conn).into()
}
}
pub(crate) fn get(&self, path: &str) -> Result<Blob, Error> {
pub(crate) fn get(&self, path: &str, max_size: usize) -> Result<Blob, Error> {
let mut blob = match self {
Self::Database(db) => db.get(path),
Self::S3(s3) => s3.get(path),
Expand Down Expand Up @@ -282,7 +282,7 @@ mod test {
let name = Path::new(&blob.path);
assert!(stored_files.contains_key(name));

let actual = backend.get(&blob.path).unwrap();
let actual = backend.get(&blob.path, std::usize::MAX).unwrap();
assert_blob_eq(blob, &actual);
}

Expand Down Expand Up @@ -324,12 +324,12 @@ mod test {
"text/rust"
);

let file = backend.get("rustdoc/Cargo.toml").unwrap();
let file = backend.get("rustdoc/Cargo.toml", std::usize::MAX).unwrap();
assert_eq!(file.content, b"data");
assert_eq!(file.mime, "text/toml");
assert_eq!(file.path, "rustdoc/Cargo.toml");

let file = backend.get("rustdoc/src/main.rs").unwrap();
let file = backend.get("rustdoc/src/main.rs", std::usize::MAX).unwrap();
assert_eq!(file.content, b"data");
assert_eq!(file.mime, "text/rust");
assert_eq!(file.path, "rustdoc/src/main.rs");
Expand Down
4 changes: 3 additions & 1 deletion src/web/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use crate::{db, error::Result};
use iron::{status, Handler, IronError, IronResult, Request, Response};
use postgres::Connection;

const MAX_FILE_SIZE: usize = 5 * 1024 * 1024; // 5MB

pub(crate) struct File(pub(crate) db::file::Blob);

impl File {
/// Gets file from database
pub fn from_path(conn: &Connection, path: &str) -> Result<File> {
Ok(File(db::file::get_path(conn, path)?))
Ok(File(db::file::get_path(conn, path, MAX_FILE_SIZE)?))
}

/// Consumes File and creates a iron response
Expand Down