Skip to content

Commit 682ae61

Browse files
committed
fix(hidi): retain signed assembly in official tool package
Replace the SDK tool publish input with the exact ESRP-signed staging assembly and verify payload provenance before NuGet signing. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 61e122e7-9a38-4455-8cb1-343245120242
1 parent 28b6c51 commit 682ae61

6 files changed

Lines changed: 228 additions & 35 deletions

File tree

‎.azure-pipelines/hidi-release.yml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,17 @@ extends:
185185
MaxRetryAttempts: '5'
186186
PendingAnalysisWaitTimeoutMinutes: '5'
187187
- pwsh: |
188-
Copy-Item artifacts\hidi\signing\Microsoft.OpenApi.Hidi.dll src\Microsoft.OpenApi.Hidi\bin\$(buildConfiguration)\net8.0\Microsoft.OpenApi.Hidi.dll
188+
$ErrorActionPreference = 'Stop'
189189
Copy-Item artifacts\hidi\signing\Microsoft.OpenApi.Hidi.exe artifacts\hidi\win-x64\Microsoft.OpenApi.Hidi.exe
190190
New-Item -ItemType Directory -Force '$(Build.ArtifactStagingDirectory)\hidi' | Out-Null
191-
dotnet pack src\Microsoft.OpenApi.Hidi\Microsoft.OpenApi.Hidi.csproj -c $(buildConfiguration) --no-build --include-symbols --include-source /p:SymbolPackageFormat=snupkg -o '$(Build.ArtifactStagingDirectory)\hidi'
191+
dotnet pack src\Microsoft.OpenApi.Hidi\Microsoft.OpenApi.Hidi.csproj -c $(buildConfiguration) --no-build --include-symbols --include-source /p:SymbolPackageFormat=snupkg '/p:HidiSignedAssemblyPath=$(Build.SourcesDirectory)\artifacts\hidi\signing\Microsoft.OpenApi.Hidi.dll' -o '$(Build.ArtifactStagingDirectory)\hidi'
192192
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
193193
Compress-Archive -Path artifacts\hidi\win-x64\* -DestinationPath '$(Build.ArtifactStagingDirectory)\hidi\hidi-win-x64-$(HidiVersion).zip'
194194
Copy-Item artifacts\hidi\win-x64\Microsoft.OpenApi.Hidi.exe '$(Build.ArtifactStagingDirectory)\hidi\Microsoft.OpenApi.Hidi.exe'
195195
displayName: Pack signed hidi binaries
196+
- pwsh: |
197+
.\scripts\verify-hidi-package-assembly.ps1 -PackagePath '$(Build.ArtifactStagingDirectory)\hidi\Microsoft.OpenApi.Hidi.$(HidiVersion).nupkg' -SignedAssemblyPath 'artifacts\hidi\signing\Microsoft.OpenApi.Hidi.dll'
198+
displayName: Verify signed Hidi DLL payload before NuGet signing
196199
- task: EsrpCodeSigning@6
197200
displayName: Sign hidi NuGet package
198201
inputs:

