-
Notifications
You must be signed in to change notification settings - Fork 571
[msbuild] Validate extracted resource paths in UnpackLibraryResources #25911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rolfbjarne
wants to merge
6
commits into
main
Choose a base branch
from
dev/rolf/fix-zip-path-traversal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11e46fc
[msbuild] Validate extracted resource paths in UnpackLibraryResources
rolfbjarne 0e38e90
[msbuild] Use PathUtils.IsPathContained for path validation
rolfbjarne ad2a84c
[tests] Add UnpackLibraryResources path traversal tests
rolfbjarne ef3d0cb
[tests] Fix CS0433 build error in UnpackLibraryResourcesTests
rolfbjarne 609bcf5
[tests] Add pre-compiled test assemblies for UnpackLibraryResources t…
rolfbjarne 8597525
[tests] Use Mono.Cecil to create test assemblies at runtime
rolfbjarne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
76 changes: 76 additions & 0 deletions
76
tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/UnpackLibraryResourcesTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
|
|
||
| using Microsoft.Build.Utilities; | ||
|
|
||
| using Mono.Cecil; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| using Xamarin.Tests; | ||
|
|
||
| namespace Xamarin.MacDev.Tasks { | ||
| [TestFixture] | ||
| public class UnpackLibraryResourcesTests : TestBase { | ||
|
|
||
| static string CreateAssemblyWithResource (string directory, string assemblyName, string resourceName, byte [] content) | ||
| { | ||
| var asmPath = Path.Combine (directory, assemblyName + ".dll"); | ||
| var assemblyDef = AssemblyDefinition.CreateAssembly ( | ||
| new AssemblyNameDefinition (assemblyName, new Version (1, 0, 0, 0)), | ||
| assemblyName, | ||
| ModuleKind.Dll); | ||
|
|
||
| var resource = new EmbeddedResource (resourceName, ManifestResourceAttributes.Public, content); | ||
| assemblyDef.MainModule.Resources.Add (resource); | ||
| assemblyDef.Write (asmPath); | ||
|
|
||
| return asmPath; | ||
| } | ||
|
|
||
| [Test] | ||
| public void PathTraversal_IsRejected () | ||
| { | ||
| var tmpdir = Cache.CreateTemporaryDirectory (); | ||
| // Resource name "__monotouch_content_.._sEvil.txt" unmangles to "../Evil.txt" (path traversal) | ||
| var assemblyPath = CreateAssemblyWithResource (tmpdir, "TestTraversal", "__monotouch_content_.._sEvil.txt", new byte [] { 0x41 }); | ||
|
|
||
| var task = CreateTask<UnpackLibraryResources> (); | ||
| task.Prefix = "monotouch"; | ||
| task.IntermediateOutputPath = Path.Combine (tmpdir, "intermediate"); | ||
| task.ReferencedLibraries = new [] { new TaskItem (assemblyPath) }; | ||
| task.TargetFrameworkDirectory = Array.Empty<TaskItem> (); | ||
|
|
||
| ExecuteTask (task, expectedErrorCount: 1); | ||
|
|
||
| Assert.That (Engine.Logger.ErrorEvents [0].Message, Does.Contain ("would extract to")); | ||
| Assert.That (Engine.Logger.ErrorEvents [0].Message, Does.Contain ("outside")); | ||
|
|
||
| // Verify the file was NOT written outside the target directory | ||
| var escapedPath = Path.Combine (tmpdir, "intermediate", "unpack", "TestTraversal", "Evil.txt"); | ||
| Assert.That (File.Exists (escapedPath), Is.False, "File should not have been extracted outside target directory"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ValidResource_IsExtracted () | ||
| { | ||
| var tmpdir = Cache.CreateTemporaryDirectory (); | ||
| // Resource name "__monotouch_content_sub_sfile.txt" unmangles to "sub/file.txt" (valid path) | ||
| var assemblyPath = CreateAssemblyWithResource (tmpdir, "TestValid", "__monotouch_content_sub_sfile.txt", new byte [] { 0x41 }); | ||
|
|
||
| var task = CreateTask<UnpackLibraryResources> (); | ||
| task.Prefix = "monotouch"; | ||
| task.IntermediateOutputPath = Path.Combine (tmpdir, "intermediate"); | ||
| task.ReferencedLibraries = new [] { new TaskItem (assemblyPath) }; | ||
| task.TargetFrameworkDirectory = Array.Empty<TaskItem> (); | ||
|
|
||
| ExecuteTask (task, expectedErrorCount: 0); | ||
|
|
||
| var extractedPath = Path.Combine (tmpdir, "intermediate", "unpack", "TestValid", "content", "sub", "file.txt"); | ||
| Assert.That (File.Exists (extractedPath), Is.True, $"File should have been extracted to {extractedPath}"); | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.