Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gomod): remove token encoding #12352

Merged
merged 6 commits into from Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/util/git/auth.spec.ts
Expand Up @@ -31,6 +31,20 @@ describe('util/git/auth', () => {
});
});

it('returns correct url if token already contains GitHub App username', () => {
expect(
getGitAuthenticatedEnvironmentVariables(
'https://github.com/',
'x-access-token:token:1234'
)
).toStrictEqual({
GIT_CONFIG_KEY_0:
'url.https://x-access-token:token%3A1234@github.com/.insteadOf',
GIT_CONFIG_VALUE_0: 'https://github.com/',
GIT_CONFIG_COUNT: '1',
});
});

it('returns url with token and already existing GIT_CONFIG_COUNT from parameter', () => {
expect(
getGitAuthenticatedEnvironmentVariables(
Expand Down
13 changes: 12 additions & 1 deletion lib/util/git/auth.ts
@@ -1,6 +1,17 @@
import { logger } from '../../logger';
import { getHttpUrl } from './url';

function encodeToken(token: string): string {
// if the token already starts with the `x-access-token:` username we encode only the token part
// This ensures that GitHub Apps work
if (token.startsWith('x-access-token:')) {
const appToken = token.replace('x-access-token:', '');
return `x-access-token:${encodeURIComponent(appToken)}`;
}

return encodeURIComponent(token);
}

/*
Add authorization to a Git Url and returns the updated environment variables
*/
Expand All @@ -24,7 +35,7 @@ export function getGitAuthenticatedEnvironmentVariables(
}
}

const gitUrlWithToken = getHttpUrl(gitUrl, encodeURIComponent(token));
const gitUrlWithToken = getHttpUrl(gitUrl, encodeToken(token));
rarkins marked this conversation as resolved.
Show resolved Hide resolved

// create a shallow copy of the environmentVariables as base so we don't modify the input parameter object
// add the two new config key and value to the returnEnvironmentVariables object
Expand Down