Skip to content
Draft
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
9 changes: 9 additions & 0 deletions packages/cli-kit/src/public/node/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,13 @@ describe('removeGitRemote()', () => {
expect(mockedExeca).toHaveBeenCalledWith('git', ['remote'], {cwd: directory})
expect(mockedExeca).not.toHaveBeenCalledWith('git', ['remote', 'remove', remoteName], {cwd: directory})
})

test('throws an error if remote name starts with a hyphen', async () => {
const directory = '/test/directory'

await expect(git.removeGitRemote(directory, '-invalid-remote')).rejects.toThrowError(
/Invalid remote name: -invalid-remote. Remote names can't start with a hyphen./,
)
expect(mockedExeca).not.toHaveBeenCalled()
})
})
5 changes: 5 additions & 0 deletions packages/cli-kit/src/public/node/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ export async function getLatestTag(directory?: string): Promise<string | undefin
* @returns A promise that resolves when the remote is removed.
*/
export async function removeGitRemote(directory: string, remoteName = 'origin'): Promise<void> {
// Guard against command/argument injection attacks if remoteName starts with '-'
if (remoteName.startsWith('-')) {
throw new AbortError(`Invalid remote name: ${remoteName}. Remote names can't start with a hyphen.`)
}

outputDebug(outputContent`Removing git remote ${remoteName} from ${outputToken.path(directory)}...`)
await ensureGitIsPresentOrAbort()

Expand Down
Loading