Skip to content

Commit

Permalink
percent-decode for user and password #166
Browse files Browse the repository at this point in the history
  • Loading branch information
suharev7 committed Feb 13, 2022
1 parent f339be2 commit 84a35d5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tokio_io = ["tokio"]

[dependencies]
byteorder = "^1.3"
chrono-tz = "0.5"
chrono-tz = "0.6"
crossbeam = "0.8.0"
thiserror = "1.0.20"
futures-core = "0.3.16"
Expand All @@ -33,6 +33,7 @@ pin-project = "1.0.8"
url="^2"
uuid = "0.8.1"
combine = "4.2.3"
percent-encoding = "2.1.0"

[dependencies.futures-util]
version = "0.3.16"
Expand Down
34 changes: 29 additions & 5 deletions src/types/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::errors::{Error, Result, UrlError};
use std::fmt::Formatter;
#[cfg(feature = "tls")]
use native_tls;
use percent_encoding::percent_decode;
use url::Url;

const DEFAULT_MIN_CONNS: usize = 10;
Expand Down Expand Up @@ -505,7 +506,8 @@ fn parse_param<'a, F, T, E>(
where
F: Fn(&str) -> std::result::Result<T, E>,
{
match parse(value.as_ref()) {
let source = percent_decode(value.as_bytes()).decode_utf8_lossy();
match parse(source.as_ref()) {
Ok(value) => Ok(value),
Err(_) => Err(UrlError::InvalidParamValue {
param: param.into(),
Expand All @@ -514,16 +516,19 @@ where
}
}

fn get_username_from_url(url: &Url) -> Option<&str> {
fn get_username_from_url(url: &Url) -> Option<Cow<'_, str>> {
let user = url.username();
if user.is_empty() {
return None;
}
Some(user)
Some(percent_decode(user.as_bytes())
.decode_utf8_lossy())
}

fn get_password_from_url(url: &Url) -> Option<&str> {
url.password()
fn get_password_from_url(url: &Url) -> Option<Cow<'_, str>> {
let password = url.password()?;
Some(percent_decode(password.as_bytes())
.decode_utf8_lossy())
}

fn get_database_from_url(url: &Url) -> Result<Option<&str>> {
Expand Down Expand Up @@ -648,6 +653,25 @@ mod test {
);
}

#[test]
fn test_parse_encoded_creds() {
let url = "tcp://user%20%3Cbar%3E:password%20%3Cbar%3E@host1:9001/database?ping_timeout=42ms&keepalive=99s&compression=lz4&connection_timeout=10s";
assert_eq!(
Options {
username: "user <bar>".into(),
password: "password <bar>".into(),
addr: Url::parse("tcp://user%20%3Cbar%3E:password%20%3Cbar%3E@host1:9001").unwrap(),
database: "database".into(),
keepalive: Some(Duration::from_secs(99)),
ping_timeout: Duration::from_millis(42),
connection_timeout: Duration::from_secs(10),
compression: true,
..Options::default()
},
from_url(url).unwrap(),
);
}

#[test]
fn test_parse_options() {
let url = "tcp://username:password@host1:9001/database?ping_timeout=42ms&keepalive=99s&compression=lz4&connection_timeout=10s";
Expand Down

0 comments on commit 84a35d5

Please sign in to comment.