From 5045c05482fc0d2b51d931e9bf2b46b8875aea32 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 05:14:04 +0000 Subject: [PATCH 1/2] bug: scope push approvals to repository and branch (sfind-51d7a881) The push id was built only from commitFrom__commitTo, and checkIfWaitingAuth reused any authorised record found under that id. An approval for a commit range on one repository or branch therefore also allowed the same range to be pushed to a different repository or branch without a second review. - Action.setCommit/setBranch now build the id as commitFrom__commitTo__sha256(url + '\\n' + branch). - checkIfWaitingAuth only reuses an approval when url, branch, commitFrom and commitTo all match the incoming push. Signed-off-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- src/proxy/actions/Action.ts | 20 +++++- .../push-action/checkIfWaitingAuth.ts | 8 ++- test/processors/checkIfWaitingAuth.test.ts | 68 +++++++++++++++++++ test/testActionId.test.ts | 62 +++++++++++++++++ 4 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 test/testActionId.test.ts 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..f2b348e8d 100644 --- a/src/proxy/processors/push-action/checkIfWaitingAuth.ts +++ b/src/proxy/processors/push-action/checkIfWaitingAuth.ts @@ -20,6 +20,12 @@ 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 && + existing.commitFrom === current.commitFrom && + existing.commitTo === current.commitTo; + // Execute function const exec = async (_req: Request, action: Action): Promise => { const step = new Step('checkIfWaitingAuth'); @@ -27,7 +33,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); + }); +}); From 1f678373d0c510bf07a962544171575f97fdf86d Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 05:20:04 +0000 Subject: [PATCH 2/2] bug: compare only url and branch when reusing an approval The push id already encodes the ref update's original old/new commits. parsePush rewrites commitFrom for new branches from pack order, so comparing it could reject a legitimate re-push of an approved range. Signed-off-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- src/proxy/processors/push-action/checkIfWaitingAuth.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/proxy/processors/push-action/checkIfWaitingAuth.ts b/src/proxy/processors/push-action/checkIfWaitingAuth.ts index f2b348e8d..466014371 100644 --- a/src/proxy/processors/push-action/checkIfWaitingAuth.ts +++ b/src/proxy/processors/push-action/checkIfWaitingAuth.ts @@ -21,10 +21,7 @@ import { getPush } from '../../../db'; import { getErrorMessage } from '../../../utils/errors'; const isSamePushTarget = (existing: Action, current: Action): boolean => - existing.url === current.url && - existing.branch === current.branch && - existing.commitFrom === current.commitFrom && - existing.commitTo === current.commitTo; + existing.url === current.url && existing.branch === current.branch; // Execute function const exec = async (_req: Request, action: Action): Promise => {