Skip to content

Commit 2d76d21

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 b9e0990 commit 2d76d21

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
@@ -923,6 +923,25 @@ describe('git-auth-helper tests', () => {
923923
}
924924
})
925925

926+
const configureGlobalAuth_overridesGitConfigGlobal =
927+
'configureGlobalAuth overrides GIT_CONFIG_GLOBAL'
928+
it(configureGlobalAuth_overridesGitConfigGlobal, async () => {
929+
// Arrange
930+
await setup(configureGlobalAuth_overridesGitConfigGlobal)
931+
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
932+
933+
// Act
934+
await authHelper.configureAuth()
935+
await authHelper.configureGlobalAuth()
936+
937+
// Assert GIT_CONFIG_GLOBAL is pinned to the temporary global config, so an
938+
// inherited GIT_CONFIG_GLOBAL cannot redirect --global writes
939+
expect(git.env['HOME']).toBeTruthy()
940+
expect(git.env['GIT_CONFIG_GLOBAL']).toBe(
941+
path.join(git.env['HOME'], '.gitconfig')
942+
)
943+
})
944+
926945
const removeGlobalConfig_removesOverride =
927946
'removeGlobalConfig removes override'
928947
it(removeGlobalConfig_removesOverride, async () => {
@@ -933,13 +952,15 @@ describe('git-auth-helper tests', () => {
933952
await authHelper.configureGlobalAuth()
934953
const homeOverride = git.env['HOME'] // Sanity check
935954
expect(homeOverride).toBeTruthy()
955+
expect(git.env['GIT_CONFIG_GLOBAL']).toBeTruthy()
936956
await fs.promises.stat(path.join(git.env['HOME'], '.gitconfig'))
937957

938958
// Act
939959
await authHelper.removeGlobalConfig()
940960

941961
// Assert
942962
expect(git.env['HOME']).toBeUndefined()
963+
expect(git.env['GIT_CONFIG_GLOBAL']).toBeUndefined()
943964
try {
944965
await fs.promises.stat(homeOverride)
945966
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
@@ -35109,6 +35109,10 @@ class GitAuthHelper {
3510935109
// Override HOME
3511035110
info(`Temporarily overriding HOME='${this.temporaryHomePath}' before making global git config changes`);
3511135111
this.git.setEnvironmentVariable('HOME', this.temporaryHomePath);
35112+
// GIT_CONFIG_GLOBAL takes precedence over HOME when locating the global
35113+
// config file. Pin it to the temporary config so an inherited
35114+
// GIT_CONFIG_GLOBAL cannot redirect our global git config writes elsewhere.
35115+
this.git.setEnvironmentVariable('GIT_CONFIG_GLOBAL', newGitConfigPath);
3511235116
return newGitConfigPath;
3511335117
}
3511435118
async configureGlobalAuth() {
@@ -35183,8 +35187,9 @@ class GitAuthHelper {
3518335187
}
3518435188
async removeGlobalConfig() {
3518535189
if (this.temporaryHomePath?.length > 0) {
35186-
core_debug(`Unsetting HOME override`);
35190+
core_debug(`Unsetting HOME and GIT_CONFIG_GLOBAL overrides`);
3518735191
this.git.removeEnvironmentVariable('HOME');
35192+
this.git.removeEnvironmentVariable('GIT_CONFIG_GLOBAL');
3518835193
await rmRF(this.temporaryHomePath);
3518935194
}
3519035195
}

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)