fix(supabase): security pass on the files foundation — ceiling counts live bytes only, confirm checks the account, null sha256 refused, browser RPCs one role; worker: cleartext only on loopback or a service name, no env proxy, key ownership cross-check, no library text in logs

Security audit (PASS, 2 MEDIUM + LOW): the global ceiling summed every
non-deleted row, so rejected uploads ratcheted it toward storage_full with no
disk used → in-flight rows by declared size, clean rows by byte_size, nothing
else (pgTAP: rejected rows do not count, in-flight rows do); confirm refuses a
deactivated account like begin; complete_avatar_job refuses a null sha256;
begin / confirm / remove granted to authenticated only. Worker: SUPABASE_URL
over http:// accepted only for loopback or a single-label Docker service
name; ureq proxy(None); the job's keys must embed its owner and asset ids;
backend_failure / claim_failed / sweep_failure lines carry the closed kind and
the HTTP status, never the library's error text. e2e: uuid shape checked
before the psql query. Contract revision 2 (oracle fixes).
This commit is contained in:
bchanot committed 2026-10-13 19:49:58 +02:00
1 parent 62eb4e5d82
commit cd2dbb3b79
11 files changed
+124 -31

No files matched your search

+41 -8
View File
@@ -117,21 +117,29 @@ impl Config {
}
/// `http(s)://host[:port]`, no path, no whitespace; a trailing slash is dropped.
/// Cleartext `http://` is accepted only where the key cannot leave the machine:
/// loopback, or a single-label host (a Docker service name on the compose network).
fn parse_url(raw: String) -> Result<String, ConfigError> {
let url = raw.trim().trim_end_matches('/').to_owned();
let rest = url
.strip_prefix("http://")
.or_else(|| url.strip_prefix("https://"))
.ok_or(ConfigError::Invalid(URL))?;
let (secure, rest) = match (url.strip_prefix("https://"), url.strip_prefix("http://")) {
(Some(rest), _) => (true, rest),
(None, Some(rest)) => (false, rest),
(None, None) => return Err(ConfigError::Invalid(URL)),
};
let host_ok = !rest.is_empty()
&& rest
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':' | '_'));
if host_ok {
Ok(url)
} else {
Err(ConfigError::Invalid(URL))
if !host_ok || (!secure && !cleartext_allowed(rest)) {
return Err(ConfigError::Invalid(URL));
}
Ok(url)
}
/// Loopback addresses and single-label names (no dot) may use plain HTTP.
fn cleartext_allowed(host_port: &str) -> bool {
let host = host_port.rsplit_once(':').map_or(host_port, |(h, _)| h);
host == "localhost" || host.starts_with("127.") || host == "[::1]" || !host.contains('.')
}
fn seconds(
@@ -201,6 +209,31 @@ mod tests {
move |name| map.get(name).cloned()
}
#[test]
fn cleartext_http_is_limited_to_loopback_and_service_names() {
for ok in [
"http://127.0.0.1:54321",
"http://localhost:54321",
"http://supabase_kong_faunosteo:8000",
"http://kong",
"https://abc.supabase.co",
] {
assert!(parse_url(ok.to_owned()).is_ok(), "{ok}");
}
for bad in [
"http://abc.supabase.co",
"http://10.0.0.5:8000",
"ftp://x",
"http://",
] {
assert_eq!(
parse_url(bad.to_owned()).err(),
Some(ConfigError::Invalid(URL)),
"{bad}"
);
}
}
#[test]
fn defaults_apply_when_only_the_required_variables_are_set() {
let cfg = Config::from_vars(vars(&[
+2
View File
@@ -29,6 +29,8 @@ impl HttpBackend {
.timeout_global(Some(Duration::from_secs(30)))
.timeout_connect(Some(Duration::from_secs(5)))
.max_redirects(0)
// Never route the service key through a proxy picked up from the environment.
.proxy(None)
.http_status_as_error(false)
.user_agent("faunosteo-worker")
.build()
+7 -1
View File
@@ -89,9 +89,13 @@ fn validated_keys(job: &Job) -> Option<&str> {
.bucket_id
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-');
// Shape, then ownership: both keys must embed this job's owner and asset ids, so a
// mismatched row can never make the worker write or read another account's key.
if !bucket_ok
|| keys::validate_object_key(&job.object_key).is_err()
|| keys::validate_quarantine_key(quarantine_key).is_err()
|| !keys::belongs_to(&job.object_key, &job.owner_id, &job.asset_id)
|| !keys::belongs_to(quarantine_key, &job.owner_id, &job.asset_id)
{
log::error("job_invalid_keys").asset(&job.asset_id).emit();
return None;
@@ -248,9 +252,11 @@ fn transport_failure(stage: &str, job: &Job, error: &BackendError) -> JobEnd {
.asset(&job.asset_id)
.stage(stage)
.code(error.kind());
// The library's error text (URI fragments, server bodies) stays out of the logs:
// the closed kind and the HTTP status are the record.
match error {
BackendError::Status(status) => line.http_status(*status).emit(),
other => line.detail(&other.to_string()).emit(),
_ => line.emit(),
}
JobEnd::LeftForLease
}
+11
View File
@@ -79,6 +79,13 @@ pub fn validate_any_key(key: &str) -> Result<(), KeyError> {
}
}
/// True when a (valid) key's owner and asset segments are exactly these ids.
pub fn belongs_to(key: &str, owner_id: &str, asset_id: &str) -> bool {
let mut segments = key.split('/');
segments.next();
segments.next() == Some(owner_id) && segments.next() == Some(asset_id)
}
/// The asset id embedded in a valid quarantine key.
pub fn asset_id_of_quarantine_key(key: &str) -> Option<&str> {
validate_quarantine_key(key).ok()?;
@@ -140,6 +147,10 @@ mod tests {
assert_eq!(validate_any_key(key).err(), Some(*expected), "{key}");
}
assert_eq!(asset_id_of_quarantine_key("quarantine/a/b/upload"), None);
let key = format!("profiles/{U1}/{U2}/avatar.webp");
assert!(belongs_to(&key, U1, U2));
assert!(!belongs_to(&key, U2, U1));
assert!(!belongs_to(&key, U1, U1));
assert!(!is_uuid("10000000-0000-0000-0000-00000000000"));
}
}
+5 -4
View File
@@ -99,10 +99,11 @@ fn claim_pass(
match backend.claim_jobs(1) {
Ok(jobs) => jobs,
Err(error) => {
log::warn("claim_failed")
.code(error.kind())
.detail(&error.to_string())
.emit();
let line = log::warn("claim_failed").code(error.kind());
match error {
crate::backend::BackendError::Status(status) => line.http_status(status).emit(),
_ => line.emit(),
}
return Ok(0);
}
}
+1 -1
View File
@@ -106,6 +106,6 @@ fn failure(call: &str, error: &BackendError, report: &mut SweepReport) {
let line = log::warn("sweep_failure").stage(call).code(error.kind());
match error {
BackendError::Status(status) => line.http_status(*status).emit(),
other => line.detail(&other.to_string()).emit(),
_ => line.emit(),
}
}