diff --git a/PlotDirector.NativeCompanion/WindowsValidation/Install-WordCompanion.ps1 b/PlotDirector.NativeCompanion/WindowsValidation/Install-WordCompanion.ps1 index 617a2ce5..67f9c129 100644 --- a/PlotDirector.NativeCompanion/WindowsValidation/Install-WordCompanion.ps1 +++ b/PlotDirector.NativeCompanion/WindowsValidation/Install-WordCompanion.ps1 @@ -13,7 +13,9 @@ function Stop-DeveloperAgent { foreach($agent in @(Get-Process 'PlotDirector.Companion.Agent' -ErrorAction SilentlyContinue)){ if(!$StopAgent){throw 'The companion agent is running. Rerun with -StopAgent to explicitly stop this user-session agent. Word must already be closed.'} if($agent.SessionId -ne (Get-Process -Id $PID).SessionId){throw 'Agent belongs to a different session; it was not stopped.'} - if(!$agent.Path -or !$agent.Path.StartsWith([Environment]::GetFolderPath('UserProfile'),[StringComparison]::OrdinalIgnoreCase)){throw 'Agent path is outside this user profile; stop it explicitly after inspection.'} + $processInfo=Get-CimInstance Win32_Process -Filter "ProcessId=$($agent.Id)" + $owner=Invoke-CimMethod -InputObject $processInfo -MethodName GetOwnerSid + if($owner.ReturnValue -ne 0 -or $owner.Sid -ne [Security.Principal.WindowsIdentity]::GetCurrent().User.Value){throw 'Agent is not owned by this user; it was not stopped.'} if($PSCmdlet.ShouldProcess("Agent PID $($agent.Id)",'Stop for explicit developer installation')){Stop-Process -Id $agent.Id -ErrorAction Stop;$agent.WaitForExit(5000)|Out-Null} } } @@ -23,7 +25,7 @@ if($Rollback){ if($saved.PSObject.Properties.Name -contains 'registryKey'){if($saved.registryKey -notmatch '^[a-zA-Z0-9._ -]+$'){throw 'Invalid saved registration key.'};$registry=Join-Path 'HKCU:\Software\Microsoft\Office\Word\Addins' $saved.registryKey} if($PSCmdlet.ShouldProcess($registry,'Restore previous per-user Word registration')){ Stop-DeveloperAgent - if($saved.existed){New-Item -Path $registry -Force|Out-Null;foreach($property in $saved.values.PSObject.Properties){$type=if($property.Name -eq "LoadBehavior"){"DWord"}else{"String"};New-ItemProperty -Path $registry -Name $property.Name -Value $property.Value -PropertyType $type -Force|Out-Null}} + if($saved.existed){New-Item -Path $registry -Force|Out-Null;foreach($name in @('Manifest','LoadBehavior','FriendlyName','Description')){Remove-ItemProperty -Path $registry -Name $name -ErrorAction SilentlyContinue};foreach($property in $saved.values.PSObject.Properties){$type=if($property.Name -eq "LoadBehavior"){"DWord"}else{"String"};New-ItemProperty -Path $registry -Name $property.Name -Value $property.Value -PropertyType $type -Force|Out-Null}} elseif(Test-Path $registry){Remove-Item -LiteralPath $registry -Recurse} Write-Output 'Previous registration restored. Signed packages were retained. Open Word to verify.' } diff --git a/PlotDirector.NativeCompanion/WindowsValidation/Phase3-AI.md b/PlotDirector.NativeCompanion/WindowsValidation/Phase3-AI.md index 5cdff9b7..71877787 100644 --- a/PlotDirector.NativeCompanion/WindowsValidation/Phase3-AI.md +++ b/PlotDirector.NativeCompanion/WindowsValidation/Phase3-AI.md @@ -33,7 +33,7 @@ Close Word normally, dealing with its save prompts. Use the exact package path p & .\PlotDirector.NativeCompanion\WindowsValidation\Install-WordCompanion.ps1 -PackagePath '' -StopAgent ``` -`-StopAgent` explicitly permits stopping only the named Companion agent in this Windows session/user profile after Word is closed. Visual Studio is not stopped. Without this switch a running agent causes a diagnostic stop. The installer detects/reuses the existing per-user PlotDirector Word add-in registry key instead of introducing a second registration. Multiple existing registrations cause a diagnostic stop. +`-StopAgent` explicitly permits stopping only the named Companion agent in this Windows session and owned by this user (checked through the process SID) after Word is closed. Visual Studio is not stopped. Without this switch a running agent causes a diagnostic stop. The installer detects/reuses the existing per-user PlotDirector Word add-in registry key instead of introducing a second registration. Multiple existing registrations cause a diagnostic stop. Packages are copied to versioned `%LOCALAPPDATA%\PlotDirector\WordCompanionDeveloper\versions\` directories and verified before registration. The former registry values/manifest location are backed up; existing Phase 2 files remain untouched. A repeated install of the same package does not replace that rollback backup. No machine-wide registry, trust store, ClickOnce updater or production distribution channel is changed. Use `-WhatIf` to preview the explicit install. diff --git a/PlotLine.Tests/NativeCompanionHttpTests.cs b/PlotLine.Tests/NativeCompanionHttpTests.cs index 571de5ec..3f828095 100644 --- a/PlotLine.Tests/NativeCompanionHttpTests.cs +++ b/PlotLine.Tests/NativeCompanionHttpTests.cs @@ -60,6 +60,7 @@ internal static class NativeCompanionHttpTests using var agent=new HttpClient(new HttpClientHandler{AllowAutoRedirect=false,UseCookies=false}){BaseAddress=new Uri(address)}; Check((await agent.GetAsync("/api/native-companion/books")).StatusCode==HttpStatusCode.Unauthorized,"Native API is not anonymous"); var verifier=new string('d',64);var challenge=Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(verifier))); + var oversized=await agent.PostAsJsonAsync("/api/native-companion/pair",new{challenge=new string('A',64),padding=new string('x',5000)});Check(!oversized.IsSuccessStatusCode,"Oversized pairing bodies are refused before pairing state creation"); var paired=await agent.PostAsJsonAsync("/api/native-companion/pair",new{challenge});Check(paired.IsSuccessStatusCode,"Pairing start accepts verifier challenge"); var code=(await paired.Content.ReadFromJsonAsync()).GetProperty("code").GetString()!; Check((await browser.GetAsync("/native-companion/authorize?code="+code)).StatusCode==HttpStatusCode.Redirect,"Browser approval requires existing site sign-in"); diff --git a/PlotLine/Controllers/NativeCompanionAuthController.cs b/PlotLine/Controllers/NativeCompanionAuthController.cs index 56872965..e23ca0d6 100644 --- a/PlotLine/Controllers/NativeCompanionAuthController.cs +++ b/PlotLine/Controllers/NativeCompanionAuthController.cs @@ -5,6 +5,7 @@ using PlotLine.Services.NativeCompanion; namespace PlotLine.Controllers; +[RequestSizeLimit(4096)] [Microsoft.AspNetCore.RateLimiting.EnableRateLimiting("native-pair")] public sealed class NativeCompanionAuthController(DesktopPairing pairing):Controller {