rxing/client/result/URIResultParser.rs
1/*
2 * Copyright 2007 ZXing authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// package com.google.zxing.client.result;
18
19// import com.google.zxing.RXingResult;
20
21// import java.util.regex.Matcher;
22// import java.util.regex.Pattern;
23
24use once_cell::sync::Lazy;
25/**
26 * Tries to parse results that are a URI of some kind.
27 *
28 * @author Sean Owen
29 */
30// public final class URIRXingResultParser extends RXingResultParser {
31use regex::Regex;
32
33use crate::RXingResult;
34
35use super::{ParsedClientResult, ResultParser, URIParsedRXingResult};
36
37static ALLOWED_URI_CHARS: Lazy<Regex> = Lazy::new(|| {
38 Regex::new(ALLOWED_URI_CHARS_PATTERN).expect("Regex patterns should always compile")
39});
40static USER_IN_HOST: Lazy<Regex> =
41 Lazy::new(|| Regex::new(":/*([^/@]+)@[^/]+").expect("Regex patterns should always compile"));
42
43/// See http://www.ietf.org/rfc/rfc2396.txt
44static URL_WITH_PROTOCOL_PATTERN: Lazy<Regex> =
45 Lazy::new(|| Regex::new("[a-zA-Z][a-zA-Z0-9+-.]+:").unwrap());
46
47/// (host name elements; allow up to say 6 domain elements), (maybe port), (query, path or nothing)
48static URL_WITHOUT_PROTOCOL_PATTERN: Lazy<Regex> =
49 Lazy::new(|| Regex::new("([a-zA-Z0-9\\-]+\\.){1,6}[a-zA-Z]{2,}(:\\d{1,5})?(/|\\?|$)").unwrap());
50
51const ALLOWED_URI_CHARS_PATTERN: &str = "[-._~:/?#\\[\\]@!$&'()*+,;=%A-Za-z0-9]+";
52
53pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
54 let raw_text = ResultParser::getMassagedText(result);
55 // We specifically handle the odd "URL" scheme here for simplicity and add "URI" for fun
56 // Assume anything starting this way really means to be a URI
57 if raw_text.starts_with("URL:") || raw_text.starts_with("URI:") {
58 return Some(ParsedClientResult::URIResult(URIParsedRXingResult::new(
59 raw_text[4..].trim().to_owned(),
60 String::default(),
61 )));
62 // return new URIParsedRXingResult(rawText.substring(4).trim(), null);
63 }
64 let raw_text = raw_text.trim();
65 if !is_basically_valid_uri(raw_text) || is_possibly_malicious_uri(raw_text) {
66 return None;
67 }
68 Some(ParsedClientResult::URIResult(URIParsedRXingResult::new(
69 raw_text.to_owned(),
70 String::default(),
71 )))
72}
73
74/**
75 * @return true if the URI contains suspicious patterns that may suggest it intends to
76 * mislead the user about its true nature. At the moment this looks for the presence
77 * of user/password syntax in the host/authority portion of a URI which may be used
78 * in attempts to make the URI's host appear to be other than it is. Example:
79 * http://yourbank.com@phisher.com This URI connects to phisher.com but may appear
80 * to connect to yourbank.com at first glance.
81 */
82pub fn is_possibly_malicious_uri(uri: &str) -> bool {
83 let allowed = if let Some(fnd) = ALLOWED_URI_CHARS.find(uri) {
84 fnd.start() == 0 && fnd.end() == uri.len()
85 } else {
86 false
87 };
88 let user = USER_IN_HOST.is_match(uri);
89
90 !allowed || user
91}
92
93pub fn is_basically_valid_uri(uri: &str) -> bool {
94 if uri.contains(' ') {
95 // Quick hack check for a common case
96 return false;
97 }
98 // let m = Regex::new(URL_WITH_PROTOCOL_PATTERN).expect("Regex patterns should always copile"); //.matcher(uri);
99 if let Some(found) = URL_WITH_PROTOCOL_PATTERN.find(uri) {
100 if found.start() == 0 {
101 // match at start only
102 return true;
103 }
104 }
105
106 // let m = Regex::new(URL_WITHOUT_PROTOCOL_PATTERN).expect("Regex patterns should always copile"); //.matcher(uri);
107 if let Some(found) = URL_WITHOUT_PROTOCOL_PATTERN.find(uri) {
108 found.start() == 0
109 } else {
110 false
111 }
112}
113
114// }