-
Notifications
You must be signed in to change notification settings - Fork 64
[Java.Interop.Tools.Maven] Assert resolved cache paths stay under CacheDirectory #1480
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
Merged
+209
−6
Merged
Changes from all commits
Commits
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
185 changes: 185 additions & 0 deletions
185
tests/Java.Interop.Tools.Maven-Tests/CachedMavenRepositoryTests.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,185 @@ | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Java.Interop.Tools.Maven.Models; | ||
| using Java.Interop.Tools.Maven.Repositories; | ||
|
|
||
| namespace Java.Interop.Tools.Maven_Tests; | ||
|
|
||
| public class CachedMavenRepositoryTests | ||
| { | ||
| string cache_dir = ""; | ||
|
|
||
| [SetUp] | ||
| public void SetUp () | ||
| { | ||
| cache_dir = Path.Combine (Path.GetTempPath (), "Java.Interop.Tools.Maven-Tests", Path.GetRandomFileName ()); | ||
| Directory.CreateDirectory (cache_dir); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown () | ||
| { | ||
| if (Directory.Exists (cache_dir)) | ||
| Directory.Delete (cache_dir, recursive: true); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetArtifactFilePath_HappyPath_ReturnsExpectedLayout () | ||
| { | ||
| var artifact = new Artifact ("com.example", "lib", "1.0.0"); | ||
| var inner = new StubRepository ("central", artifact, "lib-1.0.0.jar", new byte [] { 1, 2, 3 }); | ||
| var cache = new CachedMavenRepository (cache_dir, inner); | ||
|
|
||
| var expected = Path.GetFullPath (Path.Combine (cache_dir, "central", "com.example", "lib", "1.0.0", "lib-1.0.0.jar")); | ||
| var actual = cache.GetArtifactFilePath (artifact, "lib-1.0.0.jar"); | ||
|
|
||
| Assert.AreEqual (expected, actual); | ||
| Assert.IsFalse (File.Exists (actual), "GetArtifactFilePath must not create/download the file."); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TryGetFilePath_HappyPath_DownloadsAndReturnsExpectedPath () | ||
| { | ||
| var artifact = new Artifact ("com.example", "lib", "1.0.0"); | ||
| var content = new byte [] { 1, 2, 3 }; | ||
| var inner = new StubRepository ("central", artifact, "lib-1.0.0.jar", content); | ||
| var cache = new CachedMavenRepository (cache_dir, inner); | ||
|
|
||
| var expected = Path.GetFullPath (Path.Combine (cache_dir, "central", "com.example", "lib", "1.0.0", "lib-1.0.0.jar")); | ||
|
|
||
| Assert.IsTrue (cache.TryGetFilePath (artifact, "lib-1.0.0.jar", out var path)); | ||
| Assert.AreEqual (expected, path); | ||
| Assert.IsTrue (File.Exists (path)); | ||
| CollectionAssert.AreEqual (content, File.ReadAllBytes (path!)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetArtifactFilePath_RelativeFilename_Throws () | ||
| { | ||
| var artifact = new Artifact ("com.example", "lib", "1.0.0"); | ||
| var inner = new ThrowingRepository ("central"); | ||
| var cache = new CachedMavenRepository (cache_dir, inner); | ||
|
|
||
| var artifact_dir = Path.GetDirectoryName (cache.GetArtifactFilePath (artifact, "anchor.jar"))!; | ||
| var outside = Path.Combine (Path.GetDirectoryName (cache_dir)!, Path.GetFileName (cache_dir) + "-sibling", "relative.jar"); | ||
| var malicious = Path.GetRelativePath (artifact_dir, outside); | ||
|
|
||
| Assert.Throws<InvalidOperationException> (() => cache.GetArtifactFilePath (artifact, malicious)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TryGetFilePath_RelativeFilename_Throws () | ||
| { | ||
| var artifact = new Artifact ("com.example", "lib", "1.0.0"); | ||
| var inner = new ThrowingRepository ("central"); | ||
| var cache = new CachedMavenRepository (cache_dir, inner); | ||
|
|
||
| var artifact_dir = Path.GetDirectoryName (cache.GetArtifactFilePath (artifact, "anchor.jar"))!; | ||
| var outside = Path.Combine (Path.GetDirectoryName (cache_dir)!, Path.GetFileName (cache_dir) + "-sibling", "relative.jar"); | ||
| var malicious = Path.GetRelativePath (artifact_dir, outside); | ||
|
|
||
| Assert.Throws<InvalidOperationException> (() => cache.TryGetFilePath (artifact, malicious, out _)); | ||
| Assert.AreEqual (0, inner.CallCount, "Inner repository must not be consulted for an escaping path."); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TryGetFile_RelativeFilename_Throws () | ||
| { | ||
| var artifact = new Artifact ("com.example", "lib", "1.0.0"); | ||
| var inner = new ThrowingRepository ("central"); | ||
| var cache = new CachedMavenRepository (cache_dir, inner); | ||
|
|
||
| var artifact_dir = Path.GetDirectoryName (cache.GetArtifactFilePath (artifact, "anchor.jar"))!; | ||
| var outside = Path.Combine (Path.GetDirectoryName (cache_dir)!, Path.GetFileName (cache_dir) + "-sibling", "relative.jar"); | ||
| var malicious = Path.GetRelativePath (artifact_dir, outside); | ||
|
|
||
| Assert.Throws<InvalidOperationException> (() => cache.TryGetFile (artifact, malicious, out _)); | ||
| Assert.AreEqual (0, inner.CallCount, "Inner repository must not be consulted for an escaping path."); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetFilePathAsync_RelativeFilename_Throws () | ||
| { | ||
| var artifact = new Artifact ("com.example", "lib", "1.0.0"); | ||
| var inner = new ThrowingRepository ("central"); | ||
| var cache = new CachedMavenRepository (cache_dir, inner); | ||
|
|
||
| var artifact_dir = Path.GetDirectoryName (cache.GetArtifactFilePath (artifact, "anchor.jar"))!; | ||
| var outside = Path.Combine (Path.GetDirectoryName (cache_dir)!, Path.GetFileName (cache_dir) + "-sibling", "relative.jar"); | ||
| var malicious = Path.GetRelativePath (artifact_dir, outside); | ||
|
|
||
| Assert.ThrowsAsync<InvalidOperationException> (async () => | ||
| await cache.GetFilePathAsync (artifact, malicious, CancellationToken.None)); | ||
| Assert.AreEqual (0, inner.CallCount, "Inner repository must not be consulted for an escaping path."); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetArtifactFilePath_RelativeRepositoryName_Throws () | ||
| { | ||
| var artifact = new Artifact ("com.example", "lib", "1.0.0"); | ||
| var inner = new ThrowingRepository (Path.Combine ("..", Path.GetFileName (cache_dir) + "-sibling")); | ||
| var cache = new CachedMavenRepository (cache_dir, inner); | ||
|
|
||
| Assert.Throws<InvalidOperationException> (() => cache.GetArtifactFilePath (artifact, "lib-1.0.0.jar")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetArtifactFilePath_SiblingPrefixCacheDirectory_Throws () | ||
| { | ||
| var artifact = new Artifact ("com.example", "lib", "1.0.0"); | ||
| var sibling = cache_dir + "-sibling"; | ||
| var repo_name = Path.GetRelativePath (cache_dir, sibling); | ||
| var inner = new ThrowingRepository (repo_name); | ||
| var cache = new CachedMavenRepository (cache_dir, inner); | ||
|
|
||
| Assert.Throws<InvalidOperationException> (() => cache.GetArtifactFilePath (artifact, "lib-1.0.0.jar")); | ||
| } | ||
|
|
||
| sealed class StubRepository : IMavenRepository | ||
| { | ||
| readonly Artifact expected; | ||
| readonly string expected_filename; | ||
| readonly byte [] content; | ||
|
|
||
| public StubRepository (string name, Artifact expected, string filename, byte [] content) | ||
| { | ||
| Name = name; | ||
| this.expected = expected; | ||
| this.expected_filename = filename; | ||
| this.content = content; | ||
| } | ||
|
|
||
| public string Name { get; } | ||
|
|
||
| public bool TryGetFile (Artifact artifact, string filename, [NotNullWhen (true)] out Stream? stream) | ||
| { | ||
| if (artifact.GroupId == expected.GroupId && artifact.Id == expected.Id && artifact.Version == expected.Version && filename == expected_filename) { | ||
| stream = new MemoryStream (content); | ||
| return true; | ||
| } | ||
| stream = null; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| sealed class ThrowingRepository : IMavenRepository | ||
| { | ||
| public ThrowingRepository (string name) | ||
| { | ||
| Name = name; | ||
| } | ||
|
|
||
| public string Name { get; } | ||
|
|
||
| public int CallCount { get; private set; } | ||
|
|
||
| public bool TryGetFile (Artifact artifact, string filename, [NotNullWhen (true)] out Stream? stream) | ||
| { | ||
| CallCount++; | ||
| throw new InvalidOperationException ("Inner repository should not be consulted when the resolved path escapes the cache directory."); | ||
| } | ||
| } | ||
| } |
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.