diff --git a/src/proxy/actions/Action.ts b/src/proxy/actions/Action.ts index 38bc9c5bd..d26c2cdf4 100644 --- a/src/proxy/actions/Action.ts +++ b/src/proxy/actions/Action.ts @@ -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'; @@ -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}`; } /** diff --git a/src/proxy/processors/push-action/checkIfWaitingAuth.ts b/src/proxy/processors/push-action/checkIfWaitingAuth.ts index 3cb1b1f84..466014371 100644 --- a/src/proxy/processors/push-action/checkIfWaitingAuth.ts +++ b/src/proxy/processors/push-action/checkIfWaitingAuth.ts @@ -20,6 +20,9 @@ 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 => { const step = new Step('checkIfWaitingAuth'); @@ -27,7 +30,7 @@ const exec = async (_req: Request, action: Action): Promise => { const existingAction = await getPush(action.id); if (existingAction) { if (!action.error) { - if (existingAction.authorised) { + if (existingAction.authorised && isSamePushTarget(existingAction, action)) { action = existingAction; action.setAllowPush(); } diff --git a/test/processors/checkIfWaitingAuth.test.ts b/test/processors/checkIfWaitingAuth.test.ts index fe05ccea8..42d86c47c 100644 --- a/test/processors/checkIfWaitingAuth.test.ts +++ b/test/processors/checkIfWaitingAuth.test.ts @@ -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); diff --git a/test/testActionId.test.ts b/test/testActionId.test.ts new file mode 100644 index 000000000..ba4e27e40 --- /dev/null +++ b/test/testActionId.test.ts @@ -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); + }); +});