66 lines
4.8 KiB
PowerShell

Set-StrictMode -Version Latest
$ErrorActionPreference='Stop'
function Invoke-Checked([string]$Command,[string[]]$Arguments) {
& $Command @Arguments
if($LASTEXITCODE -ne 0){throw "$Command failed with exit code $LASTEXITCODE. No installation was changed."}
}
function Get-DownloadsFolder {
$entry=Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
$value=$entry.'{374DE290-123F-4565-9164-39C4925E467B}'
if(!$value){throw 'Cannot locate your Downloads known folder.'}
return [Environment]::ExpandEnvironmentVariables($value)
}
function Test-DeveloperPackage([string]$PackagePath) {
$root=(Resolve-Path -LiteralPath $PackagePath).Path
$manifest=Get-Content -Raw -LiteralPath (Join-Path $root 'package.json') | ConvertFrom-Json
if($manifest.publisher -ne 'Nick Beckley' -or $manifest.version -notmatch '^\d+\.\d+\.\d+\.\d+$' -or $manifest.commit -notmatch '^[0-9a-f]{40}$'){throw 'Invalid developer package metadata.'}
foreach($file in $manifest.files){
$path=[IO.Path]::GetFullPath((Join-Path $root $file.path))
if(!$path.StartsWith($root+[IO.Path]::DirectorySeparatorChar,[StringComparison]::OrdinalIgnoreCase)){throw 'Unsafe package path.'}
if(!(Test-Path -LiteralPath $path -PathType Leaf) -or (Get-FileHash -Algorithm SHA256 -LiteralPath $path).Hash -ne $file.sha256){throw "Package hash mismatch: $($file.path)"}
}
foreach($required in @('PlotDirector.WordAddIn.dll','PlotDirector.WordAddIn.vsto','PlotDirector.WordAddIn.dll.manifest','PlotDirector.Companion.Client.dll','PlotDirector.Companion.Protocol.dll','Agent/PlotDirector.Companion.Agent.exe')){
if(!($manifest.files | Where-Object { $_.path.Replace('\','/') -eq $required })){throw "Package missing $required"}
}
foreach($name in @('PlotDirector.WordAddIn.vsto','PlotDirector.WordAddIn.dll.manifest')){
[xml]$xml=Get-Content -Raw -LiteralPath (Join-Path $root $name)
if(!$xml.SelectSingleNode("//*[local-name()='Signature']")){throw "Unsigned VSTO manifest: $name"}
$identity=$xml.SelectSingleNode("/*/*[local-name()='assemblyIdentity']")
if(!$identity -or $identity.version -ne $manifest.version){throw 'Manifest/package version mismatch.'}
foreach($dependency in $xml.SelectNodes("//*[local-name()='dependentAssembly' and @dependencyType='install' and @codebase]")){
$relative=[uri]::UnescapeDataString([string]$dependency.codebase)
if([IO.Path]::IsPathRooted($relative) -or $relative -match '^[a-zA-Z]+:'){throw 'Manifest references an external build location; package was not installed.'}
$dependencyPath=[IO.Path]::GetFullPath((Join-Path $root $relative))
if(!$dependencyPath.StartsWith($root+[IO.Path]::DirectorySeparatorChar,[StringComparison]::OrdinalIgnoreCase) -or !(Test-Path -LiteralPath $dependencyPath -PathType Leaf)){throw "Missing/unsafe manifest dependency: $relative"}
}
}
# Hash inventory is integrity evidence; Office still validates signatures/trust on load.
return $manifest
}
function Get-WordCompanionRegistration {
$base='HKCU:\Software\Microsoft\Office\Word\Addins'
$registrations=@(if(Test-Path $base){Get-ChildItem $base | ForEach-Object {
$item=Get-ItemProperty $_.PSPath
if(($item.PSObject.Properties.Name -contains 'Manifest' -and $item.Manifest -like '*PlotDirector.WordAddIn.vsto*') -or ($item.PSObject.Properties.Name -contains 'FriendlyName' -and $item.FriendlyName -like '*PlotDirector*')){
[pscustomobject]@{Name=$_.PSChildName;Path=(Join-Path $base $_.PSChildName);Properties=$item}
}
}})
if($registrations.Count -gt 1){throw 'Multiple per-user PlotDirector Word registrations exist. Review them before installing; no registration was changed.'}
if($registrations.Count -eq 1){return $registrations[0]}
return $null
}
function Get-InstalledSigningThumbprint {
$registration=Get-WordCompanionRegistration
if(!$registration -or !($registration.Properties.PSObject.Properties.Name -contains 'Manifest')){return $null}
$location=([string]$registration.Properties.Manifest) -replace '\|vstolocal$',''
$uri=$null
if(![uri]::TryCreate($location,[UriKind]::Absolute,[ref]$uri) -or !$uri.IsFile -or !(Test-Path -LiteralPath $uri.LocalPath)){return $null}
[xml]$xml=Get-Content -Raw -LiteralPath $uri.LocalPath
foreach($entry in $xml.SelectNodes("//*[local-name()='X509Certificate']")){
$certificate=[Security.Cryptography.X509Certificates.X509Certificate2]::new([Convert]::FromBase64String($entry.InnerText))
$path="Cert:\CurrentUser\My\$($certificate.Thumbprint)"
if(Test-Path $path){$signer=Get-Item $path;if($signer.HasPrivateKey -and $signer.NotAfter -gt (Get-Date)){return $signer.Thumbprint}}
}
return $null
}