| 1 | use crate::*; |
| 2 | use rusqlite::OptionalExtension; |
| 3 | use std::collections::HashSet; |
| 4 | use axum::body::Body; |
| 5 | use lol_html::{RewriteStrSettings, element, html_content::ContentType, text}; |
| 6 | |
| 7 | struct Profile { |
| 8 | name: String, |
| 9 | url: Option<String>, |
| 10 | icon: &'static str, |
| 11 | picture: Option<String>, |
| 12 | owner: Option<(String, Option<String>)>, |
| 13 | } |
| 14 | |
| 15 | fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { |
| 16 | let db = auth.db.lock().unwrap(); |
| 17 | let mut query = db.prepare("SELECT json_extract(profile,'$.username'),coalesce(json_extract(profile,'$.firstName'),''),provider,subject,CASE WHEN provider='atproto' THEN CASE WHEN json_type(profile,'$.attributes.picture[0]')='text' THEN '' END ELSE json_extract(profile,'$.attributes.picture[0]') END,json_extract(profile,'$.attributes.profileUrl[0]') FROM users JOIN external_identities ON user_id=users.id WHERE json_extract(profile,'$.kind')='guest'")?; |
| 18 | let rows = query.query_map([], |row| { |
| 19 | Ok(( |
| 20 | row.get::<_, String>(0)?, |
| 21 | row.get::<_, String>(1)?, |
| 22 | row.get::<_, String>(2)?, |
| 23 | row.get::<_, String>(3)?, |
| 24 | row.get::<_, Option<String>>(4)?, |
| 25 | row.get::<_, Option<String>>(5)?, |
| 26 | )) |
| 27 | })?; |
| 28 | let mut profiles = HashMap::new(); |
| 29 | for row in rows { |
| 30 | let (username, name, provider, subject, picture, profile_url) = row?; |
| 31 | if name.is_empty() { continue; } |
| 32 | let (url, icon) = match provider.as_str() { |
| 33 | "github" => { |
| 34 | if matches!(name.as_str(), "." | "..") { continue; } |
| 35 | let mut url = url::Url::parse("https://github.com/")?; |
| 36 | url.path_segments_mut().unwrap().pop_if_empty().push(&name); |
| 37 | (Some(String::from(url)), include_str!("../web/sso/github.svg")) |
| 38 | } |
| 39 | "astheno" => (None, include_str!("astheno.svg")), |
| 40 | "atproto" => (profile_url, include_str!("../web/sso/atproto.svg")), |
| 41 | _ => continue, |
| 42 | }; |
| 43 | profiles.insert( |
| 44 | username, |
| 45 | Profile { |
| 46 | name, |
| 47 | url, |
| 48 | icon, |
| 49 | owner: None, |
| 50 | picture: if provider == "github" { |
| 51 | Some(format!( |
| 52 | "https://avatars.githubusercontent.com/u/{subject}?s=64" |
| 53 | )) |
| 54 | } else if provider == "atproto" { |
| 55 | picture.map(|_| format!("{}auth/guest/avatar/atproto?subject={}", auth.origin, encoded(&subject))) |
| 56 | } else { |
| 57 | picture |
| 58 | }, |
| 59 | }, |
| 60 | ); |
| 61 | } |
| 62 | let config: Option<String> = db.query_row("SELECT config FROM oidc_clients WHERE id='shale'", [], |r| r.get(0)).optional()?; |
| 63 | let config: Value = config.map(|config| serde_json::from_str(&config)).transpose()?.unwrap_or(Value::Null); |
| 64 | let mut query = db.prepare("SELECT u.username,json_extract(u.profile,'$.attributes.picture[0]'),i.identity FROM users u JOIN ( |
| 65 | SELECT id AS user_id,'email:'||lower(json_extract(profile,'$.email')) AS identity FROM users WHERE json_extract(profile,'$.emailVerified')=1 AND json_extract(profile,'$.email') IS NOT NULL |
| 66 | UNION SELECT user_id,'email:'||email FROM commit_emails WHERE verified=1 |
| 67 | UNION SELECT user_id,'key:'||fingerprint FROM signing_keys WHERE verified=1 |
| 68 | ) i ON i.user_id=u.id WHERE json_extract(u.profile,'$.enabled')=1 AND coalesce(json_extract(u.profile,'$.kind'),'user') NOT IN ('ai','guest')")?; |
| 69 | for row in query.query_map([], |r| Ok((r.get::<_,String>(0)?,r.get::<_,Option<String>>(1)?,r.get::<_,String>(2)?)))? { |
| 70 | let (username,picture,identity) = row?; |
| 71 | let name = config["usernameAliases"][&username].as_str().unwrap_or(&username); |
| 72 | let mut url = url::Url::parse("https://shale.invalid/")?; |
| 73 | url.path_segments_mut().unwrap().pop_if_empty().push(&format!("~{name}")); |
| 74 | profiles.insert(identity, Profile { name: format!("~{name}"),url: Some(url.path().into()),icon:"",picture,owner:None }); |
| 75 | } |
| 76 | Ok(profiles) |
| 77 | } |
| 78 | |
| 79 | pub(crate) fn assisted_model(body: &str) -> Option<&str> { |
| 80 | let line = body.trim_end().lines().last()?; |
| 81 | let model = line.strip_prefix("Assisted-by: ")?; |
| 82 | if model.is_empty() || model.len() > 128 || !model.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_')) { return None; } |
| 83 | let before = &body[..body.trim_end().len() - line.len()]; |
| 84 | let mut fence = None; |
| 85 | for line in before.lines() { |
| 86 | let line = line.trim_start(); |
| 87 | if line.starts_with("```") || line.starts_with("~~~") { |
| 88 | let marker = line.as_bytes()[0]; |
| 89 | let count = line.bytes().take_while(|&b| b == marker).count(); |
| 90 | match fence { |
| 91 | None => fence = Some((marker, count)), |
| 92 | Some((open, length)) if marker == open && count >= length && line[count..].trim().is_empty() => fence = None, |
| 93 | _ => (), |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | fence.is_none().then_some(model) |
| 98 | } |
| 99 | |
| 100 | fn ai_profile(model: Option<&str>) -> Profile { |
| 101 | let (name, icon) = match model { |
| 102 | None => ("ai".into(), include_str!("bot.svg")), |
| 103 | Some(id) => { |
| 104 | let normalize_version = id.starts_with("claude-") || id.starts_with("gpt-"); |
| 105 | let (id, icon, gpt) = if let Some(rest) = id.strip_prefix("claude-") { |
| 106 | let rest = rest.rsplit_once('-').filter(|(_, tail)| tail.len() == 8 && tail.bytes().all(|b| b.is_ascii_digit())).map(|(rest, _)| rest).unwrap_or(rest); |
| 107 | (rest, include_str!("claude.svg"), false) |
| 108 | } else if let Some(rest) = id.strip_prefix("gpt-") { |
| 109 | (rest, include_str!("openai.svg"), true) |
| 110 | } else if id.starts_with("qwen") { |
| 111 | (id, include_str!("qwen.svg"), false) |
| 112 | } else { |
| 113 | (id, include_str!("bot.svg"), false) |
| 114 | }; |
| 115 | let mut words: Vec<String> = Vec::new(); |
| 116 | for part in id.split('-').filter(|part| !part.is_empty()) { |
| 117 | let number = part.find(|c: char| c.is_ascii_digit()).unwrap_or(part.len()); |
| 118 | let (word, version) = part.split_at(number); |
| 119 | if !word.is_empty() { |
| 120 | let mut letters = word.chars(); |
| 121 | words.push(letters.next().unwrap().to_uppercase().collect::<String>() + letters.as_str()); |
| 122 | } |
| 123 | if !version.is_empty() { |
| 124 | let numeric = |value: &str| value.bytes().all(|b| b.is_ascii_digit() || b == b'.'); |
| 125 | if normalize_version && numeric(version) && words.last().is_some_and(|word| numeric(word)) { |
| 126 | let previous = words.last_mut().unwrap(); |
| 127 | previous.push('.'); |
| 128 | previous.push_str(version); |
| 129 | } else { |
| 130 | words.push(version.into()); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | let name = words.join(" "); |
| 135 | (if gpt { format!("GPT-{name}") } else { name }, icon) |
| 136 | } |
| 137 | }; |
| 138 | Profile { name, url: None, icon, picture: None, owner: None } |
| 139 | } |
| 140 | |
| 141 | fn account_path(path: &str) -> Option<&str> { |
| 142 | let name = path.strip_prefix("/~")?; |
| 143 | let name = name.strip_suffix('/').unwrap_or(name); |
| 144 | (!name.is_empty() && !name.contains('/')).then_some(name) |
| 145 | } |
| 146 | |
| 147 | fn avatar(picture: &str) -> String { |
| 148 | let picture = picture.replace('&', "&amp;").replace('"', "&quot;").replace('<', "&lt;"); |
| 149 | format!("<img src=\"{picture}\" alt=\"\" height=\"16\" referrerpolicy=\"no-referrer\">") |
| 150 | } |
| 151 | |
| 152 | fn rewrite(html: &str, origin: &url::Url, profiles: &HashMap<String, Profile>, avatars: &HashMap<String, String>, open_count: Option<u64>) -> Result<String> { |
| 153 | use std::{cell::Cell, rc::Rc}; |
| 154 | let markdown = Rc::new(Cell::new(0)); |
| 155 | let bodies = markdown.clone(); |
| 156 | let active = Rc::new(Cell::new(None::<bool>)); |
| 157 | let images = active.clone(); |
| 158 | let labels = active.clone(); |
| 159 | let compact = Rc::new(Cell::new(false)); |
| 160 | let author_column = compact.clone(); |
| 161 | let commit_author = Rc::new(std::cell::RefCell::new(None::<(String,Option<String>)>)); |
| 162 | let commit_images = commit_author.clone(); |
| 163 | let commit_names = commit_author.clone(); |
| 164 | let signature = Rc::new(std::cell::RefCell::new(String::new())); |
| 165 | Ok(lol_html::rewrite_str(html, RewriteStrSettings::new() |
| 166 | .append_element_content_handler(text!("header .usa-nav__primary a[href]", |text| { |
| 167 | if markdown.get() == 0 && let Some(count) = open_count && let Some(start) = text.as_str().find("issues (") |
| 168 | && let Some(end) = text.as_str()[start..].find(')') |
| 169 | { |
| 170 | text.replace(&format!("{}issues ({count}){}", &text.as_str()[..start], &text.as_str()[start + end + 1..]), ContentType::Text); |
| 171 | } |
| 172 | Ok(()) |
| 173 | })) |
| 174 | .append_element_content_handler(element!("#m-metainfo td", |element| { |
| 175 | *commit_author.borrow_mut() = None; |
| 176 | let closing = commit_author.clone(); |
| 177 | element.on_end_tag(lol_html::end_tag!(move |_| { *closing.borrow_mut() = None; Ok(()) }))?; |
| 178 | Ok(()) |
| 179 | })) |
| 180 | .append_element_content_handler(element!("#page-log table tbody td[title]", |element| { |
| 181 | if markdown.get() != 0 { return Ok(()); } |
| 182 | let Some(profile) = element.get_attribute("title").and_then(|email| profiles.get(&format!("email:{}", email.trim().to_lowercase()))) else { return Ok(()); }; |
| 183 | let Some(url) = &profile.url else { return Ok(()); }; |
| 184 | *commit_author.borrow_mut() = Some((url.clone(),profile.picture.clone())); |
| 185 | let closing = commit_author.clone(); |
| 186 | element.on_end_tag(lol_html::end_tag!(move |_| { *closing.borrow_mut() = None; Ok(()) }))?; |
| 187 | element.prepend(&format!("<a class=\"usa-link\" href=\"{url}\">"),ContentType::Html); |
| 188 | element.append("</a>",ContentType::Html); |
| 189 | Ok(()) |
| 190 | })) |
| 191 | .append_element_content_handler(element!("#page-log td[title] img, #m-metainfo td img[alt]", |element| { |
| 192 | if markdown.get() != 0 { return Ok(()); } |
| 193 | let profile = element.get_attribute("alt").and_then(|alt| alt.strip_prefix("gravatar for ").and_then(|email| profiles.get(&format!("email:{}",email.trim().to_lowercase())))); |
| 194 | if element.get_attribute("alt").is_some() && commit_images.borrow().is_none() { |
| 195 | *commit_images.borrow_mut() = profile.and_then(|profile| profile.url.as_ref().map(|url| (url.clone(),profile.picture.clone()))); |
| 196 | } |
| 197 | if let Some((_,Some(picture))) = &*commit_images.borrow() { |
| 198 | element.set_attribute("src",picture)?; |
| 199 | element.set_attribute("width", "14")?; |
| 200 | element.set_attribute("height", "14")?; |
| 201 | element.set_attribute("alt", "")?; |
| 202 | element.set_attribute("referrerpolicy", "no-referrer")?; |
| 203 | } |
| 204 | Ok(()) |
| 205 | })) |
| 206 | .append_element_content_handler(element!("#m-metainfo td span", |element| { |
| 207 | if markdown.get() != 0 { return Ok(()); } |
| 208 | if let Some((url,_)) = commit_names.borrow_mut().take() { |
| 209 | element.prepend(&format!("<a class=\"usa-link\" href=\"{url}\">"),ContentType::Html); |
| 210 | element.append("</a>",ContentType::Html); |
| 211 | } |
| 212 | Ok(()) |
| 213 | })) |
| 214 | .append_element_content_handler(text!("#m-metainfo td span", |chunk| { |
| 215 | if markdown.get() != 0 { return Ok(()); } |
| 216 | signature.borrow_mut().push_str(chunk.as_str()); |
| 217 | if !chunk.last_in_text_node() { chunk.remove(); return Ok(()); } |
| 218 | let value = std::mem::take(&mut *signature.borrow_mut()); |
| 219 | let profile = value.trim().strip_prefix("Signed by SSH key ").and_then(|fingerprint| profiles.get(&format!("key:{fingerprint}"))); |
| 220 | if let Some(profile) = profile { |
| 221 | let name = profile.name.replace('&',"&amp;").replace('<',"&lt;").replace('>',"&gt;"); |
| 222 | let original = &value; |
| 223 | chunk.replace(&format!("{original} (<a class=\"usa-link\" href=\"{}\">{name}</a>)",profile.url.as_deref().unwrap_or("/")),ContentType::Html); |
| 224 | } else { chunk.replace(&value,ContentType::Html); } |
| 225 | Ok(()) |
| 226 | })) |
| 227 | .append_element_content_handler(element!(".markdown", move |element| { |
| 228 | bodies.set(bodies.get() + 1); |
| 229 | let bodies = bodies.clone(); |
| 230 | element.on_end_tag(lol_html::end_tag!(move |_| { bodies.set(bodies.get() - 1); Ok(()) }))?; |
| 231 | Ok(()) |
| 232 | })) |
| 233 | .append_element_content_handler(element!(".timeline > li.spanspacer, .timeline .n-card__header > .spanspacer", |element| { |
| 234 | let style = element.get_attribute("style").unwrap_or_default(); |
| 235 | element.set_attribute("style", &format!("{style};flex-wrap:wrap"))?; |
| 236 | Ok(()) |
| 237 | })) |
| 238 | .append_element_content_handler(element!("#page-issues table td:nth-child(4)", move |element| { |
| 239 | author_column.set(true); |
| 240 | let author_column = author_column.clone(); |
| 241 | element.on_end_tag(lol_html::end_tag!(move |_| { author_column.set(false); Ok(()) }))?; |
| 242 | Ok(()) |
| 243 | })) |
| 244 | .append_element_content_handler(element!("img[src]", |element| { |
| 245 | if markdown.get() == 0 |
| 246 | && let Some(url) = element.get_attribute("src").and_then(|src| url::Url::parse(&src).ok()) |
| 247 | && url.host_str() == Some("api.dicebear.com") |
| 248 | && let Some(picture) = url.query_pairs().find(|(key, _)| key == "seed").and_then(|(_, seed)| avatars.get(seed.as_ref())) |
| 249 | { |
| 250 | element.set_attribute("src", picture)?; |
| 251 | element.set_attribute("alt", "")?; |
| 252 | element.set_attribute("referrerpolicy", "no-referrer")?; |
| 253 | } |
| 254 | Ok(()) |
| 255 | })) |
| 256 | .append_element_content_handler(element!("a[href]", |element| { |
| 257 | let target = element.get_attribute("href").and_then(|href| origin.join(&href).ok()).filter(|target| target.origin() == origin.origin()); |
| 258 | let ai_profile = target.as_ref().filter(|target| markdown.get() == 0 && target.path().trim_end_matches('/') == "/~ai") |
| 259 | .map(|_| ai_profile(None)); |
| 260 | let profile = ai_profile.as_ref().or_else(|| target.and_then(|target| account_path(target.path()).and_then(|name| profiles.get(name))).filter(|profile| profile.owner.is_none() || markdown.get() == 0)); |
| 261 | active.set(profile.map(|profile| profile.picture.is_some() || ai_profile.is_some() || profile.owner.is_some())); |
| 262 | let Some(profile) = profile else { return Ok(()); }; |
| 263 | let closing = active.clone(); |
| 264 | element.on_end_tag(lol_html::end_tag!(move |_| { closing.set(None); Ok(()) }))?; |
| 265 | if let Some(url) = &profile.url { |
| 266 | element.set_attribute("href", url)?; |
| 267 | element.set_attribute("target", "_blank")?; |
| 268 | element.set_attribute("rel", "noreferrer")?; |
| 269 | } else { |
| 270 | for attribute in ["href", "target", "rel", "tabindex"] { element.remove_attribute(attribute); } |
| 271 | } |
| 272 | if profile.owner.is_some() || ai_profile.is_some() { element.set_tag_name("span")?; } |
| 273 | let style = element.get_attribute("style").unwrap_or_default(); |
| 274 | let cursor = if profile.url.is_some() { "" } else { ";cursor:default" }; |
| 275 | let spacing = if profile.owner.is_some() { format!(";display:inline-flex;align-items:baseline;flex-wrap:{};gap:.25em;margin-right:.25em;max-width:100%", if compact.get() { "nowrap" } else { "wrap" }) } else if ai_profile.is_none() { ";display:inline-flex;align-items:center;gap:.25em;max-width:100%".into() } else { String::new() }; |
| 276 | element.set_attribute("style", &format!("{style};text-decoration:none{cursor}{spacing}"))?; |
| 277 | if let Some(picture) = &profile.picture { |
| 278 | element.prepend(&avatar(picture), ContentType::Html); |
| 279 | } |
| 280 | if profile.owner.is_some() { element.append("<span class=\"va-middle-childs\" style=\"white-space:nowrap\">", ContentType::Html); } |
| 281 | let dimensions = if ai_profile.is_some() || profile.owner.is_some() { "width:1em;height:1em;margin-right:.25em" } else { "width:.85em;height:.85em;margin-left:.25em;flex-shrink:0" }; |
| 282 | let icon = profile.icon.trim().replace("<svg ", &format!("<svg aria-hidden=\"true\" focusable=\"false\" style=\"color:inherit;stroke:currentColor;{dimensions}\" ")); |
| 283 | let icon = if profile.url.is_some() { icon.replace("<path ", "<path style=\"fill:currentColor;stroke:none\" ") } else { icon }; |
| 284 | element.append(&icon, ContentType::Html); |
| 285 | if profile.url.is_some() { |
| 286 | element.append("<span style=\"text-decoration:underline;text-decoration-color:currentColor\">", ContentType::Html); |
| 287 | } |
| 288 | let name = if compact.get() && profile.owner.is_some() { |
| 289 | let number = profile.name.find(|c: char| c.is_ascii_digit()).unwrap_or(profile.name.len()); |
| 290 | profile.name[..number].trim_end_matches([' ', '-']) |
| 291 | } else { &profile.name }; |
| 292 | element.append(name, ContentType::Text); |
| 293 | if profile.url.is_some() { element.append("</span>", ContentType::Html); } |
| 294 | if let Some((owner, picture)) = &profile.owner { |
| 295 | element.append("</span> <small style=\"font-size:.8em;white-space:nowrap\">(", ContentType::Html); |
| 296 | if !compact.get() { element.append("on behalf of ", ContentType::Text); } |
| 297 | let mut owner_url = origin.clone(); |
| 298 | owner_url.set_path(&format!("/~{owner}")); owner_url.set_query(None); owner_url.set_fragment(None); |
| 299 | let href = owner_url.path().replace('&', "&amp;").replace('"', "&quot;").replace('<', "&lt;"); |
| 300 | element.append(&format!("<a class=\"usa-link va-middle-childs\" href=\"{href}\">"), ContentType::Html); |
| 301 | if let Some(picture) = picture { element.append(&avatar(picture), ContentType::Html); } |
| 302 | element.append("<span>", ContentType::Html); |
| 303 | element.append(&format!("~{owner}"), ContentType::Text); |
| 304 | element.append("</span></a>)</small> ", ContentType::Html); |
| 305 | } |
| 306 | Ok(()) |
| 307 | })) |
| 308 | .append_element_content_handler(element!("a[href] img, a[href] span", move |child| { |
| 309 | if let Some(replace_picture) = images.get() { |
| 310 | if child.tag_name() == "span" { child.remove_and_keep_content(); } |
| 311 | else if replace_picture { child.remove(); } |
| 312 | else { |
| 313 | child.set_attribute("alt", "")?; |
| 314 | child.set_attribute("referrerpolicy", "no-referrer")?; |
| 315 | } |
| 316 | } |
| 317 | Ok(()) |
| 318 | })) |
| 319 | .append_element_content_handler(text!("a[href]", move |text| { |
| 320 | if labels.get().is_some() { text.remove(); } |
| 321 | Ok(()) |
| 322 | })))?) |
| 323 | } |
| 324 | |
| 325 | fn strip_hop_headers(headers: &mut HeaderMap) { |
| 326 | let named: Vec<_> = headers |
| 327 | .get_all("connection") |
| 328 | .iter() |
| 329 | .filter_map(|v| v.to_str().ok()) |
| 330 | .flat_map(|v| v.split(',')) |
| 331 | .map(|v| v.trim().to_owned()) |
| 332 | .collect(); |
| 333 | for name in named { |
| 334 | headers.remove(name); |
| 335 | } |
| 336 | for name in [ |
| 337 | "connection", |
| 338 | "keep-alive", |
| 339 | "proxy-authenticate", |
| 340 | "proxy-authorization", |
| 341 | "te", |
| 342 | "trailer", |
| 343 | "transfer-encoding", |
| 344 | "upgrade", |
| 345 | ] { |
| 346 | headers.remove(name); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | fn knife_uri(path: &str) -> bool { |
| 351 | let mut decoded = path.to_owned(); |
| 352 | for _ in 0..5 { |
| 353 | if decoded.split(['/', '\\']).any(|part| part.eq_ignore_ascii_case("the-knife") || part.eq_ignore_ascii_case("the-knife.git")) { |
| 354 | return true; |
| 355 | } |
| 356 | let next = percent_encoding::percent_decode_str(&decoded).decode_utf8_lossy().into_owned(); |
| 357 | if next == decoded { break; } |
| 358 | decoded = next; |
| 359 | } |
| 360 | false |
| 361 | } |
| 362 | |
| 363 | fn session_account(html: &str) -> Option<String> { |
| 364 | let document = scraper::Html::parse_document(html); |
| 365 | let selector = scraper::Selector::parse("body > header.usa-header--basic nav a.usa-nav-link[href^='/~']").unwrap(); |
| 366 | let mut accounts = document.select(&selector); |
| 367 | let name = accounts.next()?.attr("href")?.strip_prefix("/~")?; |
| 368 | let name = percent_encoding::percent_decode_str(name).decode_utf8().ok()?; |
| 369 | (accounts.next().is_none() && !name.is_empty() && !name.contains(['/', '\\', '?', '#'])) |
| 370 | .then(|| name.into_owned()) |
| 371 | } |
| 372 | |
| 373 | fn restricted(authenticate: bool) -> Response { |
| 374 | (if authenticate { StatusCode::UNAUTHORIZED } else { StatusCode::FORBIDDEN }, [ |
| 375 | ("content-type", "text/html; charset=utf-8"), |
| 376 | ("cache-control", "private, no-store"), |
| 377 | ("vary", "Cookie, Authorization"), |
| 378 | ("referrer-policy", "same-origin"), |
| 379 | ("x-content-type-options", "nosniff"), |
| 380 | ("content-security-policy", "default-src 'none'; style-src 'unsafe-inline'; frame-ancestors 'none'; base-uri 'none'"), |
| 381 | ("www-authenticate", "Basic realm=\"The Knife\""), |
| 382 | ], include_str!("knife-restricted.html")).into_response() |
| 383 | } |
| 384 | |
| 385 | fn redact_knife(html: &str, policy: &Value) -> Result<String> { |
| 386 | let commits: HashSet<&str> = array(&policy["commits"]).iter().filter_map(Value::as_str).collect(); |
| 387 | let document = scraper::Html::parse_document(html); |
| 388 | let diff = policy["safeDiff"].as_str(); |
| 389 | if diff.is_some() && document.select(&scraper::Selector::parse("#m-code").unwrap()).count() != 1 { |
| 390 | return Err(Error::new(403, "The Knife's Native UI is restricted")); |
| 391 | } |
| 392 | let links = scraper::Selector::parse("a[href]").unwrap(); |
| 393 | let rows: Vec<bool> = document.select(&scraper::Selector::parse("tr").unwrap()).map(|row| { |
| 394 | row.select(&links).any(|link| link.attr("href").is_some_and(|href| { |
| 395 | href.split(['/', '?', '#', '.']).any(|part| commits.contains(part)) |
| 396 | })) |
| 397 | }).collect(); |
| 398 | let mut row_index = 0; |
| 399 | Ok(lol_html::rewrite_str(html, RewriteStrSettings::new() |
| 400 | .append_element_content_handler(element!("#m-code", |element| { |
| 401 | if let Some(diff) = diff { |
| 402 | let diff = diff.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;"); |
| 403 | element.set_inner_content(&format!("<pre><code>{diff}</code></pre>"), ContentType::Html); |
| 404 | } |
| 405 | Ok(()) |
| 406 | })) |
| 407 | .append_element_content_handler(element!("tr", |element| { |
| 408 | if *rows.get(row_index).unwrap_or(&true) { element.remove(); } |
| 409 | row_index += 1; |
| 410 | Ok(()) |
| 411 | })) |
| 412 | )?) |
| 413 | } |
| 414 | |
| 415 | /// Relays git http-backend's CGI response for an anonymous clone of The Knife's main branch. |
| 416 | async fn clone(request: Value, body: Body) -> Result<Response> { |
| 417 | use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt}; |
| 418 | let (_, stream) = host::open(request).await?; |
| 419 | let (reader, mut writer) = stream.into_split(); |
| 420 | tokio::spawn(async move { |
| 421 | let mut body = body.into_data_stream(); |
| 422 | while let Some(Ok(chunk)) = body.next().await { |
| 423 | if writer.write_all(&chunk).await.is_err() { return; } |
| 424 | } |
| 425 | let _ = writer.shutdown().await; |
| 426 | }); |
| 427 | let mut reader = tokio::io::BufReader::new(reader); |
| 428 | let mut response = Response::builder(); |
| 429 | loop { |
| 430 | let mut line = String::new(); |
| 431 | if reader.read_line(&mut line).await? == 0 { |
| 432 | return Err(Error::new(502, "The Knife clone ended early.")); |
| 433 | } |
| 434 | let line = line.trim_end(); |
| 435 | if line.is_empty() { break; } |
| 436 | let (name, value) = line.split_once(':').ok_or_else(|| Error::new(502, "The Knife clone response is malformed."))?; |
| 437 | let value = value.trim(); |
| 438 | if name.eq_ignore_ascii_case("status") { |
| 439 | response = response.status(value.split_whitespace().next().unwrap_or_default().parse::<u16>()?); |
| 440 | } else { |
| 441 | response = response.header(name, value); |
| 442 | } |
| 443 | } |
| 444 | let stream = futures::stream::unfold(reader, |mut reader| async move { |
| 445 | let mut buffer = vec![0; 64 * 1024]; |
| 446 | match reader.read(&mut buffer).await { |
| 447 | Ok(0) => None, |
| 448 | Ok(length) => { buffer.truncate(length); Some((Ok(Bytes::from(buffer)), reader)) } |
| 449 | Err(error) => Some((Err(error), reader)), |
| 450 | } |
| 451 | }); |
| 452 | Ok(response.body(Body::from_stream(stream))?) |
| 453 | } |
| 454 | |
| 455 | pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> { |
| 456 | let (mut parts, body) = request.into_parts(); |
| 457 | let host = parts |
| 458 | .headers |
| 459 | .get("host") |
| 460 | .and_then(|v| v.to_str().ok()) |
| 461 | .unwrap_or_default(); |
| 462 | let domain = env("SNOWGLOBE_DOMAIN", "snowglobe.test"); |
| 463 | let service = host.strip_suffix(&format!(".{domain}")).unwrap_or_default().to_owned(); |
| 464 | if service != "shale" |
| 465 | && !(service |
| 466 | .strip_prefix("shale-preview-") |
| 467 | .is_some_and(|id| id.len() == 8 && id.bytes().all(|b| b.is_ascii_hexdigit()))) |
| 468 | { |
| 469 | return Err(Error::new(403, "Open Shale to continue.")); |
| 470 | } |
| 471 | let upstream: std::net::SocketAddr = parts |
| 472 | .headers |
| 473 | .get("snowglobe-shale-upstream") |
| 474 | .and_then(|v| v.to_str().ok()) |
| 475 | .ok_or_else(|| Error::new(403, "Open Shale to continue."))? |
| 476 | .parse()?; |
| 477 | let uri: axum::http::Uri = parts |
| 478 | .headers |
| 479 | .get("snowglobe-shale-uri") |
| 480 | .and_then(|v| v.to_str().ok()) |
| 481 | .ok_or_else(|| Error::new(403, "Open Shale to continue."))? |
| 482 | .parse()?; |
| 483 | if uri.scheme().is_some() || uri.authority().is_some() || !uri.path().starts_with('/') { |
| 484 | return Err(Error::new(403, "Open Shale to continue.")); |
| 485 | } |
| 486 | let origin = url::Url::parse(&format!("https://{host}{uri}"))?; |
| 487 | let knife = knife_uri(uri.path()); |
| 488 | let mut target = match &app.internal { |
| 489 | Some((base, _)) => format!("{base}services/{service}{uri}"), |
| 490 | None => format!("http://{upstream}{uri}"), |
| 491 | }; |
| 492 | let mut knife_policy = Value::Null; |
| 493 | if knife { |
| 494 | let mut account = String::new(); |
| 495 | if let Some(cookie) = parts.headers.get("cookie") { |
| 496 | // Opening settings rotates the session's CSRF token, invalidating pending forms. |
| 497 | let index = match &app.internal { |
| 498 | Some((base, _)) => format!("{base}services/{service}/"), |
| 499 | None => format!("http://{upstream}/"), |
| 500 | }; |
| 501 | let mut identity = app.shale_http.get(index).header("cookie", cookie).header("accept", "text/html").timeout(Duration::from_secs(3)); |
| 502 | if app.internal.is_none() { identity = identity.header("host", host); } |
| 503 | for name in ["x-forwarded-for", "x-real-ip", "user-agent"] { |
| 504 | if let Some(value) = parts.headers.get(name) { identity = identity.header(name, value); } |
| 505 | } |
| 506 | if let Ok(response) = identity.send().await |
| 507 | && response.status() == StatusCode::OK |
| 508 | && let Ok(body) = response.text().await |
| 509 | { |
| 510 | account = session_account(&body).unwrap_or_default(); |
| 511 | } |
| 512 | } |
| 513 | knife_policy = match host::call(json!({"operation":"shale.access","service":service,"uri":uri.to_string(),"account":account,"authorization":parts.headers.get("authorization").and_then(|value| value.to_str().ok()).unwrap_or_default()})).await { |
| 514 | Ok(policy) => policy, |
| 515 | Err(_) => return Ok(restricted(false)), |
| 516 | }; |
| 517 | if knife_policy["deny"] != false { return Ok(restricted(knife_policy["authenticate"] == true)); } |
| 518 | if knife_policy["clone"] == true { |
| 519 | let header = |name| parts.headers.get(name).and_then(|value| value.to_str().ok()).unwrap_or_default(); |
| 520 | let request = json!({"operation":"shale.clone","service":service,"uri":uri.to_string(),"method":parts.method.as_str(),"contentType":header("content-type"),"contentEncoding":header("content-encoding")}); |
| 521 | return Ok(clone(request, body).await.unwrap_or_else(|_| restricted(true))); |
| 522 | } |
| 523 | if let Some(uri) = knife_policy["uri"].as_str() { |
| 524 | target = match &app.internal { |
| 525 | Some((base, _)) => format!("{base}services/{service}{uri}"), |
| 526 | None => format!("http://{upstream}{uri}"), |
| 527 | }; |
| 528 | } |
| 529 | if knife_policy["rawDiff"] == true && let Some(diff) = knife_policy["safeDiff"].as_str() { |
| 530 | return Ok(([("content-type", "text/plain; charset=utf-8"), ("cache-control", "private, no-store"), ("vary", "Cookie, Authorization")], diff.to_owned()).into_response()); |
| 531 | } |
| 532 | } |
| 533 | let profiles = profiles(&app.auth)?; |
| 534 | if matches!(parts.method, Method::GET | Method::HEAD) |
| 535 | && let Some(url) = account_path(uri.path()).and_then(|name| profiles.get(name)).and_then(|profile| profile.url.as_deref()) |
| 536 | { |
| 537 | return Ok(( |
| 538 | StatusCode::FOUND, |
| 539 | [ |
| 540 | ("location", url), |
| 541 | ("cache-control", "no-store"), |
| 542 | ("referrer-policy", "no-referrer"), |
| 543 | ], |
| 544 | ) |
| 545 | .into_response()); |
| 546 | } |
| 547 | if app.internal.is_some() { |
| 548 | parts.headers.remove("host"); |
| 549 | } |
| 550 | strip_hop_headers(&mut parts.headers); |
| 551 | for name in [ |
| 552 | "snowglobe-shale-upstream", |
| 553 | "snowglobe-shale-uri", |
| 554 | "snowglobe-proxy-token", |
| 555 | "user-name", |
| 556 | "user-groups", |
| 557 | "user-id", |
| 558 | "if-none-match", |
| 559 | "if-modified-since", |
| 560 | "accept-encoding", |
| 561 | ] { |
| 562 | parts.headers.remove(name); |
| 563 | } |
| 564 | parts |
| 565 | .headers |
| 566 | .insert("accept-encoding", "identity".parse().unwrap()); |
| 567 | let mut upstream_response = app |
| 568 | .shale_http |
| 569 | .request(parts.method.clone(), target) |
| 570 | .headers(parts.headers) |
| 571 | .body(reqwest::Body::wrap_stream(body.into_data_stream())) |
| 572 | .send() |
| 573 | .await?; |
| 574 | let status = upstream_response.headers().get("status") |
| 575 | .and_then(|value| value.to_str().ok()).and_then(|value| value.split_whitespace().next()) |
| 576 | .and_then(|value| value.parse::<u16>().ok()).and_then(|value| StatusCode::from_u16(value).ok()) |
| 577 | .unwrap_or(upstream_response.status()); |
| 578 | let mut headers = upstream_response.headers().clone(); |
| 579 | strip_hop_headers(&mut headers); |
| 580 | headers.remove("status"); |
| 581 | if knife { |
| 582 | headers.insert("cache-control", "private, no-store".parse().unwrap()); |
| 583 | headers.insert("vary", "Cookie, Authorization".parse().unwrap()); |
| 584 | } |
| 585 | let html = parts.method != Method::HEAD |
| 586 | && headers |
| 587 | .get("content-type") |
| 588 | .and_then(|v| v.to_str().ok()) |
| 589 | .is_some_and(|v| { |
| 590 | v.split(';') |
| 591 | .next() |
| 592 | .unwrap_or_default() |
| 593 | .trim() |
| 594 | .eq_ignore_ascii_case("text/html") |
| 595 | }) |
| 596 | && headers |
| 597 | .get("content-encoding") |
| 598 | .is_none_or(|v| v == "identity"); |
| 599 | let mut prefix = Vec::new(); |
| 600 | if html { |
| 601 | while let Some(chunk) = upstream_response.chunk().await? { |
| 602 | prefix.extend_from_slice(&chunk); |
| 603 | if prefix.len() > 8 * 1024 * 1024 { |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | if prefix.len() <= 8 * 1024 * 1024 |
| 608 | && let Ok(text) = std::str::from_utf8(&prefix) |
| 609 | { |
| 610 | let metadata = if service == "shale" && status.is_success() { |
| 611 | let (repository, accounts) = { |
| 612 | let document = scraper::Html::parse_document(text); |
| 613 | let repository = document.select(&scraper::Selector::parse("meta[name='astheno.shale.repo.name']").unwrap()).next().and_then(|e| e.attr("content")).map(str::to_owned); |
| 614 | let mut accounts: HashSet<String> = document.select(&scraper::Selector::parse("a[href]").unwrap()).filter_map(|element| { |
| 615 | let url = origin.join(element.attr("href")?).ok()?; |
| 616 | if url.origin() != origin.origin() { return None; } |
| 617 | account_path(url.path()).map(str::to_owned) |
| 618 | }).take(1024).collect(); |
| 619 | if accounts.len() < 1024 && let Some(name) = account_path(origin.path()) { accounts.insert(name.to_owned()); } |
| 620 | (repository, accounts) |
| 621 | }; |
| 622 | match tokio::time::timeout(Duration::from_secs(3), host::call(json!({"operation":"shale.metadata","repository":repository,"accounts":accounts}))).await { |
| 623 | Ok(Ok(value)) => value, |
| 624 | _ => Value::Null, |
| 625 | } |
| 626 | } else { Value::Null }; |
| 627 | let mut profiles = profiles; |
| 628 | let mut avatars = HashMap::new(); |
| 629 | if let Some(accounts) = metadata["accounts"].as_object() { |
| 630 | for (name, account) in accounts { |
| 631 | if let (Some(seed), Some(picture)) = (account["avatar"].as_str(), account["picture"].as_str()) { |
| 632 | avatars.insert(seed.to_owned(), picture.to_owned()); |
| 633 | } |
| 634 | let Some(owner) = account["owner"].as_str() else { continue; }; |
| 635 | let mut profile = ai_profile(account["model"].as_str()); |
| 636 | profile.owner = Some((owner.into(), account["picture"].as_str().map(str::to_owned))); |
| 637 | profiles.insert(name.clone(), profile); |
| 638 | } |
| 639 | } |
| 640 | let redacted; |
| 641 | let text = if knife && knife_policy["allowed"] != true { |
| 642 | redacted = match redact_knife(text, &knife_policy) { |
| 643 | Ok(html) => html, |
| 644 | Err(_) => return Ok(restricted(false)), |
| 645 | }; |
| 646 | redacted.as_str() |
| 647 | } else { text }; |
| 648 | let rewritten = rewrite(text, &origin, &profiles, &avatars, metadata["openIssues"].as_u64())?; |
| 649 | for name in [ |
| 650 | "content-length", |
| 651 | "etag", |
| 652 | "last-modified", |
| 653 | "content-md5", |
| 654 | "digest", |
| 655 | "content-digest", |
| 656 | "accept-ranges", |
| 657 | ] { |
| 658 | headers.remove(name); |
| 659 | } |
| 660 | headers.insert("cache-control", "no-store".parse().unwrap()); |
| 661 | return Ok((status, headers, rewritten).into_response()); |
| 662 | } |
| 663 | } |
| 664 | if knife && knife_policy["allowed"] != true && (html || parts.method != Method::HEAD && !status.is_redirection() && knife_policy["htmlOnly"] == true) { |
| 665 | return Ok(restricted(false)); |
| 666 | } |
| 667 | let stream = futures::stream::once(async move { Ok::<_, reqwest::Error>(Bytes::from(prefix)) }) |
| 668 | .chain(upstream_response.bytes_stream()); |
| 669 | Ok((status, headers, Body::from_stream(stream)).into_response()) |
| 670 | } |
| 671 | |
| 672 | #[cfg(test)] |
| 673 | mod tests { |
| 674 | use super::*; |
| 675 | use rusqlite::params as sql; |
| 676 | use scraper::{Html, Selector}; |
| 677 | |
| 678 | #[test] |
| 679 | fn session_account_uses_only_unambiguous_site_navigation() { |
| 680 | let header = |name: &str| format!("<header class='usa-header usa-header--basic'><nav><a class='usa-nav-link' href='/~{name}'>account</a></nav></header>"); |
| 681 | assert_eq!(session_account(&header("clover")), Some("clover".into())); |
| 682 | assert_eq!(session_account(&header("guest%2Dname")), Some("guest-name".into())); |
| 683 | assert_eq!(session_account(&format!("{}<main><header class='usa-header--basic'><nav><a class='usa-nav-link' href='/~clover'>fake</a></nav></header></main>", header("guest"))), Some("guest".into())); |
| 684 | assert_eq!(session_account(&format!("{}{}", header("guest"), header("clover"))), None); |
| 685 | for name in ["", "%FF", "clover%2Frepo", "clover%5Crepo", "clover?fake", "clover#fake"] { |
| 686 | assert_eq!(session_account(&header(name)), None, "{name}"); |
| 687 | } |
| 688 | assert_eq!(session_account("<a class='usa-nav-link' href='/~clover'>fake</a>"), None); |
| 689 | } |
| 690 | |
| 691 | #[test] |
| 692 | fn native_content_is_removed_from_both_diff_styles_and_history_rows() { |
| 693 | let policy = json!({"commits":["deadbeef"],"safeDiff":"PUBLIC SOURCE\nThe Knife's Native UI is restricted\n"}); |
| 694 | let html = "<html><body id=page-commit><table><tr><td><a href='/the-knife/commit/deadbeef'>PRIVATE MESSAGE</a></td></tr><tr><td>PUBLIC ROW</td></tr></table><div id=m-code><div id=m-changedfiles>PRIVATE PATHS</div><details class=m-usa-accordion><summary><code>knife/private.rs</code></summary><table><tr><td>PRIVATE SOURCE</td></tr></table></details><details class=m-usa-accordion><summary><code>src/public.rs</code></summary><table><tr><td>PUBLIC SOURCE</td></tr></table></details><details class=m-usa-accordion><summary><code>nested/Cargo.lock</code></summary><table><tr><td>PRIVATE LOCK</td></tr></table></details></div></body></html>"; |
| 695 | let output = redact_knife(html, &policy).unwrap(); |
| 696 | assert!(!output.contains("PRIVATE"), "{output}"); |
| 697 | assert!(output.contains("PUBLIC ROW"), "{output}"); |
| 698 | assert!(output.contains("PUBLIC SOURCE"), "{output}"); |
| 699 | assert_eq!(output.matches("The Knife's Native UI is restricted").count(), 1); |
| 700 | assert!(redact_knife("<div>unknown diff markup</div>", &policy).is_err()); |
| 701 | for path in ["/the-knife/tree/main/knife", "/THE-KNIFE.git/info/refs", "/%74he-knife/plain/main/x", "/%2574he-knife/tree/main/x", "//the-knife/tree/main/x"] { assert!(knife_uri(path), "{path}"); } |
| 702 | assert!(!knife_uri("/snowbound/commit/native")); |
| 703 | } |
| 704 | |
| 705 | #[test] |
| 706 | fn persisted_atproto_profiles_enrich_shale_authors_and_account_links() { |
| 707 | let path = std::env::temp_dir().join(format!("shale-atproto-test-{}", uuid::Uuid::new_v4())); |
| 708 | let store = || auth::Store::new(&path, "https://snowglobe.example.com", "https://file.example.com", "auth.example.com").unwrap(); |
| 709 | let accounts = [ |
| 710 | ("did:web:tangled.example.com", "Alice <Tangled> & \"friends\"", "https://tangled.org/alice.example.com", true), |
| 711 | ("did:web:bluesky.example.com", "Bob <Bluesky> & \"friends\"", "https://bsky.app/profile/bob.example.com", true), |
| 712 | ("did:web:pds.example.com", "Carol <PDSLS> & \"friends\"", "https://pdsls.dev/at://did:web:pds.example.com", true), |
| 713 | ("did:web:no-avatar.example.com", "No avatar", "https://tangled.org/no-avatar.example.com", false), |
| 714 | ]; |
| 715 | { |
| 716 | let auth = store(); |
| 717 | let db = auth.db.lock().unwrap(); |
| 718 | for (did, name, url, picture) in accounts { |
| 719 | let id = uuid::Uuid::new_v4().to_string(); |
| 720 | let username = format!("guest-atproto-{}", &mcp::hash(did)[..24]); |
| 721 | let mut profile = json!({"kind":"guest","guestProvider":"atproto","username":username,"enabled":true,"email":null,"emailVerified":false,"firstName":name,"attributes":{"profileUrl":[url]}}); |
| 722 | if picture { |
| 723 | profile["attributes"]["picture"] = json!(["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+j3ioAAAAASUVORK5CYII="]); |
| 724 | } |
| 725 | db.execute("INSERT INTO users(id,profile) VALUES (?,?)", sql![id,profile.to_string()]).unwrap(); |
| 726 | db.execute("INSERT INTO external_identities VALUES ('atproto',?,?)", sql![did,id]).unwrap(); |
| 727 | } |
| 728 | } |
| 729 | let auth = store(); |
| 730 | let profiles = profiles(&auth).unwrap(); |
| 731 | assert_eq!(profiles.len(), accounts.len()); |
| 732 | let origin = url::Url::parse("https://shale.example.com/home-infra/issues/50").unwrap(); |
| 733 | for (did, name, url, picture) in accounts { |
| 734 | let username = format!("guest-atproto-{}", &mcp::hash(did)[..24]); |
| 735 | let local = format!("/~{username}"); |
| 736 | let html = format!(r#"<body id="page-issue"><ol class="timeline"><li class="spanspacer"><div class="n-card__header"><div class="spanspacer"><a class="usa-link va-middle-childs" href="{local}"><img src="old-avatar" height="16"><span>~{username}</span></a><time>opened this issue</time></div></div><div class="markdown"><p>A synthetic issue.</p><a id="external" href="https://other.example.com{local}">external</a><a id="http" href="http://shale.example.com{local}">http</a><a id="repo" href="{local}/repo">repo</a><a id="unknown" href="/~guest-atproto-unknown">unknown</a><pre>~{username}</pre></div></li></ol><h1 id="profile"><a href="https://shale.example.com{local}/"><span>~{username}</span></a></h1></body>"#); |
| 737 | let output = rewrite(&html, &origin, &profiles, &HashMap::new(), None).unwrap(); |
| 738 | let document = Html::parse_document(&output); |
| 739 | for selector in [".n-card__header a", "#profile a"] { |
| 740 | let link = document.select(&Selector::parse(selector).unwrap()).next().unwrap(); |
| 741 | assert_eq!(link.attr("href"), Some(url)); |
| 742 | assert_eq!(link.attr("target"), Some("_blank")); |
| 743 | assert_eq!(link.attr("rel"), Some("noreferrer")); |
| 744 | let style = link.attr("style").unwrap(); |
| 745 | assert!(style.contains("display:inline-flex;align-items:center;gap:.25em")); |
| 746 | assert_eq!(link.children().filter_map(scraper::ElementRef::wrap).map(|e| e.value().name()).collect::<Vec<_>>(), if picture || selector.starts_with('.') { vec!["img", "svg", "span"] } else { vec!["svg", "span"] }); |
| 747 | assert_eq!(link.select(&Selector::parse("span[style]").unwrap()).next().unwrap().text().collect::<String>(), name); |
| 748 | let icon = link.select(&Selector::parse("svg").unwrap()).next().unwrap(); |
| 749 | assert_eq!(icon.attr("aria-hidden"), Some("true")); |
| 750 | assert_eq!(icon.attr("focusable"), Some("false")); |
| 751 | assert!(!icon.attr("style").unwrap().contains("margin-right")); |
| 752 | assert_eq!(link.select(&Selector::parse("img").unwrap()).count(), if picture { 1 } else { usize::from(selector.starts_with('.')) }); |
| 753 | if picture { |
| 754 | let avatar = link.select(&Selector::parse("img").unwrap()).next().unwrap(); |
| 755 | let expected = format!("https://snowglobe.example.com/auth/guest/avatar/atproto?subject=did%3Aweb%3A{}", did.strip_prefix("did:web:").unwrap()); |
| 756 | assert_eq!(avatar.attr("src"), Some(expected.as_str())); |
| 757 | assert_eq!(avatar.attr("alt"), Some("")); |
| 758 | assert_eq!(avatar.attr("referrerpolicy"), Some("no-referrer")); |
| 759 | } |
| 760 | } |
| 761 | for (id, href, label) in [("external",format!("https://other.example.com{local}"),"external"),("http",format!("http://shale.example.com{local}"),"http"),("repo",format!("{local}/repo"),"repo"),("unknown","/~guest-atproto-unknown".into(),"unknown")] { |
| 762 | let link = document.select(&Selector::parse(&format!("#{id}")).unwrap()).next().unwrap(); |
| 763 | assert_eq!(link.attr("href"), Some(href.as_str())); |
| 764 | assert_eq!(link.text().collect::<String>(), label); |
| 765 | assert_eq!(link.attr("target"), None); |
| 766 | assert_eq!(link.select(&Selector::parse("svg,img").unwrap()).count(), 0); |
| 767 | } |
| 768 | assert_eq!(document.select(&Selector::parse("pre").unwrap()).next().unwrap().text().collect::<String>(), format!("~{username}")); |
| 769 | assert_eq!(document.select(&Selector::parse("script,tangled,bluesky,pdsls").unwrap()).count(), 0); |
| 770 | assert!(!output.contains("data:image")); |
| 771 | assert!(!output.contains("base64")); |
| 772 | } |
| 773 | drop(auth); |
| 774 | std::fs::remove_dir_all(path).unwrap(); |
| 775 | } |
| 776 | |
| 777 | #[test] |
| 778 | fn commit_identity_keeps_entities_and_cannot_escape_its_metadata_cell() { |
| 779 | let origin = url::Url::parse("https://shale.test/repo/commit/abc").unwrap(); |
| 780 | let profile = || Profile { name: "~clover".into(), url: Some("/~clover".into()), icon:"",picture:Some("https://snow.test/picture".into()),owner:None }; |
| 781 | let profiles = HashMap::from([("email:known@example.test".into(),profile()),("key:SHA256:known".into(),profile())]); |
| 782 | let html = "<body id='page-commit'><table id=m-metainfo><tr><td><img src=old alt='gravatar for known@example.test'></td></tr><tr><td><img src=unknown alt='gravatar for other@example.test'><span>John &amp; Jane &LT;other@example.test&GT;</span></td></tr><tr><td><span> Signed by SSH key SHA256:known</span></td></tr></table><div class=markdown><table id=m-metainfo><tr><td><img src=example alt='gravatar for known@example.test'><span> Signed by SSH key SHA256:known</span></td></tr></table></div></body>"; |
| 783 | let output = rewrite(html,&origin,&profiles,&HashMap::new(),None).unwrap(); |
| 784 | let document = Html::parse_document(&output); |
| 785 | assert!(!output.contains("&amp;amp;")); |
| 786 | assert!(output.contains("John &amp; Jane &LT;other@example.test&GT;")); |
| 787 | assert_eq!(document.select(&Selector::parse("a[href='/~clover']").unwrap()).count(),1); |
| 788 | assert!(output.contains("src=unknown")); |
| 789 | assert!(output.contains("src=example")); |
| 790 | assert_eq!(document.select(&Selector::parse(".markdown a").unwrap()).count(),0); |
| 791 | let html = "<body id=page-log><table><tbody><tr><td title='known@example.test'><img src=old><span>Author &amp; name</span></td></tr><tr><td title='other@example.test'><img src=unknown><span>Other</span></td></tr></tbody></table></body>"; |
| 792 | let output = rewrite(html,&origin,&profiles,&HashMap::new(),None).unwrap(); |
| 793 | assert!(output.contains("Author &amp; name")); |
| 794 | let document = Html::parse_document(&output); |
| 795 | assert_eq!(document.select(&Selector::parse("a[href='/~clover']").unwrap()).count(),1); |
| 796 | assert_eq!(document.select(&Selector::parse("img[src='https://snow.test/picture']").unwrap()).count(),1); |
| 797 | assert_eq!(document.select(&Selector::parse("img[src='https://snow.test/picture'][width='14'][height='14']").unwrap()).count(),1); |
| 798 | } |
| 799 | |
| 800 | #[test] |
| 801 | fn navigation_uses_the_current_repository_count() { |
| 802 | let html = "<header><ul class='usa-nav__primary'><li><a href='/chat/issues/'>issues (9)</a></li></ul></header><div class='markdown'><a href='/chat/issues/'>issues (9)</a></div>"; |
| 803 | let origin = url::Url::parse("https://shale.paperclover.net/chat/issues/25").unwrap(); |
| 804 | let output = rewrite(html, &origin, &HashMap::new(), &HashMap::new(), Some(8)).unwrap(); |
| 805 | assert!(output.contains(">issues (8)</a>")); |
| 806 | assert!(output.contains("<div class='markdown'><a href='/chat/issues/'>issues (9)</a></div>")); |
| 807 | } |
| 808 | |
| 809 | #[test] |
| 810 | fn reconstructed_markdown_anchors_cannot_relabel_humans() { |
| 811 | let html = "<div class='markdown'><b><a href='https://example.com'>external</b>again</a></div><a href='/~ai'>~ai</a><a href='/~clo'>~clo</a>"; |
| 812 | let origin = url::Url::parse("https://shale.paperclover.net/chat/issues/25").unwrap(); |
| 813 | let output = rewrite(html, &origin, &HashMap::new(), &HashMap::new(), None).unwrap(); |
| 814 | assert!(output.contains("<a href='/~clo'>~clo</a>")); |
| 815 | assert!(output.contains("</svg>ai")); |
| 816 | } |
| 817 | |
| 818 | #[test] |
| 819 | fn terminal_trailers_ignore_examples_and_unclosed_fences() { |
| 820 | assert_eq!(assisted_model("Body\n\nAssisted-by: gpt-6-astra\n"), Some("gpt-6-astra")); |
| 821 | for body in ["Assisted-by: <model-id>", "> Assisted-by: claude-opus-5.5", " Assisted-by: gpt-6-astra", "```\nAssisted-by: gpt-6-astra", "~~~~\n```\nAssisted-by: gpt-6-astra", "Assisted-by: gpt-6-astra\nMore text", "Assisted-by: gpt-6-astra extra"] { |
| 822 | assert_eq!(assisted_model(body), None, "{body}"); |
| 823 | } |
| 824 | assert_eq!(assisted_model("```\nexample\n```\n\nAssisted-by: claude-opus-5.5"), Some("claude-opus-5.5")); |
| 825 | } |
| 826 | |
| 827 | #[test] |
| 828 | fn ai_identity_is_durable_and_never_reads_comment_trailers() { |
| 829 | let origin = url::Url::parse("https://shale.paperclover.net/chat/issues/25").unwrap(); |
| 830 | let mut opus = ai_profile(Some("claude-opus-5.5")); |
| 831 | opus.owner = Some(("clover".into(), Some("https://example.com/picture?a=1&b=2".into()))); |
| 832 | let mut sol = ai_profile(Some("gpt-6.1-sol")); |
| 833 | sol.owner = Some(("clover".into(), None)); |
| 834 | let profiles = HashMap::from([("ai-clover-claude-opus-5.5".into(), opus), ("ai-clover-gpt-6.1-sol".into(), sol)]); |
| 835 | let html = "<a href='/~ai-clover-claude-opus-5.5'><img src='old'><span>ai account</span></a><a href='/~ai-clover-gpt-6.1-sol'>ai account</a><div class='markdown'><a href='/~ai-clover-claude-opus-5.5'>Mention</a><p>Assisted-by: unknown-model</p></div><a href='/~clover'>~clover</a>"; |
| 836 | let output = rewrite(html, &origin, &profiles, &HashMap::new(), None).unwrap(); |
| 837 | let document = Html::parse_document(&output); |
| 838 | assert!(output.contains("Opus 5.5</span> <small")); |
| 839 | assert!(output.contains(">(on behalf of <a class=")); |
| 840 | assert!(output.contains("GPT-6.1 Sol</span> <small")); |
| 841 | assert!(!output.contains("src=\"old\"")); |
| 842 | assert!(output.contains("<a href='/~clover'>~clover</a>")); |
| 843 | assert!(output.contains(">Mention</a>")); |
| 844 | assert_eq!(document.select(&Selector::parse("a[href='/~clover']").unwrap()).count(), 3); |
| 845 | assert!(document.select(&Selector::parse("svg").unwrap()).all(|e| e.attr("aria-hidden") == Some("true") && e.attr("focusable") == Some("false"))); |
| 846 | assert_eq!(ai_profile(Some("gpt-6.1-sol")).name, "GPT-6.1 Sol"); |
| 847 | assert_eq!(ai_profile(Some("claude-opus-5.5")).name, "Opus 5.5"); |
| 848 | assert_eq!(ai_profile(Some("claude-haiku-5-5")).name, "Haiku 5.5"); |
| 849 | assert_eq!(ai_profile(Some("claude-haiku-5.5")).name, "Haiku 5.5"); |
| 850 | assert_eq!(ai_profile(Some("claude-sonnet-4-5-20250929")).name, "Sonnet 4.5"); |
| 851 | for (id, name) in [ |
| 852 | ("claude-new-variant-5-5", "New Variant 5.5"), |
| 853 | ("gpt-6-1-new-variant", "GPT-6.1 New Variant"), |
| 854 | ("qwen-3.8-27b", "Qwen 3.8 27b"), |
| 855 | ("qwen3.8-27b", "Qwen 3.8 27b"), |
| 856 | ("big-pickle-2", "Big Pickle 2"), |
| 857 | ("llama-3-70", "Llama 3 70"), |
| 858 | ] { assert_eq!(ai_profile(Some(id)).name, name, "{id}"); } |
| 859 | } |
| 860 | |
| 861 | #[test] |
| 862 | fn compact_authors_keep_native_owner_links_and_avatars() { |
| 863 | let origin = url::Url::parse("https://shale.paperclover.net/chat/issues/").unwrap(); |
| 864 | let mut profile = ai_profile(Some("gpt-6.1-sol")); |
| 865 | profile.owner = Some(("clover".into(), Some("https://snowglobe.test/picture".into()))); |
| 866 | let profiles = HashMap::from([("ai-clover-gpt-6.1-sol".into(), profile)]); |
| 867 | let avatars = HashMap::from([("native-clover".into(), "https://snowglobe.test/picture".into())]); |
| 868 | let html = "<body id='page-issues'><table><tr><td>1</td><td>Task</td><td>Todo</td><td><a class='usa-link va-middle-childs' href='/~ai-clover-gpt-6.1-sol'>ai</a></td></tr></table><a class='usa-link va-middle-childs' href='/~clover'><img height='16' src='https://api.dicebear.com/10.x/line-face/svg?seed=native-clover'><span>~clover</span></a><div class='markdown'><img src='https://api.dicebear.com/10.x/line-face/svg?seed=native-clover'></div>"; |
| 869 | let output = rewrite(html, &origin, &profiles, &avatars, None).unwrap(); |
| 870 | let document = Html::parse_document(&output); |
| 871 | let author = document.select(&Selector::parse("td:nth-child(4)").unwrap()).next().unwrap(); |
| 872 | assert_eq!(author.text().collect::<String>().trim(), "GPT (~clover)"); |
| 873 | assert!(!output.contains("on behalf of")); |
| 874 | let owners: Vec<_> = document.select(&Selector::parse("a[href='/~clover']").unwrap()).collect(); |
| 875 | assert_eq!(owners.len(), 2); |
| 876 | for owner in owners { |
| 877 | assert_eq!(owner.attr("class"), Some("usa-link va-middle-childs")); |
| 878 | let image = owner.select(&Selector::parse("img").unwrap()).next().unwrap(); |
| 879 | assert_eq!(image.attr("src"), Some("https://snowglobe.test/picture")); |
| 880 | assert_eq!(image.attr("height"), Some("16")); |
| 881 | assert_eq!(image.attr("style"), None); |
| 882 | assert_eq!(image.attr("alt"), Some("")); |
| 883 | } |
| 884 | assert!(output.contains("<div class='markdown'><img src='https://api.dicebear.com/10.x/line-face/svg?seed=native-clover'>")); |
| 885 | for (model, short) in [("qwen3.8-27b", "Qwen"), ("big-pickle-2", "Big Pickle"), ("claude-new-variant-5-5", "New Variant")] { |
| 886 | let mut profile = ai_profile(Some(model)); |
| 887 | profile.owner = Some(("clover".into(), None)); |
| 888 | let profiles = HashMap::from([("ai-model".into(), profile)]); |
| 889 | let html = "<body id='page-issues'><div class='grid-container'><table><tr><td>1</td><td>Task</td><td>Todo</td><td><a href='/~ai-model'>ai</a></td></tr></table></div></body>"; |
| 890 | let output = rewrite(html, &origin, &profiles, &HashMap::new(), None).unwrap(); |
| 891 | let document = Html::parse_document(&output); |
| 892 | assert_eq!(document.select(&Selector::parse("td:nth-child(4)").unwrap()).next().unwrap().text().collect::<String>().trim(), format!("{short} (~clover)")); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | #[test] |
| 897 | fn rewrites_only_known_local_account_links_and_escapes_provider_names() { |
| 898 | let origin = url::Url::parse("https://shale.paperclover.net/").unwrap(); |
| 899 | let profiles = HashMap::from([ |
| 900 | ( |
| 901 | "guest-github-123".into(), |
| 902 | Profile { |
| 903 | name: "<&\"clover".into(), |
| 904 | url: Some("https://github.com/clover".into()), |
| 905 | icon: include_str!("../web/sso/github.svg"), |
| 906 | owner: None, |
| 907 | picture: Some("https://avatars.githubusercontent.com/u/123?s=64".into()), |
| 908 | }, |
| 909 | ), |
| 910 | ( |
| 911 | "guest-astheno-abc".into(), |
| 912 | Profile { |
| 913 | name: "Astheno user".into(), |
| 914 | url: None, |
| 915 | icon: include_str!("astheno.svg"), |
| 916 | owner: None, |
| 917 | picture: None, |
| 918 | }, |
| 919 | ), |
| 920 | ]); |
| 921 | let html = r#"<a class="usa-nav-link" href="/~guest-github-123"><img src="avatar"><span>~guest-github-123</span></a><a href="https://shale.paperclover.net/~guest-astheno-abc/"><img src="astheno-avatar" alt="avatar">guest</a><a href="/~guest-github-unknown">unknown</a><a href="https://other.example/~guest-github-123">external</a><a href="/~guest-github-123/repo">repo</a><pre>~guest-github-123</pre>"#; |
| 922 | let rewritten = rewrite(html, &origin, &profiles, &HashMap::new(), None).unwrap(); |
| 923 | let page = Html::parse_document(&rewritten); |
| 924 | let links: Vec<_> = page.select(&Selector::parse("body > a").unwrap()).collect(); |
| 925 | assert_eq!(links[0].attr("target"), Some("_blank")); |
| 926 | assert_eq!(links[0].attr("rel"), Some("noreferrer")); |
| 927 | assert!(links[0].attr("style").unwrap().contains("text-decoration:none")); |
| 928 | assert!(links[0].select(&Selector::parse("span[style]").unwrap()).next().unwrap().attr("style").unwrap().contains("text-decoration:underline")); |
| 929 | assert_eq!(links[1].value().name(), "a"); |
| 930 | assert_eq!(links[1].attr("target"), None); |
| 931 | assert_eq!(links[1].attr("rel"), None); |
| 932 | assert_eq!(links[1].text().collect::<String>().trim(), "Astheno user"); |
| 933 | for link in &links[..2] { |
| 934 | assert_eq!( |
| 935 | link.select(&Selector::parse("svg[aria-hidden=true]").unwrap()) |
| 936 | .count(), |
| 937 | 1 |
| 938 | ); |
| 939 | } |
| 940 | assert_eq!( |
| 941 | links[1] |
| 942 | .select(&Selector::parse("img").unwrap()) |
| 943 | .next() |
| 944 | .unwrap() |
| 945 | .attr("src"), |
| 946 | Some("astheno-avatar") |
| 947 | ); |
| 948 | assert_eq!(links[0].select(&Selector::parse("img").unwrap()).count(), 1); |
| 949 | assert_eq!( |
| 950 | links[0] |
| 951 | .select(&Selector::parse("img").unwrap()) |
| 952 | .next() |
| 953 | .unwrap() |
| 954 | .attr("src"), |
| 955 | Some("https://avatars.githubusercontent.com/u/123?s=64") |
| 956 | ); |
| 957 | assert_eq!(links[0].text().collect::<String>().trim(), "<&\"clover"); |
| 958 | assert_eq!(links[0].attr("class"), Some("usa-nav-link")); |
| 959 | assert_eq!(links[1].attr("href"), None); |
| 960 | for link in &links[2..] { |
| 961 | assert_eq!(link.attr("target"), None); |
| 962 | } |
| 963 | assert!(rewritten.contains("<pre>~guest-github-123</pre>")); |
| 964 | assert_eq!(account_path("/~guest-github-123//"), None); |
| 965 | } |
| 966 | } |