Skip to content
3 changes: 2 additions & 1 deletion src/proxy/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ const tagPushChainElements: ChainElement[] = [
proc.push.checkRepoInAuthorisedList,
PushPhase.AFTER_PERMISSIONS,
proc.push.checkUserPushPermission,
proc.push.checkIfWaitingAuth,
proc.push.checkMessages,
proc.push.pullRemote,
proc.push.writePack,
PushPhase.AFTER_CHECKOUT,
proc.push.checkHiddenCommits,
proc.push.checkIfWaitingAuth,
proc.push.preReceive,
PushPhase.BEFORE_APPROVAL,
proc.push.blockForAuth,
Expand Down
18 changes: 16 additions & 2 deletions src/proxy/processors/push-action/checkHiddenCommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import path from 'path';
import { Action, Step } from '../../actions';
import { Action, PushType, Step } from '../../actions';
import { spawnSync } from 'child_process';
import { EMPTY_COMMIT_HASH } from '../../constants';
import { Request } from 'express';
Expand All @@ -33,9 +33,23 @@ const exec = async (_req: Request, action: Action): Promise<Action> => {
throw new Error('Both action.commitFrom and action.commitTo must be defined');
}

const isTagPush = action.actionType === PushType.TAG;

if (isTagPush && !action.newIdxFiles?.length) {
const packMeta = action.steps.find((s) => s.stepName === 'parsePackFile')?.content?.meta;
const onlyTagObjects = packMeta?.entries === (action.tagData?.length ?? 0);
if (!onlyTagObjects) {
step.setError(
'Unable to verify the pushed pack. Push the commits on a branch first, then push the tag.',
);
action.error = true;
return action;
}
}

// build introducedCommits set
const introducedCommits = new Set<string>();
const revRange = oldOid === EMPTY_COMMIT_HASH ? newOid : `${oldOid}..${newOid}`;
const revRange = oldOid === EMPTY_COMMIT_HASH || isTagPush ? newOid : `${oldOid}..${newOid}`;
const revList = spawnSync('git', ['rev-list', revRange], { cwd: repoPath, encoding: 'utf-8' })
.stdout.trim()
.split('\n')
Expand Down
85 changes: 85 additions & 0 deletions test/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,91 @@ describe('proxy chain', function () {
expect(tagChain).toEqual(chain.tagPushChain);
});

it('tagPushChain runs checkHiddenCommits after writePack and before checkIfWaitingAuth', async () => {
await chain.getChain({ type: RequestType.PUSH, actionType: PushType.TAG });

const tagChain = chain.tagPushChain as unknown[];
const writePackIdx = tagChain.indexOf(mockPushProcessors.writePack);
const hiddenIdx = tagChain.indexOf(mockPushProcessors.checkHiddenCommits);
const waitingAuthIdx = tagChain.indexOf(mockPushProcessors.checkIfWaitingAuth);

expect(writePackIdx).toBeGreaterThanOrEqual(0);
expect(hiddenIdx).toBeGreaterThan(writePackIdx);
expect(waitingAuthIdx).toBeGreaterThan(hiddenIdx);
expect(tagChain).toContain(mockPushProcessors.checkHiddenCommits);
expect(tagChain).not.toContain(mockPushProcessors.getDiff);
expect(tagChain).not.toContain(mockPushProcessors.gitleaks);
expect(tagChain).not.toContain(mockPushProcessors.scanDiff);
expect(tagChain).not.toContain(mockPushProcessors.checkAuthorEmails);
});

it('executeChain runs checkHiddenCommits for tag pushes', async () => {
const req = {};
const continuingAction = {
type: RequestType.PUSH,
actionType: PushType.TAG,
continue: () => true,
allowPush: false,
proxyGitPath: './.remote/test',
};
mockPreProcessors.parseAction.mockResolvedValue({ type: RequestType.PUSH });
mockPreProcessors.parsePush.mockResolvedValue(continuingAction);

const result = await chain.executeChain(req);

expect(mockPushProcessors.resolveUserFromToken).toHaveBeenCalled();
expect(mockPushProcessors.checkRepoInAuthorisedList).toHaveBeenCalled();
expect(mockPushProcessors.checkUserPushPermission).toHaveBeenCalled();
expect(mockPushProcessors.checkIfWaitingAuth).toHaveBeenCalled();
expect(mockPushProcessors.checkMessages).toHaveBeenCalled();
expect(mockPushProcessors.pullRemote).toHaveBeenCalled();
expect(mockPushProcessors.writePack).toHaveBeenCalled();
expect(mockPushProcessors.checkHiddenCommits).toHaveBeenCalled();
expect(mockPushProcessors.preReceive).toHaveBeenCalled();
expect(mockPushProcessors.blockForAuth).toHaveBeenCalled();

// branch-only processors must not run on the tag path
expect(mockPushProcessors.checkEmptyBranch).not.toHaveBeenCalled();
expect(mockPushProcessors.checkAuthorEmails).not.toHaveBeenCalled();
expect(mockPushProcessors.getDiff).not.toHaveBeenCalled();
expect(mockPushProcessors.gitleaks).not.toHaveBeenCalled();
expect(mockPushProcessors.scanDiff).not.toHaveBeenCalled();

expect(mockPostProcessors.audit).toHaveBeenCalled();
expect(mockPostProcessors.clearBareClone).toHaveBeenCalled();
expect(result.type).toBe(RequestType.PUSH);
});

it('executeChain stops a tag push when checkHiddenCommits blocks', async () => {
const req = {};
mockPreProcessors.parseAction.mockResolvedValue({ type: RequestType.PUSH });
mockPreProcessors.parsePush.mockResolvedValue({
type: RequestType.PUSH,
actionType: PushType.TAG,
continue: () => true,
allowPush: false,
proxyGitPath: './.remote/test',
});

mockPushProcessors.checkHiddenCommits.mockResolvedValue({
type: RequestType.PUSH,
actionType: PushType.TAG,
continue: () => false,
allowPush: false,
proxyGitPath: './.remote/test',
steps: [{ stepName: 'checkHiddenCommits', error: true }],
});

await chain.executeChain(req);

expect(mockPushProcessors.writePack).toHaveBeenCalled();
expect(mockPushProcessors.checkHiddenCommits).toHaveBeenCalled();
expect(mockPushProcessors.checkIfWaitingAuth).not.toHaveBeenCalled();
expect(mockPushProcessors.preReceive).not.toHaveBeenCalled();
expect(mockPushProcessors.blockForAuth).not.toHaveBeenCalled();
expect(mockPostProcessors.audit).toHaveBeenCalled();
});

it('returns branchPushChain when action.type is push and actionType is BRANCH', async () => {
const action = new Action(
'3',
Expand Down
155 changes: 154 additions & 1 deletion test/checkHiddenCommit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

import { describe, it, beforeEach, afterEach, expect, vi } from 'vitest';
import { exec as checkHidden } from '../src/proxy/processors/push-action/checkHiddenCommits';
import { Action } from '../src/proxy/actions';
import { Action, PushType, Step } from '../src/proxy/actions';
import { TagData } from '../src/types/models';
import { EMPTY_COMMIT_HASH } from '../src/proxy/constants';
import { Request } from 'express';

Expand Down Expand Up @@ -227,3 +228,155 @@ describe('checkHiddenCommits.exec - parent (commitFrom) exemption', () => {
expect(action.error).toBe(true);
});
});

describe('checkHiddenCommits.exec - annotated tag pushes', () => {
let action: Action;
let req: Request;

const TAG_OID = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
const PEELED_COMMIT = 'ffffffffffffffffffffffffffffffffffffffff';
const HIDDEN = 'dddddddddddddddddddddddddddddddddddddddd';

beforeEach(() => {
vi.clearAllMocks();

action = new Action('some-id', 'push', 'POST', Date.now(), 'repo.git');
action.proxyGitPath = '/fake';
action.commitFrom = EMPTY_COMMIT_HASH;
action.commitTo = TAG_OID;
action.newIdxFiles = ['pack-test.idx'];
req = { body: '' } as Request;

mockReaddirSync.mockReturnValue(['pack-test.idx']);
});

afterEach(() => {
vi.clearAllMocks();
});

it('allows a tag-only pack with no commit objects', async () => {
mockSpawnSync
.mockReturnValueOnce({ stdout: `${PEELED_COMMIT}\n` })
.mockReturnValueOnce({ stdout: `${TAG_OID} tag 100 1\n` });

await checkHidden(req, action);

const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
expect(step?.logs).toContain('checkHiddenCommits - Total commits in the pack: 0');
expect(step?.logs).toContain(
'checkHiddenCommits - All pack commits are referenced in the introduced range.',
);
expect(action.error).toBe(false);
});

it('blocks unreferenced commits smuggled in an annotated tag pack', async () => {
mockSpawnSync.mockReturnValueOnce({ stdout: `${PEELED_COMMIT}\n` }).mockReturnValueOnce({
stdout: `${PEELED_COMMIT} commit 100 1\n${HIDDEN} commit 100 2\n${TAG_OID} tag 100 3\n`,
});

await checkHidden(req, action);

const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
expect(step?.logs).toContain('checkHiddenCommits - Referenced commits: 1');
expect(step?.logs).toContain('checkHiddenCommits - Unreferenced commits: 1');
expect(step?.logs).toContain(
`checkHiddenCommits - Unreferenced commits in pack (1): ${HIDDEN}.\n` +
`This usually happens when a branch was made from a commit that hasn't been approved and pushed to the remote.\n` +
`Please rebase the branch and push again.`,
);
expect(action.error).toBe(true);
});

describe('when the pushed pack could not be indexed', () => {
const addParsePackStep = (entries: number) => {
const parseStep = new Step('parsePackFile');
parseStep.content = { meta: { sig: 'PACK', version: 2, entries } };
action.addStep(parseStep);
};

beforeEach(() => {
action.actionType = PushType.TAG;
action.newIdxFiles = [];
action.tagData = [{ object: PEELED_COMMIT } as TagData];
});

it('fails when the pack carries more than tag objects', async () => {
addParsePackStep(4);

await checkHidden(req, action);

const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
expect(step?.error).toBe(true);
expect(step?.errorMessage).toBe(
'Unable to verify the pushed pack. Push the commits on a branch first, then push the tag.',
);
expect(action.error).toBe(true);
expect(mockSpawnSync).not.toHaveBeenCalled();
});

it('fails when the pack metadata is unavailable', async () => {
await checkHidden(req, action);

expect(action.error).toBe(true);
expect(mockSpawnSync).not.toHaveBeenCalled();
});

it('allows a pack holding only the tag object', async () => {
addParsePackStep(1);
mockSpawnSync.mockReturnValueOnce({ stdout: `${PEELED_COMMIT}\n` });

await checkHidden(req, action);

const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
expect(step?.logs).toContain('checkHiddenCommits - Total commits in the pack: 0');
expect(action.error).toBe(false);
});
});

describe('when force-moving an existing tag', () => {
const OLD_TAG_OID = '1111111111111111111111111111111111111111';

beforeEach(() => {
action.actionType = PushType.TAG;
action.commitFrom = OLD_TAG_OID;
});

it('resolves introduced commits from the new tag alone', async () => {
mockSpawnSync
.mockReturnValueOnce({ stdout: `${PEELED_COMMIT}\n` })
.mockReturnValueOnce({ stdout: `${PEELED_COMMIT} commit 100 1\n${TAG_OID} tag 100 2\n` });

await checkHidden(req, action);

expect(mockSpawnSync.mock.calls[0][1]).toEqual(['rev-list', TAG_OID]);
const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
expect(step?.logs).toContain(
'checkHiddenCommits - All pack commits are referenced in the introduced range.',
);
expect(action.error).toBe(false);
});

it('still blocks unreferenced commits in the pack', async () => {
mockSpawnSync.mockReturnValueOnce({ stdout: `${PEELED_COMMIT}\n` }).mockReturnValueOnce({
stdout: `${PEELED_COMMIT} commit 100 1\n${HIDDEN} commit 100 2\n${TAG_OID} tag 100 3\n`,
});

await checkHidden(req, action);

const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
expect(step?.logs).toContain('checkHiddenCommits - Unreferenced commits: 1');
expect(action.error).toBe(true);
});

it('keeps the old..new range for branch pushes', async () => {
action.actionType = PushType.BRANCH;
mockSpawnSync
.mockReturnValueOnce({ stdout: `${PEELED_COMMIT}\n` })
.mockReturnValueOnce({ stdout: `${PEELED_COMMIT} commit 100 1\n` });

await checkHidden(req, action);

expect(mockSpawnSync.mock.calls[0][1]).toEqual(['rev-list', `${OLD_TAG_OID}..${TAG_OID}`]);
});
});
});
Loading