Skip to content

Commit 8f79f86

Browse files
committed
fix(sdk): map git push/pull auth and upstream failures with credentials
push() and pull() ran the isAuthFailure/isMissingUpstream mapping only on the no-credentials branch. With username/password the raw command error escaped instead, so an expired token surfaced as git's exit code and stderr rather than the actionable typed error — even though clone() applies the same mapping on every path, including the credentials one. Wrap the whole operation, credentials branch included, in the mapping try/catch in both the JS SDK and the sync/async Python SDKs, so both paths throw the typed errors. The remote URL is still restored before the mapped error is thrown.
1 parent 6a608ef commit 8f79f86

6 files changed

Lines changed: 413 additions & 115 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'e2b': patch
3+
'@e2b/python-sdk': patch
4+
---
5+
6+
Throw the typed git auth/upstream errors from `git.push()` and `git.pull()` when they fail with username/password credentials, instead of the raw command error (`GitAuthError`/`GitUpstreamError` in JS, `GitAuthException`/`GitUpstreamException` in Python). The auth/upstream failure mapping only ran on the no-credentials path, so pushing or pulling with an expired token surfaced git's exit code and stderr rather than the actionable typed error — even though `git.clone()` applies the same mapping on every path, including the credentials one. The temporary credentials are still stripped from the remote URL before the typed error is thrown.

‎packages/js-sdk/src/sandbox/git/index.ts‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -723,24 +723,24 @@ export class Git {
723723
)
724724
}
725725

