Skip to content

Commit

Permalink
feat: trim whitespace from scanned QR codes
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Nov 21, 2024
1 parent 4aad8fb commit 75e1517
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ fn starts_with_ignore_case(string: &str, pattern: &str) -> bool {
/// The function should be called after a QR code is scanned.
/// The function takes the raw text scanned and checks what can be done with it.
pub async fn check_qr(context: &Context, qr: &str) -> Result<Qr> {
let qr = qr.trim();
let qrcode = if starts_with_ignore_case(qr, OPENPGP4FPR_SCHEME) {
decode_openpgp(context, qr)
.await
Expand Down Expand Up @@ -994,6 +995,17 @@ mod tests {
}
);

// Test that QR code whitespace is stripped.
// Users can copy-paste QR code contents and "scan"
// from the clipboard.
let qr = check_qr(&ctx.ctx, " \thttp://www.hello.com/hello \n\t \r\n ").await?;
assert_eq!(
qr,
Qr::Url {
url: "http://www.hello.com/hello".to_string(),
}
);

Ok(())
}

Expand Down Expand Up @@ -1743,7 +1755,9 @@ mod tests {
);

// Test URL without port.
let res = set_config_from_qr(&t, "https://t.me/socks?server=1.2.3.4").await;
//
// Also check that whitespace is trimmed.
let res = set_config_from_qr(&t, " https://t.me/socks?server=1.2.3.4\n").await;
assert!(res.is_ok());
assert_eq!(t.get_config_bool(Config::ProxyEnabled).await?, true);
assert_eq!(
Expand Down

0 comments on commit 75e1517

Please sign in to comment.