Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,44 @@ function isSafePathSegment(value: unknown): value is string {
return typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(value) && !value.includes('..')
}

/**
* A path from a checkpoint's metadata.json joined to rootPath, or undefined when it isn't a relative path that stays
* inside rootPath: an absolute or drive path, a '..' segment, or on Windows a ':' (a drive or a stream). Checked as
* text, so a link the customer made inside the folder is still followed, as before.
*/
export function checkpointEntryPath(rootPath: string, relativePath: unknown): string | undefined {
if (typeof relativePath !== 'string' || relativePath.trim() === '') {
return undefined
}
if (
path.isAbsolute(relativePath) ||
/^[A-Za-z]:/.test(relativePath) ||
relativePath.split(/[\\/]/).includes('..') ||
(process.platform === 'win32' && relativePath.includes(':'))
) {
return undefined
}
const root = path.resolve(rootPath)
return path.resolve(root, relativePath).startsWith(root + path.sep) ? path.join(rootPath, relativePath) : undefined
}

/**
* A path from a checkpoint's metadata.json in the solution folder (see checkpointEntryPath), or undefined when it is
* not inside the solution folder or is in the job data folder (workspaceFolderName), whose files the transform uses to
* track the job.
*/
export function solutionEntryPath(solutionRootPath: string, relativePath: unknown): string | undefined {
const entryPath = checkpointEntryPath(solutionRootPath, relativePath)
if (!entryPath) {
return undefined
}
const [firstSegment] = path.relative(path.resolve(solutionRootPath), path.resolve(entryPath)).split(path.sep)
// Windows ignores trailing dots and spaces in a folder name
return firstSegment.replace(/[. ]+$/, '').toLowerCase() === workspaceFolderName.toLowerCase()
? undefined
: entryPath
}

