Repository navigation
Add ClickOnce manifest foundation #1050
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b89f362
Add ClickOnce manifest foundation
dtivel 9d68fda
Remove redundant manifest reader provider
dtivel 287a62b
Fix ClickOnce manifest serialization
dtivel 80e39e1
Preserve unmodeled ClickOnce manifest XML
dtivel 241f724
Address manifest reader formatting feedback
dtivel f57c26a
Consolidate ClickOnce manifest interfaces
dtivel 4caf174
Simplify preserved manifest sanitization
dtivel 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
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
20 changes: 20 additions & 0 deletions
20
src/Sign.Core/Tools/ClickOnce/ApplicationManifestAdapter.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,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 ApplicationManifestAdapter : | ||
| ClickOnceManifestAdapter<ApplicationManifest>, | ||
| IApplicationManifest | ||
| { | ||
| internal ApplicationManifestAdapter(ApplicationManifest manifest) | ||
| : base(manifest) | ||
| { | ||
| } | ||
|
|
||
| public FileReferenceCollection FileReferences => Manifest.FileReferences; | ||
| } | ||
| } | ||
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,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
182
src/Sign.Core/Tools/ClickOnce/ClickOnceManifestReader.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,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( | ||
|
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; | ||
| } | ||
| } | ||
| } | ||
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,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; | ||
| } | ||
| } |
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,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; } | ||
| } | ||
| } |
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,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); | ||
| } | ||
| } |
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,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); | ||
| } | ||
| } |
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.