green_copper_runtime/moduleloaders/
mod.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use log::trace;
use quickjs_runtime::jsutils::modules::ScriptModuleLoader;
use quickjs_runtime::jsutils::JsError;
use quickjs_runtime::quickjsrealmadapter::QuickJsRealmAdapter;
use std::fs;
use std::ops::Add;
use std::path::{Path, PathBuf};
use url::Url;

pub struct FileSystemModuleLoader {
    base_path: PathBuf,
}

fn last_index_of(haystack: &str, needle: &str) -> Option<usize> {
    let start = haystack.len() - needle.len();
    let mut x = start;
    loop {
        if haystack[x..(x + needle.len())].eq(needle) {
            return Some(x);
        }
        if x == 0 {
            break;
        }
        x -= 1;
    }
    None
}

pub fn normalize_path(ref_path: &str, name: &str) -> Result<String, JsError> {
    // todo support:
    // name starting with /
    // name starting or containing ../ or starting with ./

    let ref_path = if let Some(last_slash_idx) = last_index_of(ref_path, "/") {
        let mut path = ref_path.to_string();
        let _file_name = path.split_off(last_slash_idx);
        path
    } else {
        ref_path.to_string()
    };

    let url = Url::parse(ref_path.as_str()).map_err(|e| {
        JsError::new_string(format!("failed to parse Url [{ref_path}] due to : {e}"))
    })?;
    let path = if let Some(stripped) = name.strip_prefix('/') {
        stripped.to_string()
    } else {
        let url_path = url.path();
        if url_path.eq("/") {
            name.to_string()
        } else {
            format!("{}/{}", &url_path[1..], name)
        }
    };

    // remove ./
    // remove ..
    let mut path_parts: Vec<String> = path.split('/').map(|s| s.to_string()).collect();

    let mut x = 1;
    while x < path_parts.len() {
        if path_parts[x].as_str().eq("..") {
            path_parts.remove(x);
            path_parts.remove(x - 1);
            x = 0;
        }
        if path_parts[x].as_str().eq(".") {
            path_parts.remove(x);
            x = 0;
        }
        x += 1;
    }
    let path = path_parts.join("/");

    let mut res = url.scheme().to_string();
    res = res.add("://");
    if let Some(host) = url.host_str() {
        res = res.add(host);
        if let Some(port) = url.port() {
            res = res.add(format!(":{port}").as_str());
        }
    }
    res = res.add("/");

    res = res.add(path.as_str());

    log::debug!("normalize_path returning: {}", res);

    Ok(res)
}

impl FileSystemModuleLoader {
    pub fn new(base_path: &'static str) -> Self {
        log::trace!("FileSystemModuleLoader::new {}", base_path);
        Self {
            base_path: Path::new(base_path).canonicalize().expect("path not found"),
        }
    }

    fn get_real_fs_path(&self, abs_file_path: &str) -> PathBuf {
        assert!(abs_file_path.starts_with("file:///"));
        self.base_path.join(Path::new(&abs_file_path[8..]))
    }

    fn read_file(&self, filename: &str) -> Result<String, String> {
        trace!("FileSystemModuleLoader::read_file -> {}", filename);

        let path = self.get_real_fs_path(filename);
        if !path.exists() {
            return Err(format!("File not found: {filename}"));
        }
        let path = path.canonicalize().unwrap();
        if !path.starts_with(&self.base_path) {
            return Err(format!("File not allowed: {filename}"));
        }

        fs::read_to_string(path).map_err(|e| format!("failed to read: {filename}, caused by: {e}"))
    }

    fn file_exists(&self, filename: &str) -> bool {
        trace!("FileSystemModuleLoader::file_exists -> {}", filename);
        let path = self.get_real_fs_path(filename);
        path.exists() && path.canonicalize().unwrap().starts_with(&self.base_path)
    }
    fn normalize_file_path(&self, ref_path: &str, path: &str) -> Option<String> {
        // the ref path will always be an absolute path, so no need to parse . or ..
        // but even though we call it an absolute path here it will will be a relative path to the loader's main dir
        // so basically the file:// prefix is just to recognize the path a a path the FileSystemModuleLoader can handle

        if !ref_path.starts_with("file://") {
            return None;
        }
        if path.starts_with("file://") {
            return Some(path.to_string());
        }
        if path.contains("://") && !path.starts_with("file://") {
            // e.g. including a http:// based module from a file based module, should be handled by http loader
            return None;
        }

        match normalize_path(ref_path, path) {
            Ok(normalized) => {
                if self.file_exists(normalized.as_str()) {
                    Some(normalized)
                } else {
                    // todo support other module extensions
                    let ts_opt = format!("{normalized}.ts");
                    if self.file_exists(ts_opt.as_str()) {
                        Some(ts_opt)
                    } else {
                        None
                    }
                }
            }
            Err(e) => {
                log::error!("could not normalize {}: {}", path, e);
                None
            }
        }
    }
}

