Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(server): accept custom-scheme origins (e.g. app://obsidian.md) in CORS trusted hosts

snowingfoxa484359a2b51
diff --git a/src-tauri/utils/src/http.rs b/src-tauri/utils/src/http.rs
index 83fdb1c73..820f7b569 100644
--- a/src-tauri/utils/src/http.rs
+++ b/src-tauri/utils/src/http.rs
@@ -15,6 +15,11 @@ pub fn is_cors_header(header_name: &str) -> bool {
header_lower.starts_with("access-control-")
}
+/// Strips a leading scheme (e.g. "app://", "http://") from a host/origin value.
+fn strip_scheme(value: &str) -> &str {
+ value.split("://").nth(1).unwrap_or(value)
+}
+
/// Validates if host is in trusted hosts list
pub fn is_valid_host(host: &str, trusted_hosts: &[Vec<String>]) -> bool {
if trusted_hosts.iter().any(|hosts| hosts.contains(&"*".to_string())) {
@@ -71,6 +76,9 @@ fn host_without_port(host: &str) -> &str {
fn host_in_trusted_list(host: &str, trusted_hosts: &[Vec<String>]) -> bool {
let host_without_port = host_without_port(host);
trusted_hosts.iter().flatten().any(|valid| {
+ // Users may list a scheme-qualified origin (e.g. "app://obsidian.md")
+ // in Trusted Hosts. Normalize both sides so the bare host matches.
+ let valid = strip_scheme(valid);
let host_lower = host.to_lowercase();
let valid_lower = valid.to_lowercase();
diff --git a/src-tauri/utils/tests/cors_origin.rs b/src-tauri/utils/tests/cors_origin.rs
new file mode 100644
index 000000000..e6e337734
--- /dev/null
+++ b/src-tauri/utils/tests/cors_origin.rs
@@ -0,0 +1,18 @@
+use jan_utils::{extract_host_from_origin, is_valid_host};
+
+// Issue #8608: a custom Electron scheme origin (e.g. app://obsidian.md) must
+// be accepted when the user lists the scheme-qualified origin in Trusted
+// Hosts. The CORS preflight path extracts "obsidian.md" from the origin and
+// compares it against the user's "app://obsidian.md" entry, so the trusted
+// value must be normalized by stripping its scheme.
+#[test]
+fn custom_scheme_origin_in_trusted_hosts_is_accepted() {
+ let trusted = vec![vec!["app://obsidian.md".to_string()]];
+ let origin_host = extract_host_from_origin("app://obsidian.md");
+ assert_eq!(origin_host, "obsidian.md");
+ assert!(is_valid_host(&origin_host, &trusted));
+ // A port-bearing host for the same origin must also match.
+ assert!(is_valid_host("obsidian.md:1337", &trusted));
+ // Untrusted custom-scheme origins are still rejected.
+ assert!(!is_valid_host("evil.com", &trusted));
+}