v0.42.2_fix2

This commit is contained in:
Mateusz Gruszczyński committed 2026-11-26 12:31:28 +01:00
1 parent 15c7f9ff9e
commit 25148f26eb
14 files changed
+407 -54

No files matched your search

+63 -1
View File
@@ -165,6 +165,42 @@ fn parse_file(path: &Path, components: Vec<String>, config: &Config) -> Result<(
if !state.rewrite_engine { state.pending_conditions.clear(); state.skip_next_rule = false; }
}
"rewritebase" => {
ensure!(parts.len() == 2, "{}:{line_no}: RewriteBase expects one absolute URL prefix", path.display());
let base = &parts[1];
ensure!(base.starts_with('/') && !base.starts_with("//")
&& !base.contains('\\') && !base.contains('?') && !base.contains('#')
&& !base.chars().any(char::is_control),
"{}:{line_no}: RewriteBase must be a safe absolute URL path", path.display());
crate::acl::validate_decoded_routing_path(base)
.map_err(|_| anyhow::anyhow!("{}:{line_no}: invalid RewriteBase path", path.display()))?;
scope.rewrite_base = Some(if base.ends_with('/') { base.to_owned() } else { format!("{base}/") });
}
"acceptpathinfo" => {
ensure!(parts.len() == 2, "{}:{line_no}: AcceptPathInfo expects On, Off or Default", path.display());
scope.accept_path_info = if parts[1].eq_ignore_ascii_case("on") {
Some(AcceptPathInfoMode::On)
} else if parts[1].eq_ignore_ascii_case("off") {
Some(AcceptPathInfoMode::Off)
} else if parts[1].eq_ignore_ascii_case("default") {
Some(AcceptPathInfoMode::Default)
} else {
bail!("{}:{line_no}: AcceptPathInfo expects On, Off or Default", path.display());
};
}
"errordocument" => {
ensure!(parts.len() == 3, "{}:{line_no}: ErrorDocument expects an HTTP error status and a local URL", path.display());
let status: u16 = parts[1].parse().context("ErrorDocument status must be numeric")?;
ensure!((400..600).contains(&status), "{}:{line_no}: ErrorDocument status must be 400..599", path.display());
let target = &parts[2];
ensure!(target.len() <= 1024 && target.starts_with('/') && !target.starts_with("//")
&& !target.contains('\\') && !target.contains('#')
&& !target.chars().any(char::is_control),
"{}:{line_no}: ErrorDocument requires a safe local URL, not an external redirect or literal response", path.display());
let path_only = target.split('?').next().unwrap_or_default();
crate::acl::validate_decoded_routing_path(path_only)
.map_err(|_| anyhow::anyhow!("{}:{line_no}: invalid ErrorDocument URL path", path.display()))?;
target.parse::<Uri>().context("invalid ErrorDocument URL")?;
scope.error_documents.insert(status, target.to_owned());
}
"rewritecond" if state.rewrite_engine => {
condition_count += 1;
@@ -269,7 +305,7 @@ fn parse_file(path: &Path, components: Vec<String>, config: &Config) -> Result<(
scope.redirects.push(redirect_rule(&parts, directive == "redirectmatch")
.with_context(|| format!("{}:{line_no}", path.display()))?);
}
"errordocument" | "setenvif" | "fallbackresource" => {
"setenvif" | "fallbackresource" => {
unsupported(&mut state, config, path, line_no, format!("directive {} is not in the RustProxy compatibility subset", parts[0]))?;
}
_ => unsupported(&mut state, config, path, line_no, format!("unsupported directive {}", parts[0]))?,
@@ -335,6 +371,12 @@ pub fn adds_php_extensions(compiled: Option<&Compiled>) -> bool {
compiled.is_some_and(|compiled| compiled.scopes.iter().any(|scope| !scope.php_extensions.is_empty()))
}
pub fn rewrites_to_php(compiled: Option<&Compiled>) -> bool {
compiled.is_some_and(|compiled| compiled.scopes.iter().any(|scope| {
scope.rewrites.iter().any(|rule| rule.replacement.to_ascii_lowercase().contains(".php"))
}))
}
pub fn protected_component(compiled: Option<&Compiled>, value: &str) -> bool {
let lower = value.to_ascii_lowercase();
if lower == ".htaccess" || lower == ".htpasswd" || lower.starts_with(".ht") { return true; }
@@ -379,6 +421,25 @@ pub fn index_files<'a>(compiled: Option<&'a Compiled>, directory_components: &[S
compiled.scopes.iter().filter(|scope| scope_matches(scope, directory_components)).filter_map(|scope| scope.directory_index.as_deref()).next_back().unwrap_or(fallback)
}
pub fn accept_path_info(compiled: Option<&Compiled>, script_path: &str, fallback: bool) -> bool {
match compiled.and_then(|compiled| matching_scopes(compiled, script_path).into_iter()
.filter_map(|scope| scope.accept_path_info).last()) {
Some(AcceptPathInfoMode::On) => true,
Some(AcceptPathInfoMode::Off) => false,
Some(AcceptPathInfoMode::Default) | None => fallback,
}
}
pub fn has_error_documents(compiled: Option<&Compiled>) -> bool {
compiled.is_some_and(|compiled| compiled.scopes.iter().any(|scope| !scope.error_documents.is_empty()))
}
pub fn error_document<'a>(compiled: Option<&'a Compiled>, path: &str, status: StatusCode) -> Option<&'a str> {
compiled?.scopes.iter().filter(|scope| {
path_components(path).ok().is_some_and(|parts| scope_matches(scope, &parts))
}).filter_map(|scope| scope.error_documents.get(&status.as_u16()).map(String::as_str)).last()
}
pub fn directory_listing(compiled: Option<&Compiled>, directory_components: &[String], fallback: bool) -> bool {
let Some(compiled) = compiled else { return fallback; };
compiled.scopes.iter().filter(|scope| scope_matches(scope, directory_components)).filter_map(|scope| scope.directory_listing).next_back().unwrap_or(fallback)
@@ -535,6 +596,7 @@ fn replacement_with_captures(rule: &RewriteRule, captures: &regex::Captures<'_>,
fn scoped_target(scope: &Scope, replacement: &str) -> String {
if replacement.starts_with('/') { return replacement.to_owned(); }
if let Some(base) = &scope.rewrite_base { return format!("{base}{replacement}"); }
if scope.components.is_empty() { format!("/{replacement}") } else if replacement.is_empty() { format!("/{}/", scope.components.join("/")) } else { format!("/{}/{}", scope.components.join("/"), replacement) }
}