From 69b18cc659d6fd0a9e0744861bcb4f65175ad771 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Tue, 10 May 2022 01:07:03 +0100 Subject: [PATCH] Change content length limits of http routes --- src/net/import.rs | 4 ++- src/net/key.rs | 77 +++++++++++++++++++++++++++++------------------ src/net/signin.rs | 7 ++--- src/net/signup.rs | 7 ++--- src/net/sql.rs | 4 ++- 5 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/net/import.rs b/src/net/import.rs index d7e9b2f7..34c7e5fa 100644 --- a/src/net/import.rs +++ b/src/net/import.rs @@ -1,6 +1,8 @@ use warp::http; use warp::Filter; +const MAX: u64 = 1024 * 1024 * 1024 * 4; // 4 GiB + pub fn config() -> impl Filter + Clone { // Set base path let base = warp::path("import").and(warp::path::end()); @@ -9,7 +11,7 @@ pub fn config() -> impl Filter, pub start: Option, } @@ -30,24 +32,30 @@ pub fn config() -> impl Filter(http::header::CONTENT_TYPE.as_str())); - // Set base path for all - let base = base.and(path!("key" / String).and(warp::path::end())); // Set select method - let select = base.and(warp::get()).and(warp::query()).and_then(select_all); + let select = warp::any() + .and(warp::get()) + .and(conf::build()) + .and(warp::header::(http::header::CONTENT_TYPE.as_str())) + .and(path!("key" / String).and(warp::path::end())) + .and(warp::query()) + .and_then(select_all); // Set create method - let create = base + let create = warp::any() .and(warp::post()) - .and(warp::body::content_length_limit(1024 * 1024)) // 1MiB + .and(conf::build()) + .and(warp::header::(http::header::CONTENT_TYPE.as_str())) + .and(path!("key" / String).and(warp::path::end())) + .and(warp::body::content_length_limit(MAX)) .and(warp::body::bytes()) .and_then(create_all); // Set delete method - let delete = base.and(warp::delete()).and_then(delete_all); + let delete = warp::any() + .and(warp::delete()) + .and(conf::build()) + .and(warp::header::(http::header::CONTENT_TYPE.as_str())) + .and(path!("key" / String).and(warp::path::end())) + .and_then(delete_all); // Specify route let all = select.or(create).or(delete); @@ -55,36 +63,47 @@ pub fn config() -> impl Filter(http::header::CONTENT_TYPE.as_str())); - // Set base path for one - let base = base.and(path!("key" / String / String).and(warp::path::end())); // Set select method - let select = base.and(warp::get()).and_then(select_one); + let select = warp::any() + .and(warp::get()) + .and(conf::build()) + .and(warp::header::(http::header::CONTENT_TYPE.as_str())) + .and(path!("key" / String / String).and(warp::path::end())) + .and_then(select_one); // Set create method - let create = base + let create = warp::any() .and(warp::post()) - .and(warp::body::content_length_limit(1024 * 1024)) // 1MiB + .and(conf::build()) + .and(warp::header::(http::header::CONTENT_TYPE.as_str())) + .and(path!("key" / String / String).and(warp::path::end())) + .and(warp::body::content_length_limit(MAX)) .and(warp::body::bytes()) .and_then(create_one); // Set update method - let update = base + let update = warp::any() .and(warp::put()) - .and(warp::body::content_length_limit(1024 * 1024)) // 1MiB + .and(conf::build()) + .and(warp::header::(http::header::CONTENT_TYPE.as_str())) + .and(path!("key" / String / String).and(warp::path::end())) + .and(warp::body::content_length_limit(MAX)) .and(warp::body::bytes()) .and_then(update_one); // Set modify method - let modify = base + let modify = warp::any() .and(warp::patch()) - .and(warp::body::content_length_limit(1024 * 1024)) // 1MiB + .and(conf::build()) + .and(warp::header::(http::header::CONTENT_TYPE.as_str())) + .and(path!("key" / String / String).and(warp::path::end())) + .and(warp::body::content_length_limit(MAX)) .and(warp::body::bytes()) .and_then(modify_one); // Set delete method - let delete = base.and(warp::delete()).and_then(delete_one); + let delete = warp::any() + .and(warp::delete()) + .and(conf::build()) + .and(warp::header::(http::header::CONTENT_TYPE.as_str())) + .and(path!("key" / String / String).and(warp::path::end())) + .and_then(delete_one); // Specify route let one = select.or(create).or(update).or(modify).or(delete); diff --git a/src/net/signin.rs b/src/net/signin.rs index 846a37ea..50f23d1b 100644 --- a/src/net/signin.rs +++ b/src/net/signin.rs @@ -2,16 +2,15 @@ use crate::net::head; use warp::http; use warp::Filter; +const MAX: u64 = 1024; // 1 KiB + pub fn config() -> impl Filter + Clone { // Set base path let base = warp::path("signin").and(warp::path::end()); // Set opts method let opts = base.and(warp::options()).map(warp::reply); // Set post method - let post = base - .and(warp::post()) - .and(warp::body::content_length_limit(1024 * 1024)) // 1MiB - .and_then(handler); + let post = base.and(warp::post()).and(warp::body::content_length_limit(MAX)).and_then(handler); // Specify route opts.or(post).with(head::cors()) } diff --git a/src/net/signup.rs b/src/net/signup.rs index a90a991a..3c1e5938 100644 --- a/src/net/signup.rs +++ b/src/net/signup.rs @@ -2,16 +2,15 @@ use crate::net::head; use warp::http; use warp::Filter; +const MAX: u64 = 1024; // 1 KiB + pub fn config() -> impl Filter + Clone { // Set base path let base = warp::path("signup").and(warp::path::end()); // Set opts method let opts = base.and(warp::options()).map(warp::reply); // Set post method - let post = base - .and(warp::post()) - .and(warp::body::content_length_limit(1024 * 1024)) // 1MiB - .and_then(handler); + let post = base.and(warp::post()).and(warp::body::content_length_limit(MAX)).and_then(handler); // Specify route opts.or(post).with(head::cors()) } diff --git a/src/net/sql.rs b/src/net/sql.rs index 711feaaa..b224d38c 100644 --- a/src/net/sql.rs +++ b/src/net/sql.rs @@ -8,6 +8,8 @@ use futures::{FutureExt, StreamExt}; use surrealdb::Session; use warp::Filter; +const MAX: u64 = 1024 * 1024; // 1 MiB + pub fn config() -> impl Filter + Clone { // Set base path let base = warp::path("sql").and(warp::path::end()); @@ -18,7 +20,7 @@ pub fn config() -> impl Filter(http::header::CONTENT_TYPE.as_str())) - .and(warp::body::content_length_limit(1024 * 1024)) // 1MiB + .and(warp::body::content_length_limit(MAX)) .and(warp::body::bytes()) .and_then(handler); // Set sock method