impl ScriptModuleLoader for FileSystemModuleLoader {
    fn normalize_path(
        &self,
        _realm: &QuickJsRealmAdapter,
        ref_path: &str,
        path: &str,
    ) -> Option<String> {
        self.normalize_file_path(ref_path, path)
    }

    fn load_module(&self, _realm: &QuickJsRealmAdapter, absolute_path: &str) -> String {
        self.read_file(absolute_path)
            .unwrap_or_else(|_| "".to_string())
    }
}

#[cfg(any(feature = "all", feature = "com", feature = "http"))]
pub struct HttpModuleLoader {
    is_secure_only: bool,
    is_validate_content_type: bool,
    allowed_domains: Option<Vec<String>>,
    _basic_auth: Option<(String, String)>,
    // todo stuff like clientcert / servercert checking
}

#[cfg(any(feature = "all", feature = "com", feature = "http"))]
impl HttpModuleLoader {
    pub fn new() -> Self {
        Self {
            is_secure_only: false,
            is_validate_content_type: true,
            allowed_domains: None,
            _basic_auth: None,
        }
    }

    pub fn secure_only(mut self) -> Self {
        self.is_secure_only = true;
        self
    }

    pub fn validate_content_type(mut self, validate: bool) -> Self {
        self.is_validate_content_type = validate;
        self
    }

    pub fn allow_domain(mut self, domain: &str) -> Self {
        if self.allowed_domains.is_none() {
            self.allowed_domains = Some(vec![]);
        }
        let domains = self.allowed_domains.as_mut().unwrap();
        domains.push(domain.to_string());
        self
    }

    fn read_url(&self, url: &str) -> Option<String> {
        let resp = reqwest::blocking::get(url);
        //let req = reqwest::get(url);
        // todo make read_url async
        if resp.is_err() {
            return None;
        }
        let resp = resp.expect("wtf");
        if self.is_validate_content_type {
            let ct = &resp.headers()["Content-Type"];
            if !(ct.eq("application/javascript") || ct.eq("text/javascript")) {
                log::error!("loaded module {} did not have javascript Content-Type", url);
                return None;
            }
        }
        // todo async
        let res = resp.text();
        match res {
            Ok(script) => Some(script),
            Err(e) => {
                log::error!("could not load {} due to: {}", url, e);
                None
            }
        }
    }

    fn is_allowed(&self, absolute_path: &str) -> bool {
        if self.is_secure_only || self.allowed_domains.is_some() {
            match Url::parse(absolute_path) {
                Ok(url) => {
                    if self.is_secure_only && !url.scheme().eq("https") {
                        false
                    } else if let Some(domains) = &self.allowed_domains {
                        if let Some(host) = url.host_str() {
                            domains.contains(&host.to_string())
                        } else {
                            false
                        }
                    } else {
                        true
                    }
                }
                Err(e) => {
                    log::error!(
                        "HttpModuleLoader.is_allowed: could not parse url: {}, {}",
                        absolute_path,
                        e
                    );
                    false
                }
            }
        } else {
            true
        }
    }

    fn normalize_http_path(&self, ref_path: &str, path: &str) -> Option<String> {
        // the ref path will always be an absolute path

        if path.starts_with("http://") || path.starts_with("https://") {
            return if self.is_allowed(path) {
                Some(path.to_string())
            } else {
                None
            };
        }

        if path.contains("://") {
            return None;
        }

        if !(ref_path.starts_with("http://") || ref_path.starts_with("https://")) {
            return None;
        }

        match normalize_path(ref_path, path) {
            Ok(normalized) => {
                if self.is_allowed(normalized.as_str()) {
                    Some(normalized)
                } else {
                    None
                }
            }
            Err(e) => {
                log::error!("could not normalize: {}: {}", path, e);
                None
            }
        }
    }
}