726-
if (username && password) {
727-
const remoteName = await this.resolveRemoteName(path, remote, rest)
728-
return this.withRemoteCredentials(
729-
path,
730-
remoteName,
731-
username,
732-
password,
733-
rest,
734-
() =>
735-
this.runGit(
736-
buildPushArgs(remoteName, { remote, branch, setUpstream }),
737-
path,
738-
rest
739-
)
740-
)
741-
}
742-
743726
try {
727+
if (username && password) {
728+
const remoteName = await this.resolveRemoteName(path, remote, rest)
729+
return await this.withRemoteCredentials(
730+
path,
731+
remoteName,
732+
username,
733+
password,
734+
rest,
735+
() =>
736+
this.runGit(
737+
buildPushArgs(remoteName, { remote, branch, setUpstream }),
738+
path,
739+
rest
740+
)
741+
)
742+
}
743+
744744
return await this.runGit(
745745
buildPushArgs(undefined, { remote, branch, setUpstream }),
746746
path,
@@ -793,19 +793,19 @@ export class Git {
793793
return args
794794
}
795795

796-
if (username && password) {
797-
const remoteName = await this.resolveRemoteName(path, remote, rest)
798-
return this.withRemoteCredentials(
799-
path,
800-
remoteName,
801-
username,
802-
password,
803-
rest,
804-
() => this.runGit(buildArgs(remoteName), path, rest)
805-
)
806-
}
807-
808796
try {
797+
if (username && password) {
798+
const remoteName = await this.resolveRemoteName(path, remote, rest)
799+
return await this.withRemoteCredentials(
800+
path,
801+
remoteName,
802+
username,
803+
password,
804+
rest,
805+
() => this.runGit(buildArgs(remoteName), path, rest)
806+
)
807+
}
808+
809809
return await this.runGit(buildArgs(), path, rest)
810810
} catch (err) {
811811
if (isAuthFailure(err)) {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { test as base, expect } from 'vitest'
2+
3+
import { isGitTestsEnabled } from '../../setup.js'
4+
import { Git } from '../../../src/sandbox/git'
5+
import type { Commands } from '../../../src/sandbox/commands'
6+
import {
7+
CommandExitError,
8+
CommandResult,
9+
} from '../../../src/sandbox/commands/commandHandle'
10+
import { GitAuthError, GitUpstreamError } from '../../../src/errors'
11+
12+
const test = base.skipIf(!isGitTestsEnabled)
13+
14+
const REMOTE_URL = 'https://github.com/e2b-dev/e2b.git'
15+
16+
function gitExitFailure(stderr: string): CommandExitError {
17+
return new CommandExitError({ exitCode: 128, stdout: '', stderr })
18+
}
19+
20+
// Fake command runner standing in for the sandbox: every git invocation is
21+
// recorded, the push/pull itself fails with the given error, and the remote
22+
// bookkeeping around it (`remote get-url` / `remote set-url`) succeeds.
23+
function fakeCommands(failure: CommandExitError): {
24+
commands: Commands
25+
runCalls: string[]
26+
} {
27+
const runCalls: string[] = []
28+
const commands = {
29+
run: async (cmd: string): Promise<CommandResult> => {
30+
runCalls.push(cmd)
31+
if (/\s(push|pull)\b/.test(cmd)) {
32+
throw failure
33+
}
34+
if (cmd.includes(' remote get-url ')) {
35+
return { exitCode: 0, stdout: `${REMOTE_URL}\n`, stderr: '' }
36+
}
37+
return { exitCode: 0, stdout: '', stderr: '' }
38+
},
39+
} as unknown as Commands
40+
return { commands, runCalls }
41+
}
42+
43+
test('git push without credentials maps an auth failure to GitAuthError', async () => {
44+
const { commands } = fakeCommands(
45+
gitExitFailure(`fatal: Authentication failed for '${REMOTE_URL}/'`)
46+
)
47+
const git = new Git(commands)
48+
49+
await expect(git.push('/repo', { remote: 'origin' })).rejects.toBeInstanceOf(
50+
GitAuthError
51+
)
52+
})
53+
54+
test('git push with username/password maps an auth failure to GitAuthError', async () => {
55+
const { commands, runCalls } = fakeCommands(
56+
gitExitFailure(`fatal: Authentication failed for '${REMOTE_URL}/'`)
57+
)
58+
const git = new Git(commands)
59+
60+
await expect(
61+
git.push('/repo', {
62+
remote: 'origin',
63+
username: 'user',
64+
password: 'expired-token',
65+
})
66+
).rejects.toBeInstanceOf(GitAuthError)
67+
68+
// The credentials temporarily embedded in the remote URL must be stripped
69+
// again even when the push fails.
70+
expect(runCalls[runCalls.length - 1]).toBe(
71+
`git -C /repo remote set-url origin ${REMOTE_URL}`
72+
)
73+
})
74+
75+
test('git pull with username/password maps an auth failure to GitAuthError', async () => {
76+
const { commands } = fakeCommands(
77+
gitExitFailure(`fatal: Authentication failed for '${REMOTE_URL}/'`)
78+
)
79+
const git = new Git(commands)
80+
81+
await expect(
82+
git.pull('/repo', {
83+
remote: 'origin',
84+
branch: 'main',
85+
username: 'user',
86+
password: 'expired-token',
87+
})
88+
).rejects.toBeInstanceOf(GitAuthError)
89+
})
90+
91+
test('git push with username/password maps a missing upstream to GitUpstreamError', async () => {
92+
const { commands } = fakeCommands(
93+
gitExitFailure(
94+
'fatal: The current branch main has no upstream branch.\n' +
95+
'To push the current branch and set the remote as upstream, use\n\n' +
96+
' git push --set-upstream origin main'
97+
)
98+
)
99+
const git = new Git(commands)
100+
101+
await expect(
102+
git.push('/repo', {
103+
remote: 'origin',
104+
setUpstream: false,
105+
username: 'user',
106+
password: 'token',
107+
})
108+
).rejects.toBeInstanceOf(GitUpstreamError)
109+
})
110+
111+
test('git pull with username/password maps a missing upstream to GitUpstreamError', async () => {
112+
const { commands } = fakeCommands(
113+
gitExitFailure(
114+
'There is no tracking information for the current branch.\n' +
115+
'Please specify which branch you want to merge with.'
116+
)
117+
)
118+
const git = new Git(commands)
119+
120+
await expect(
121+
git.pull('/repo', {
122+
remote: 'origin',
123+
username: 'user',
124+
password: 'token',
125+
})
126+
).rejects.toBeInstanceOf(GitUpstreamError)
127+
})

‎packages/python-sdk/e2b/sandbox_async/git.py‎

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -760,37 +760,37 @@ async def push(
760760
"Username is required when using a password or token for git push."
761761
)
762762

763-
if username and password:
764-
remote_name = await self._resolve_remote_name(
765-
path, remote, envs, user, cwd, timeout, request_timeout
766-
)
767-
return await self._with_remote_credentials(
768-
path,
769-
remote_name,
770-
username,
771-
password,
772-
envs,
773-
user,
774-
cwd,
775-
timeout,
776-
request_timeout,
777-
operation=lambda: self._run_git(
778-
build_push_args(
779-
remote_name,
780-
remote=remote,
781-
branch=branch,
782-
set_upstream=set_upstream,
783-
),
763+
try:
764+
if username and password:
765+
remote_name = await self._resolve_remote_name(
766+
path, remote, envs, user, cwd, timeout, request_timeout
767+
)
768+
return await self._with_remote_credentials(
784769
path,
770+
remote_name,
771+
username,
772+
password,
785773
envs,
786774
user,
787775
cwd,
788776
timeout,
789777
request_timeout,
790-
),
791-
)
778+
operation=lambda: self._run_git(
779+
build_push_args(
780+
remote_name,
781+
remote=remote,
782+
branch=branch,
783+
set_upstream=set_upstream,
784+
),
785+
path,
786+
envs,
787+
user,
788+
cwd,
789+
timeout,
790+
request_timeout,
791+
),
792+
)
792793

793-
try:
794794
return await self._run_git(
795795
build_push_args(
796796
None,
@@ -856,32 +856,32 @@ async def pull(
856856
if not has_upstream:
857857
raise GitUpstreamException(build_upstream_error_message("pull"))
858858

859-
if username and password:
860-
remote_name = await self._resolve_remote_name(
861-
path, remote, envs, user, cwd, timeout, request_timeout
862-
)
863-
return await self._with_remote_credentials(
864-
path,
865-
remote_name,
866-
username,
867-
password,
868-
envs,
869-
user,
870-
cwd,
871-
timeout,
872-
request_timeout,
873-
operation=lambda: self._run_git(
874-
build_pull_args(remote, branch, remote_name),
859+
try:
860+
if username and password:
861+
remote_name = await self._resolve_remote_name(
862+
path, remote, envs, user, cwd, timeout, request_timeout
863+
)
864+
return await self._with_remote_credentials(
875865
path,
866+
remote_name,
867+
username,
868+
password,
876869
envs,
877870
user,
878871
cwd,
879872
timeout,
880873
request_timeout,
881-
),
882-
)
874+
operation=lambda: self._run_git(
875+
build_pull_args(remote, branch, remote_name),
876+
path,
877+
envs,
878+
user,
879+
cwd,
880+
timeout,
881+
request_timeout,
882+
),
883+
)
883884

884-
try:
885885
return await self._run_git(
886886
build_pull_args(remote, branch),
887887
path,

0 commit comments

Comments
 (0)