Skip to content

Commit a4ef708

Browse files
fix: strip credentials from url on merge message
1 parent dcd2a26 commit a4ef708

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

src/commands/fetch.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
import { Buffer } from 'buffer'
1+
import {
2+
AuthCallback,
3+
AuthFailureCallback,
4+
AuthSuccessCallback,
5+
HttpClient,
6+
HttpHeaders,
7+
MessageCallback,
8+
ProgressCallback
9+
} from '../models'
210

3-
import { FileSystem } from '../models/FileSystem'
11+
import { Buffer } from 'buffer'
412
import { Cache } from '../models/Cache'
5-
import { _currentBranch } from '../commands/currentBranch'
6-
import { MissingParameterError } from '../errors/MissingParameterError'
7-
import { RemoteCapabilityError } from '../errors/RemoteCapabilityError'
13+
import { FileSystem } from '../models/FileSystem'
14+
import { GitCommit } from '../models/GitCommit'
815
import { GitConfigManager } from '../managers/GitConfigManager'
16+
import { GitPackIndex } from '../models/GitPackIndex'
917
import { GitRefManager } from '../managers/GitRefManager'
1018
import { GitRemoteManager } from '../managers/GitRemoteManager'
1119
import { GitShallowManager } from '../managers/GitShallowManager'
12-
import { GitCommit } from '../models/GitCommit'
13-
import { GitPackIndex } from '../models/GitPackIndex'
14-
import { hasObject } from '../storage/hasObject'
15-
import { _readObject as readObject } from '../storage/readObject'
20+
import { MissingParameterError } from '../errors/MissingParameterError'
21+
import { RemoteCapabilityError } from '../errors/RemoteCapabilityError'
22+
import { _currentBranch } from '../commands/currentBranch'
1623
import { abbreviateRef } from '../utils/abbreviateRef'
1724
import { collect } from '../utils/collect'
1825
import { emptyPackfile } from '../utils/emptyPackfile'
1926
import { filterCapabilities } from '../utils/filterCapabilities'
20-
import { join } from '../utils/join'
27+
import { forAwait } from '../utils/forAwait'
2128
import { getGitClientAgent } from '../utils/pkg'
22-
import { splitLines } from '../utils/splitLines'
29+
import { hasObject } from '../storage/hasObject'
30+
import { join } from '../utils/join'
2331
import { parseUploadPackResponse } from '../wire/parseUploadPackResponse'
32+
import { _readObject as readObject } from '../storage/readObject'
33+
import { splitLines } from '../utils/splitLines'
34+
import { stripCredentialsFromUrl } from 'git-essentials/utils/url'
2435
import { writeUploadPackRequest } from '../wire/writeUploadPackRequest'
25-
import { forAwait } from '../utils/forAwait'
26-
import {
27-
AuthCallback,
28-
AuthFailureCallback,
29-
AuthSuccessCallback,
30-
HttpClient,
31-
HttpHeaders,
32-
MessageCallback,
33-
ProgressCallback
34-
} from '../models'
35-
3636

3737
type FetchParams = {
3838
fs: FileSystem
@@ -346,7 +346,7 @@ export async function _fetch({
346346
const noun = fullref.startsWith('refs/tags') ? 'tag' : 'branch'
347347
const FETCH_HEAD = {
348348
oid,
349-
description: `${noun} '${abbreviateRef(fullref)}' of ${url}`,
349+
description: `${noun} '${abbreviateRef(fullref)}' of ${stripCredentialsFromUrl(url)}`,
350350
}
351351

352352
if (onProgress || onMessage) {

src/utils/url.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function stripCredentialsFromUrl(urlString: string): string {
2+
try {
3+
const url = new URL(urlString);
4+
url.username = '';
5+
url.password = '';
6+
7+
return url.toString();
8+
} catch {
9+
return urlString;
10+
}
11+
}

tests/fetch.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ describe('fetch', () => {
4040
expect(await fs.exists(`${dir}/.git/refs/remotes/origin/main`)).toBe(false)
4141
})
4242

43+
it('strip credentials in git commit message', async () => {
44+
// arrange
45+
const { fs, dir } = await makeFsFixture(fetchEmptyRepoFsFixtureData as FsFixtureData)
46+
const http = makeHttpFixture(fetchHttpFixtureData as HttpFixtureData)
47+
const fetchUrl = 'http://username:password@localhost/fetch-server.git';
48+
const fetchUrlWithoutCreds = 'http://localhost/fetch-server.git';
49+
50+
// act
51+
const { fetchHeadDescription } = await fetch({
52+
fs,
53+
http,
54+
url: fetchUrl,
55+
dir,
56+
singleBranch: true,
57+
remote: 'origin',
58+
ref: 'test'
59+
})
60+
61+
// assert
62+
expect(fetchHeadDescription).not.toContain(fetchUrl)
63+
expect(fetchHeadDescription).toContain(fetchUrlWithoutCreds)
64+
})
65+
4366
it('shallow fetch', async () => {
4467
// arrange
4568
const { fs, dir } = await makeFsFixture(fetchEmptyRepoFsFixtureData as FsFixtureData)

0 commit comments

Comments
 (0)