Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/proxy/actions/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { createHash } from 'crypto';

import { processGitURLForNameAndOrg, processUrlPath } from '../routes/helper';
import { Step } from './Step';
import { CompletedAttestation, CommitData, Rejection } from '../processors/types';
Expand Down Expand Up @@ -112,22 +114,34 @@ class Action {

/**
* Set the commit range for the action. Changes the action.id to be based on
* the commit details.
* the commit details, the target repository URL and the target branch, so
* that an approval for one repository/branch cannot be reused for another.
* @param {string} commitFrom the starting commit
* @param {string} commitTo the ending commit
*/
setCommit(commitFrom: string, commitTo: string): void {
this.commitFrom = commitFrom;
this.commitTo = commitTo;
this.id = `${commitFrom}__${commitTo}`;
this.updatePushId();
}

/**
* Set the branch for the action.
* Set the branch for the action. Updates the action.id if the commit range
* has already been set.
* @param {string} branch the branch
*/
setBranch(branch: string): void {
this.branch = branch;
if (this.commitFrom !== undefined && this.commitTo !== undefined) {
this.updatePushId();
}
}

private updatePushId(): void {
const target = createHash('sha256')
.update(`${this.url}\n${this.branch ?? ''}`)
.digest('hex');
this.id = `${this.commitFrom}__${this.commitTo}__${target}`;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/proxy/processors/push-action/checkIfWaitingAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ import { Action, Step } from '../../actions';
import { getPush } from '../../../db';
import { getErrorMessage } from '../../../utils/errors';

const isSamePushTarget = (existing: Action, current: Action): boolean =>
existing.url === current.url && existing.branch === current.branch;

// Execute function
const exec = async (_req: Request, action: Action): Promise<Action> => {
const step = new Step('checkIfWaitingAuth');
try {
const existingAction = await getPush(action.id);
if (existingAction) {
if (!action.error) {
if (existingAction.authorised) {
if (existingAction.authorised && isSamePushTarget(existingAction, action)) {
action = existingAction;
action.setAllowPush();
}
Expand Down
68 changes: 68 additions & 0 deletions test/processors/checkIfWaitingAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,74 @@ describe('checkIfWaitingAuth', () => {
expect(result.error).toBe(true);
});

it('should not set allowPush when the approved push targeted a different repository', async () => {
const commitFrom = 'a'.repeat(40);
const commitTo = 'b'.repeat(40);
action = new Action('1', 'push', 'POST', 1, 'https://github.com/acme/payments.git');
action.branch = 'refs/heads/main';
action.setCommit(commitFrom, commitTo);

const approvedElsewhere = new Action(
'2',
'push',
'POST',
2,
'https://github.com/acme/sandbox.git',
);
approvedElsewhere.branch = 'refs/heads/main';
approvedElsewhere.setCommit(commitFrom, commitTo);
approvedElsewhere.id = action.id;
approvedElsewhere.authorised = true;
getPushMock.mockResolvedValue(approvedElsewhere);

const result = await checkIfWaitingAuthModule.exec(req, action);

expect(result.allowPush).toBe(false);
expect(result.url).toBe('https://github.com/acme/payments.git');
expect(result.authorised).toBe(false);
});

it('should not set allowPush when the approved push targeted a different branch', async () => {
const commitFrom = 'a'.repeat(40);
const commitTo = 'b'.repeat(40);
const url = 'https://github.com/acme/payments.git';
action = new Action('1', 'push', 'POST', 1, url);
action.branch = 'refs/heads/main';
action.setCommit(commitFrom, commitTo);

const approvedElsewhere = new Action('2', 'push', 'POST', 2, url);
approvedElsewhere.branch = 'refs/heads/feature';
approvedElsewhere.setCommit(commitFrom, commitTo);
approvedElsewhere.id = action.id;
approvedElsewhere.authorised = true;
getPushMock.mockResolvedValue(approvedElsewhere);

const result = await checkIfWaitingAuthModule.exec(req, action);

expect(result.allowPush).toBe(false);
expect(result.branch).toBe('refs/heads/main');
});

it('should set allowPush when the approved push targeted the same repository and branch', async () => {
const commitFrom = 'a'.repeat(40);
const commitTo = 'b'.repeat(40);
const url = 'https://github.com/acme/payments.git';
action = new Action('1', 'push', 'POST', 1, url);
action.branch = 'refs/heads/main';
action.setCommit(commitFrom, commitTo);

const approved = new Action('2', 'push', 'POST', 2, url);
approved.branch = 'refs/heads/main';
approved.setCommit(commitFrom, commitTo);
approved.authorised = true;
getPushMock.mockResolvedValue(approved);

const result = await checkIfWaitingAuthModule.exec(req, action);

expect(getPushMock).toHaveBeenCalledWith(action.id);
expect(result.allowPush).toBe(true);
});

it('should add step with error when getPush throws', async () => {
const error = new Error('DB error');
getPushMock.mockRejectedValue(error);
Expand Down
62 changes: 62 additions & 0 deletions test/testActionId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2026 GitProxy Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, it, expect } from 'vitest';
import { Action } from '../src/proxy/actions';

const commitFrom = 'a'.repeat(40);
const commitTo = 'b'.repeat(40);

const makePush = (url: string, branch: string): Action => {
const action = new Action('1', 'push', 'POST', 1, url);
action.branch = branch;
action.setCommit(commitFrom, commitTo);
return action;
};

describe('Action push id', () => {
it('is stable for the same repository, branch and commit range', () => {
const first = makePush('https://github.com/acme/payments.git', 'refs/heads/main');
const second = makePush('https://github.com/acme/payments.git', 'refs/heads/main');

expect(first.id).toBe(second.id);
expect(first.id.startsWith(`${commitFrom}__${commitTo}__`)).toBe(true);
});

it('differs when the same commit range is pushed to a different repository', () => {
const sandbox = makePush('https://github.com/acme/sandbox.git', 'refs/heads/main');
const payments = makePush('https://github.com/acme/payments.git', 'refs/heads/main');

expect(sandbox.id).not.toBe(payments.id);
});

it('differs when the same commit range is pushed to a different branch', () => {
const main = makePush('https://github.com/acme/payments.git', 'refs/heads/main');
const feature = makePush('https://github.com/acme/payments.git', 'refs/heads/feature');

expect(main.id).not.toBe(feature.id);
});

it('is recomputed when the branch is set after the commit range', () => {
const action = new Action('1', 'push', 'POST', 1, 'https://github.com/acme/payments.git');
action.setCommit(commitFrom, commitTo);
const idWithoutBranch = action.id;
action.setBranch('refs/heads/main');

expect(action.id).not.toBe(idWithoutBranch);
expect(action.id).toBe(makePush('https://github.com/acme/payments.git', 'refs/heads/main').id);
});
});
Loading