Skip to content

Commit 125b59f

Browse files
authored
Merge pull request #27 from dmno-dev/fix/revert-extraheader
Fix git push auth: revert to URL rewriting, add token redaction
2 parents 87b29f1 + 545913b commit 125b59f

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

.bumpy/fix-git-push-auth.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@varlock/bumpy': patch
3+
---
4+
5+
Fix git push auth: revert to URL rewriting approach and add token redaction on errors

packages/bumpy/src/commands/ci.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,15 @@ function pushWithToken(rootDir: string, branch: string, config: BumpyConfig): vo
239239
const server = process.env.GITHUB_SERVER_URL || 'https://github.com';
240240

241241
if (token && repo) {
242-
// Use an ephemeral `-c` flag to inject auth so the token never touches .git/config.
243-
// GitHub accepts HTTP basic auth with "x-access-token" as the username.
244-
const basicAuth = Buffer.from(`x-access-token:${token}`).toString('base64');
245-
const extraHeaderKey = `http.${server}/.extraheader`;
246-
const authHeader = `Authorization: basic ${basicAuth}`;
242+
const authedUrl = `${server.replace('://', `://x-access-token:${token}@`)}/${repo}.git`;
243+
const originalUrl = tryRunArgs(['git', 'remote', 'get-url', 'origin'], { cwd: rootDir });
247244

248245
// `actions/checkout@v6` persists the default GITHUB_TOKEN in two ways:
249246
// 1. Direct http.<server>/.extraheader config
250247
// 2. includeIf.gitdir entries pointing to a credentials config file
251248
// that also sets http.<server>/.extraheader
252249
// Both must be cleared for our custom token to be used.
250+
const extraHeaderKey = `http.${server}/.extraheader`;
253251
const savedHeader = tryRunArgs(['git', 'config', '--local', extraHeaderKey], { cwd: rootDir });
254252

255253
// Collect includeIf entries that point to credential config files
@@ -273,12 +271,19 @@ function pushWithToken(rootDir: string, branch: string, config: BumpyConfig): vo
273271
for (const entry of savedIncludeIfs) {
274272
tryRunArgs(['git', 'config', '--local', '--unset', entry.key], { cwd: rootDir });
275273
}
276-
// Pass auth via ephemeral -c flag — never written to .git/config
277-
runArgs(['git', '-c', `${extraHeaderKey}=${authHeader}`, 'push', '-u', 'origin', branch, '--force'], {
278-
cwd: rootDir,
279-
});
274+
runArgs(['git', 'remote', 'set-url', 'origin', authedUrl], { cwd: rootDir });
275+
try {
276+
runArgs(['git', 'push', '-u', 'origin', branch, '--force'], { cwd: rootDir });
277+
} catch (err) {
278+
// Redact token from error messages to prevent leakage in CI logs
279+
const msg = err instanceof Error ? err.message : String(err);
280+
throw new Error(msg.replaceAll(token, '***'));
281+
}
280282
} finally {
281-
// Restore extraheader and includeIf entries cleared above
283+
// Restore original URL, extraheader, and includeIf entries
284+
if (originalUrl) {
285+
runArgs(['git', 'remote', 'set-url', 'origin', originalUrl], { cwd: rootDir });
286+
}
282287
if (savedHeader) {
283288
runArgs(['git', 'config', '--local', extraHeaderKey, savedHeader], { cwd: rootDir });
284289
}

0 commit comments

Comments
 (0)