mirror of
https://github.com/ollama/ollama.git
synced 2026-01-19 04:51:17 -05:00
Compare commits
2 Commits
parth/decr
...
hoyyeva/de
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e67495a0f9 | ||
|
|
fdc36c8e3c |
@@ -434,37 +434,31 @@ func openInBrowser(url string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseURLScheme parses an ollama:// URL and returns whether it's a connect URL and the UI path
|
// parseURLScheme parses an ollama:// URL and validates it
|
||||||
func parseURLScheme(urlSchemeRequest string) (isConnect bool, uiPath string, err error) {
|
// Supports: ollama:// (open app) and ollama://connect (OAuth)
|
||||||
|
func parseURLScheme(urlSchemeRequest string) (isConnect bool, err error) {
|
||||||
parsedURL, err := url.Parse(urlSchemeRequest)
|
parsedURL, err := url.Parse(urlSchemeRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, "", err
|
return false, fmt.Errorf("invalid URL: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this is a connect URL
|
// Check if this is a connect URL
|
||||||
if parsedURL.Host == "connect" || strings.TrimPrefix(parsedURL.Path, "/") == "connect" {
|
if parsedURL.Host == "connect" || strings.TrimPrefix(parsedURL.Path, "/") == "connect" {
|
||||||
return true, "", nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the UI path
|
// Allow bare ollama:// or ollama:/// to open the app
|
||||||
path := "/"
|
if (parsedURL.Host == "" && parsedURL.Path == "") || parsedURL.Path == "/" {
|
||||||
if parsedURL.Path != "" && parsedURL.Path != "/" {
|
return false, nil
|
||||||
// For URLs like ollama:///settings, use the path directly
|
|
||||||
path = parsedURL.Path
|
|
||||||
} else if parsedURL.Host != "" {
|
|
||||||
// For URLs like ollama://settings (without triple slash),
|
|
||||||
// the "settings" part is parsed as the host, not the path.
|
|
||||||
// We need to convert it to a path by prepending "/"
|
|
||||||
// This also handles ollama://settings/ where Windows adds a trailing slash
|
|
||||||
path = "/" + parsedURL.Host
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, path, nil
|
// Reject everything else (arbitrary paths, injection attempts)
|
||||||
|
return false, fmt.Errorf("unsupported ollama:// URL path: %s", urlSchemeRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleURLSchemeInCurrentInstance processes URL scheme requests in the current instance
|
// handleURLSchemeInCurrentInstance processes URL scheme requests in the current instance
|
||||||
func handleURLSchemeInCurrentInstance(urlSchemeRequest string) {
|
func handleURLSchemeInCurrentInstance(urlSchemeRequest string) {
|
||||||
isConnect, uiPath, err := parseURLScheme(urlSchemeRequest)
|
isConnect, err := parseURLScheme(urlSchemeRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to parse URL scheme request", "url", urlSchemeRequest, "error", err)
|
slog.Error("failed to parse URL scheme request", "url", urlSchemeRequest, "error", err)
|
||||||
return
|
return
|
||||||
@@ -473,6 +467,6 @@ func handleURLSchemeInCurrentInstance(urlSchemeRequest string) {
|
|||||||
if isConnect {
|
if isConnect {
|
||||||
handleConnectURLScheme()
|
handleConnectURLScheme()
|
||||||
} else {
|
} else {
|
||||||
sendUIRequestMessage(uiPath)
|
sendUIRequestMessage("/")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ func (app *appCallbacks) HandleURLScheme(urlScheme string) {
|
|||||||
|
|
||||||
// handleURLSchemeRequest processes URL scheme requests from other instances
|
// handleURLSchemeRequest processes URL scheme requests from other instances
|
||||||
func handleURLSchemeRequest(urlScheme string) {
|
func handleURLSchemeRequest(urlScheme string) {
|
||||||
isConnect, uiPath, err := parseURLScheme(urlScheme)
|
isConnect, err := parseURLScheme(urlScheme)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to parse URL scheme request", "url", urlScheme, "error", err)
|
slog.Error("failed to parse URL scheme request", "url", urlScheme, "error", err)
|
||||||
return
|
return
|
||||||
@@ -147,7 +147,7 @@ func handleURLSchemeRequest(urlScheme string) {
|
|||||||
if isConnect {
|
if isConnect {
|
||||||
handleConnectURLScheme()
|
handleConnectURLScheme()
|
||||||
} else {
|
} else {
|
||||||
sendUIRequestMessage(uiPath)
|
sendUIRequestMessage("/")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -466,6 +466,8 @@ func (w *Webview) Run(path string) unsafe.Pointer {
|
|||||||
w.webview = wv
|
w.webview = wv
|
||||||
w.webview.Navigate(url)
|
w.webview.Navigate(url)
|
||||||
} else {
|
} else {
|
||||||
|
// Path has been validated by parseURLScheme() to only contain
|
||||||
|
// allowed paths, so it's safe to use directly
|
||||||
w.webview.Eval(fmt.Sprintf(`
|
w.webview.Eval(fmt.Sprintf(`
|
||||||
history.pushState({}, '', '%s');
|
history.pushState({}, '', '%s');
|
||||||
`, path))
|
`, path))
|
||||||
|
|||||||
@@ -2978,7 +2978,12 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string js = "history.pushState({}, '', '" + path + "'); window.dispatchEvent(new PopStateEvent('popstate'));";
|
// Safely encode the path for JavaScript using JSON encoding
|
||||||
|
// This handles all special characters: quotes, newlines, backslashes, etc.
|
||||||
|
// json_escape adds quotes around the string and escapes all special chars
|
||||||
|
std::string path_json = detail::json_escape(path, true);
|
||||||
|
|
||||||
|
std::string js = "history.pushState({}, '', " + path_json + "); window.dispatchEvent(new PopStateEvent('popstate'));";
|
||||||
std::wstring wjs = widen_string(js);
|
std::wstring wjs = widen_string(js);
|
||||||
sender->ExecuteScript(wjs.c_str(), nullptr);
|
sender->ExecuteScript(wjs.c_str(), nullptr);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user