Skip to content

Commit

Permalink
Added non-zero check. (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius authored Jun 12, 2023
1 parent 4a74ba8 commit 5ed8f45
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Also you can configure number of actix `workers` that handle connections.
`--cors` is a list of allowed hosts with wildcards separated by commas. By default all hosts are allowed.
You can define which hosts are allowed for your particular application.

`--allow-empty` is a parameter that allows users to upload empty files. Empty
file means that while creation 0 bytes was passed as an `Upload-Length`.

For example if you add `--cors "*.staging.domain,*.prod.domain"`, it allows all origins
like `my.staging.domain` or `my.prod.domain`, but it will refuse to serve other origins.

Expand All @@ -39,7 +42,8 @@ Also you can disable access log for `/health` endpoint, by using `--disable-heal
--url "/files" \
--log-level "INFO" \
--cors "my.*.domain.com,your.*.domain.com" \
--disable-health-access-log
--disable-health-access-log \
--allow-empty
```

=== "ENV"
Expand All @@ -53,6 +57,7 @@ Also you can disable access log for `/health` endpoint, by using `--disable-heal
export RUSTUS_LOG_LEVEL="INFO"
export RUSTUS_CORS="my.*.domain.com,your.*.domain.com"
export RUSTUS_DISABLE_HEALTH_ACCESS_LOG="true"
export RUSTUS_ALLOW_EMPTY="true"

rustus
```
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ pub struct RustusConf {
)]
pub tus_extensions: Vec<Extensions>,

/// Enabling this parameter
/// Will allow creation of empty files
/// when Upload-Length header equals to 0.
#[arg(long, env = "RUSTUS_ALLOW_EMPTY")]
pub allow_empty: bool,

/// Remove part files after concatenation is done.
/// By default rustus does nothing with part files after concatenation.
///
Expand Down
34 changes: 34 additions & 0 deletions src/protocol/creation/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ pub async fn create_file(
) -> actix_web::Result<HttpResponse> {
// Getting Upload-Length header value as usize.
let length = parse_header(&request, "Upload-Length");

// With this option enabled,
// we have to check whether length is a non-zero number.
if !state.config.allow_empty {
if let Some(0) = length {
return Ok(HttpResponse::BadRequest().body("Upload-Length should be greater than zero"));
}
}

// Checking Upload-Defer-Length header.
let defer_size = check_header(&request, "Upload-Defer-Length", |val| val == "1");

Expand Down Expand Up @@ -284,6 +293,31 @@ mod tests {
assert_eq!(file_info.offset, 0);
}

#[actix_rt::test]
async fn wrong_length() {
let state = State::test_new().await;
let mut rustus = get_service(state.clone()).await;
let request = TestRequest::post()
.uri(state.config.test_url().as_str())
.insert_header(("Upload-Length", 0))
.to_request();
let resp = call_service(&mut rustus, request).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

#[actix_rt::test]
async fn allow_empty() {
let mut state = State::test_new().await;
state.config.allow_empty = true;
let mut rustus = get_service(state.clone()).await;
let request = TestRequest::post()
.uri(state.config.test_url().as_str())
.insert_header(("Upload-Length", 0))
.to_request();
let resp = call_service(&mut rustus, request).await;
assert_eq!(resp.status(), StatusCode::CREATED);
}

#[actix_rt::test]
async fn success_with_bytes() {
let state = State::test_new().await;
Expand Down

0 comments on commit 5ed8f45

Please sign in to comment.