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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::collections::HashSet;
use std::hash::Hash;

use url::{Host, Url};

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum HostIs {
    Valid,
    Invalid,
}

pub trait ValidateHosts<RHS = Self> {
    fn validate_hosts(&self, valid_hosts: &[Url]) -> HostIs;
}

impl ValidateHosts for Url {
    fn validate_hosts(&self, valid_hosts: &[Url]) -> HostIs {
        if valid_hosts.is_empty() {
            return HostIs::Invalid;
        }

        let size_before = valid_hosts.len();
        let hosts: Vec<Host<&str>> = valid_hosts.iter().flat_map(|url| url.host()).collect();
        assert_eq!(size_before, hosts.len());

        if let Some(host) = self.host() {
            if hosts.contains(&host) {
                return HostIs::Valid;
            }
        }

        for value in valid_hosts.iter() {
            if !value.scheme().eq("https") {
                return HostIs::Invalid;
            }
        }

        HostIs::Invalid
    }
}

impl ValidateHosts for String {
    fn validate_hosts(&self, valid_hosts: &[Url]) -> HostIs {
        if let Ok(url) = Url::parse(self) {
            return url.validate_hosts(valid_hosts);
        }

        HostIs::Invalid
    }
}

impl ValidateHosts for &str {
    fn validate_hosts(&self, valid_hosts: &[Url]) -> HostIs {
        if let Ok(url) = Url::parse(self) {
            return url.validate_hosts(valid_hosts);
        }

        HostIs::Invalid
    }
}

#[derive(Clone, Debug)]
pub struct AllowedHostValidator {
    allowed_hosts: HashSet<Url>,
}

impl AllowedHostValidator {
    pub fn new(allowed_hosts: HashSet<Url>) -> AllowedHostValidator {
        for url in allowed_hosts.iter() {
            if !url.scheme().eq("https") {
                panic!("Requires https scheme");
            }
        }

        AllowedHostValidator { allowed_hosts }
    }

    pub fn validate_str(&self, url_str: &str) -> HostIs {
        if let Ok(url) = Url::parse(url_str) {
            return self.validate_hosts(&[url]);
        }

        HostIs::Invalid
    }

    pub fn validate_url(&self, url: &Url) -> HostIs {
        self.validate_hosts(&[url.clone()])
    }
}

impl From<&[Url]> for AllowedHostValidator {
    fn from(value: &[Url]) -> Self {
        let hash_set = HashSet::from_iter(value.iter().cloned());
        AllowedHostValidator::new(hash_set)
    }
}

impl ValidateHosts for AllowedHostValidator {
    fn validate_hosts(&self, valid_hosts: &[Url]) -> HostIs {
        if valid_hosts.is_empty() {
            return HostIs::Invalid;
        }

        let urls: Vec<Url> = self.allowed_hosts.iter().cloned().collect();
        for url in valid_hosts.iter() {
            if url.validate_hosts(urls.as_slice()).eq(&HostIs::Invalid) {
                return HostIs::Invalid;
            }
        }

        HostIs::Valid
    }
}

impl Default for AllowedHostValidator {
    fn default() -> Self {
        let urls: HashSet<Url> = [
            "https://graph.microsoft.com",
            "https://graph.microsoft.us",
            "https://dod-graph.microsoft.us",
            "https://graph.microsoft.de",
            "https://microsoftgraph.chinacloudapi.cn",
            "https://canary.graph.microsoft.com",
        ]
        .iter()
        .flat_map(|url_str| Url::parse(url_str))
        .collect();
        assert_eq!(6, urls.len());

        AllowedHostValidator::new(urls)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_valid_hosts() {
        let valid_hosts: Vec<String> = [
            "graph.microsoft.com",
            "graph.microsoft.us",
            "dod-graph.microsoft.us",
            "graph.microsoft.de",
            "microsoftgraph.chinacloudapi.cn",
            "canary.graph.microsoft.com",
        ]
        .iter()
        .map(|s| s.to_string())
        .collect();

        let host_urls: Vec<Url> = valid_hosts
            .iter()
            .map(|s| format!("https://{s}"))
            .flat_map(|s| Url::parse(&s))
            .collect();

        assert_eq!(6, host_urls.len());

        for url in host_urls.iter() {
            assert_eq!(HostIs::Valid, url.validate_hosts(&host_urls));
        }
    }

    #[test]
    fn test_invalid_hosts() {
        let invalid_hosts = [
            "graph.on.microsoft.com",
            "microsoft.com",
            "windows.net",
            "example.org",
        ];

        let valid_hosts: Vec<Url> = [
            "graph.microsoft.com",
            "graph.microsoft.us",
            "dod-graph.microsoft.us",
            "graph.microsoft.de",
            "microsoftgraph.chinacloudapi.cn",
            "canary.graph.microsoft.com",
        ]
        .iter()
        .map(|s| Url::parse(&format!("https://{s}")).unwrap())
        .collect();
        assert_eq!(6, valid_hosts.len());

        let host_urls: Vec<Url> = invalid_hosts
            .iter()
            .map(|s| format!("https://{s}"))
            .flat_map(|s| Url::parse(&s))
            .collect();

        assert_eq!(4, host_urls.len());

        for url in host_urls.iter() {
            assert_eq!(HostIs::Invalid, url.validate_hosts(valid_hosts.as_slice()));
        }
    }

    #[test]
    fn test_allowed_host_validator() {
        let valid_hosts: Vec<String> = [
            "graph.microsoft.com",
            "graph.microsoft.us",
            "dod-graph.microsoft.us",
            "graph.microsoft.de",
            "microsoftgraph.chinacloudapi.cn",
            "canary.graph.microsoft.com",
        ]
        .iter()
        .map(|s| s.to_string())
        .collect();

        let host_urls: Vec<Url> = valid_hosts
            .iter()
            .map(|s| format!("https://{s}"))
            .flat_map(|s| Url::parse(&s))
            .collect();

        assert_eq!(6, host_urls.len());

        let allowed_host_validator = AllowedHostValidator::from(host_urls.as_slice());

        for url in host_urls.iter() {
            assert_eq!(HostIs::Valid, allowed_host_validator.validate_url(url));
        }
    }
}