Skip to content
Merged
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
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<PackageVersion Include="AzureSign.Core" Version="7.0.1" />
<PackageVersion Include="coverlet.collector" Version="10.0.1" />
<PackageVersion Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.3.11" />
<!-- 17.8.43 is the latest serviced line with net8.0 runtime assets. Newer MSBuild lines
provide only a netstandard reference assembly for net8.0 consumers. -->
<PackageVersion Include="Microsoft.Build.Tasks.Core" Version="17.8.43" />
<PackageVersion Include="Microsoft.Dynamics.BusinessCentral.Sip.Main" Version="24.0.15760" />
<PackageVersion Include="Microsoft.Extensions.Azure" Version="1.14.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.10" />
Expand Down
4 changes: 4 additions & 0 deletions scripts/VerifyNuGetPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ If ($packageFilePaths.Count -ne 1)

[string[]] $expectedEntryFullNames =
'tools/net8.0/any/sign.dll',
'tools/net8.0/any/Microsoft.Build.Framework.dll',
'tools/net8.0/any/Microsoft.Build.Tasks.Core.dll',
'tools/net8.0/any/Microsoft.Build.Utilities.Core.dll',
'tools/net8.0/any/Microsoft.NET.StringTools.dll',
'tools/net8.0/any/tools/SDK/x64/appxpackaging.dll',
'tools/net8.0/any/tools/SDK/x64/appxsip.dll',
'tools/net8.0/any/tools/SDK/x64/makeappx.exe',
Expand Down
2 changes: 2 additions & 0 deletions src/Sign.Core/Sign.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

<ItemGroup>
<PackageReference Include="AzureSign.Core" PrivateAssets="analyzers;build;compile;contentfiles" />
<!-- Redistribute the manifest utilities with Sign CLI instead of requiring an installed MSBuild instance. -->
<PackageReference Include="Microsoft.Build.Tasks.Core" />
<PackageReference Include="Microsoft.Dynamics.BusinessCentral.Sip.Main" />
<PackageReference Include="Microsoft.Extensions.Azure" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
Expand Down
20 changes: 20 additions & 0 deletions src/Sign.Core/Tools/ClickOnce/ApplicationManifestAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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 Microsoft.Build.Tasks.Deployment.ManifestUtilities;

namespace Sign.Core
Comment thread
dtivel marked this conversation as resolved.
Resolved
{
internal sealed class ApplicationManifestAdapter :
ClickOnceManifestAdapter<ApplicationManifest>,
IApplicationManifest
{
internal ApplicationManifestAdapter(ApplicationManifest manifest)
: base(manifest)
{
}

public FileReferenceCollection FileReferences => Manifest.FileReferences;
}
}
79 changes: 79 additions & 0 deletions src/Sign.Core/Tools/ClickOnce/ClickOnceManifestAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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 Microsoft.Build.Tasks.Deployment.ManifestUtilities;

namespace Sign.Core
{
internal abstract class ClickOnceManifestAdapter<TManifest> : IClickOnceManifest
where TManifest : Manifest
{
private const string TargetFrameworkVersion = "v4.5";

protected TManifest Manifest { get; }

protected ClickOnceManifestAdapter(TManifest manifest)
{
ArgumentNullException.ThrowIfNull(manifest, nameof(manifest));

Manifest = manifest;
}

public AssemblyIdentity AssemblyIdentity => Manifest.AssemblyIdentity;
public AssemblyReferenceCollection AssemblyReferences => Manifest.AssemblyReferences;
public AssemblyReference? EntryPoint => Manifest.EntryPoint;
public OutputMessageCollection OutputMessages => Manifest.OutputMessages;

public bool ReadOnly
{
get => Manifest.ReadOnly;
set => Manifest.ReadOnly = value;
}

public void ResolveFiles(IReadOnlyList<DirectoryInfo> searchDirectories)
{
ArgumentNullException.ThrowIfNull(
searchDirectories,
nameof(searchDirectories));

string[] searchPaths = new string[searchDirectories.Count];

for (int i = 0; i < searchDirectories.Count; ++i)
{
DirectoryInfo? searchDirectory = searchDirectories[i];

if (searchDirectory is null)
{
throw new ArgumentException(
message: null,
nameof(searchDirectories));
}

searchPaths[i] = searchDirectory.FullName;
}

Manifest.ResolveFiles(searchPaths);
}

public void UpdateFileInfo()
{
Manifest.UpdateFileInfo(TargetFrameworkVersion);
}

public void Write(FileInfo file)
{
ArgumentNullException.ThrowIfNull(file, nameof(file));

if (Manifest.InputStream is { CanSeek: true } inputStream)
{
inputStream.Position = 0;
}

ManifestWriter.WriteManifest(
Manifest,
file.FullName,
TargetFrameworkVersion);
}
}
}
182 changes: 182 additions & 0 deletions src/Sign.Core/Tools/ClickOnce/ClickOnceManifestReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Diagnostics.CodeAnalysis;
using System.Text;
using System.Xml;
using Microsoft.Build.Tasks.Deployment.ManifestUtilities;

