Skip to content
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

Illegal characters in file path #2791

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* DEP: Remove spurious references to `System.Collections.Immutable`.
* DEP: Update `Microsoft.Data.SqlClient` reference from 2.1.2 to 2.1.7 in `WorkItems` and `Sarif.Multitool.Library` to resolve [CVE-2024-0056](https://github.com/advisories/GHSA-98g6-xh36-x2p7).
* DEP: Update `System.Data.SqlClient` reference from 4.8.5 to 4.8.6 in `WorkItems` to resolve [CVE-2024-0056](https://github.com/advisories/GHSA-98g6-xh36-x2p7).
* BUG: Eliminate `ArgumentException: Illegal characters in path` in `ZipArchiveArtifact.GetArtifactData` when the archive entry contains an illegal file name or path character.
* BUG: Update `Stack.Create` method to populate missing `PhysicalLocation` instances when stack frames reference relative file paths.
* BUG: Fix `UnsupportedOperationException` in `ZipArchiveArtifact`.
* BUG: Fix `MultithreadedAnalyzeCommandBase` to return rich return code with the `--rich-return-code` option.
Expand Down
37 changes: 37 additions & 0 deletions src/Sarif/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -455,5 +456,41 @@ public string PathGetFileNameWithoutExtension(string path)
{
return Path.GetFileNameWithoutExtension(path);
}

// Returns the extension of the given path. The returned value includes the
// period (".") character of the extension except when you have a terminal period when you get String.Empty, such as ".exe" or
// ".cpp". The returned value is null if the given path is
// null or if the given path does not include an extension.
//
public static string SafePathGetExtension(string path)
{
// https://referencesource.microsoft.com/#mscorlib/system/io/path.cs,f424e433705aeb09
if (path == null)
{
return null;
}

int length = path.Length;
for (int i = length; --i >= 0;)
{
char ch = path[i];
if (ch == '.')
{
if (i != length - 1)
{
return path.Substring(i, length - i);
}
else
{
return string.Empty;
}
}
if (ch == Path.DirectorySeparatorChar || ch == Path.AltDirectorySeparatorChar || ch == Path.VolumeSeparatorChar)
{
break;
}
}
return string.Empty;
}
}
}
3 changes: 1 addition & 2 deletions src/Sarif/ZipArchiveArtifact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Microsoft.CodeAnalysis.Sarif
{

public class ZipArchiveArtifact : IEnumeratedArtifact
{
private readonly ISet<string> binaryExtensions;
Expand Down Expand Up @@ -91,7 +90,7 @@ public byte[] Bytes
{
if (this.contents == null && this.bytes == null)
{
string extension = Path.GetExtension(Uri.ToString());
string extension = FileSystem.SafePathGetExtension(Uri.ToString());
if (this.binaryExtensions.Contains(extension))
{
// The underlying System.IO.Compression.DeflateStream throws on reads to get_Length.
Expand Down
4 changes: 2 additions & 2 deletions src/Test.UnitTests.Sarif/ArtifactProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ArtifactProviderTests
[Fact]
public void MultithreadedZipArchiveArtifactProvider_RetrieveSizeInBytesBeforeRetrievingContents()
{
string entryContents = $"{Guid.NewGuid}";
string entryContents = $"{Guid.NewGuid()}";
ZipArchive zip = CreateZipArchiveWithTextContents("test.txt", entryContents);
var artifactProvider = new MultithreadedZipArchiveArtifactProvider(zip, FileSystem.Instance);

Expand Down Expand Up @@ -125,7 +125,7 @@ private static ZipArchive CreateZipArchiveWithBinaryContents(string fileName, by
using (var populateArchive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
{
ZipArchiveEntry entry = populateArchive.CreateEntry(fileName, CompressionLevel.NoCompression);
entry.Open().Write(bytes);
entry.Open().Write(bytes, 0, bytes.Length);
}
stream.Flush();
stream.Position = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Test.UnitTests.Sarif/EnumeratedArtifactTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void EnumeratedArtifact_TextFile_OnDisk()
using var tempFile = new TempFile();
string filePath = tempFile.Name;

File.WriteAllText(filePath, $"{Guid.NewGuid}");
File.WriteAllText(filePath, $"{Guid.NewGuid()}");

var artifact = new EnumeratedArtifact(new FileSystem())
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public void EnumeratedArtifact_TextFile_SeekableStream()
using var tempFile = new TempFile();
string filePath = tempFile.Name;

File.WriteAllText(filePath, $"{Guid.NewGuid}");
File.WriteAllText(filePath, $"{Guid.NewGuid()}");

var artifact = new EnumeratedArtifact(new FileSystem())
{
Expand Down
8 changes: 7 additions & 1 deletion src/Test.UnitTests.Sarif/Test.UnitTests.Sarif.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), build.props))\build.props" />

<PropertyGroup>
<TargetFrameworks>net6.0;</TargetFrameworks>
<TargetFrameworks>net472;</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>Test.UnitTests.Sarif</RootNamespace>
Expand Down Expand Up @@ -356,4 +356,10 @@
<ItemGroup>
<EmbeddedResource Include="TestData\FileEncoding\Utf8.txt" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Web">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\System.Web.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
16 changes: 15 additions & 1 deletion src/Test.UnitTests.Sarif/ZipArchiveArtifactTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ namespace Test.UnitTests.Sarif
{
public class ZipArchiveArtifactTests
{
[Fact]
public void ZipArchiveArtifact_IllegalFileAndPathCharacters()
{
string text = $"{Guid.NewGuid()}";
byte[] contents = Encoding.UTF8.GetBytes(text);
string filePath = $"{Path.GetInvalidPathChars()[0]}{Path.GetInvalidFileNameChars()[1]}MyZippedFile.txt";
ZipArchive archive = EnumeratedArtifactTests.CreateZipArchive(filePath, contents);

foreach (IEnumeratedArtifact entry in new MultithreadedZipArchiveArtifactProvider(archive, new FileSystem()).Artifacts)
{
entry.IsBinary.Should().BeFalse();
entry.Contents.Should().BeEquivalentTo(text);
}
}
[Fact]
public void ZipArchiveArtifact_BytesAndContentsCannotBeSet()
{
Expand Down Expand Up @@ -66,7 +80,7 @@ public void ZipArchiveArtifact_NonNullArchiveAndEntryAreRequired()
}

[Fact]
public void ZipArchiveArtifact_MixedTxtAndBinaryZipEntry()
public void ZipArchiveArtifact_MixedTextAndBinaryZipEntry()
{
string textData = Guid.NewGuid().ToString();

Expand Down
Loading