im-web: recognize Chromium agents (token precedes Chrome); precedence test
Browse files at this commitdf6210864ce951962d413af576a38fc1bf88985fParent: f465b361 changed file +26 -2
- crates/im-web/src/pages.rs +26 -2
crates/im-web/src/pages.rsmodified+26 -2
| @@ -491,8 +491,9 @@ pub(crate) fn escape(raw: &str) -> String { | |||
| 491 | 491 | ||
| 492 | /// What the agent string means to a person — "Chrome on Linux", not the raw | 492 | /// What the agent string means to a person — "Chrome on Linux", not the raw |
| 493 | /// header. The full string stays one click away in the session's detail. | 493 | /// header. The full string stays one click away in the session's detail. |
| 494 | -/// Substring matching in precedence order (Edge before Chrome before Safari: | 494 | +/// Substring matching in precedence order (Edge, then Chromium before Chrome, |
| 495 | -/// each carries the later one's token too); shared with the admin panel. | 495 | +/// then Safari: each carries the later one's token too); shared with the |
| 496 | +/// admin panel. | ||
| 496 | /// Browser and system names are proper nouns and stay as the agent spelled | 497 | /// Browser and system names are proper nouns and stay as the agent spelled |
| 497 | /// them; the connector and the generic fallback follow the viewer's language | 498 | /// them; the connector and the generic fallback follow the viewer's language |
| 498 | /// (`i18n::device_summary`, `Key::BrowserWord`). | 499 | /// (`i18n::device_summary`, `Key::BrowserWord`). |
| @@ -504,6 +505,8 @@ pub(crate) fn device_label(agent: Option<&str>, lang: Lang) -> String { | |||
| 504 | "Edge" | 505 | "Edge" |
| 505 | } else if agent.contains("Firefox/") { | 506 | } else if agent.contains("Firefox/") { |
| 506 | "Firefox" | 507 | "Firefox" |
| 508 | + } else if agent.contains("Chromium/") { | ||
| 509 | + "Chromium" | ||
| 507 | } else if agent.contains("Chrome/") { | 510 | } else if agent.contains("Chrome/") { |
| 508 | "Chrome" | 511 | "Chrome" |
| 509 | } else if agent.contains("Safari/") { | 512 | } else if agent.contains("Safari/") { |
| @@ -1128,3 +1131,24 @@ async fn email_change_page(cx: &Cx) -> Result<Response> { | |||
| 1128 | .await? | 1131 | .await? |
| 1129 | .into_response(cx) | 1132 | .into_response(cx) |
| 1130 | } | 1133 | } |
| 1134 | + | ||
| 1135 | +#[cfg(test)] | ||
| 1136 | +mod tests { | ||
| 1137 | + use super::device_label; | ||
| 1138 | + use crate::i18n::Lang; | ||
| 1139 | + | ||
| 1140 | + // Token overlap is the bug class: a Chromium agent also carries | ||
| 1141 | + // "Chrome/", an Edge agent carries both — the match order is the | ||
| 1142 | + // contract, so pin it with real-world agent strings. | ||
| 1143 | + #[test] | ||
| 1144 | + fn device_label_respects_token_precedence() { | ||
| 1145 | + let chromium = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chromium/140.0.0.0 Chrome/140.0.0.0 Safari/537.36"; | ||
| 1146 | + assert_eq!(device_label(Some(chromium), Lang::En), "Chromium on Linux"); | ||
| 1147 | + let chrome = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36"; | ||
| 1148 | + assert_eq!(device_label(Some(chrome), Lang::En), "Chrome on Linux"); | ||
| 1149 | + let edge = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0"; | ||
| 1150 | + assert_eq!(device_label(Some(edge), Lang::En), "Edge on Windows"); | ||
| 1151 | + let safari = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1"; | ||
| 1152 | + assert_eq!(device_label(Some(safari), Lang::En), "Safari on iPhone"); | ||
| 1153 | + } | ||
| 1154 | +} | ||