/**
* ITGen: the folder under itgen/ for a test-run request's project, or undefined for no project (absent or empty).
* Project names come from the customer's .csproj file names, so they can hold spaces, parentheses or other
Expand Down Expand Up @@ -3171,7 +3209,11 @@ export class ATXTransformHandler {
responseType: 'buffer',
timeout: { request: 30000 },
})
const rawPath = path.join(pathToDownload, `hitl-artifact-${hitlTag || 'unknown'}`)
// The tag names the file: one that isn't a plain file name falls back like a missing one
const rawPath = path.join(
pathToDownload,
`hitl-artifact-${isSafePathSegment(hitlTag) ? hitlTag : 'unknown'}`
)
fs.writeFileSync(rawPath, response.body)
this.logging.log(`ATX: Raw artifact saved to ${rawPath} (${response.body.length} bytes)`)
} catch (downloadError) {
Expand Down Expand Up @@ -4250,6 +4292,13 @@ export class ATXTransformHandler {
solutionRootPath: string,
stepId: string
): Promise<string> {
// The job and step IDs name the folder the checkpoint is extracted to
if (!isSafePathSegment(jobId) || !isSafePathSegment(stepId)) {
this.logging.error(
`ATX: Skipped the diff artifact of step ${JSON.stringify(stepId)}: the job or step ID is not a folder name`
)
return ''
}
const maxRetries = 3

for (let attempt = 1; attempt <= maxRetries; attempt++) {
Expand Down Expand Up @@ -5061,6 +5110,13 @@ export class ATXTransformHandler {
this.logging.log(`ATX: Found ${completedSteps.length} completed steps to check for artifacts`)

for (const step of completedSteps) {
// The job and step IDs name the folder the step's checkpoint is extracted to
if (!isSafePathSegment(jobId) || !isSafePathSegment(step.StepId)) {
this.logging.error(
`ATX: Skipped completed step ${JSON.stringify(step.StepId)}: the job or step ID is not a folder name`
)
continue
}
const stepCheckpointPath = path.join(
solutionRootPath,
workspaceFolderName,
Expand Down Expand Up @@ -6279,8 +6335,12 @@ export class ATXTransformHandler {
// Handle filesAdded: Copy from {checkpointFolder}/after/{relativePath} to {solutionRootPath}/{relativePath}
for (const relativePath of filesAdded) {
try {
const sourcePath = path.join(checkpointFolderPath, 'after', relativePath)
const destPath = path.join(solutionRootPath, relativePath)
const sourcePath = checkpointEntryPath(path.join(checkpointFolderPath, 'after'), relativePath)
const destPath = solutionEntryPath(solutionRootPath, relativePath)
if (!sourcePath || !destPath) {
this.logging.error(`ATX: Skipped added file outside the solution folder: ${relativePath}`)
continue
}

// Preserve a customer edit rather than clobber it (see userModifiedFiles).
if (
Expand Down Expand Up @@ -6316,7 +6376,11 @@ export class ATXTransformHandler {
// and record the conflict rather than silently deleting their work.
for (const relativePath of filesRemoved) {
try {
const filePath = path.join(solutionRootPath, relativePath)
const filePath = solutionEntryPath(solutionRootPath, relativePath)
if (!filePath) {
this.logging.error(`ATX: Skipped removed file outside the solution folder: ${relativePath}`)
continue
}
if (fs.existsSync(filePath)) {
if (userModifiedFiles.size > 0 && userModifiedFiles.has(path.resolve(filePath))) {
try {
Expand All @@ -6342,8 +6406,12 @@ export class ATXTransformHandler {
// Handle filesUpdated: Copy from {checkpointFolder}/after/{relativePath} to {solutionRootPath}/{relativePath}
for (const relativePath of filesUpdated) {
try {
const sourcePath = path.join(checkpointFolderPath, 'after', relativePath)
const destPath = path.join(solutionRootPath, relativePath)
const sourcePath = checkpointEntryPath(path.join(checkpointFolderPath, 'after'), relativePath)
const destPath = solutionEntryPath(solutionRootPath, relativePath)
if (!sourcePath || !destPath) {
this.logging.error(`ATX: Skipped updated file outside the solution folder: ${relativePath}`)
continue
}

// The customer-reported edit clobber: if the customer edited this
// already-applied file since the last sync, preserve their version instead
Expand Down Expand Up @@ -6378,8 +6446,14 @@ export class ATXTransformHandler {
// Handle filesMoved: Use movedFilesMap to copy content from before to after location, delete before file
for (const mapping of movedFilesMap) {
try {
const beforePath = path.join(solutionRootPath, mapping.before)
const afterPath = path.join(solutionRootPath, mapping.after)
const beforePath = solutionEntryPath(solutionRootPath, mapping.before)
const afterPath = solutionEntryPath(solutionRootPath, mapping.after)
if (!beforePath || !afterPath) {
this.logging.error(
`ATX: Skipped moved file outside the solution folder: ${mapping.before} -> ${mapping.after}`
)
continue
}

// Ensure destination directory exists
const afterDir = path.dirname(afterPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,20 @@ describe('ATXTransformHandler - getTransformationPlan & helpers', () => {
expect(downloadStub.callCount).to.equal(4)
})

// The step ID names the folder its checkpoint is extracted to (LS103)
it('skips a completed step whose ID is not a folder name', async () => {
const plan = itGenPlan()
plan.Root.Children[0].Children.push({ StepId: '..', Status: 'SUCCEEDED', Children: [] })
const listed = listedArtifacts()
listed.artifacts.push({ artifactId: 'a-bad', planStepId: '..' })
sendStub.resolves(listed)

await (handler as any).downloadCompletedStepArtifacts('ws-1', JOB_ID, tmpRoot, plan)

expect(downloadStub.getCalls().map(c => c.args[2])).to.not.include('..')
expect(appliedOrder()).to.deep.equal(['proj-a', 'round-1', 'round-2'])
})

// Beam-to-IDE: the parent web job's plan. A pre-beam transform step, then the beamed repo's card with its
// unit tests, and Local Verification with LBV (and an error-fix round), an ITGen code fix and adding the tests.
const beamPlan = () => ({
Expand Down Expand Up @@ -1845,6 +1859,69 @@ describe('ATXTransformHandler - updateWorkspace & applyChanges', () => {
expect(result.filesMoved).to.equal(0)
})

// LS40: an entry must not reach a file outside the solution folder
it('skips added, updated, removed and moved entries outside the solution folder', async () => {
const outside = path.join(tmpRoot, 'outside')
fs.mkdirSync(outside)
fs.writeFileSync(path.join(outside, 'victim.cs'), '// outside')
fs.writeFileSync(path.join(outside, 'moved.cs'), '// outside')
fs.mkdirSync(path.join(checkpointFolder, 'after', 'src'), { recursive: true })
fs.writeFileSync(path.join(checkpointFolder, 'after', 'src', 'Inside.cs'), '// inside')
fs.writeFileSync(path.join(tmpRoot, 'escape.cs'), '// staged outside after/')
writeMetadata({
filesAdded: ['../outside/added.cs', 'src/../../outside/added2.cs', '../escape.cs', 'src/Inside.cs'],
filesUpdated: ['../outside/victim.cs', path.join(outside, 'victim.cs')],
filesRemoved: ['../outside/victim.cs'],
movedFilesMap: [{ before: '../outside/moved.cs', after: 'stolen.cs' }],
})

const result = await (handler as any).applyChanges(checkpointFolder, solutionRoot)

expect(result.success).to.be.true
expect(result.addedFiles).to.deep.equal(['src/Inside.cs'])
expect(fs.existsSync(path.join(solutionRoot, 'src', 'Inside.cs'))).to.be.true
expect(fs.readdirSync(outside).sort()).to.deep.equal(['moved.cs', 'victim.cs'])
expect(fs.readFileSync(path.join(outside, 'victim.cs'), 'utf-8')).to.equal('// outside')
expect(fs.existsSync(path.join(solutionRoot, 'stolen.cs'))).to.be.false
expect(fs.existsSync(path.join(tmpRoot, 'added.cs'))).to.be.false
expect((logging.error as sinon.SinonStub).args.flat().join('\n')).to.include(
'Skipped added file outside the solution folder: ../outside/added.cs'
)
})

it('skips entries in the job data folder', async () => {
const jobFile = path.join(workspaceFolderName, 'job-1', 'checkpoints', 'source-files.json')
fs.mkdirSync(path.join(solutionRoot, path.dirname(jobFile)), { recursive: true })
fs.writeFileSync(path.join(solutionRoot, jobFile), '{"sourceFiles":[]}')
fs.mkdirSync(path.join(checkpointFolder, 'after', path.dirname(jobFile)), { recursive: true })
fs.writeFileSync(path.join(checkpointFolder, 'after', jobFile), 'replaced')
writeMetadata({ filesUpdated: [jobFile], filesRemoved: [jobFile.toUpperCase()] })

const result = await (handler as any).applyChanges(checkpointFolder, solutionRoot)

expect(result.success).to.be.true
expect(result.filesUpdated).to.equal(0)
expect(result.filesRemoved).to.equal(0)
expect(fs.readFileSync(path.join(solutionRoot, jobFile), 'utf-8')).to.equal('{"sourceFiles":[]}')
})

it('skips a diff artifact whose step ID is not a folder name, before downloading it', async () => {
const downloadUrl = sinon.stub(handler as any, 'createArtifactDownloadUrl').resolves(undefined)

for (const stepId of ['..', '.', '', 'a/../b', 'a\\b']) {
const result = await (handler as any).downloadDiffArtifact(
'ws-1',
'job-1',
'art-1',
solutionRoot,
stepId
)
expect(result, stepId).to.equal('')
}

sinon.assert.notCalled(downloadUrl)
})

it('should skip removal when target file does not exist', async () => {
writeMetadata({ filesRemoved: ['NotThere.cs'] })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ describe('Test Transform handler ', () => {
}

await transformHandler.extractAllEntriesTo(pathContainingArchive, zipEntries)
expect(mockedLogging.log.args.flat()).to.include(
'Attempted to extract a file that does not exist : file1.txt'
expect(mockedLogging.error.args.flat()).to.include(
'Attempted to extract a file that does not exist: file1.txt'
)
})

Expand Down Expand Up @@ -450,6 +450,27 @@ describe('Test Transform handler ', () => {
sinon.assert.calledWith(writeFileStub, '/test/path/dir1/dir2/file1.txt')
})

it('should skip entries that escape the extraction folder (zip slip)', async () => {
mockedLogging.error.resetHistory()
const pathContainingArchive = '/test/path'
const zipEntries = [
createMockZipEntry(`..${path.sep}..${path.sep}evil.txt`, false, 'malicious'),
createMockZipEntry(`..${path.sep}outside${path.sep}`, true),
createMockZipEntry('dir1/file1.txt', false, 'content1'),
]

await transformHandler.extractAllEntriesTo(pathContainingArchive, zipEntries)

// Only the entry inside the folder is written; nothing is created or written for the escaping ones.
sinon.assert.calledOnce(writeFileStub)
sinon.assert.calledWith(writeFileStub, '/test/path/dir1/file1.txt')
sinon.assert.calledOnce(mkdirStub)
const blocked = mockedLogging.error.args
.flat()
.filter(message => String(message).includes('escaping extraction directory'))
expect(blocked).to.have.length(2)
})

it('should handle empty entry list', async () => {
const pathContainingArchive = '/test/path'
const zipEntries: IZipEntry[] = []
Expand Down Expand Up @@ -493,7 +514,9 @@ describe('Test Transform handler ', () => {
const invalidError = new Error('Invalid path') as NodeJS.ErrnoException
invalidError.code = 'EINVAL'

const zipEntries = [createMockZipEntry('invalid/../../file.txt', false, 'content')]
// An entry inside the folder, so it reaches mkdir. A ../ entry is now skipped before mkdir (see the zip
// slip test above), so it can no longer exercise this error path.
const zipEntries = [createMockZipEntry('invalid/file.txt', false, 'content')]

;(fs.promises.mkdir as sinon.SinonStub).rejects(invalidError)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,24 +404,9 @@ export class TransformHandler {
}

async extractAllEntriesTo(pathContainingArchive: string, zipEntries: AdmZip.IZipEntry[]) {
for (const entry of zipEntries) {
try {
const entryPath = path.join(pathContainingArchive, entry.entryName)
if (entry.isDirectory) {
await fs.promises.mkdir(entryPath, { recursive: true })
} else {
const parentDir = path.dirname(entryPath)
await fs.promises.mkdir(parentDir, { recursive: true })
await fs.promises.writeFile(entryPath, entry.getData())
}
} catch (extractError: any) {
if (extractError instanceof Error && 'code' in extractError && extractError.code === 'ENOENT') {
this.logging.log(`Attempted to extract a file that does not exist : ${entry.entryName}`)
} else {
throw extractError
}
}
}
// Use the shared extractor: it skips any entry that would land outside pathContainingArchive (zip slip).
// This copy used to write every entry wherever its name pointed, including ../ paths.
await Utils.extractAllEntriesTo(pathContainingArchive, zipEntries, this.logging)
}

async archivePathGenerator(exportId: string, buffer: Uint8Array[], saveToDir: string) {
Expand Down
Loading