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
7 changes: 6 additions & 1 deletion src/Sign.Core/Tools/VsixSignTool/OpcPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ internal sealed class OpcPart : IEquatable<OpcPart>

internal OpcPart(OpcPackage package, string path, ZipArchiveEntry entry, OpcPackageFileMode mode)
{
if (path.Contains('#'))
{
throw new InvalidDataException($"Package part name '{path}' contains the unsupported '#' character.");
}

Uri = new Uri(OpcPackage.BasePackageUri, path);
Package = package;
_path = path;
Expand Down Expand Up @@ -111,4 +116,4 @@ private OpcRelationships ConstructRelationships()
/// <inheritdoc />
public override int GetHashCode() => Uri.GetHashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE.txt file in the project root for more information.

using System.IO.Compression;
using System.Globalization;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using Microsoft.Extensions.Logging;
using Sign.Core.Timestamp;
using Xunit.Abstractions;

Expand Down Expand Up @@ -70,6 +72,40 @@ public static IEnumerable<object[]> RsaSigningTheories
}
}

[Theory]
[InlineData("ab#c.txt")]
[InlineData("folder/ab#c.txt")]
public async Task ShouldRejectPartNameContainingFragmentDelimiter(string entryName)
{
string path = CreatePackageWithEntry(entryName);
VsixSignTool signTool = new(Moq.Mock.Of<ILogger<IVsixSignTool>>());

using (X509Certificate2 certificate = _pfxFilesFixture.GetPfx(
keySizeInBits: 2048,
HashAlgorithmName.SHA256))
using (RSA? rsaPrivateKey = certificate.GetRSAPrivateKey())
{
SignConfigurationSet configuration = new(
publicCertificate: certificate,
signatureDigestAlgorithm: HashAlgorithmName.SHA256,
fileDigestAlgorithm: HashAlgorithmName.SHA256,
signingKey: rsaPrivateKey!);
SignOptions options = new(
fileHashAlgorithm: HashAlgorithmName.SHA256,
timestampService: null!);

InvalidDataException exception = await Assert.ThrowsAsync<InvalidDataException>(
() => signTool.SignAsync(new FileInfo(path), configuration, options));

Assert.Contains(entryName, exception.Message, StringComparison.Ordinal);
}

using (OpcPackage package = OpcPackage.Open(path))
{
Assert.Empty(package.GetSignatures());
}
}

[Theory]
[MemberData(nameof(RsaTimestampTheories))]
public async Task ShouldTimestampFileWithRsa(int keySizeInBits, HashAlgorithmName hashAlgorithmName, HashAlgorithmName timestampDigestAlgorithm)
Expand Down Expand Up @@ -274,6 +310,21 @@ public static IEnumerable<object[]> RsaTimestampTheories
}
}

private string CreatePackageWithEntry(string entryName)
{
string path = Path.GetTempFileName();
_shadowFiles.Add(path);
File.Copy(SamplePackage, path, overwrite: true);

using (ZipArchive archive = ZipFile.Open(path, ZipArchiveMode.Update))
using (StreamWriter writer = new(archive.CreateEntry(entryName).Open()))
{
writer.Write("test");
}

return path;
}

private OpcPackage ShadowCopyPackage(string packagePath, out string path, OpcPackageFileMode mode = OpcPackageFileMode.Read)
{
string temp = Path.GetTempFileName();
Expand Down