diff --git a/apps/desktop/electron/windows-remote-inventory.test.ts b/apps/desktop/electron/windows-remote-inventory.test.ts index 0ca2acf935..657ce27dc2 100644 --- a/apps/desktop/electron/windows-remote-inventory.test.ts +++ b/apps/desktop/electron/windows-remote-inventory.test.ts @@ -8,7 +8,7 @@ import { test } from 'vitest' import { WINDOWS_PROFILE_LIST_PY, windowsInventoryScript } from './windows-remote-inventory' import { atomicWindowsSpawn, atomicWindowsSpawnCommand, encodedPowerShell, powerShellStdinCommand, probeWindowsFiles, probeWindowsRemote } from './windows-remote-lifecycle' -import { windowsHelperArgv, windowsRuntimeProgramArgv } from './windows-runtime-command' +import { WINDOWS_NO_REPARSE_PS, windowsHelperArgv, windowsRuntimeProgramArgv } from './windows-runtime-command' const repo = path.resolve(import.meta.dirname, '../../..') const python = process.env.HERMES_TEST_PYTHON || path.join(repo, '.venv/bin/python') @@ -243,3 +243,22 @@ test.runIf(Boolean(process.env.HERMES_TEST_PWSH))('real PowerShell parser accept assert.equal(result.status, 0, result.stderr + result.stdout) assert.equal(result.stdout.trim(), 'HERMES_PS_PROGRAMS_PARSE_OK') }) + +test.runIf(Boolean(process.env.HERMES_TEST_PWSH))('no-reparse guard checks a leaf file and every ancestor, not FileInfo.Parent', () => { + const pwsh = String(process.env.HERMES_TEST_PWSH) + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-no-reparse-')) + fs.mkdirSync(path.join(root, 'real')) + fs.writeFileSync(path.join(root, 'real', 'leaf'), 'fixture') + fs.symlinkSync(path.join(root, 'real'), path.join(root, 'linked')) + + try { + const file = path.join(root, 'linked', 'leaf').replace(/'/g, "''") + const program = `${WINDOWS_NO_REPARSE_PS};Assert-NoReparse '${file}' $false` + const result = spawnSync(pwsh, ['-NoProfile', '-NonInteractive', '-EncodedCommand', encodedPowerShell(program)], { encoding: 'utf8', timeout: 20000 }) + + assert.notEqual(result.status, 0, 'a file beneath a reparse directory must be refused') + assert.match(result.stderr, /reparse point/) + } finally { + fs.rmSync(root, { recursive: true, force: true }) + } +}) diff --git a/apps/desktop/electron/windows-remote-lifecycle.ts b/apps/desktop/electron/windows-remote-lifecycle.ts index 00433af503..0ff0eebdf5 100644 --- a/apps/desktop/electron/windows-remote-lifecycle.ts +++ b/apps/desktop/electron/windows-remote-lifecycle.ts @@ -60,7 +60,7 @@ async function probeWindowsFiles(ssh, explicitHermesPath = '') { 'while($true){', 'try{$item=Get-Item -LiteralPath $current -Force -ErrorAction Stop}catch [Management.Automation.ItemNotFoundException]{if(-not $allowMissing -and $first){throw "Path was not found: $candidate"};$parent=[IO.Path]::GetDirectoryName($current);if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false;continue}', 'if(($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0){throw "Path contains a link or reparse point: $current"}', - '$parent=$item.Parent.FullName;if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false', + '$parent=[IO.Path]::GetDirectoryName($current);if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false', '}', '}', `$explicit=${explicit}`, @@ -209,7 +209,7 @@ public static class HermesMarkerNoFollow { 'while($true){', 'try{$item=Get-Item -LiteralPath $current -Force -ErrorAction Stop}catch [Management.Automation.ItemNotFoundException]{if(-not $allowMissing -and $first){throw "Path was not found: $candidate"};$parent=[IO.Path]::GetDirectoryName($current);if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false;continue}', 'if(($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0){throw "Path contains a link or reparse point: $current"}', - '$parent=$item.Parent.FullName;if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false', + '$parent=[IO.Path]::GetDirectoryName($current);if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false', '}', '}', `$hermesHome=${psLiteral(hermesHome)}`, diff --git a/apps/desktop/electron/windows-runtime-command.ts b/apps/desktop/electron/windows-runtime-command.ts index c01acfe963..7dbe00970a 100644 --- a/apps/desktop/electron/windows-runtime-command.ts +++ b/apps/desktop/electron/windows-runtime-command.ts @@ -42,7 +42,7 @@ export const WINDOWS_NO_REPARSE_PS = [ 'while($true){', 'try{$item=Get-Item -LiteralPath $current -Force -ErrorAction Stop}catch [Management.Automation.ItemNotFoundException]{if(-not $allowMissing -and $first){throw "Path was not found: $candidate"};$parent=[IO.Path]::GetDirectoryName($current);if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false;continue}', 'if(($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0){throw "Path contains a link or reparse point: $current"}', - '$parent=$item.Parent.FullName;if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false', + '$parent=[IO.Path]::GetDirectoryName($current);if(-not $parent -or $parent -eq $current){break};$current=$parent;$first=$false', '}', '}' ].join('\r\n')