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"))
}
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 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);
}
}