Skip to content

Commit

Permalink
Allow stashing of redirected URLs
Browse files Browse the repository at this point in the history
If the user unstashes and then re-stashes a privileged URL, the internal
Tab Stash redirection page might get saved instead of the actual URL the
user wants.  Check for and explicitly allow (re-)stashing of redirection
pages, and save the original URL instead.

This has the added benefit that we don't put Tab Stash-specific
droppings in the user's bookmarks, either.
  • Loading branch information
josh-berry committed Sep 3, 2023
1 parent c7a2653 commit bc18539
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
textMatcher,
tryAgain,
urlToOpen,
urlToStash,
} from "../util";
import {trace_fn} from "../util/debug";
import {logError, logErrorsFrom, UserError} from "../util/oops";
Expand Down Expand Up @@ -282,8 +283,8 @@ export class Model {
return false;
}

// Tab Stash URLs are never stashable.
return !url_str.startsWith(browser.runtime.getURL(""));
// The Tab Stash UI is never stashable.
return !url_str.startsWith(browser.runtime.getURL("stash-list.html"));
}

/** If the topmost folder in the stash root is an unnamed folder which was
Expand Down Expand Up @@ -692,7 +693,7 @@ export class Model {
"url" in item
? await this.bookmarks.create({
title: item.title || item.url,
url: item.url,
url: urlToStash(item.url),
parentId,
index,
})
Expand Down
11 changes: 11 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ export function urlToOpen(urlstr: string): OpenableURL {
}
}

/** Does the inverse of urlToOpen() -- turns a possibly-redirected URL back into
* its original form. */
export function urlToStash(url: string | OpenableURL): string {
if (url.startsWith(REDIR_PAGE + "?")) {
const redirUrl = new URL(url);
const origUrl = redirUrl.searchParams.get("url");
if (origUrl) return origUrl;
}
return url;
}

function redirUrl(url: string): OpenableURL {
return `${REDIR_PAGE}?url=${encodeURIComponent(url)}` as OpenableURL;
}
Expand Down

0 comments on commit bc18539

Please sign in to comment.