Skip to content

Commit ff66e5c

Browse files
committed
fix(web): reject URL delimiters in update repository segments
1 parent cc7d40e commit ff66e5c

4 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎apps/web/src/components/desktopUpdate.logic.test.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,19 @@ describe("desktop update UI helpers", () => {
215215
);
216216
});
217217

218-
it("falls back to the upstream repository for an invalid configured repository", () => {
219-
expect(getDesktopUpdateReleaseHistoryUrl("owner/repo/extra")).toBe(
218+
it.each([
219+
"owner/repo/extra",
220+
"owner#notes/repo",
221+
"owner/repo#notes",
222+
"owner?tab=notes/repo",
223+
"owner/repo?tab=notes",
224+
])("falls back to upstream for an invalid configured repository: %s", (repository) => {
225+
expect(getDesktopUpdateReleaseHistoryUrl(repository)).toBe(
220226
"https://github.com/pingdotgg/t3code/releases",
221227
);
228+
expect(getDesktopUpdateReleaseUrl("1.2.3", repository)).toBe(
229+
"https://github.com/pingdotgg/t3code/releases/tag/v1.2.3",
230+
);
222231
});
223232

224233
it("builds the nightly release URL without dropping its version suffix", () => {

‎apps/web/src/components/desktopUpdate.logic.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/con
33
export type DesktopUpdateButtonAction = "download" | "install" | "none";
44

55
const DEFAULT_DESKTOP_RELEASE_REPOSITORY = "pingdotgg/t3code";
6+
const GITHUB_REPOSITORY_SEGMENT = /^[A-Za-z0-9._-]+$/u;
7+
8+
function isValidRepositorySegment(value: string | undefined): value is string {
9+
return (
10+
value !== undefined && value !== "." && value !== ".." && GITHUB_REPOSITORY_SEGMENT.test(value)
11+
);
12+
}
613

714
function desktopReleaseRepositoryUrl(repository: string | undefined): string {
815
const configuredRepository = repository?.trim() ?? "";
916
const [owner, name, ...extra] = configuredRepository.split("/");
1017
const releaseRepository =
11-
owner && name && extra.length === 0 ? configuredRepository : DEFAULT_DESKTOP_RELEASE_REPOSITORY;
18+
isValidRepositorySegment(owner) && isValidRepositorySegment(name) && extra.length === 0
19+
? configuredRepository
20+
: DEFAULT_DESKTOP_RELEASE_REPOSITORY;
1221
return `https://github.com/${releaseRepository}/releases`;
1322
}
1423

‎apps/web/src/components/desktopUpdate.toast.test.tsx‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isValidElement, type ReactElement, type ReactNode } from "react";
2-
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
33
import type { DesktopUpdateState } from "@t3tools/contracts";
44

55
const testState = vi.hoisted(() => ({
@@ -64,6 +64,24 @@ describe("showDesktopUpdateDownloadedToast", () => {
6464
testState.addToast.mockReset();
6565
});
6666

67+
afterEach(() => {
68+
vi.unstubAllEnvs();
69+
});
70+
71+
it("opens release notes from the configured repository", async () => {
72+
vi.stubEnv("T3CODE_DESKTOP_UPDATE_REPOSITORY", "JustYannicc/t3code");
73+
const openExternal = vi.fn().mockResolvedValue(true);
74+
75+
showDesktopUpdateDownloadedToast({ openExternal }, downloadedState());
76+
findReleaseNotesLink(getDescription())?.props.onClick?.();
77+
78+
await vi.waitFor(() => {
79+
expect(openExternal).toHaveBeenCalledWith(
80+
"https://github.com/JustYannicc/t3code/releases/tag/v0.0.30",
81+
);
82+
});
83+
});
84+
6785
it("opens the downloaded version's release notes", async () => {
6886
const openExternal = vi.fn().mockResolvedValue(true);
6987

‎apps/web/src/components/sidebar/SidebarUpdateReleaseNotes.test.tsx‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DesktopUpdateState } from "@t3tools/contracts";
22
import { isValidElement, type MouseEvent, type ReactElement, type ReactNode } from "react";
3-
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
44

55
const testState = vi.hoisted(() => ({
66
addToast: vi.fn(),
@@ -74,6 +74,26 @@ describe("SidebarUpdateReleaseNotes", () => {
7474
testState.addToast.mockReset();
7575
});
7676

77+
afterEach(() => {
78+
vi.unstubAllEnvs();
79+
});
80+
81+
it("uses the configured repository for release links", () => {
82+
vi.stubEnv("T3CODE_DESKTOP_UPDATE_REPOSITORY", "JustYannicc/t3code");
83+
const anchors = collectAnchors(
84+
renderNotes({
85+
...baseState,
86+
releaseNotes: [{ version: "0.0.36-nightly.3", items: ["Change 3"], totalItems: 1 }],
87+
omittedReleaseCount: 1,
88+
}),
89+
);
90+
91+
expect(anchors.map(({ props }) => props.href)).toEqual([
92+
"https://github.com/JustYannicc/t3code/releases/tag/v0.0.36-nightly.3",
93+
"https://github.com/JustYannicc/t3code/releases",
94+
]);
95+
});
96+
7797
it("links each preview to its exact release and labels hidden changes", () => {
7898
const anchors = collectAnchors(
7999
renderNotes({

0 commit comments

Comments
 (0)