From 8975e3bee9fc6cf993995593b8d622f230840031 Mon Sep 17 00:00:00 2001 From: Markus Olsson Date: Tue, 10 Dec 2024 09:48:52 +0100 Subject: [PATCH 1/4] Prohibit absolute paths in filepath --- app/src/ui/dispatcher/dispatcher.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/ui/dispatcher/dispatcher.ts b/app/src/ui/dispatcher/dispatcher.ts index 57804ea3306..944852a19f9 100644 --- a/app/src/ui/dispatcher/dispatcher.ts +++ b/app/src/ui/dispatcher/dispatcher.ts @@ -122,6 +122,7 @@ import { UnreachableCommitsTab } from '../history/unreachable-commits-dialog' import { sendNonFatalException } from '../../lib/helpers/non-fatal-exception' import { SignInResult } from '../../lib/stores/sign-in-store' import { ICustomIntegration } from '../../lib/custom-integration' +import { isAbsolute } from 'path' /** * An error handler function. @@ -1768,6 +1769,10 @@ export class Dispatcher { } if (filepath !== null) { + if (isAbsolute(filepath)) { + return + } + const resolved = await resolveWithin(repository.path, filepath) if (resolved !== null) { From ca3c3ad7552af9932b1489330eecba26851381d8 Mon Sep 17 00:00:00 2001 From: Markus Olsson Date: Tue, 10 Dec 2024 12:58:10 +0100 Subject: [PATCH 2/4] Incorrect metric, this is selecting an existing, not adding an existing --- app/src/ui/dispatcher/dispatcher.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/ui/dispatcher/dispatcher.ts b/app/src/ui/dispatcher/dispatcher.ts index 944852a19f9..93519a2149b 100644 --- a/app/src/ui/dispatcher/dispatcher.ts +++ b/app/src/ui/dispatcher/dispatcher.ts @@ -1900,7 +1900,6 @@ export class Dispatcher { if (existingRepository) { await this.selectRepository(existingRepository) - this.statsStore.recordAddExistingRepository() } else { await this.showPopup({ type: PopupType.AddRepository, path }) } From 2cfff35739ab083ac58aeb2881affd26deabf6bf Mon Sep 17 00:00:00 2001 From: Markus Olsson Date: Tue, 10 Dec 2024 13:05:01 +0100 Subject: [PATCH 3/4] Find repo without executing rev-parse --- app/src/ui/dispatcher/dispatcher.ts | 32 ++++++++++++++++------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/app/src/ui/dispatcher/dispatcher.ts b/app/src/ui/dispatcher/dispatcher.ts index 93519a2149b..42c9b4b5e54 100644 --- a/app/src/ui/dispatcher/dispatcher.ts +++ b/app/src/ui/dispatcher/dispatcher.ts @@ -122,7 +122,7 @@ import { UnreachableCommitsTab } from '../history/unreachable-commits-dialog' import { sendNonFatalException } from '../../lib/helpers/non-fatal-exception' import { SignInResult } from '../../lib/stores/sign-in-store' import { ICustomIntegration } from '../../lib/custom-integration' -import { isAbsolute } from 'path' +import { dirname, isAbsolute } from 'path' /** * An error handler function. @@ -1886,22 +1886,26 @@ export class Dispatcher { // user may accidentally provide a folder within the repository // this ensures we use the repository root, if it is actually a repository // otherwise we consider it an untracked repository - const path = await getRepositoryType(action.path) - .then(t => - t.kind === 'regular' ? t.topLevelWorkingDirectory : action.path - ) - .catch(e => { - log.error('Could not determine repository type', e) - return action.path - }) - const { repositories } = this.appStore.getState() - const existingRepository = matchExistingRepository(repositories, path) - if (existingRepository) { - await this.selectRepository(existingRepository) + let repo + let path = action.path + + while (!(repo = matchExistingRepository(repositories, path))) { + const parent = dirname(path) + if (parent === path) { + break + } + path = parent + } + + if (repo) { + await this.selectRepository(repo) } else { - await this.showPopup({ type: PopupType.AddRepository, path }) + await this.showPopup({ + type: PopupType.AddRepository, + path: action.path, + }) } break From 85648902613c46780234a604b95f3e32dbd6dba1 Mon Sep 17 00:00:00 2001 From: Markus Olsson Date: Tue, 10 Dec 2024 13:07:57 +0100 Subject: [PATCH 4/4] log --- app/src/ui/dispatcher/dispatcher.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/ui/dispatcher/dispatcher.ts b/app/src/ui/dispatcher/dispatcher.ts index 42c9b4b5e54..06d19509152 100644 --- a/app/src/ui/dispatcher/dispatcher.ts +++ b/app/src/ui/dispatcher/dispatcher.ts @@ -33,7 +33,6 @@ import { getCommitsBetweenCommits, getBranches, getRebaseSnapshot, - getRepositoryType, } from '../../lib/git' import { isGitOnPath } from '../../lib/is-git-on-path' import { @@ -1770,6 +1769,7 @@ export class Dispatcher { if (filepath !== null) { if (isAbsolute(filepath)) { + log.error(`Refusing to open absolute path: ${filepath}`) return }