use serde_json::Value;
pub const MAX_REWRITE: usize = 16 * 1024 * 1024;
pub fn loopback_url(url: &str, listeners: &[(String, u16)]) -> Option<String> {
let rest = url.get(..8).filter(|s| s.eq_ignore_ascii_case("https://")).map(|_| &url[8..])?;
let end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
let (authority, tail) = rest.split_at(end);
if authority.contains('@') {
return None;
}
let host = match authority.rsplit_once(':') {
Some((h, "443")) => h,
Some(_) => return None,
None => authority,
};
let host = host.strip_suffix('.').unwrap_or(host).to_ascii_lowercase();
let port = listeners.iter().find(|(h, _)| *h == host)?.1;
let tail = if tail.is_empty() || tail.starts_with(['?', '#']) { format!("/{tail}") } else { tail.to_string() };
Some(format!("http://127.0.0.1:{port}{tail}"))
}
pub fn location(value: &[u8], listeners: &[(String, u16)]) -> Option<Vec<u8>> {
loopback_url(std::str::from_utf8(value).ok()?, listeners).map(String::into_bytes)
}
pub fn json_fields(body: &[u8], fields: &[String], listeners: &[(String, u16)]) -> Option<Vec<u8>> {
let mut doc: Value = serde_json::from_slice(body).ok()?;
let mut changed = false;
for f in fields {
let path: Vec<&str> = f.split('.').collect();
changed |= rewrite_at(&mut doc, &path, listeners);
}
changed.then(|| serde_json::to_vec(&doc).expect("a parsed JSON value serializes"))
}
pub fn org_list(body: &[u8], fields: &[String], allowed: &[String], id_of: &dyn Fn(&str) -> Option<String>) -> Option<Vec<u8>> {
let Value::Array(items) = serde_json::from_slice(body).ok()? else { return None };
let mut ids: Option<Vec<String>> = None;
let kept: Vec<Value> = items
.into_iter()
.filter(|item| {
let values: Vec<String> = fields
.iter()
.filter_map(|f| match item.get(f)? {
Value::String(s) => Some(s.clone()),
Value::Number(n) => n.as_u64().map(|n| n.to_string()),
_ => None,
})
.collect();
if values.iter().any(|v| allowed.iter().any(|a| a.eq_ignore_ascii_case(v))) {
return true;
}
let digits: Vec<&String> =
values.iter().filter(|v| !v.is_empty() && v.bytes().all(|b| b.is_ascii_digit())).collect();
if digits.is_empty() {
return false;
}
let ids = ids.get_or_insert_with(|| allowed.iter().filter_map(|s| id_of(s)).collect());
digits.iter().any(|d| ids.contains(d))
})
.collect();
Some(serde_json::to_vec(&Value::Array(kept)).expect("a parsed JSON value serializes"))
}
fn rewrite_at(v: &mut Value, path: &[&str], listeners: &[(String, u16)]) -> bool {
let Some((first, rest)) = path.split_first() else {
let Value::String(s) = v else { return false };
return match loopback_url(s, listeners) {
Some(new) => {
*s = new;
true
}
None => false,
};
};
let (name, each) = match first.strip_suffix("[]") {
Some(n) => (n, true),
None => (*first, false),
};
let Some(child) = v.as_object_mut().and_then(|o| o.get_mut(name)) else { return false };
if !each {
return rewrite_at(child, rest, listeners);
}
let Value::Array(items) = child else { return false };
let mut changed = false;
for item in items {
changed |= rewrite_at(item, rest, listeners);
}
changed
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn listeners() -> Vec<(String, u16)> {
vec![("sentry.io".into(), 29001), ("us.sentry.io".into(), 29002)]
}
#[test]
fn urls() {
let l = listeners();
let cases = [
("https://us.sentry.io/api/0/x/?a=1", Some("http://127.0.0.1:29002/api/0/x/?a=1")),
("HTTPS://US.Sentry.IO:443/p", Some("http://127.0.0.1:29002/p")),
("https://sentry.io.", Some("http://127.0.0.1:29001/")),
("https://sentry.io?q", Some("http://127.0.0.1:29001/?q")),
("https://sentry.io:8443/p", None),
("http://sentry.io/p", None),
("https://evil.example/p", None),
("https://u@sentry.io/p", None),
("/relative", None),
];
for (url, want) in cases {
assert_eq!(loopback_url(url, &l).as_deref(), want, "{url}");
}
assert_eq!(location(b"https://sentry.io/next", &l), Some(b"http://127.0.0.1:29001/next".to_vec()));
assert_eq!(location(b"\xff", &l), None);
}
#[test]
fn chunk_upload_and_regions() {
let l = listeners();
let body = json!({ "url": "https://us.sentry.io/api/0/organizations/acme/chunk-upload/", "chunkSize": 8388608 });
let out = json_fields(&serde_json::to_vec(&body).unwrap(), &["url".into()], &l).unwrap();
let v: Value = serde_json::from_slice(&out).unwrap();
assert_eq!(v["url"], "http://127.0.0.1:29002/api/0/organizations/acme/chunk-upload/");
assert_eq!(v["chunkSize"], 8388608);
let body = json!({ "regions": [
{ "name": "us", "url": "https://us.sentry.io" },
{ "name": "de", "url": "https://de.sentry.io" },
{ "name": "x", "url": 3 },
] });
let out = json_fields(&serde_json::to_vec(&body).unwrap(), &["regions[].url".into()], &l).unwrap();
let v: Value = serde_json::from_slice(&out).unwrap();
assert_eq!(v["regions"][0]["url"], "http://127.0.0.1:29002/");
assert_eq!(v["regions"][1]["url"], "https://de.sentry.io", "no listener: untouched");
assert_eq!(v["regions"][2]["url"], 3);
}
#[test]
fn org_lists_keep_the_fences_orgs() {
let fields = vec!["slug".to_string(), "id".to_string()];
let allowed = vec!["acme".to_string(), "beta".to_string()];
let asked = std::cell::Cell::new(0);
let id_of = |slug: &str| {
asked.set(asked.get() + 1);
(slug == "beta").then(|| "22".to_string())
};
let filter = |v: Value| {
let out = org_list(&serde_json::to_vec(&v).unwrap(), &fields, &allowed, &id_of)?;
Some(serde_json::from_slice::<Value>(&out).unwrap())
};
let list = json!([
{ "slug": "ACME", "id": "11", "name": "Acme" },
{ "slug": "other", "id": "33" },
{ "name": "no fields" },
"not an object",
]);
assert_eq!(filter(list), Some(json!([{ "slug": "ACME", "id": "11", "name": "Acme" }])));
assert_eq!(asked.get(), 2, "`other`'s id resolved the allowed slugs once");
asked.set(0);
let by_id = json!([{ "id": "22" }, { "id": 22 }, { "id": "11" }, { "slug": "x" }, { "slug": "acme" }]);
assert_eq!(filter(by_id), Some(json!([{ "id": "22" }, { "id": 22 }, { "slug": "acme" }])));
assert_eq!(asked.get(), 2, "once per body");
asked.set(0);
assert_eq!(filter(json!([{ "slug": "acme" }, { "slug": "x" }])), Some(json!([{ "slug": "acme" }])));
assert_eq!(asked.get(), 0, "no digits, no lookup");
assert_eq!(filter(json!([])), Some(json!([])));
assert_eq!(filter(json!({ "detail": "x" })), None, "not an array");
assert_eq!(org_list(b"nope", &fields, &allowed, &id_of), None);
let none: Vec<String> = Vec::new();
assert_eq!(org_list(br#"[{"slug":"acme"}]"#, &fields, &none, &id_of).as_deref(), Some(&b"[]"[..]));
}
#[test]
fn untouched_bodies() {
let l = listeners();
let other = serde_json::to_vec(&json!({ "url": "https://evil.example/x" })).unwrap();
assert_eq!(json_fields(&other, &["url".into()], &l), None);
assert_eq!(json_fields(b"not json", &["url".into()], &l), None);
let relative = serde_json::to_vec(&json!({ "url": "/api/0/x/" })).unwrap();
assert_eq!(json_fields(&relative, &["url".into()], &l), None);
let missing = serde_json::to_vec(&json!({ "regions": "nope" })).unwrap();
assert_eq!(json_fields(&missing, &["regions[].url".into(), "a.b".into()], &l), None);
}
}