#[cfg(any(feature = "all", feature = "com", feature = "http"))]
impl Default for HttpModuleLoader {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(any(feature = "all", feature = "com", feature = "http"))]
impl ScriptModuleLoader for HttpModuleLoader {
    fn normalize_path(
        &self,
        _realm: &QuickJsRealmAdapter,
        ref_path: &str,
        path: &str,
    ) -> Option<String> {
        self.normalize_http_path(ref_path, path)
    }

    fn load_module(&self, _realm: &QuickJsRealmAdapter, absolute_path: &str) -> String {
        // todo, load_module should really return a Result
        if let Some(script) = self.read_url(absolute_path) {
            script
        } else {
            "".to_string()
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::moduleloaders::{
        last_index_of, normalize_path, FileSystemModuleLoader, HttpModuleLoader,
    };
    use std::path::Path;

    #[test]
    fn test_last_index_of() {
        assert_eq!(last_index_of("abcba", "b").unwrap(), 3);
        assert_eq!(last_index_of("abbcbba", "bb").unwrap(), 4);
    }

    #[test]
    fn test_normalize() {
        {
            assert_eq!(
                normalize_path("http://test.com/scripts/foo.es", "bar.mes")
                    .ok()
                    .unwrap(),
                "http://test.com/scripts/bar.mes"
            );
            assert_eq!(
                normalize_path("http://test.com/scripts/foo.es", "/bar.mes")
                    .ok()
                    .unwrap(),
                "http://test.com/bar.mes"
            );
            assert_eq!(
                normalize_path("http://test.com/scripts/foo.es", "../bar.mes")
                    .ok()
                    .unwrap(),
                "http://test.com/bar.mes"
            );
            assert_eq!(
                normalize_path("http://test.com/scripts/foo.es", "./bar.mes")
                    .ok()
                    .unwrap(),
                "http://test.com/scripts/bar.mes"
            );
            assert_eq!(
                normalize_path("file:///scripts/test.es", "bar.mes")
                    .ok()
                    .unwrap(),
                "file:///scripts/bar.mes"
            );
            assert_eq!(
                normalize_path("file:///scripts/test.es", "./bar.mes")
                    .ok()
                    .unwrap(),
                "file:///scripts/bar.mes"
            );
            assert_eq!(
                normalize_path("file:///scripts/test.es", "../bar.mes")
                    .ok()
                    .unwrap(),
                "file:///bar.mes"
            );
        }
    }

    #[test]
    fn test_http() {
        let loader = HttpModuleLoader::new()
            .secure_only()
            .validate_content_type(false)
            .allow_domain("github.com")
            .allow_domain("httpbin.org");
        // disallow http
        assert!(loader
            .normalize_http_path("http://github.com/example.js", "module.mjs")
            .is_none());
        // disallow domain
        assert!(loader
            .normalize_http_path("https://other.github.com/example.js", "module.mjs")
            .is_none());
        // allow domain
        assert!(loader
            .normalize_http_path("https://github.com/example.js", "module.mjs")
            .is_some());
        assert_eq!(
            loader
                .normalize_http_path("https://github.com/scripts/example.js", "module.mjs")
                .unwrap(),
            "https://github.com/scripts/module.mjs"
        );
        assert_eq!(
            loader
                .normalize_http_path("https://github.com/example.js", "module.mjs")
                .unwrap(),
            "https://github.com/module.mjs"
        );
    }

    #[test]
    fn test_fs() {
        let loader = FileSystemModuleLoader::new("./modules");
        let path = Path::new("./modules").canonicalize().unwrap();
        println!("path = {path:?}");
        assert!(loader
            .normalize_file_path("file:///test.es", "utils/assertions.mes")
            .is_some());
        assert!(loader
            .normalize_file_path("file:///test.es", "utils/notfound.mes")
            .is_none());
    }

    #[test]
    fn test_gcs() {
        match normalize_path("gcsproject:///hello/world.ts", "../project2/world") {
            Ok(p) => {
                assert_eq!(p.as_str(), "gcsproject:///project2/world")
            }
            Err(e) => {
                panic!("{}", e)
            }
        }
    }
}