Skip to content

Commit

Permalink
fix: strip credentials from url on merge message
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-titarenko committed Oct 24, 2024
1 parent dcd2a26 commit a4ef708
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/commands/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { Buffer } from 'buffer'
import {
AuthCallback,
AuthFailureCallback,
AuthSuccessCallback,
HttpClient,
HttpHeaders,
MessageCallback,
ProgressCallback
} from '../models'

import { FileSystem } from '../models/FileSystem'
import { Buffer } from 'buffer'
import { Cache } from '../models/Cache'
import { _currentBranch } from '../commands/currentBranch'
import { MissingParameterError } from '../errors/MissingParameterError'
import { RemoteCapabilityError } from '../errors/RemoteCapabilityError'
import { FileSystem } from '../models/FileSystem'
import { GitCommit } from '../models/GitCommit'
import { GitConfigManager } from '../managers/GitConfigManager'
import { GitPackIndex } from '../models/GitPackIndex'
import { GitRefManager } from '../managers/GitRefManager'
import { GitRemoteManager } from '../managers/GitRemoteManager'
import { GitShallowManager } from '../managers/GitShallowManager'
import { GitCommit } from '../models/GitCommit'
import { GitPackIndex } from '../models/GitPackIndex'
import { hasObject } from '../storage/hasObject'
import { _readObject as readObject } from '../storage/readObject'
import { MissingParameterError } from '../errors/MissingParameterError'
import { RemoteCapabilityError } from '../errors/RemoteCapabilityError'
import { _currentBranch } from '../commands/currentBranch'
import { abbreviateRef } from '../utils/abbreviateRef'
import { collect } from '../utils/collect'
import { emptyPackfile } from '../utils/emptyPackfile'
import { filterCapabilities } from '../utils/filterCapabilities'
import { join } from '../utils/join'
import { forAwait } from '../utils/forAwait'
import { getGitClientAgent } from '../utils/pkg'
import { splitLines } from '../utils/splitLines'
import { hasObject } from '../storage/hasObject'
import { join } from '../utils/join'
import { parseUploadPackResponse } from '../wire/parseUploadPackResponse'
import { _readObject as readObject } from '../storage/readObject'
import { splitLines } from '../utils/splitLines'
import { stripCredentialsFromUrl } from 'git-essentials/utils/url'
import { writeUploadPackRequest } from '../wire/writeUploadPackRequest'
import { forAwait } from '../utils/forAwait'
import {
AuthCallback,
AuthFailureCallback,
AuthSuccessCallback,
HttpClient,
HttpHeaders,
MessageCallback,
ProgressCallback
} from '../models'


type FetchParams = {
fs: FileSystem
Expand Down Expand Up @@ -346,7 +346,7 @@ export async function _fetch({
const noun = fullref.startsWith('refs/tags') ? 'tag' : 'branch'
const FETCH_HEAD = {
oid,
description: `${noun} '${abbreviateRef(fullref)}' of ${url}`,
description: `${noun} '${abbreviateRef(fullref)}' of ${stripCredentialsFromUrl(url)}`,
}

if (onProgress || onMessage) {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function stripCredentialsFromUrl(urlString: string): string {
try {
const url = new URL(urlString);
url.username = '';
url.password = '';

return url.toString();
} catch {
return urlString;
}
}
23 changes: 23 additions & 0 deletions tests/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ describe('fetch', () => {
expect(await fs.exists(`${dir}/.git/refs/remotes/origin/main`)).toBe(false)
})

it('strip credentials in git commit message', async () => {
// arrange
const { fs, dir } = await makeFsFixture(fetchEmptyRepoFsFixtureData as FsFixtureData)
const http = makeHttpFixture(fetchHttpFixtureData as HttpFixtureData)
const fetchUrl = 'http://username:password@localhost/fetch-server.git';
const fetchUrlWithoutCreds = 'http://localhost/fetch-server.git';

// act
const { fetchHeadDescription } = await fetch({
fs,
http,
url: fetchUrl,
dir,
singleBranch: true,
remote: 'origin',
ref: 'test'
})

// assert
expect(fetchHeadDescription).not.toContain(fetchUrl)
expect(fetchHeadDescription).toContain(fetchUrlWithoutCreds)
})

it('shallow fetch', async () => {
// arrange
const { fs, dir } = await makeFsFixture(fetchEmptyRepoFsFixtureData as FsFixtureData)
Expand Down

0 comments on commit a4ef708

Please sign in to comment.