File size: 8,592 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
229
230
231
232
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::sync::Arc;

use serde::{Deserialize, Deserializer};
use url::Url;
use urlpattern::{UrlPattern, UrlPatternMatchInput};

#[allow(rustdoc::bare_urls)]
#[derive(Debug)]
pub struct Entry {
    pub url: UrlPattern,
}

fn parse_url_pattern(s: &str) -> Result<UrlPattern, urlpattern::quirks::Error> {
    let mut init = urlpattern::UrlPatternInit::parse_constructor_string::<regex::Regex>(s, None)?;
    if init.search.as_ref().map(|p| p.is_empty()).unwrap_or(true) {
        init.search.replace("*".to_string());
    }
    if init.hash.as_ref().map(|p| p.is_empty()).unwrap_or(true) {
        init.hash.replace("*".to_string());
    }
    if init
        .pathname
        .as_ref()
        .map(|p| p.is_empty() || p == "/")
        .unwrap_or(true)
    {
        init.pathname.replace("*".to_string());
    }
    UrlPattern::parse(init, Default::default())
}

#[derive(Deserialize)]
#[serde(untagged)]
pub(crate) enum EntryRaw {
    Value(String),
    Object { url: String },
}

impl<'de> Deserialize<'de> for Entry {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        EntryRaw::deserialize(deserializer).and_then(|raw| {
            let url = match raw {
                EntryRaw::Value(url) => url,
                EntryRaw::Object { url } => url,
            };
            Ok(Entry {
                url: parse_url_pattern(&url).map_err(|e| {
                    serde::de::Error::custom(format!("`{url}` is not a valid URL pattern: {e}"))
                })?,
            })
        })
    }
}

/// Scope for filesystem access.
#[derive(Debug)]
pub struct Scope<'a> {
    allowed: Vec<&'a Arc<Entry>>,
    denied: Vec<&'a Arc<Entry>>,
}

impl<'a> Scope<'a> {
    /// Creates a new scope from the scope configuration.
    pub(crate) fn new(allowed: Vec<&'a Arc<Entry>>, denied: Vec<&'a Arc<Entry>>) -> Self {
        Self { allowed, denied }
    }

