From 54323267f54c328da2f64aa403b10e278c91711f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=9F=BA=E9=AD=81?= <56265583+fengjikui@users.noreply.github.com> Date: Tue, 7 Jul 2026 00:55:05 +0800 Subject: [PATCH] fix(mirror): reject empty commit uploads (#303) * fix(mirror): reject empty commit uploads * refactor(mirror): unify totalParts validation into shared predicate Consolidate the duplicated positive-integer check and error message into isValidTotalParts/TOTAL_PARTS_INVALID_MESSAGE, reused by the commit route and extractPartsToStaging. Validate totalParts before the session lookup to match the parts route, and update the delete-session test to use a valid totalParts so it unambiguously covers the 404 path. --------- Co-authored-by: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> --- .../routes/internal/repo-mirror-helpers.ts | 10 ++++++++++ backend/src/routes/internal/repo-mirror.ts | 4 +++- .../test/routes/internal/repo-mirror.test.ts | 18 +++++++++++++++++- .../test/services/repo-mirror-helpers.test.ts | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/backend/src/routes/internal/repo-mirror-helpers.ts b/backend/src/routes/internal/repo-mirror-helpers.ts index 130a2fce..d8874385 100644 --- a/backend/src/routes/internal/repo-mirror-helpers.ts +++ b/backend/src/routes/internal/repo-mirror-helpers.ts @@ -10,6 +10,12 @@ import { getReposPath } from '@opencode-manager/shared/config/env' export const MIRROR_CHUNK_SIZE = 8 * 1024 * 1024 const STALE_UPLOAD_MS = 24 * 60 * 60 * 1000 +export const TOTAL_PARTS_INVALID_MESSAGE = 'totalParts must be a positive integer' + +export function isValidTotalParts(totalParts: number): boolean { + return Number.isInteger(totalParts) && totalParts >= 1 +} + export interface UploadMeta { uploadId: string repoId: number @@ -88,6 +94,10 @@ export interface ExtractResult { } export async function extractPartsToStaging(uploadId: string, totalParts: number, gzip: boolean): Promise { + if (!isValidTotalParts(totalParts)) { + throw new Error(TOTAL_PARTS_INVALID_MESSAGE) + } + const stagingParent = getStagingRoot() mkdirSync(stagingParent, { recursive: true }) const staging = mkdtempSync(join(stagingParent, 'recv-')) diff --git a/backend/src/routes/internal/repo-mirror.ts b/backend/src/routes/internal/repo-mirror.ts index fd0ec3f2..30349a13 100644 --- a/backend/src/routes/internal/repo-mirror.ts +++ b/backend/src/routes/internal/repo-mirror.ts @@ -15,6 +15,8 @@ import { getErrorMessage } from '../../utils/error-utils' import { safeGitOut, gitOut } from './repo-sync-helpers' import { MIRROR_CHUNK_SIZE, + TOTAL_PARTS_INVALID_MESSAGE, + isValidTotalParts, createUploadSession, readUploadMeta, deleteUploadSession, @@ -246,7 +248,7 @@ export function createInternalRepoMirrorRoutes(db: Database) { const { uploadId, totalParts, gzip } = body if (!uploadId) return c.json({ error: 'uploadId required' }, 400) - if (!Number.isInteger(totalParts) || totalParts < 0) return c.json({ error: 'totalParts must be a non-negative integer' }, 400) + if (!isValidTotalParts(totalParts)) return c.json({ error: TOTAL_PARTS_INVALID_MESSAGE }, 400) const meta = await readUploadMeta(uploadId) if (!meta) return c.json({ error: 'upload session not found' }, 404) diff --git a/backend/test/routes/internal/repo-mirror.test.ts b/backend/test/routes/internal/repo-mirror.test.ts index e800ef20..4abe95d3 100644 --- a/backend/test/routes/internal/repo-mirror.test.ts +++ b/backend/test/routes/internal/repo-mirror.test.ts @@ -621,6 +621,22 @@ describe('internal-repo-mirror routes', () => { expect(res.status).toBe(404) }) + it('rejects commit requests with zero uploaded parts', async () => { + const targetPath = join(getTmpRoot(), 'empty-parts-repo') + mockEnsureMirrorTargetPath.mockReturnValue({ localPath: 'empty-parts-repo', fullPath: targetPath }) + mockCreateRepoRow.mockImplementation((_db: any, input: any) => ({ repo: { id: 1, fullPath: input.fullPath, localPath: input.localPath }, created: true })) + + const beginRes = await begin(app, 0, { create: true, name: 'empty-parts-repo' }) + expect(beginRes.status).toBe(200) + const beginJson = (await beginRes.json()) as BeginResponse + + const commitRes = await commit(app, beginJson.repoId, beginJson.uploadId, 0) + expect(commitRes.status).toBe(400) + const json = (await commitRes.json()) as { error: string } + expect(json.error).toBe('totalParts must be a positive integer') + expect(mockUpdateLastPulled).not.toHaveBeenCalled() + }) + it('rolls back created DB row when commit fails on invalid tarball', async () => { const targetPath = join(getTmpRoot(), 'test-repo') mockEnsureMirrorTargetPath.mockReturnValue({ localPath: 'test-repo', fullPath: targetPath }) @@ -739,7 +755,7 @@ describe('internal-repo-mirror routes', () => { expect(delRes.status).toBe(200) expect(mockDeleteRepo).toHaveBeenCalledWith({}, 11) - const commitRes = await commit(app, beginJson.repoId, beginJson.uploadId, 0) + const commitRes = await commit(app, beginJson.repoId, beginJson.uploadId, 1) expect(commitRes.status).toBe(404) }) }) diff --git a/backend/test/services/repo-mirror-helpers.test.ts b/backend/test/services/repo-mirror-helpers.test.ts index a3b47cb4..9095470e 100644 --- a/backend/test/services/repo-mirror-helpers.test.ts +++ b/backend/test/services/repo-mirror-helpers.test.ts @@ -292,3 +292,22 @@ describe('isRepoInUse', () => { expect(result).toBe(true) }) }) + +describe('extractPartsToStaging', () => { + beforeEach(() => { + vi.clearAllMocks() + tmpRoot = path.join(os.tmpdir(), `mirror-test-${Date.now()}-${Math.random().toString(36).slice(2)}`) + fs.mkdirSync(tmpRoot, { recursive: true }) + }) + + afterEach(() => { + fs.rmSync(tmpRoot, { recursive: true, force: true }) + }) + + it('rejects zero-part commits before creating a staging directory', async () => { + const { extractPartsToStaging } = await import('../../src/routes/internal/repo-mirror-helpers') + + await expect(extractPartsToStaging('upload-id', 0, false)).rejects.toThrow('totalParts must be a positive integer') + expect(fs.existsSync(path.join(tmpRoot, '.ocm-staging'))).toBe(false) + }) +})