From 815c667374d47bf4e30a457dca2bc4122c7fea0b Mon Sep 17 00:00:00 2001 From: "shumeiko.it" Date: Sat, 24 Oct 2026 12:07:21 +0200 Subject: [PATCH] perf(core): check the Windows vault key ACL once per process (P1.2) Every VaultStore construction started PowerShell to re-apply the key ACL. The ACL does not change while the process runs; a created key is still written and hardened by the same PowerShell call. --- packages/core/src/vault.ts | 10 +++++++++- packages/core/tests/vault.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/core/src/vault.ts b/packages/core/src/vault.ts index e84c33f..7648a22 100644 --- a/packages/core/src/vault.ts +++ b/packages/core/src/vault.ts @@ -18,6 +18,13 @@ const ALGORITHM = 'aes-256-gcm'; const KEY_LENGTH = 32; const IV_LENGTH = 16; +/** + * Key files whose Windows ACL this process already set or verified. The check + * starts PowerShell (hundreds of milliseconds), and the vault is opened from + * several places; the ACL does not change while the process runs. + */ +const windowsKeyAclChecked = new Set(); + export interface VaultEntry { name: string; value: string; @@ -82,9 +89,10 @@ export class VaultStore { created = true; } - if (process.platform === 'win32') { + if (process.platform === 'win32' && (created || !windowsKeyAclChecked.has(this.keyPath))) { try { this.restrictWindowsKeyPermissions(created ? key : undefined); + windowsKeyAclChecked.add(this.keyPath); } catch (err) { const reason = err instanceof Error ? err.message : String(err); throw new Error(`Could not restrict vault key permissions on Windows: ${reason}.`); diff --git a/packages/core/tests/vault.test.ts b/packages/core/tests/vault.test.ts index 623b77f..595428f 100644 --- a/packages/core/tests/vault.test.ts +++ b/packages/core/tests/vault.test.ts @@ -471,6 +471,32 @@ describe('VaultStore', () => { } }); + it('Windows key protection — an existing key is checked once per process', () => { + const dir = makeTempDir(); + const keyPath = path.join(dir, '.vault-key'); + fs.writeFileSync(keyPath, 'cd'.repeat(32), { mode: 0o600 }); + + const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')!; + const originalSystemRoot = process.env.SystemRoot; + Object.defineProperty(process, 'platform', { value: 'win32', configurable: true }); + process.env.SystemRoot = 'C:\\Windows'; + mockExecFileSync.mockImplementation(() => ''); + + try { + new VaultStore(dir); + new VaultStore(dir); + new VaultStore(dir); + const powershellCalls = mockExecFileSync.mock.calls + .filter(([cmd]) => String(cmd).endsWith('powershell.exe')); + expect(powershellCalls).toHaveLength(1); + } finally { + Object.defineProperty(process, 'platform', originalPlatform); + if (originalSystemRoot === undefined) delete process.env.SystemRoot; + else process.env.SystemRoot = originalSystemRoot; + mockExecFileSync.mockReset(); + } + }); + it('Windows key protection — existing keys are re-hardened and retained on failure', () => { const dir = makeTempDir(); const keyPath = path.join(dir, '.vault-key');