Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): validate custom server URLs before saving

Aditya Garud8b81ec42c8c4
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index cd7dd5b65..fb1800cbc 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -4621,13 +4621,11 @@ const ZOOM_MAX: f32 = 4.5;
/// origin before storing it, and throws (leaving the setting alone) when it
/// does not parse.
fn origin_of(value: &str) -> Option<String> {
- let value = value.trim();
- let (scheme, rest) = value.split_once("://")?;
- if !matches!(scheme, "http" | "https") || rest.is_empty() {
+ let url = reqwest::Url::parse(value.trim()).ok()?;
+ if !matches!(url.scheme(), "http" | "https") {
return None;
}
- let authority = rest.split(['/', '?', '#']).next()?;
- (!authority.is_empty()).then(|| format!("{scheme}://{authority}"))
+ Some(url.origin().ascii_serialization())
}
/// The preview under the project-name template.
@@ -4684,6 +4682,37 @@ mod tests {
assert_eq!(origin_of(""), None);
}
+ #[test]
+ fn invalid_server_authorities_are_rejected_before_saving() {
+ for value in [
+ "https://cap.example.com:invalid",
+ "https://cap.example.com:65536",
+ "https://cap example.com",
+ "http://[::1",
+ "https://user@",
+ ] {
+ assert_eq!(origin_of(value), None, "{value}");
+ }
+ }
+
+ #[test]
+ fn server_origins_are_normalised_for_api_requests() {
+ for (value, expected) in [
+ ("https://cap.example.com/", "https://cap.example.com"),
+ (
+ "https://user:[email protected]/dashboard?x=1#top",
+ "https://cap.example.com",
+ ),
+ (" HTTPS://CAP.EXAMPLE.COM:443/ ", "https://cap.example.com"),
+ ("http://localhost:3000/path", "http://localhost:3000"),
+ ("http://[::1]:3000/path", "http://[::1]:3000"),
+ ] {
+ let origin = origin_of(value).expect(value);
+ assert_eq!(origin, expected);
+ assert!(reqwest::Url::parse(&format!("{origin}/api/desktop/feedback")).is_ok());
+ }
+ }
+
#[test]
fn account_name_matches_the_tauri_profile_fallbacks() {
let mut profile = UserProfile {