pastes: validate URLs using regex

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2020-09-24 22:45:48 +05:30
parent 8ac5b3ca8b
commit c075068649
No known key found for this signature in database
GPG Key ID: F475143EDEDEBA3C
3 changed files with 5 additions and 18 deletions

17
Cargo.lock generated
View File

@ -806,6 +806,7 @@ dependencies = [
"jirachi", "jirachi",
"jirachi_cli", "jirachi_cli",
"jsonwebtoken", "jsonwebtoken",
"regex 1.3.9",
"rocket", "rocket",
"rocket_contrib", "rocket_contrib",
"rocket_cors", "rocket_cors",
@ -816,7 +817,6 @@ dependencies = [
"slog-async", "slog-async",
"slog-term", "slog-term",
"uuid", "uuid",
"validator",
] ]
[[package]] [[package]]
@ -1873,21 +1873,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "validator"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e60fadf92c22236de4028ceb0b8af50ed3430d41ad43d7a7d63b6bd1a8f47c38"
dependencies = [
"idna 0.2.0",
"lazy_static",
"regex 1.3.9",
"serde",
"serde_derive",
"serde_json",
"url 2.1.1",
]
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.10" version = "0.2.10"

View File

@ -23,13 +23,13 @@ jsonwebtoken = "7.2.0"
slog = "2.5.2" slog = "2.5.2"
slog-term = "2.6.0" slog-term = "2.6.0"
anyhow = "1.0.31" anyhow = "1.0.31"
validator = "0.10.1"
slog-async = "2.5.0" slog-async = "2.5.0"
bcrypt = "0.8.1" bcrypt = "0.8.1"
chrono = "0.4.13" chrono = "0.4.13"
uuid = { version = "0.8.1", features = ["serde", "v4"] } uuid = { version = "0.8.1", features = ["serde", "v4"] }
jirachi = { version = "0.1.4", features = ["collision-resistant"] } jirachi = { version = "0.1.4", features = ["collision-resistant"] }
rocket_cors = "0.5.1" rocket_cors = "0.5.1"
regex = "1.3.9"
[dev-dependencies] [dev-dependencies]
diesel_cli = { version = "1.4.1", default-features = false, features = ["postgres"] } diesel_cli = { version = "1.4.1", default-features = false, features = ["postgres"] }

View File

@ -1,11 +1,13 @@
use anyhow::Result; use anyhow::Result;
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use regex::Regex;
use super::entity::Paste; use super::entity::Paste;
use super::postgres; use super::postgres;
pub fn create_paste(paste: &mut Paste, conn: &PgConnection) -> Result<usize> { pub fn create_paste(paste: &mut Paste, conn: &PgConnection) -> Result<usize> {
paste.is_url = Some(validator::validate_url(paste.content.clone())); let re = Regex::new("^(https?://)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(:\\d+)?(/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(#[-a-z\\d_]*)?$").unwrap();
paste.is_url = Some(re.is_match(&*paste.content.clone()));
postgres::create_paste(paste, conn) postgres::create_paste(paste, conn)
} }