|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +use file_loader; |
| 6 | +use mime_classifier::MIMEClassifier; |
| 7 | +use net_traits::{LoadConsumer, LoadData, NetworkError}; |
| 8 | +use resource_thread::{CancellationListener, send_error}; |
| 9 | +use std::path::Path; |
| 10 | +use std::sync::Arc; |
| 11 | +use url::Url; |
| 12 | +use util::resource_files::resources_dir_path; |
| 13 | + |
| 14 | +pub fn resolve_chrome_url(url: &Url) -> Result<Url, ()> { |
| 15 | + assert_eq!(url.scheme, "chrome"); |
| 16 | + // Skip the initial // |
| 17 | + let non_relative_scheme_data = &url.non_relative_scheme_data().unwrap()[2..]; |
| 18 | + let relative_path = Path::new(non_relative_scheme_data); |
| 19 | + // Don't allow chrome URLs access to files outside of the resources directory. |
| 20 | + if non_relative_scheme_data.find("..").is_some() || |
| 21 | + relative_path.is_absolute() || |
| 22 | + relative_path.has_root() { |
| 23 | + return Err(()); |
| 24 | + } |
| 25 | + |
| 26 | + let mut path = resources_dir_path(); |
| 27 | + path.push(non_relative_scheme_data); |
| 28 | + assert!(path.exists()); |
| 29 | + return Ok(Url::from_file_path(&*path).unwrap()); |
| 30 | +} |
| 31 | + |
| 32 | +pub fn factory(mut load_data: LoadData, |
| 33 | + start_chan: LoadConsumer, |
| 34 | + classifier: Arc<MIMEClassifier>, |
| 35 | + cancel_listener: CancellationListener) { |
| 36 | + let file_url = match resolve_chrome_url(&load_data.url) { |
| 37 | + Ok(url) => url, |
| 38 | + Err(_) => { |
| 39 | + send_error(load_data.url, |
| 40 | + NetworkError::Internal("Invalid chrome URL.".to_owned()), |
| 41 | + start_chan); |
| 42 | + return; |
| 43 | + } |
| 44 | + }; |
| 45 | + load_data.url = file_url; |
| 46 | + file_loader::factory(load_data, start_chan, classifier, cancel_listener) |
| 47 | +} |
0 commit comments