Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix(runtime): escape # ? and % in a local app's home address

Tw93b2e4e839754b
diff --git a/src-tauri/src/app/local_site.rs b/src-tauri/src/app/local_site.rs
index c3f049ef..fb99d567 100644
--- a/src-tauri/src/app/local_site.rs
+++ b/src-tauri/src/app/local_site.rs
@@ -13,9 +13,6 @@ use std::path::{Component, Path, PathBuf};
use tauri::utils::assets::{AssetKey, AssetsIter, CspHash};
use tauri::{Assets, Runtime};
-/// `raw` as a path of plain names, or None when it is empty, absolute, or
-/// steps out with `..`. Asset keys arrive percent-decoded, so a request for
-/// `/%2e%2e/secret` reaches this as `../secret`.
/// The entry page as the path a `WebviewUrl::App` joins onto the app's origin.
/// That join reads `#` as the start of a fragment and `?` as a query, so
/// `chart #1.html` would open `chart `; escaped here, they reach the asset
@@ -28,6 +25,9 @@ pub fn entry_path(file: &str) -> PathBuf {
)
}
+/// `raw` as a path of plain names, or None when it is empty, absolute, or
+/// steps out with `..`. Asset keys arrive percent-decoded, so a request for
+/// `/%2e%2e/secret` reaches this as `../secret`.
pub fn relative_path(raw: &str) -> Option<PathBuf> {
let mut path = PathBuf::new();
for component in Path::new(raw.trim_start_matches(['/', '\\'])).components() {
diff --git a/src-tauri/src/app/navigation.rs b/src-tauri/src/app/navigation.rs
index a2e94836..9efca1f3 100644
--- a/src-tauri/src/app/navigation.rs
+++ b/src-tauri/src/app/navigation.rs
@@ -27,7 +27,8 @@ pub fn resolve_home_url(url_type: &str, url: &str) -> Option<tauri::Url> {
let base = "http://tauri.localhost";
#[cfg(not(windows))]
let base = "tauri://localhost";
- tauri::Url::parse(&format!("{base}/{url}")).ok()
+ let entry = crate::app::local_site::entry_path(url);
+ tauri::Url::parse(&format!("{base}/{}", entry.to_string_lossy())).ok()
}
_ => None,
}
@@ -207,6 +208,18 @@ mod tests {
assert!(!goes_home("pake-popup-3"), "a sign-in popup keeps its page");
}
+ #[test]
+ fn a_local_home_keeps_hash_question_and_percent_in_the_file_name() {
+ #[cfg(not(windows))]
+ let base = "tauri://localhost";
+ #[cfg(windows)]
+ let base = "http://tauri.localhost";
+ let url = resolve_home_url("local", "chart #1?100%.html").unwrap();
+ assert_eq!(url.as_str(), format!("{base}/chart%20%231%3F100%25.html"));
+ assert_eq!(url.fragment(), None);
+ assert_eq!(url.query(), None);
+ }
+
#[test]
fn a_home_url_with_a_path_keeps_it() {
assert_eq!(
diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js
index 721afd2a..4f3674df 100644
--- a/src-tauri/src/inject/event.js
+++ b/src-tauri/src/inject/event.js
@@ -38,7 +38,10 @@ function packagedHomeUrl() {
const base = /win/i.test(getDesktopPlatform())
? "http://tauri.localhost/"
: "tauri://localhost/";
- return base + url;
+ // Same escaping as local_site::entry_path, so `chart #1.html` stays one file name.
+ return (
+ base + url.replace(/%/g, "%25").replace(/#/g, "%23").replace(/\?/g, "%3F")
+ );
}
const shortcuts = {
diff --git a/tests/unit/event-link-guard.test.js b/tests/unit/event-link-guard.test.js
index c58dffcc..77307052 100644
--- a/tests/unit/event-link-guard.test.js
+++ b/tests/unit/event-link-guard.test.js
@@ -1400,6 +1400,30 @@ describe("event link guard", () => {
});
});
+describe("packagedHomeUrl for local apps", () => {
+ it.each([
+ ["Mozilla/5.0 (Macintosh; Intel Mac OS X 15_5)", "tauri://localhost/"],
+ ["Mozilla/5.0 (Windows NT 10.0; Win64; x64)", "http://tauri.localhost/"],
+ ])("escapes #, ? and %% in the entry file name on %s", (userAgent, base) => {
+ const context = loadEventHelpers({ userAgent });
+ context.window.pakeConfig = {
+ url: "chart #1?100%.html",
+ url_type: "local",
+ };
+ const home = context.packagedHomeUrl();
+ expect(home).toBe(`${base}chart #1%3F100%25.html`.replace("#", "%23"));
+ const parsed = new URL(home.replace("tauri://", "http://"));
+ expect(parsed.hash).toBe("");
+ expect(parsed.search).toBe("");
+ });
+
+ it("returns a web app's url unchanged", () => {
+ const context = loadEventHelpers();
+ context.window.pakeConfig = { url: "https://example.com/a?b#c" };
+ expect(context.packagedHomeUrl()).toBe("https://example.com/a?b#c");
+ });
+});
+
describe("getFilenameFromUrl data URI extension", () => {
it("maps a structured image subtype to a real extension (svg+xml -> svg)", () => {
const context = loadEventHelpers();