Updated Code to skip files which can trigger outside agent root direc…#5566
Open
sanjuyadav24 wants to merge 5 commits into
Open
Updated Code to skip files which can trigger outside agent root direc…#5566sanjuyadav24 wants to merge 5 commits into
sanjuyadav24 wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Hardens CodeCoverageUtilities.CopyFilesFromFileListWithDirStructure against path-traversal and absolute-path-injection attacks that could cause coverage file copies to land outside the configured destination directory. The publisher now collects skipped files and emits a warning per skipped entry; new L0 tests exercise both attack vectors and the happy path.
Changes:
- Replace fragile
string.Replace-based prefix stripping withSubstring+ leading-separatorTrimStart, plus a canonicalizedPath.GetFullPathboundary check that rejects paths escaping the destination. - Add an optional
skippedFilesout-list and surface skipped entries as warnings inCodeCoverageCommands.PublishCodeCoverageAsync, backed by a newCodeCoverageFileSkippedPathTraversallocalized string. - Add three L0 tests covering path-traversal skip, leading-separator absolute-path injection, and the legitimate-copy baseline.
Show a summary per file
| File | Description |
|---|---|
| src/Agent.Worker/CodeCoverage/CodeCoverageUtilities.cs | Core fix: prefix stripping, separator trimming, and canonicalized boundary check; new optional skippedFiles parameter. |
| src/Agent.Worker/CodeCoverage/CodeCoverageCommands.cs | Passes a skippedFiles list to the utility and warns per skipped file. |
| src/Misc/layoutbin/en-US/strings.json | Adds the CodeCoverageFileSkippedPathTraversal warning string. |
| src/Test/L0/Worker/CodeCoverage/CodeCoverageUtilitiesTests.cs | Adds three L0 tests for traversal skip, absolute-path injection, and legitimate copy. |
Copilot's findings
- Files reviewed: 4/4 changed files
- Comments generated: 5
added 2 commits
May 14, 2026 16:22
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
| CodeCoverageUtilities.CopyFilesFromFileListWithDirStructure(_additionalCodeCoverageFiles, ref additionalCodeCoverageFilePath, skippedFiles); | ||
| foreach (var skipped in skippedFiles) | ||
| { | ||
| executionContext.Warning(StringUtil.Loc("CodeCoverageFileSkippedPathTraversal", skipped)); |
Contributor
There was a problem hiding this comment.
can we make this error but not fail the task...
Contributor
Author
There was a problem hiding this comment.
this will trigger a warning, not fail the pipeline
|
|
||
| if (!string.IsNullOrEmpty(commonPath)) | ||
| // FIX 1: Use Substring instead of Replace to safely remove only the prefix | ||
| if (!string.IsNullOrEmpty(commonPath) && file.StartsWith(commonPath, StringComparison.OrdinalIgnoreCase)) |
Contributor
There was a problem hiding this comment.
ignore case is not needed for case-sensitive os IMO, we shuold figure out side effects of it
added 2 commits
May 15, 2026 16:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Fix for path traversal vulnerability in
CodeCoverageUtilities.CopyFilesFromFileListWithDirStructureThe vulnerability allows an authenticated pipeline user to achieve arbitrary file write on self-hosted agents by crafting
additionalcodecoveragefilespaths in##vso[codecoverage.publish]. The flawedString.Replace+Path.Combinelogic enables sandbox escape, potentially overwriting agent binaries and achieving RCE.Description
Three-layer defense-in-depth fix for
CopyFilesFromFileListWithDirStructureinCodeCoverageUtilities.cs:Fix 1 —
Substringinstead ofReplace: The originalfile.Replace(commonPath, "")removes all occurrences of the common path string, not just the prefix. An attacker crafts filenames where after all occurrences are removed, the result becomes an absolute path. Changed tofile.StartsWith(commonPath) + file.Substring(commonPath.Length)which mathematically guarantees only the leading prefix is removed.Fix 2 —
TrimStartleading separators: .NET'sPath.Combine(dest, newFile)ignoresdestentirely ifnewFilestarts with\or/. After Fix 1, the remaining string can start with a separator.TrimStart(DirectorySeparatorChar, AltDirectorySeparatorChar)forcesnewFileto always be a relative path.Fix 3 —
GetFullPathcanonicalization boundary check: Even with Fixes 1+2,../sequences in crafted paths could navigate upward out of the destination directory.Path.GetFullPathresolves all..sequences, thenStartsWithverifies the result stays inside the destination. If not, the file is skipped and reported viaskippedFilesoutput parameter.Caller changes (
CodeCoverageCommands.cs): Passes askippedFileslist and emitsexecutionContext.Warning()for each skipped file, ensuring pipeline authors see which files were blocked.Localization (
strings.json): AddedCodeCoverageFileSkippedPathTraversalkey for the warning message.Risk Assessment (Low / Medium / High)
Low
Unit Tests Added or Updated (Yes)
3 new L0 tests added to
CodeCoverageUtilitiesTests.cs:CopyFilesSkipsPathTraversalFiles../../../evil.xmltraversalskippedFiles, not written outside destinationCopyFilesHandlesAbsolutePathInjectionReplacetrick producing\sub\evil.txtCopyFilesSucceedsWithLegitimateFilesAdditional Testing Performed
Manual Testing
Change Behind Feature Flag (No)
This is a security fix that must apply unconditionally. A feature flag would leave the vulnerability exploitable when the flag is off, defeating the purpose of the fix. The skip+warn approach already provides graceful handling without needing a kill switch.
Tech Design / Approach
NA
Documentation Changes Required (No)
No user-facing documentation changes needed. The fix is internal to the agent's file copy logic. The new warning message is self-explanatory for pipeline authors who encounter it.
Logging Added/Updated (Yes)
NA
Telemetry Added/Updated (No)
No new telemetry added. The warning log is sufficient for detection. Telemetry can be added as a follow-up if monitoring of traversal attempts is desired.
Rollback Scenario and Process (Yes)
NA
Dependency Impact Assessed and Regression Tested (Yes)
CopyFilesFromFileListWithDirStructureis called from one location:PublishCodeCoverageCommand.ProcessCommandInternalAsyncinCodeCoverageCommands.cs.skippedFiles = nullensures no other callers (if any) are affected.