Skip to content

Commit

Permalink
Fix escaping of custom links
Browse files Browse the repository at this point in the history
  • Loading branch information
tfedor committed Dec 5, 2024
1 parent ff9553c commit 07fb273
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Settings from "@Options/Data/Settings";
import ProfileLink from "@Content/Modules/Community/ProfileLink.svelte";
import HTML from "@Core/Html/Html";
import UrlUtils from "@Core/Utils/UrlUtils";
export let steamId: string;
export let clear: boolean;
Expand Down Expand Up @@ -55,7 +56,7 @@
{#if customLink.enabled}
<ProfileLink
id="custom"
href={HTML.formatUrl(customLink.url.replace("[ID]", steamId))}
href={UrlUtils.escapeUserUrl(customLink.url, {"ID": steamId})}
iconUrl={customLink.icon ? HTML.formatUrl(customLink.icon) : undefined}>
{customLink.name}
</ProfileLink>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Settings from "@Options/Data/Settings";
import ExtraLink from "@Content/Features/Store/Common/ExtraLinks/ExtraLink.svelte";
import CommonExtraLinks from "@Content/Features/Common/CommonExtraLinks.svelte";
import UrlUtils from "@Core/Utils/UrlUtils";
export let appid: number;
export let communityAppid: number;
Expand Down Expand Up @@ -66,9 +67,7 @@

{#each Settings.app_custom_link as link}
{#if link.enabled}
<ExtraLink href={HTML.formatUrl(link.url
.replace("[NAME]", appName ? encodeURIComponent(appName) : "")
.replace("[ID]", String(appid)))}
<ExtraLink href={UrlUtils.escapeUserUrl(link.url, {"NAME": appName, "ID": String(appid)})}
iconUrl={link.icon ? HTML.formatUrl(link.icon) : null}>
{L(__viewOnWebsite, {"website": link.name})}
</ExtraLink>
Expand Down
21 changes: 21 additions & 0 deletions src/js/Core/Utils/UrlUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

export default class UrlUtils {

public static escapeUserUrl(url: string, replacements: Record<string, string>): string {
const result = new URL(url);

/*
* For some reason
* for (const ... of result.searchParams.entries())
* does not work, and I have no idea why (says it's not iterable)
*/

result.searchParams.forEach((value, name, searchParams) => {
for (const [pattern, replacement] of Object.entries(replacements)) {
value = value.replace(`[${pattern}]`, replacement);
}
searchParams.set(name, value);
});
return result.toString();
}
}

0 comments on commit 07fb273

Please sign in to comment.