abstract_std/objects/validation/
verifiers.rs

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use super::ValidationError;

pub(crate) const MIN_DESC_LENGTH: usize = 1;
pub(crate) const MAX_DESC_LENGTH: usize = 1024;
/// Minimum link length is 11, because the shortest url could be http://a.be
pub(crate) const MIN_LINK_LENGTH: usize = 11;
pub(crate) const MAX_LINK_LENGTH: usize = 128;
pub(crate) const MIN_TITLE_LENGTH: usize = 1;
pub(crate) const MAX_TITLE_LENGTH: usize = 64;

pub(crate) const DANGEROUS_CHARS: &[char] = &['"', '\'', '=', '>', '<'];

fn contains_dangerous_characters(input: &str) -> bool {
    input.chars().any(|c| DANGEROUS_CHARS.contains(&c))
}

fn is_valid_url(link: &str) -> bool {
    link.starts_with("http://") || link.starts_with("https://") || link.starts_with("ipfs://")
}

pub fn validate_link(link: Option<&str>) -> Result<(), ValidationError> {
    if let Some(link) = link {
        if link.len() < MIN_LINK_LENGTH {
            Err(ValidationError::LinkInvalidShort(MIN_LINK_LENGTH))
        } else if link.len() > MAX_LINK_LENGTH {
            Err(ValidationError::LinkInvalidLong(MAX_LINK_LENGTH))
        } else if !is_valid_url(link) {
            Err(ValidationError::LinkInvalidFormat {})
        } else if contains_dangerous_characters(link) {
            Err(ValidationError::LinkContainsDangerousCharacters {})
        } else {
            Ok(())
        }
    } else {
        Ok(())
    }
}

pub fn validate_name(title: &str) -> Result<(), ValidationError> {
    if title.len() < MIN_TITLE_LENGTH {
        Err(ValidationError::TitleInvalidShort(MIN_TITLE_LENGTH))
    } else if title.len() > MAX_TITLE_LENGTH {
        Err(ValidationError::TitleInvalidLong(MAX_TITLE_LENGTH))
    } else if contains_dangerous_characters(title) {
        Err(ValidationError::TitleContainsDangerousCharacters {})
    } else {
        Ok(())
    }
}

pub fn validate_description(maybe_description: Option<&str>) -> Result<(), ValidationError> {
    if let Some(description) = maybe_description {
        if description.len() < MIN_DESC_LENGTH {
            return Err(ValidationError::DescriptionInvalidShort(MIN_DESC_LENGTH));
        } else if description.len() > MAX_DESC_LENGTH {
            return Err(ValidationError::DescriptionInvalidLong(MAX_DESC_LENGTH));
        } else if contains_dangerous_characters(description) {
            return Err(ValidationError::DescriptionContainsDangerousCharacters {});
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;

    mod link {
        use super::*;

        #[rstest(
            input,
            case("https://www.google.com"),
            case("http://example.com"),
            case("https://example.net:8080")
        )]
        fn valid(input: &str) {
            assert!(validate_link(Some(input)).is_ok());
        }

        #[rstest(
            input,
            case("http://a.b"),
            case("://example.com"),
            case("example.com"),
            case("https://example.org/path?query=value"),
            case("https:/example.com")
        )]
        fn invalid(input: &str) {
            assert!(validate_link(Some(input)).is_err());
        }
    }

    mod name {
        use super::*;

        #[rstest(input,
        case("name"),
        case("name123"),
        case("name 123"),
        case("a"),
        case(& "a".repeat(MAX_TITLE_LENGTH)),
        case("name!$%&*+,-.;@^_`|~"),
        case("名前"),
        )]
        fn valid_names(input: &str) {
            assert!(validate_name(input).is_ok());
        }

        #[rstest(input,
        case(""),
        case(& "a".repeat(MAX_TITLE_LENGTH + 1)),
        case("name<>'\""),
        )]
        fn invalid_names(input: &str) {
            assert!(validate_name(input).is_err());
        }
    }

    mod description {
        use super::*;

        #[rstest(input,
        case("d"),
        case("description123"),
        case("description 123"),
        case(& "a".repeat(MAX_DESC_LENGTH)),
        case("description!$%&*+,-.;@^_`|~"),
        case("説明"),
        )]
        fn valid_descriptions(input: &str) {
            assert!(validate_description(Some(input)).is_ok());
        }

        #[rstest(input,
        case(""),
        case(& "a".repeat(MAX_DESC_LENGTH + 1)),
        case("description<>'\""),
        )]
        fn invalid_descriptions(input: &str) {
            assert!(validate_description(Some(input)).is_err());
        }
    }
}