namespace Sign.Core
{
internal sealed class ClickOnceManifestReader : IClickOnceManifestReader
{
private const string AssemblyV2Namespace =
"urn:schemas-microsoft-com:asm.v2";
private const string SignatureNamespace =
"http://www.w3.org/2000/09/xmldsig#";

public bool TryReadApplicationManifest(
Comment thread
dtivel marked this conversation as resolved.
Resolved
Stream stream,
[NotNullWhen(true)] out IApplicationManifest? manifest)
{
Manifest readManifest = ReadManifest(stream);

if (readManifest is not ApplicationManifest applicationManifest)
{
readManifest.InputStream.Dispose();
stream.Position = 0;
manifest = null;

return false;
}

ReplaceManifestInputStream(applicationManifest);
manifest = new ApplicationManifestAdapter(applicationManifest);

return true;
}

public bool TryReadDeployManifest(
Stream stream,
[NotNullWhen(true)] out IDeployManifest? manifest)
{
Manifest readManifest = ReadManifest(stream);

if (readManifest is not DeployManifest deployManifest)
{
readManifest.InputStream.Dispose();
stream.Position = 0;
manifest = null;

return false;
}

ReplaceManifestInputStream(deployManifest);
manifest = new DeployManifestAdapter(deployManifest);

return true;
}

private static Manifest ReadManifest(Stream stream)
{
ArgumentNullException.ThrowIfNull(stream, nameof(stream));

if (!stream.CanRead)
{
throw new ArgumentException(
"The manifest stream must be readable.",
nameof(stream));
}

if (!stream.CanSeek)
{
throw new ArgumentException(
"The manifest stream must be seekable.",
nameof(stream));
}

if (stream.Position != 0)
{
throw new ArgumentException(
"The manifest stream must be positioned at the beginning.",
nameof(stream));
}

return ManifestReader.ReadManifest(stream, preserveStream: true);
}

private static void ReplaceManifestInputStream(Manifest manifest)
{
Stream input = manifest.InputStream;
Stream? output = null;

try
{
output = CreateSanitizedManifestInputStream(input);
manifest.InputStream = output;
output = null;
}
finally
{
output?.Dispose();
input.Dispose();
}
}

private static Stream CreateSanitizedManifestInputStream(Stream input)
{
input.Position = 0;
MemoryStream output = new();
XmlReaderSettings readerSettings = new()
{
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null
};
XmlWriterSettings writerSettings = new()
{
CloseOutput = false,
Encoding = new UTF8Encoding(
encoderShouldEmitUTF8Identifier: false),
Indent = false
};

try
{
using XmlReader reader =
XmlReader.Create(input, readerSettings);
XmlDocument document = new()
{
PreserveWhitespace = true,
XmlResolver = null
};
document.Load(reader);

XmlElement root = document.DocumentElement ??
throw new XmlException(
"The manifest XML does not have a document element.");

for (XmlNode? node = root.FirstChild;
node is not null;)
{
XmlNode? nextNode = node.NextSibling;

if (node is XmlElement element &&
IsStaleSigningElement(element))
{
root.RemoveChild(node);
}

node = nextNode;
}

using (XmlWriter writer =
XmlWriter.Create(output, writerSettings))
{
document.Save(writer);
}

output.Position = 0;

return output;
}
catch
{
output.Dispose();
throw;
}
}

private static bool IsStaleSigningElement(XmlElement element)
{
bool isPublisherIdentity =
element.LocalName == "publisherIdentity" &&
element.NamespaceURI == AssemblyV2Namespace;
bool isSignature =
element.LocalName == "Signature" &&
element.NamespaceURI == SignatureNamespace;

return isPublisherIdentity || isSignature;
}
}
}
20 changes: 20 additions & 0 deletions src/Sign.Core/Tools/ClickOnce/DeployManifestAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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 Microsoft.Build.Tasks.Deployment.ManifestUtilities;

namespace Sign.Core
{
internal sealed class DeployManifestAdapter :
ClickOnceManifestAdapter<DeployManifest>,
IDeployManifest
{
internal DeployManifestAdapter(DeployManifest manifest)
: base(manifest)
{
}

public bool MapFileExtensions => Manifest.MapFileExtensions;
}
}
13 changes: 13 additions & 0 deletions src/Sign.Core/Tools/ClickOnce/IApplicationManifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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 Microsoft.Build.Tasks.Deployment.ManifestUtilities;

namespace Sign.Core
{
internal interface IApplicationManifest : IClickOnceManifest
{
FileReferenceCollection FileReferences { get; }
}
}
21 changes: 21 additions & 0 deletions src/Sign.Core/Tools/ClickOnce/IClickOnceManifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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 Microsoft.Build.Tasks.Deployment.ManifestUtilities;

namespace Sign.Core
{
internal interface IClickOnceManifest
{
AssemblyIdentity AssemblyIdentity { get; }
AssemblyReferenceCollection AssemblyReferences { get; }
AssemblyReference? EntryPoint { get; }
OutputMessageCollection OutputMessages { get; }
bool ReadOnly { get; set; }

void ResolveFiles(IReadOnlyList<DirectoryInfo> searchDirectories);
void UpdateFileInfo();
void Write(FileInfo file);
}
}
19 changes: 19 additions & 0 deletions src/Sign.Core/Tools/ClickOnce/IClickOnceManifestReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Diagnostics.CodeAnalysis;

namespace Sign.Core
{
internal interface IClickOnceManifestReader
{
bool TryReadApplicationManifest(
Stream stream,
[NotNullWhen(true)] out IApplicationManifest? manifest);

bool TryReadDeployManifest(
Stream stream,
[NotNullWhen(true)] out IDeployManifest? manifest);
}
}
Loading