// One bounded, synthetic-testable step. Never embeds credentials or source URLs. export async function runCopyStep(p, api) { const BLOCK = 4 * 1024 * 1024 const meta = await api.getFileMetadata({ account: p.account, fileId: p.fileId, fields: 'id,size,md5Checksum,version,modifiedTime' }) const target = await api.dropboxRpc('users/get_current_account', {}) const identity = JSON.stringify([p.account, meta.id, meta.size, meta.md5Checksum, meta.version, meta.modifiedTime, target.account_id, p.dest, p.mode]) if (!meta.md5Checksum || !meta.version || !target.account_id || !Number.isSafeInteger(+meta.size)) throw new Error('SOURCE_IDENTITY_REQUIRED') if (p.expectedMd5 && p.expectedMd5 !== meta.md5Checksum) throw new Error('RECONCILIATION_REQUIRED: listed source changed') if (p.identity && p.identity !== identity) throw new Error('RECONCILIATION_REQUIRED: source/account/options changed') const size = +meta.size const read = async (start) => { const parts = []; let at = start const end = Math.min(size, start + BLOCK) while (at < end) { const r = await api.downloadFile({ account: p.account, fileId: p.fileId, start: at, maxBytes: Math.min(2 * 1024 * 1024, end - at) }) if (r.start !== at || r.end <= at || r.end > end || r.totalSize !== size || r.bytes.length !== r.end - at) throw new Error('Invalid source range') parts.push(r.bytes); at = r.end } const after = await api.getFileMetadata({ account: p.account, fileId: p.fileId, fields: 'id,size,md5Checksum,version,modifiedTime' }) if (JSON.stringify([after.id, after.size, after.md5Checksum, after.version, after.modifiedTime]) !== JSON.stringify([meta.id, meta.size, meta.md5Checksum, meta.version, meta.modifiedTime])) throw new Error('RECONCILIATION_REQUIRED: source changed while reading') const bytes = new Uint8Array(end-start); let n=0 for (const part of parts) { bytes.set(part,n); n+=part.length } return bytes } const hex = b => Array.from(b, x=>x.toString(16).padStart(2,'0')).join('') const sha = async b => new Uint8Array(await crypto.subtle.digest('SHA-256',b)) const hashBlocks = async blocks => { const b=new Uint8Array(blocks.length*32); blocks.forEach((h,i)=>b.set(h.match(/../g).map(x=>parseInt(x,16)),i*32)); return hex(await sha(b)) } const base = { ...p, identity, size } // Prehash one bounded block per step. Size alone never proves destination equality. if (!p.phase || p.phase === 'hashing') { const hashOffset=p.hashOffset??0; const blocks=p.blocks??[] if(hashOffsetsize) throw new Error('Invalid acknowledged offset') if(actual===size) { if(p.phase!=='ready_finish') return {...base,offset:actual,phase:'ready_finish'} const m=await api.finishUploadSession({sessionId:p.sessionId,offset:actual,bytes:new Uint8Array(),commit:{path:p.dest,mode:p.mode},confirm:true}) if(m.size!==size||m.content_hash!==p.contentHash) throw new Error('RECONCILIATION_REQUIRED: destination hash mismatch') return {...base,done:true,metadata:m} } const blockStart=Math.floor(actual/BLOCK)*BLOCK const bytes=await read(blockStart) if(hex(await sha(bytes))!==p.blocks[Math.floor(blockStart/BLOCK)]) throw new Error('RECONCILIATION_REQUIRED: source hash mismatch') const r=await api.appendUploadSession({sessionId:p.sessionId,offset:actual,bytes:bytes.subarray(actual-blockStart),confirm:true}) if(r.offset!==blockStart+bytes.length) throw new Error('Invalid append acknowledgement') return {...base,offset:r.offset,phase:r.offset===size?'ready_finish':'uploading'} }