diff --git a/Cargo.lock b/Cargo.lock index 54001f2..3abfa88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -977,7 +977,7 @@ checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" [[package]] name = "remote-text-server" -version = "0.1.2" +version = "0.1.3" dependencies = [ "base64 0.21.0", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 6fd1178..5b33895 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "remote-text-server" -version = "0.1.2" +version = "0.1.3" edition = "2021" build = "build.rs" diff --git a/default.nix b/default.nix index 7aa63b0..dbe49e8 100644 --- a/default.nix +++ b/default.nix @@ -20,7 +20,7 @@ rustPlatform.buildRustPackage rec { src = ./.; - cargoHash = "sha256-dGnOVvQ2nk3SHSBYjThkY5kRIBoQKKpCYvuRlPIpDyA="; + cargoHash = "sha256-iI4uwLAzP1hAPa0tKlJZHnuwxepZWnW+CsIZLMTe9a0="; nativeBuildInputs = [ pkg-config diff --git a/src/routes.rs b/src/routes.rs index a2bd8c5..147d80b 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -8,9 +8,15 @@ use warp::Filter; use crate::handlers; -// Filter that limits the size of JSON files -pub(crate) fn json_body() -> impl Filter + Clone { - warp::body::content_length_limit(1024 * 16) +// Filter that limits requests to 4KiB and deserializes JSON +pub(crate) fn json_body_small() -> impl Filter + Clone { + warp::body::content_length_limit(1024 * 4) + .and(warp::body::json()) +} + +// Filter that limits requests to 16MiB and deserializes JSON +pub(crate) fn json_body_big() -> impl Filter + Clone { + warp::body::content_length_limit(1024 * 1024 * 16) .and(warp::body::json()) } @@ -23,7 +29,7 @@ pub(crate) fn list_files(repos: Arc>>) -> impl F // Filter that maps to the create_file api call, uses the json_body to restrict file size, then tries to fulfill the request pub(crate) fn create_file(repos: Arc>>) -> impl Filter + Clone { warp::path("createFile") - .and(json_body()) + .and(json_body_big()) .and(warp::filters::addr::remote()) .and_then(move |obj, addr| handlers::create_file(obj, addr, repos.clone())) } @@ -31,14 +37,14 @@ pub(crate) fn create_file(repos: Arc>>) -> impl // Filter that maps to the get_file api call, uses the json_body to restrict file size, then tries to fulfill the request pub(crate) fn get_file(repos: Arc>>) -> impl Filter + Clone { warp::path("getFile") - .and(json_body()) + .and(json_body_small()) .and_then(move |obj| handlers::get_file(obj, repos.clone())) } // Filter that maps to the save_file api call, uses the json_body to restrict file size, then tries to fulfill the request pub(crate) fn save_file(repos: Arc>>) -> impl Filter + Clone { warp::path("saveFile") - .and(json_body()) + .and(json_body_big()) .and(warp::filters::addr::remote()) .and_then(move |obj, addr| handlers::save_file(obj, addr, repos.clone())) } @@ -46,28 +52,28 @@ pub(crate) fn save_file(repos: Arc>>) -> impl Fi // Filter that maps to the delete_file api call, then attempts to fufill the request using handler code pub(crate) fn delete_file(repos: Arc>>) -> impl Filter + Clone { warp::path("deleteFile") - .and(json_body()) + .and(json_body_small()) .and_then(move |obj| handlers::delete_file(obj, repos.clone())) } // Filter that maps to the preview_file api call, uses the json_body to restrict file size, then tries to fulfill the request pub(crate) fn preview_file(repos: Arc>>) -> impl Filter + Clone { warp::path("previewFile") - .and(json_body()) + .and(json_body_small()) .and_then(move |obj| handlers::preview_file(obj, repos.clone())) } // Filter that maps to the get_preview api call, uses the json_body to restrict file size, then tries to fulfill the request pub(crate) fn get_preview(repos: Arc>>) -> impl Filter + Clone { warp::path("getPreview") - .and(json_body()) + .and(json_body_small()) .and_then(move |obj| handlers::get_preview(obj, repos.clone())) } // Filter that maps to the get_history api call, uses the json_body to restrict file size, then tries to fulfill the request pub(crate) fn get_history(repos: Arc>>) -> impl Filter + Clone { warp::path("getHistory") - .and(json_body()) + .and(json_body_small()) .and_then(move |obj| handlers::get_history(obj, repos.clone())) } diff --git a/src/tests/filter_tests.rs b/src/tests/filter_tests.rs index 11e4cc1..b7d1f38 100644 --- a/src/tests/filter_tests.rs +++ b/src/tests/filter_tests.rs @@ -91,7 +91,7 @@ async fn create_file_too_large() { let repositories = repos(); let filter = routes::create_file(repositories); - let bytes = vec![b'a' ; (1024 * 16) + 1]; + let bytes = vec![b'a' ; (1024 * 1024 * 16) + 1]; let body = std::str::from_utf8(&bytes).unwrap(); let body = format!("{}", body); let body = Some(body);