Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/raycast/local-repo-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export function collectRaycastLocalRepoMetadata(input: {
throw new Error("Raycast branch analysis supports metadata-only mode; source upload mode is rejected.");
}
const git = input.git ?? gitLines;
const baseRef = input.baseRef ?? git(input.cwd, ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"])[0]?.replace(/^origin\//, "") ?? "main";
const rawBaseRef = input.baseRef ?? git(input.cwd, ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"])[0]?.replace(/^origin\//, "") ?? "main";
const baseRef = validateSafeBaseRef(rawBaseRef);
const remoteUrl = git(input.cwd, ["config", "--get", "remote.origin.url"])[0] ?? "";
const repoFullName = input.repoFullName ?? parseGitHubRemote(remoteUrl);
if (!repoFullName) throw new Error("Could not infer repoFullName from the git remote; pass repoFullName explicitly.");
Expand Down Expand Up @@ -200,6 +201,13 @@ async function postJson(
return payload;
}

function validateSafeBaseRef(baseRef: string): string {
if (!baseRef || baseRef.startsWith("-")) {
throw new Error("Unsafe git baseRef; pass a branch or ref name that does not begin with '-'.");
}
return baseRef;
}

function collectChangedFiles(cwd: string, baseRef: string, git: RaycastGitRunner): RaycastChangedFileMetadata[] {
const numstat = new Map(parseNumstat(cwd, baseRef, git).map((entry) => [entry.path, entry]));
return git(cwd, ["diff", "--name-status", "-M", baseRef, "--"]).map((row) => {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/raycast-local-repo-analyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@ describe("Raycast local repo analyzer", () => {
expect(calls.map((call) => call.split(" ")[0]).join("\n")).not.toMatch(/^(cat|show|grep|archive)$/m);
});

it("rejects inferred base refs that could be parsed as git options", () => {
const { git, calls } = fakeGit({
"symbolic-ref --short refs/remotes/origin/HEAD": "origin/--output=/tmp/gittensory-owned\n",
});

expect(() =>
collectRaycastLocalRepoMetadata({
cwd: "/tmp/private-checkout",
login: "jsonbored",
repoFullName: "JSONbored/gittensory",
git,
}),
).toThrow(/unsafe git baseRef/i);
expect(calls).toEqual(["symbolic-ref --short refs/remotes/origin/HEAD"]);
});

it("rejects explicit base refs that could be parsed as git options", () => {
const git = vi.fn<RaycastGitRunner>();

expect(() =>
collectRaycastLocalRepoMetadata({
cwd: "/tmp/private-checkout",
login: "jsonbored",
repoFullName: "JSONbored/gittensory",
baseRef: "--output=/tmp/gittensory-owned",
git,
}),
).toThrow(/unsafe git baseRef/i);
expect(git).not.toHaveBeenCalled();
});

it("rejects source upload mode before running git", () => {
const git = vi.fn<RaycastGitRunner>();

Expand Down