Skip to content

Commit d888ed3

Browse files
committed
Make new test use local resources only.
1 parent 16096f1 commit d888ed3

File tree

12 files changed

+121
-15
lines changed

12 files changed

+121
-15
lines changed

components/net/chrome_loader.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

components/net/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern crate webrender_traits;
3939
extern crate websocket;
4040

4141
pub mod about_loader;
42+
pub mod chrome_loader;
4243
pub mod cookie;
4344
pub mod cookie_storage;
4445
pub mod data_loader;

components/net/resource_thread.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! A thread that takes a URL and streams back the binary data.
66
77
use about_loader;
8+
use chrome_loader;
89
use cookie;
910
use cookie_storage::CookieStorage;
1011
use data_loader;
@@ -332,6 +333,7 @@ impl ResourceManager {
332333

333334
let cancel_listener = CancellationListener::new(cancel_resource);
334335
let loader = match &*load_data.url.scheme {
336+
"chrome" => from_factory(chrome_loader::factory),
335337
"file" => from_factory(file_loader::factory),
336338
"http" | "https" | "view-source" => {
337339
let http_state = HttpState {

resources/badcert.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<title>Certificate error</title>
44
</head>
55
<body>
6-
<img src="badcert.jpg">
6+
<img src="chrome://badcert.jpg">
77
</body>
88
</html>

tests/unit/net/chrome_loader.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 net::chrome_loader::resolve_chrome_url;
6+
use url::Url;
7+
8+
#[test]
9+
fn test_relative() {
10+
let url = Url::parse("chrome://../something").unwrap();
11+
assert!(resolve_chrome_url(&url).is_err());
12+
}
13+
14+
#[test]
15+
fn test_relative_2() {
16+
let url = Url::parse("chrome://subdir/../something").unwrap();
17+
assert!(resolve_chrome_url(&url).is_err());
18+
}
19+
20+
#[test]
21+
#[cfg(not(target_os = "windows"))]
22+
fn test_absolute() {
23+
let url = Url::parse("chrome:///etc/passwd").unwrap();
24+
assert!(resolve_chrome_url(&url).is_err());
25+
}
26+
27+
#[test]
28+
#[cfg(target_os = "windows")]
29+
fn test_absolute_2() {
30+
let url = Url::parse("chrome://C:\\Windows").unwrap();
31+
assert!(resolve_chrome_url(&url).is_err());
32+
}
33+
34+
#[test]
35+
#[cfg(target_os = "windows")]
36+
fn test_absolute_3() {
37+
let url = Url::parse("chrome://\\\\server/C$").unwrap();
38+
assert!(resolve_chrome_url(&url).is_err());
39+
}
40+
41+
#[test]
42+
fn test_valid() {
43+
let url = Url::parse("chrome://badcert.jpg").unwrap();
44+
let resolved = resolve_chrome_url(&url).unwrap();
45+
assert_eq!(resolved.scheme, "file");
46+
}

tests/unit/net/http_loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use net::hsts::HstsEntry;
2323
use net::http_loader::LoadErrorType;
2424
use net::http_loader::{load, LoadError, HttpRequestFactory, HttpRequest, HttpResponse, UIProvider, HttpState};
2525
use net::resource_thread::{AuthCacheEntry, CancellationListener};
26-
use net_traits::{LoadData, CookieSource, LoadContext, NetworkError, IncludeSubdomains};
26+
use net_traits::{LoadData, CookieSource, LoadContext, IncludeSubdomains};
2727
use std::borrow::Cow;
2828
use std::io::{self, Write, Read, Cursor};
2929
use std::sync::mpsc::Receiver;

tests/unit/net/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern crate unicase;
1818
extern crate url;
1919
extern crate util;
2020

21+
#[cfg(test)] mod chrome_loader;
2122
#[cfg(test)] mod cookie;
2223
#[cfg(test)] mod data_loader;
2324
#[cfg(test)] mod file_loader;

tests/wpt/mozilla/tests/mozilla/bad_cert_detected.html

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,14 @@
1010
t.step(function() {
1111
var target = location.href.replace(HTTP_ORIGIN, HTTPS_ORIGIN)
1212
.replace('bad_cert_detected.html',
13-
'resources/origin_helpers.js');
14-
// Servo currently lacks the ability to introspect any content that is blocked
15-
// due to a cert error, so we use a roundabout method to infer that that's happened.
16-
// When the worker has a cert failure, that translates into attempting to evaluate the
17-
// contents of badcert.html as JS, which triggers an exception that currently does not
18-
// propagate to the parent scope. If we _do_ get an error event in the parent scope,
19-
// that means that the cert verification was treated no different than any other
20-
// network error, since we dispatch an error event in that case.
13+
'resources/worker_success.js');
2114
var w = new Worker(target);
22-
w.addEventListener('error', t.unreached_func("cert not detected as invalid"), false);
23-
// We infer that we detected an invalid cert if nothing happens for a few seconds.
24-
setTimeout(function() { t.done() }, 3000);
15+
// If the script executes successfully, it should send a message. That indicates that
16+
// there was no validation failure, which is bad.
17+
w.addEventListener('message', t.unreached_func("cert not detected as invalid"), true);
18+
// When the worker has a cert failure, that translates into an early error that is reported
19+
// to the Worker object.
20+
w.addEventListener('error', t.step_func_done(), true);
2521
});
2622
</script>
2723
</body>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var HTTP_PORT = '{{ports[http][0]}}';
22
var HTTPS_PORT = '{{ports[https][0]}}';
3-
var ORIGINAL_HOST = '\'{{host}}\'';
3+
var ORIGINAL_HOST = '{{host}}';
44
var HTTP_ORIGIN = 'http://' + ORIGINAL_HOST + ':' + HTTP_PORT;
55
var HTTPS_ORIGIN = 'https://' + ORIGINAL_HOST + ':' + HTTPS_PORT;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
this should be a secure connection
4+
</body>
5+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
postMessage('load succeeded');

tests/wpt/mozilla/tests/mozilla/sslfail.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
<meta charset=utf-8>
44
<title>SSL Failure</title>
55
<link rel=match href=sslfail-ref.html>
6+
<script src="resources/origin_helpers.js?pipe=sub"></script>
67
</head>
78
<body>
8-
<iframe src="https://somesite.org/blah"></iframe>
9+
<script>
10+
var iframe = document.createElement('iframe');
11+
document.body.appendChild(iframe);
12+
iframe.src = location.href
13+
.replace(HTTP_ORIGIN, HTTPS_ORIGIN)
14+
.replace('sslfail.html', 'resources/ssl.https.html');
15+
</script>
916
</body>
1017
</html>

0 commit comments

Comments
 (0)