    /// Determines if the given URL is allowed on this scope.
    pub fn is_allowed(&self, url: &Url) -> bool {
        let denied = self.denied.iter().any(|entry| {
            entry
                .url
                .test(UrlPatternMatchInput::Url(url.clone()))
                .unwrap_or_default()
        });
        if denied {
            false
        } else {
            self.allowed.iter().any(|entry| {
                entry
                    .url
                    .test(UrlPatternMatchInput::Url(url.clone()))
                    .unwrap_or_default()
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{str::FromStr, sync::Arc};

    use super::Entry;

    impl FromStr for Entry {
        type Err = urlpattern::quirks::Error;

        fn from_str(s: &str) -> Result<Self, Self::Err> {
            let pattern = super::parse_url_pattern(s)?;
            Ok(Self { url: pattern })
        }
    }

    #[test]
    fn denied_takes_precedence() {
        let allow = Arc::new("http://localhost:8080/file.png".parse().unwrap());
        let deny = Arc::new("http://localhost:8080/*".parse().unwrap());
        let scope = super::Scope::new(vec![&allow], vec![&deny]);
        assert!(!scope.is_allowed(&"http://localhost:8080/file.png".parse().unwrap()));
        assert!(!scope.is_allowed(&"http://localhost:8080?framework=tauri".parse().unwrap()));
    }

    #[test]
    fn fixed_url() {
        // plain URL
        let entry = Arc::new("http://localhost:8080".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());
        assert!(scope.is_allowed(&"http://localhost:8080".parse().unwrap()));
        assert!(scope.is_allowed(&"http://localhost:8080/".parse().unwrap()));
        assert!(scope.is_allowed(&"http://localhost:8080/file".parse().unwrap()));
        assert!(scope.is_allowed(&"http://localhost:8080/path/to/asset.png".parse().unwrap()));
        assert!(scope.is_allowed(&"http://localhost:8080/path/list?limit=50".parse().unwrap()));

        assert!(!scope.is_allowed(&"https://localhost:8080".parse().unwrap()));
        assert!(!scope.is_allowed(&"http://localhost:8081".parse().unwrap()));
        assert!(!scope.is_allowed(&"http://local:8080".parse().unwrap()));
    }

    #[test]
    fn fixed_path() {
        // URL with fixed path
        let entry = Arc::new("http://localhost:8080/file.png".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());

        assert!(scope.is_allowed(&"http://localhost:8080/file.png".parse().unwrap()));
        assert!(scope.is_allowed(&"http://localhost:8080/file.png?q=1".parse().unwrap()));

        assert!(!scope.is_allowed(&"http://localhost:8080".parse().unwrap()));
        assert!(!scope.is_allowed(&"http://localhost:8080/file".parse().unwrap()));
        assert!(!scope.is_allowed(&"http://localhost:8080/file.png/other.jpg".parse().unwrap()));
    }

    #[test]
    fn pattern_wildcard() {
        let entry = Arc::new("http://localhost:8080/*.png".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());

        assert!(scope.is_allowed(&"http://localhost:8080/file.png".parse().unwrap()));
        assert!(scope.is_allowed(&"http://localhost:8080/file.png#head".parse().unwrap()));
        assert!(scope.is_allowed(&"http://localhost:8080/assets/file.png".parse().unwrap()));
        assert!(scope.is_allowed(
            &"http://localhost:8080/assets/file.png?width=100&height=200"
                .parse()
                .unwrap()
        ));

        assert!(!scope.is_allowed(&"http://localhost:8080/file.jpeg".parse().unwrap()));
    }

    #[test]
    fn domain_wildcard() {
        let entry = Arc::new("http://*".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());

        assert!(scope.is_allowed(&"http://something.else".parse().unwrap()));
        assert!(scope.is_allowed(&"http://something.else#tauri".parse().unwrap()));
        assert!(scope.is_allowed(&"http://something.else/path/to/file".parse().unwrap()));
        assert!(scope.is_allowed(&"http://something.else?rel=tauri".parse().unwrap()));
        assert!(scope.is_allowed(
            &"http://something.else/path/to/file.mp4?start=500"
                .parse()
                .unwrap()
        ));

        assert!(!scope.is_allowed(&"https://something.else".parse().unwrap()));

        let entry = Arc::new("http://*/*".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());

        assert!(scope.is_allowed(&"http://something.else".parse().unwrap()));
        assert!(scope.is_allowed(&"http://something.else/path/to/file".parse().unwrap()));
    }

    #[test]
    fn scheme_wildcard() {
        let entry = Arc::new("*://*".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());

        assert!(scope.is_allowed(&"http://something.else".parse().unwrap()));
        assert!(scope.is_allowed(&"http://something.else/path/to/file".parse().unwrap()));
        assert!(scope.is_allowed(&"file://path".parse().unwrap()));
        assert!(scope.is_allowed(&"file://path/to/file".parse().unwrap()));
        assert!(scope.is_allowed(&"https://something.else".parse().unwrap()));
        assert!(scope.is_allowed(&"https://something.else?x=1#frag".parse().unwrap()));

        let entry = Arc::new("*://*/*".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());

        assert!(scope.is_allowed(&"http://something.else".parse().unwrap()));
        assert!(scope.is_allowed(&"http://something.else/path/to/file".parse().unwrap()));
        assert!(scope.is_allowed(&"file://path/to/file".parse().unwrap()));
        assert!(scope.is_allowed(&"https://something.else".parse().unwrap()));
    }

    #[test]
    fn validate_query() {
        let entry = Arc::new("https://tauri.app/path?x=*".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());

        assert!(scope.is_allowed(&"https://tauri.app/path?x=5".parse().unwrap()));

        assert!(!scope.is_allowed(&"https://tauri.app/path?y=5".parse().unwrap()));
    }

    #[test]
    fn validate_hash() {
        let entry = Arc::new("https://tauri.app/path#frame*".parse().unwrap());
        let scope = super::Scope::new(vec![&entry], Vec::new());

        assert!(scope.is_allowed(&"https://tauri.app/path#frame".parse().unwrap()));

        assert!(!scope.is_allowed(&"https://tauri.app/path#work".parse().unwrap()));
    }
}