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>
This commit is contained in:
冯基魁andChris Scott authored and GitHub committed 2026-07-06 12:55:05 -04:00
1 parent 256078341b
commit 54323267f5
4 files changed
+49 -2

No files matched your search

@@ -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<ExtractResult> {
if (!isValidTotalParts(totalParts)) {
throw new Error(TOTAL_PARTS_INVALID_MESSAGE)
}
const stagingParent = getStagingRoot()
mkdirSync(stagingParent, { recursive: true })
const staging = mkdtempSync(join(stagingParent, 'recv-'))
+3 -1
View File
@@ -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)
@@ -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)
})
})
@@ -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)
})
})