Skip to content

Commit e1d9189

Browse files
committed
Pin GIT_CONFIG_GLOBAL to the temp config during global auth setup
configureTempGlobalConfig isolates global git config by overriding HOME to a temporary directory. But GIT_CONFIG_GLOBAL takes precedence over HOME when git locates the global config file, so when a workflow already has GIT_CONFIG_GLOBAL set in the environment, 'git config --global' writes land in that file instead of the temporary config. replaceTokenPlaceholder then reads the temporary config, cannot find the placeholder, and fails with 'Unable to replace auth placeholder'. Set GIT_CONFIG_GLOBAL to the temporary config alongside the HOME override so global config operations always target the temp file regardless of any inherited value, and unset it again in removeGlobalConfig. Assisted-By: Claude Opus 4.8
1 parent df4cb1c commit e1d9189

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

__test__/git-auth-helper.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,25 @@ describe('git-auth-helper tests', () => {
902902
}
903903
})
904904

905+
const configureGlobalAuth_overridesGitConfigGlobal =
906+
'configureGlobalAuth overrides GIT_CONFIG_GLOBAL'
907+
it(configureGlobalAuth_overridesGitConfigGlobal, async () => {
908+
// Arrange
909+
await setup(configureGlobalAuth_overridesGitConfigGlobal)
910+
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
911+
912+
// Act
913+
await authHelper.configureAuth()
914+
await authHelper.configureGlobalAuth()
915+
916+
// Assert GIT_CONFIG_GLOBAL is pinned to the temporary global config, so an
917+
// inherited GIT_CONFIG_GLOBAL cannot redirect --global writes
918+
expect(git.env['HOME']).toBeTruthy()
919+
expect(git.env['GIT_CONFIG_GLOBAL']).toBe(
920+
path.join(git.env['HOME'], '.gitconfig')
921+
)
922+
})
923+
905924
const removeGlobalConfig_removesOverride =
906925
'removeGlobalConfig removes override'
907926
it(removeGlobalConfig_removesOverride, async () => {
@@ -912,13 +931,15 @@ describe('git-auth-helper tests', () => {
912931
await authHelper.configureGlobalAuth()
913932
const homeOverride = git.env['HOME'] // Sanity check
914933
expect(homeOverride).toBeTruthy()
934+
expect(git.env['GIT_CONFIG_GLOBAL']).toBeTruthy()
915935
await fs.promises.stat(path.join(git.env['HOME'], '.gitconfig'))
916936

917937
// Act
918938
await authHelper.removeGlobalConfig()
919939

920940
// Assert
921941
expect(git.env['HOME']).toBeUndefined()
942+
expect(git.env['GIT_CONFIG_GLOBAL']).toBeUndefined()
922943
try {
923944
await fs.promises.stat(homeOverride)
924945
throw new Error(`Should have been deleted '${homeOverride}'`)

dist/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ class GitAuthHelper {
224224
// Override HOME
225225
core.info(`Temporarily overriding HOME='${this.temporaryHomePath}' before making global git config changes`);
226226
this.git.setEnvironmentVariable('HOME', this.temporaryHomePath);
227+
// GIT_CONFIG_GLOBAL takes precedence over HOME when locating the global
228+
// config file. Pin it to the temporary config so an inherited
229+
// GIT_CONFIG_GLOBAL cannot redirect our global git config writes elsewhere.
230+
this.git.setEnvironmentVariable('GIT_CONFIG_GLOBAL', newGitConfigPath);
227231
return newGitConfigPath;
228232
});
229233
}
@@ -307,8 +311,9 @@ class GitAuthHelper {
307311
return __awaiter(this, void 0, void 0, function* () {
308312
var _a;
309313
if (((_a = this.temporaryHomePath) === null || _a === void 0 ? void 0 : _a.length) > 0) {
310-
core.debug(`Unsetting HOME override`);
314+
core.debug(`Unsetting HOME and GIT_CONFIG_GLOBAL overrides`);
311315
this.git.removeEnvironmentVariable('HOME');
316+
this.git.removeEnvironmentVariable('GIT_CONFIG_GLOBAL');
312317
yield io.rmRF(this.temporaryHomePath);
313318
}
314319
});

src/git-auth-helper.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ class GitAuthHelper {
122122
)
123123
this.git.setEnvironmentVariable('HOME', this.temporaryHomePath)
124124

125+
// GIT_CONFIG_GLOBAL takes precedence over HOME when locating the global
126+
// config file. Pin it to the temporary config so an inherited
127+
// GIT_CONFIG_GLOBAL cannot redirect our global git config writes elsewhere.
128+
this.git.setEnvironmentVariable('GIT_CONFIG_GLOBAL', newGitConfigPath)
129+
125130
return newGitConfigPath
126131
}
127132

@@ -237,8 +242,9 @@ class GitAuthHelper {
237242

238243
async removeGlobalConfig(): Promise<void> {
239244
if (this.temporaryHomePath?.length > 0) {
240-
core.debug(`Unsetting HOME override`)
245+
core.debug(`Unsetting HOME and GIT_CONFIG_GLOBAL overrides`)
241246
this.git.removeEnvironmentVariable('HOME')
247+
this.git.removeEnvironmentVariable('GIT_CONFIG_GLOBAL')
242248
await io.rmRF(this.temporaryHomePath)
243249
}
244250
}

0 commit comments

Comments
 (0)