v0.42.2_fix1
This commit is contained in:
1 parent
f38a2b9e5e
commit
15c7f9ff9e
24 files changed
+464
-77
No files matched your search
@@ -168,7 +168,7 @@ impl LegacyAccess {
|
||||
#[derive(Debug, Clone)]
|
||||
struct SimpleRedirect {
|
||||
matcher: RedirectMatcher,
|
||||
replacement: String,
|
||||
replacement: Option<String>,
|
||||
status: StatusCode,
|
||||
}
|
||||
|
||||
@@ -233,6 +233,7 @@ pub enum RewriteOutcome {
|
||||
Internal { path: String, query: Option<String> },
|
||||
Redirect(Response),
|
||||
Forbidden,
|
||||
Status(StatusCode),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
||||
@@ -5,27 +5,33 @@ fn redirect_status(value: &str) -> Option<StatusCode> {
|
||||
"seeother" | "303" => Some(StatusCode::SEE_OTHER),
|
||||
"307" => Some(StatusCode::TEMPORARY_REDIRECT),
|
||||
"308" => Some(StatusCode::PERMANENT_REDIRECT),
|
||||
"404" => Some(StatusCode::NOT_FOUND),
|
||||
"gone" | "410" => Some(StatusCode::GONE),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn redirect_rule(parts: &[String], regex: bool) -> Result<SimpleRedirect> {
|
||||
let (status, source_index) = if parts.len() == 4 {
|
||||
(redirect_status(&parts[1]).context("unsupported Redirect status")?, 2)
|
||||
} else {
|
||||
ensure!(parts.len() == 3, "Redirect / RedirectMatch expects [status] source target");
|
||||
(StatusCode::FOUND, 1)
|
||||
};
|
||||
let explicit = parts.get(1).and_then(|value| redirect_status(value));
|
||||
let (status, source_index) = explicit.map_or((StatusCode::FOUND, 1), |status| (status, 2));
|
||||
let status_only = matches!(status, StatusCode::NOT_FOUND | StatusCode::GONE);
|
||||
ensure!(parts.len() == source_index + if status_only { 1 } else { 2 },
|
||||
"Redirect / RedirectMatch expects [status] source target, or 404/gone source without target");
|
||||
let source = &parts[source_index];
|
||||
let replacement = &parts[source_index + 1];
|
||||
ensure!(!source.is_empty() && source.len() <= 512 && replacement.len() <= 1024,
|
||||
"Redirect source/target is invalid");
|
||||
ensure!((replacement.starts_with('/') && !replacement.starts_with("//"))
|
||||
|| replacement.starts_with("http://") || replacement.starts_with("https://"),
|
||||
"Redirect target must be a local absolute path or an explicit http(s) URL");
|
||||
ensure!(!replacement.contains('\\') && !replacement.contains('\r') && !replacement.contains('\n'),
|
||||
"Redirect target contains unsafe characters");
|
||||
HeaderValue::from_str(replacement).context("invalid Redirect target")?;
|
||||
ensure!(!source.is_empty() && source.len() <= 512, "Redirect source is invalid");
|
||||
let replacement = if status_only {
|
||||
None
|
||||
} else {
|
||||
let replacement = &parts[source_index + 1];
|
||||
ensure!(replacement.len() <= 1024, "Redirect target is too long");
|
||||
ensure!((replacement.starts_with('/') && !replacement.starts_with("//"))
|
||||
|| replacement.starts_with("http://") || replacement.starts_with("https://"),
|
||||
"Redirect target must be a local absolute path or an explicit http(s) URL");
|
||||
ensure!(!replacement.contains('\\') && !replacement.contains('\r') && !replacement.contains('\n'),
|
||||
"Redirect target contains unsafe characters");
|
||||
HeaderValue::from_str(replacement).context("invalid Redirect target")?;
|
||||
Some(replacement.to_owned())
|
||||
};
|
||||
let matcher = if regex {
|
||||
RedirectMatcher::Regex(Regex::new(source).context("invalid RedirectMatch regex")?)
|
||||
} else {
|
||||
@@ -34,7 +40,7 @@ fn redirect_rule(parts: &[String], regex: bool) -> Result<SimpleRedirect> {
|
||||
&& !source.contains('#') && !source.contains('\\'), "invalid Redirect path prefix");
|
||||
RedirectMatcher::Prefix(source.to_owned())
|
||||
};
|
||||
Ok(SimpleRedirect { matcher, replacement: replacement.to_owned(), status })
|
||||
Ok(SimpleRedirect { matcher, replacement, status })
|
||||
}
|
||||
|
||||
// Only a safe, deterministic subset of Apache access rules is allowed in
|
||||
|
||||
@@ -33,15 +33,20 @@ pub async fn apply_rewrites(
|
||||
}
|
||||
RedirectMatcher::Regex(_) => None,
|
||||
};
|
||||
let target = match &rule.matcher {
|
||||
RedirectMatcher::Prefix(_) => tail.map(|tail| format!("{}{}", rule.replacement, tail)),
|
||||
RedirectMatcher::Regex(regex) => regex.captures(&path).map(|captures| {
|
||||
let target = match (&rule.matcher, rule.replacement.as_deref()) {
|
||||
(RedirectMatcher::Prefix(_), None) => tail.map(|_| String::new()),
|
||||
(RedirectMatcher::Regex(regex), None) => regex.is_match(&path).then(String::new),
|
||||
(RedirectMatcher::Prefix(_), Some(base)) => tail.map(|tail| format!("{base}{tail}")),
|
||||
(RedirectMatcher::Regex(regex), Some(base)) => regex.captures(&path).map(|captures| {
|
||||
let mut target = String::new();
|
||||
captures.expand(&rule.replacement, &mut target);
|
||||
captures.expand(base, &mut target);
|
||||
target
|
||||
}),
|
||||
};
|
||||
if let Some(mut target) = target {
|
||||
if rule.replacement.is_none() {
|
||||
return Ok(RewriteOutcome::Status(rule.status));
|
||||
}
|
||||
ensure!((target.starts_with('/') && !target.starts_with("//"))
|
||||
|| target.starts_with("http://") || target.starts_with("https://"),
|
||||
"unsafe .htaccess Redirect expansion");
|
||||
|
||||
@@ -109,6 +109,9 @@ async fn serve_inner(
|
||||
Ok(htaccess::RewriteOutcome::Forbidden) => {
|
||||
return builtin_error_response(StatusCode::FORBIDDEN, &method)
|
||||
}
|
||||
Ok(htaccess::RewriteOutcome::Status(status)) => {
|
||||
return builtin_error_response(status, &method)
|
||||
}
|
||||
Ok(htaccess::RewriteOutcome::Internal { path, query }) => (path, query),
|
||||
Err(_) => {
|
||||
return status_response(
|
||||
|
||||
@@ -2685,3 +2685,65 @@ ErrorDocument 404 /error.html
|
||||
assert_eq!(regex.status(), StatusCode::FOUND);
|
||||
assert_eq!(regex.headers()[LOCATION], "/archive/file.txt");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn htaccess_status_only_redirects_do_not_require_location_headers() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
std::fs::write(tmp.path().join(".htaccess"), r#"
|
||||
RedirectMatch 404 ^/missing/
|
||||
Redirect gone /removed
|
||||
Redirect 302 /old /new
|
||||
"#).unwrap();
|
||||
let config = Service {
|
||||
id: "status-only".into(),
|
||||
name: "Status only".into(),
|
||||
document_root: tmp.path().to_path_buf(),
|
||||
htaccess: htaccess::Config { enabled: true, ..Default::default() },
|
||||
..Default::default()
|
||||
};
|
||||
let service = config.compile().unwrap();
|
||||
let not_found = serve_request(&service, &request(Method::GET, "/missing/secret"), "/missing/secret").await;
|
||||
assert_eq!(not_found.status(), StatusCode::NOT_FOUND);
|
||||
assert!(!not_found.headers().contains_key(LOCATION));
|
||||
let gone = serve_request(&service, &request(Method::GET, "/removed/file"), "/removed/file").await;
|
||||
assert_eq!(gone.status(), StatusCode::GONE);
|
||||
assert!(!gone.headers().contains_key(LOCATION));
|
||||
let redirect = serve_request(&service, &request(Method::GET, "/old"), "/old").await;
|
||||
assert_eq!(redirect.status(), StatusCode::FOUND);
|
||||
assert_eq!(redirect.headers()[LOCATION], "/new");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn htaccess_popular_php_front_controller_file_compiles_without_strict_compatibility() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
std::fs::write(tmp.path().join(".htaccess"), r#"
|
||||
# Typical monitoring/PHP installation, without application-specific rules in RustProxy.
|
||||
Options FollowSymlinks Multiviews
|
||||
ErrorDocument 404 /error.php?404
|
||||
RedirectMatch 404 /\.
|
||||
RewriteEngine On
|
||||
RewriteBase /
|
||||
RewriteRule ^\..*$ - [F,L]
|
||||
RewriteCond %{REQUEST_FILENAME} !^favicon\.ico
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteCond %{REQUEST_URI} !\.(js|ico|txt|gif|jpg|png|css|php)
|
||||
RewriteCond %{REQUEST_URI} !^api/
|
||||
RewriteCond %{REQUEST_URI} !^rrd/
|
||||
RewriteCond %{REQUEST_URI} !=/server-status
|
||||
RewriteRule ^(.*)$ index.php/$1/
|
||||
AcceptPathInfo On
|
||||
"#).unwrap();
|
||||
let config = Service {
|
||||
id: "monitoring-app".into(),
|
||||
name: "Monitoring app".into(),
|
||||
document_root: tmp.path().to_path_buf(),
|
||||
htaccess: htaccess::Config { enabled: true, strict: false, ..Default::default() },
|
||||
..Default::default()
|
||||
};
|
||||
let service = config.compile().unwrap();
|
||||
let (_, warnings) = service.htaccess_diagnostics().unwrap();
|
||||
assert!(warnings.iter().any(|warning| warning.contains("ErrorDocument")));
|
||||
assert!(warnings.iter().any(|warning| warning.contains("AcceptPathInfo")));
|
||||
assert!(!warnings.iter().any(|warning| warning.contains("RedirectMatch")));
|
||||
}
|
||||
Reference in new issue
Block a user