‎.github/workflows/sonarcloud.yml‎

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,59 +60,65 @@ jobs:
6060
- name: Install PowerShell test dependency
6161
shell: pwsh
6262
run: Install-Module Pester -RequiredVersion 5.7.1 -Scope CurrentUser -Force
63-
- name: Test Hidi NuGet helper with measured coverage
63+
- name: Test Hidi NuGet helpers with measured coverage
6464
shell: pwsh
6565
run: |
6666
$ErrorActionPreference = 'Stop'
6767
Import-Module Pester -RequiredVersion 5.7.1 -ErrorAction Stop
68-
$helperPath = 'scripts/check-nuget-package-published.ps1'
69-
$helper = (Resolve-Path $helperPath).Path
68+
$helperPaths = @('scripts/check-nuget-package-published.ps1', 'scripts/verify-hidi-package-assembly.ps1')
69+
$helpers = @($helperPaths | ForEach-Object { (Resolve-Path $_).Path })
7070
$outputRoot = Join-Path $PWD 'artifacts\hidi\powershell-coverage'
7171
New-Item -ItemType Directory -Force -Path $outputRoot | Out-Null
7272
$configuration = New-PesterConfiguration
73-
$configuration.Run.Path = 'test\Microsoft.OpenApi.Hidi.Tests\check-nuget-package-published.Tests.ps1'
73+
$configuration.Run.Path = @('test\Microsoft.OpenApi.Hidi.Tests\check-nuget-package-published.Tests.ps1', 'test\Microsoft.OpenApi.Hidi.Tests\verify-hidi-package-assembly.Tests.ps1')
7474
$configuration.Run.PassThru = $true
7575
$configuration.CodeCoverage.Enabled = $true
76-
$configuration.CodeCoverage.Path = $helper
76+
$configuration.CodeCoverage.Path = $helpers
7777
$configuration.CodeCoverage.OutputFormat = 'JaCoCo'
7878
$configuration.CodeCoverage.OutputPath = Join-Path $outputRoot 'pester-coverage.xml'
7979
$configuration.CodeCoverage.CoveragePercentTarget = 80
8080
$result = Invoke-Pester -Configuration $configuration
81-
if ($result.FailedCount -gt 0 -or $result.PassedCount -lt 24) {
82-
throw "Hidi NuGet helper tests failed or did not execute all 24 cases."
81+
if ($result.FailedCount -gt 0 -or $result.PassedCount -lt 45) {
82+
throw "Hidi NuGet helper tests failed or did not execute all 45 cases."
8383
}
8484
$report = [xml](Get-Content $configuration.CodeCoverage.OutputPath.Value -Raw)
8585
$sourceFiles = @($report.SelectNodes('//sourcefile'))
86-
if ($sourceFiles.Count -ne 1 -or $sourceFiles[0].name -ne [IO.Path]::GetFileName($helper)) {
87-
throw 'Expected measured coverage of only the Hidi NuGet helper.'
88-
}
89-
$lines = @($sourceFiles[0].SelectNodes('line'))
90-
$sourceLineCount = @(Get-Content $helper).Count
91-
$lineNumbers = @($lines | ForEach-Object { [int]$_.nr })
92-
if ($lines.Count -eq 0 -or @($lineNumbers | Select-Object -Unique).Count -ne $lines.Count -or
93-
@($lines | Where-Object {
94-
[int]$_.nr -le 0 -or [int]$_.nr -gt $sourceLineCount -or
95-
-not $_.HasAttribute('ci') -or -not $_.HasAttribute('mi') -or
96-
[int]$_.ci -lt 0 -or [int]$_.mi -lt 0 -or ([int]$_.ci + [int]$_.mi) -le 0
97-
}).Count -gt 0) {
98-
throw 'Missing or invalid measured helper coverage lines.'
99-
}
100-
$covered = @($lines | Where-Object { [int]$_.ci -gt 0 }).Count
101-
if ($covered / $lines.Count -lt 0.8) {
102-
throw "Insufficient measured Hidi helper coverage: $covered/$($lines.Count) lines."
86+
if ($sourceFiles.Count -ne $helpers.Count) {
87+
throw 'Expected measured coverage of both Hidi NuGet helpers.'
10388
}
10489
$coverage = [xml]'<coverage version="1"/>'
105-
$file = $coverage.CreateElement('file')
106-
$file.SetAttribute('path', $helperPath)
107-
[void]$coverage.DocumentElement.AppendChild($file)
108-
foreach ($line in $lines) {
109-
$entry = $coverage.CreateElement('lineToCover')
110-
$entry.SetAttribute('lineNumber', $line.nr)
111-
$entry.SetAttribute('covered', ([int]$line.ci -gt 0).ToString().ToLowerInvariant())
112-
[void]$file.AppendChild($entry)
90+
foreach ($helperPath in $helperPaths) {
91+
$helper = (Resolve-Path $helperPath).Path
92+
$sourceFile = @($sourceFiles | Where-Object { $_.name -eq [IO.Path]::GetFileName($helper) })
93+
if ($sourceFile.Count -ne 1) { throw "Missing or ambiguous helper coverage: $helperPath" }
94+
$lines = @($sourceFile[0].SelectNodes('line'))
95+
$sourceLineCount = @(Get-Content $helper).Count
96+
$lineNumbers = @($lines | ForEach-Object { [int]$_.nr })
97+
if ($lines.Count -eq 0 -or @($lineNumbers | Select-Object -Unique).Count -ne $lines.Count -or
98+
@($lines | Where-Object {
99+
[int]$_.nr -le 0 -or [int]$_.nr -gt $sourceLineCount -or
100+
-not $_.HasAttribute('ci') -or -not $_.HasAttribute('mi') -or
101+
[int]$_.ci -lt 0 -or [int]$_.mi -lt 0 -or ([int]$_.ci + [int]$_.mi) -le 0
102+
}).Count -gt 0) {
103+
throw "Missing or invalid measured helper coverage lines: $helperPath"
104+
}
105+
$covered = @($lines | Where-Object { [int]$_.ci -gt 0 }).Count
106+
if ($covered / $lines.Count -lt 0.8) {
107+
throw "Insufficient measured Hidi helper coverage: $helperPath $covered/$($lines.Count) lines."
108+
}
109+
$file = $coverage.CreateElement('file')
110+
$file.SetAttribute('path', $helperPath)
111+
[void]$coverage.DocumentElement.AppendChild($file)
112+
foreach ($line in $lines) {
113+
$entry = $coverage.CreateElement('lineToCover')
114+
$entry.SetAttribute('lineNumber', $line.nr)
115+
$entry.SetAttribute('covered', ([int]$line.ci -gt 0).ToString().ToLowerInvariant())
116+
[void]$file.AppendChild($entry)
117+
}
118+
Write-Host "Measured Hidi helper coverage: $helperPath $covered/$($lines.Count) lines."
113119
}
114120
$coverage.Save((Join-Path $outputRoot 'sonar-coverage.xml'))
115-
Write-Host "Measured Hidi NuGet helper coverage: $covered/$($lines.Count) lines; Sonar generic report generated."
121+
Write-Host 'Sonar generic report generated for both Hidi NuGet helpers.'
116122
- name: Build and analyze
117123
env:
118124
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
<#
5+
.SYNOPSIS
6+
Verifies the Hidi tool package contains the exact Microsoft-signed staging DLL.
7+
.DESCRIPTION
8+
Run after tool packing and before NuGet signing. A signed NuGet container does
9+
not prove that its assembly payload retained the ESRP signature.
10+
#>
11+
[CmdletBinding()]
12+
param(
13+
[Parameter(Mandatory = $true)]
14+
[string]$PackagePath,
15+
[Parameter(Mandatory = $true)]
16+
[string]$SignedAssemblyPath
17+
)
18+
19+
$ErrorActionPreference = 'Stop'
20+
21+
foreach ($path in @($PackagePath, $SignedAssemblyPath)) {
22+
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) {
23+
throw "Missing Hidi signing verification input: $path"
24+
}
25+
}
26+
27+
$temporaryDirectory = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString())
28+
New-Item -ItemType Directory -Path $temporaryDirectory | Out-Null
29+
$packagedAssembly = Join-Path $temporaryDirectory 'Microsoft.OpenApi.Hidi.dll'
30+
$archive = $null
31+
try {
32+
$archive = [IO.Compression.ZipFile]::OpenRead((Resolve-Path -LiteralPath $PackagePath).Path)
33+
$assemblies = @($archive.Entries | Where-Object { $_.Name -ieq 'Microsoft.OpenApi.Hidi.dll' })
34+
if ($assemblies.Count -ne 1 -or $assemblies[0].FullName -cne 'tools/net8.0/any/Microsoft.OpenApi.Hidi.dll') {
35+
throw 'Expected exactly one Hidi DLL at tools/net8.0/any/Microsoft.OpenApi.Hidi.dll.'
36+
}
37+
[IO.Compression.ZipFileExtensions]::ExtractToFile($assemblies[0], $packagedAssembly)
38+
39+
$stagingHash = (Get-FileHash -LiteralPath $SignedAssemblyPath -Algorithm SHA256).Hash
40+
$packageHash = (Get-FileHash -LiteralPath $packagedAssembly -Algorithm SHA256).Hash
41+
if ($packageHash -cne $stagingHash) {
42+
throw "Packaged Hidi DLL differs from the signed staging DLL: $packageHash != $stagingHash"
43+
}
44+
foreach ($assembly in @($SignedAssemblyPath, $packagedAssembly)) {
45+
$signature = Get-AuthenticodeSignature -LiteralPath $assembly
46+
if ($signature.Status -ne 'Valid' -or
47+
$signature.SignerCertificate.Subject -notmatch '(^|,\s*)O=Microsoft Corporation(,|$)') {
48+
throw "Hidi DLL must have a valid Microsoft Corporation Authenticode signature: $assembly ($($signature.Status))"
49+
}
50+
}
51+
Write-Host "Verified packaged Hidi DLL: valid Microsoft Authenticode signature; signed staging SHA256=$stagingHash"
52+
}
53+
finally {
54+
if ($null -ne $archive) { $archive.Dispose() }
55+
if (Test-Path -LiteralPath $packagedAssembly) {
56+
Remove-Item -LiteralPath $packagedAssembly -Force
57+
}
58+
Remove-Item -LiteralPath $temporaryDirectory -Force
59+
}

‎src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,24 @@
7070
<None Include="./readme.md" Pack="true" PackagePath="" />
7171
</ItemGroup>
7272

73+
<Target Name="UseHidiSignedAssemblyForPublish"
74+
AfterTargets="ComputeFilesToPublish"
75+
Condition="'$(PackAsTool)' == 'true' and '$(HidiSignedAssemblyPath)' != ''">
76+
<Error Condition="!Exists('$(HidiSignedAssemblyPath)')"
77+
Text="The signed Hidi assembly does not exist: $(HidiSignedAssemblyPath)" />
78+
<ItemGroup>
79+
<_HidiAssemblyToReplace Include="@(ResolvedFileToPublish)"
80+
Condition="'%(ResolvedFileToPublish.RelativePath)' == '$(TargetFileName)'" />
81+
</ItemGroup>
82+
<Error Condition="'@(_HidiAssemblyToReplace->Count())' != '1'"
83+
Text="Expected exactly one Hidi assembly in ResolvedFileToPublish." />
84+
<ItemGroup>
85+
<!-- Tool packing publishes the intermediate assembly, not the bin copy. -->
86+
<ResolvedFileToPublish Remove="@(_HidiAssemblyToReplace)" />
87+
<ResolvedFileToPublish Include="$(HidiSignedAssemblyPath)"
88+
RelativePath="$(TargetFileName)"
89+
CopyToPublishDirectory="Always" />
90+
</ItemGroup>
91+
</Target>
92+
7393
</Project>

‎src/Microsoft.OpenApi.Hidi/readme.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ the signed Hidi `.nupkg`, a matching `.snupkg` and the private-feed version-chec
7373
script in the `Hidi` artifact. The `nuget-org` release job consumes that artifact
7474
without a repository checkout or the Docker context.
7575

76+
Tool packing republishes the SDK's intermediate assembly, so replacing the `bin`
77+
DLL does not preserve signing. The official no-build pack supplies
78+
`HidiSignedAssemblyPath` to replace only the Hidi `ResolvedFileToPublish` item
79+
with the ESRP-signed staging DLL. Missing or ambiguous inputs stop packing.
80+
Before NuGet signing, `scripts/verify-hidi-package-assembly.ps1` requires exactly
81+
one Hidi DLL at the expected tool path, byte-for-byte SHA256 equality with staging,
82+
and valid Microsoft Corporation Authenticode signatures on both DLLs.
83+
A signed NuGet container alone is not proof of a signed assembly payload.
84+
Local packing without this property retains the normal SDK behavior.
85+
7686
Before ESRP publication, the job requires the exact `hidi-v3.*` release package
7787
and symbols with a version newer than 3.10.2. It checks
7888
`GraphDeveloperExperiences_Public` using `System.AccessToken` through
@@ -94,6 +104,11 @@ execute the cases. The test resides physically inside the Hidi test project so
94104
SonarScanner for .NET classifies it as test code, rather than root source code.
95105
The Sonar workflow also measures helper line coverage and
96106
imports a generic coverage report alongside the existing C# OpenCover reports.
107+
The payload verifier has 21 isolated Pester cases in
108+
`test\Microsoft.OpenApi.Hidi.Tests\verify-hidi-package-assembly.Tests.ps1`,
109+
included in that measured coverage. These tests mock signature verification;
110+
they do not establish real ESRP signing. Final readiness requires a
111+
publish-disabled official build and verification of the downloaded DLL payload.
97112

98113
### Windows executable
99114

0 commit comments

Comments
 (0)