Skip to content

Commit

Permalink
Merge pull request #9 from filiptronicek/main
Browse files Browse the repository at this point in the history
Validate clip format
  • Loading branch information
filiptronicek authored Jan 7, 2024
2 parents 55e544c + f4b699f commit 4b657ba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
44 changes: 37 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fern = "0.5"
git2 = "0.16.1"
dotenv = "0.15.0"
diesel = { version = "2.1.4", features = ["postgres", "chrono"] }
regex = "1.10"

[dependencies.rocket]
version = "0.5.0"
Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod models;
mod schema;
mod utils;

use regex::Regex;
use rocket::http::Status;
use rocket::response::status::Custom;
use utils::id::gen_id;
Expand Down Expand Up @@ -138,6 +139,14 @@ fn set_clip(
}
};

if url.scheme() != "http" && url.scheme() != "https" {
let response = APIResponse {
status: APIStatus::Error,
result: "Invalid URL scheme".to_string(),
};
return Err(Custom(Status::BadRequest, Json(response)));
}

let mut db_connection = match db::initialize() {
Ok(conn) => conn,
Err(err) => {
Expand Down Expand Up @@ -204,6 +213,15 @@ fn get_clip(
return Err(Custom(Status::BadRequest, Json(response)));
}

let code_pattern = Regex::new(r"^(?i)[A-Z0-9]{5}$").unwrap();
if !code_pattern.is_match(code.as_str()) {
let response = APIResponse {
status: APIStatus::Error,
result: "Invalid clip code format".to_string(),
};
return Err(Custom(Status::BadRequest, Json(response)));
}

let mut db_connection = match db::initialize() {
Ok(conn) => conn,
Err(err) => {
Expand Down

0 comments on commit 4b657ba

Please sign in to comment.