1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.

//! Common validation logic.
//!
//! All functions are predicates,
//! returning `true` if a value is valid
//! and `false` if it isn't.
//! Note that the intention is not to provide
//! perfect validation in each case.
//! Mostly it is to rule out obvious mistakes
//! when setting values in config
//! or wiring in request parameters.

use regex::Regex;
use rusoto_core::Region;

#[cfg(test)]
mod test;

lazy_static! {
    static ref AWS_ACCESS_FORMAT: Regex = Regex::new("^[A-Z0-9]+$").unwrap();
    static ref AWS_SECRET_FORMAT: Regex = Regex::new("^[A-Za-z0-9+/=]+$").unwrap();
    static ref BASE_URI_FORMAT: Regex = Regex::new(
        r"^https?://[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*(?::[0-9]+)?/(?:[A-Za-z0-9-]+/)*$"
    ).unwrap();
    static ref EMAIL_ADDRESS_FORMAT: Regex = Regex::new(
        r"^[a-zA-Z0-9.\pL\pN!#$%&'*+/=?^_`{|}~-]{1,64}@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)+$"
    ).unwrap();
    static ref HOST_FORMAT: Regex = Regex::new(r"^[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*$").unwrap();
    static ref SENDER_NAME_FORMAT: Regex =
        Regex::new(r"^[A-Za-z0-9-]+(?: [A-Za-z0-9-]+)*$").unwrap();
    static ref SENDGRID_API_KEY_FORMAT: Regex = Regex::new("^[A-Za-z0-9._-]+$").unwrap();
    static ref SENTRY_DSN_FORMAT: Regex =
        Regex::new(r"^https?://[A-Za-z0-9+/:=\.]+@[A-Za-z0-9+/:=\.]+/[0-9]+$").unwrap();
    static ref SQS_URL_FORMAT: Regex =
        Regex::new(r"^https://sqs\.[a-z0-9-]+\.amazonaws\.com/[0-9]+/[A-Za-z0-9-]+$").unwrap();
}

/// Validate an AWS region.
pub fn aws_region(value: &str) -> bool {
    value.parse::<Region>().is_ok()
}

/// Validate an AWS access key.
pub fn aws_access(value: &str) -> bool {
    AWS_ACCESS_FORMAT.is_match(value)
}

/// Validate an AWS secret key.
pub fn aws_secret(value: &str) -> bool {
    AWS_SECRET_FORMAT.is_match(value)
}

/// Validate a base URI.
pub fn base_uri(value: &str) -> bool {
    BASE_URI_FORMAT.is_match(value)
}

/// Validate an email address.
pub fn email_address(value: &str) -> bool {
    value.len() < 254 && EMAIL_ADDRESS_FORMAT.is_match(value)
}

/// Validate a host name or IP address.
pub fn host(value: &str) -> bool {
    HOST_FORMAT.is_match(value)
}

/// Validate a sender name.
pub fn sender_name(value: &str) -> bool {
    SENDER_NAME_FORMAT.is_match(value)
}

/// Validate a Sendgrid API key.
pub fn sendgrid_api_key(value: &str) -> bool {
    SENDGRID_API_KEY_FORMAT.is_match(value)
}

/// Validate a Sentry DSN.
pub fn sentry_dsn(value: &str) -> bool {
    SENTRY_DSN_FORMAT.is_match(value)
}

/// Validate an AWS SQS URL.
pub fn sqs_url(value: &str) -> bool {
    SQS_URL_FORMAT.is_match(value)
}