Files
MyAiConfiguration/scripts/lib/with-credential.ps1
T
Julian LechnerandClaude Opus 5.5 b35e2254b3 feat(extensions): add Roast Room, bring Codex to parity, fix audit findings
Roast Room is installed as a managed Git clone under
~/.my-ai-configuration/roast-room, shared by Claude and Codex. A personal
launcher skill runs it from any folder, starts its live room when needed,
and only runs when invoked by name (/roast-room, $roast-room).

Codex now gets what was Claude-only before: the Caveman skill with a
SessionStart hook that keeps it on, PreCompact and session-pointer hooks,
execpolicy deny rules mirroring Claude's permissions.deny, and Impeccable's
custom agents registered in ~/.codex/agents. The command-safety hook checks
both shell dialects for Codex on Windows, where PowerShell text arrives as
Bash, in both the PowerShell and Bash packages.

Audit fixes:
- command-safety blocks git checkout -- / restore / push --force /
  stash clear and find -delete, and treats heredoc bodies as text
- removing a clone clears read-only Git files, git errors no longer abort
  the extension sync, and a lock left by a dead process is replaced
- with-credential passes child flags such as -k or -d through unchanged
  and resolves commands from PATH
- uninstall strips only owned hooks from settings.json and config.toml and
  keeps the selection while another client is installed; update keeps
  CLAUDE_CODE_MAX_TOOL_USE_CONCURRENCY; Bash merges write the same bytes as
  PowerShell
- qmd no longer trusts a stale daemon.json after a lost start race and
  prunes collections of deleted repositories
- Bash scripts avoid GNU-only find -printf

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 09:02:47 +01:00

139 lines
7.1 KiB
PowerShell

# Usage: with-credential.ps1 -Client <Codex|Claude> -Key <NAME> -FilePath <command> [child arguments...]
# No param() block: PowerShell would bind abbreviated child flags such as -k or -d to -Key or -Debug.
# Only the exact names below are read; everything after the command goes to the child unchanged.
# Own variable names: scripts that dot-source this file keep their own $Client and $Key.
$credentialClient = $null
$credentialKey = $null
$childPath = $null
$childArguments = @()
for ($index = 0; $index -lt $args.Count; $index++) {
$token = [string]$args[$index]
if ($null -ne $childPath) { $childArguments = @($args[$index..($args.Count - 1)] | ForEach-Object { [string]$_ }); break }
if ($token -eq '-Client' -and $index + 1 -lt $args.Count) { $credentialClient = [string]$args[++$index] }
elseif ($token -eq '-Key' -and $index + 1 -lt $args.Count) { $credentialKey = [string]$args[++$index] }
elseif ($token -eq '-FilePath' -and $index + 1 -lt $args.Count) { $childPath = [string]$args[++$index] }
else { $childPath = $token }
}
$ErrorActionPreference = 'Stop'
function Assert-CredentialKey {
param([string]$Value)
if ($Value -notmatch '^[A-Za-z_][A-Za-z0-9_]{0,127}$') { throw 'Credential key must be an environment-variable-compatible name.' }
}
function Get-CredentialTarget {
param([string]$Client, [string]$Key)
Assert-CredentialKey $Key
return "MyAiConfiguration/$($Client.ToLowerInvariant())/$Key"
}
function Initialize-NativeCredentialApi {
if ('MyAiConfiguration.NativeCredential' -as [type]) { return }
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
namespace MyAiConfiguration {
public static class NativeCredential {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CREDENTIAL {
public UInt32 Flags;
public UInt32 Type;
public IntPtr TargetName;
public IntPtr Comment;
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
public UInt32 CredentialBlobSize;
public IntPtr CredentialBlob;
public UInt32 Persist;
public UInt32 AttributeCount;
public IntPtr Attributes;
public IntPtr TargetAlias;
public IntPtr UserName;
}
[DllImport("Advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool CredWrite(ref CREDENTIAL credential, UInt32 flags);
[DllImport("Advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool CredRead(string target, UInt32 type, UInt32 flags, out IntPtr credential);
[DllImport("Advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool CredDelete(string target, UInt32 type, UInt32 flags);
[DllImport("Advapi32.dll", SetLastError = true)]
public static extern void CredFree(IntPtr buffer);
}
}
'@
}
function Set-ManagedCredential {
param([ValidateSet('Codex', 'Claude')][string]$Client, [string]$Key, [securestring]$Value)
if (-not $IsWindows -and $PSVersionTable.PSVersion.Major -ge 6) { throw 'The OS credential store is currently supported on Windows only.' }
Initialize-NativeCredentialApi
$plain = [System.Net.NetworkCredential]::new('', $Value).Password
$bytes = [Text.Encoding]::UTF8.GetBytes($plain)
if ($bytes.Length -eq 0 -or $bytes.Length -gt 2560) { throw 'Credential value must contain between 1 and 2560 UTF-8 bytes.' }
$native = New-Object MyAiConfiguration.NativeCredential+CREDENTIAL
$native.Type = 1
$native.Persist = 2
$native.TargetName = [Runtime.InteropServices.Marshal]::StringToCoTaskMemUni((Get-CredentialTarget $Client $Key))
$native.UserName = [Runtime.InteropServices.Marshal]::StringToCoTaskMemUni($Key)
$native.CredentialBlobSize = $bytes.Length
$native.CredentialBlob = [Runtime.InteropServices.Marshal]::AllocCoTaskMem($bytes.Length)
try {
[Runtime.InteropServices.Marshal]::Copy($bytes, 0, $native.CredentialBlob, $bytes.Length)
if (-not [MyAiConfiguration.NativeCredential]::CredWrite([ref]$native, 0)) { throw [ComponentModel.Win32Exception]::new([Runtime.InteropServices.Marshal]::GetLastWin32Error()) }
} finally {
[Array]::Clear($bytes, 0, $bytes.Length)
$plain = $null
if ($native.TargetName -ne [IntPtr]::Zero) { [Runtime.InteropServices.Marshal]::FreeCoTaskMem($native.TargetName) }
if ($native.UserName -ne [IntPtr]::Zero) { [Runtime.InteropServices.Marshal]::FreeCoTaskMem($native.UserName) }
if ($native.CredentialBlob -ne [IntPtr]::Zero) { [Runtime.InteropServices.Marshal]::FreeCoTaskMem($native.CredentialBlob) }
}
}
function Get-ManagedCredential {
param([ValidateSet('Codex', 'Claude')][string]$Client, [string]$Key)
if (-not $IsWindows -and $PSVersionTable.PSVersion.Major -ge 6) { throw 'The OS credential store is currently supported on Windows only.' }
Initialize-NativeCredentialApi
$pointer = [IntPtr]::Zero
if (-not [MyAiConfiguration.NativeCredential]::CredRead((Get-CredentialTarget $Client $Key), 1, 0, [ref]$pointer)) { throw 'Credential was not found.' }
try {
$native = [Runtime.InteropServices.Marshal]::PtrToStructure($pointer, [type]'MyAiConfiguration.NativeCredential+CREDENTIAL')
$bytes = New-Object byte[] $native.CredentialBlobSize
[Runtime.InteropServices.Marshal]::Copy($native.CredentialBlob, $bytes, 0, $bytes.Length)
try { return [Text.Encoding]::UTF8.GetString($bytes) } finally { [Array]::Clear($bytes, 0, $bytes.Length) }
} finally {
[MyAiConfiguration.NativeCredential]::CredFree($pointer)
}
}
function Remove-ManagedCredential {
param([ValidateSet('Codex', 'Claude')][string]$Client, [string]$Key)
if (-not $IsWindows -and $PSVersionTable.PSVersion.Major -ge 6) { throw 'The OS credential store is currently supported on Windows only.' }
Initialize-NativeCredentialApi
if (-not [MyAiConfiguration.NativeCredential]::CredDelete((Get-CredentialTarget $Client $Key), 1, 0)) { throw [ComponentModel.Win32Exception]::new([Runtime.InteropServices.Marshal]::GetLastWin32Error()) }
}
function Invoke-WithManagedCredential {
param([ValidateSet('Codex', 'Claude')][string]$Client, [string]$Key, [string]$FilePath, [string[]]$ArgumentList)
Assert-CredentialKey $Key
# Accept a full path or a command name on PATH (gh, curl.exe).
$command = Get-Command -Name $FilePath -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $command) { throw 'Child command was not found.' }
$FilePath = $command.Source
$prior = [Environment]::GetEnvironmentVariable($Key, 'Process')
$value = Get-ManagedCredential $Client $Key
try {
[Environment]::SetEnvironmentVariable($Key, $value, 'Process')
# Output goes straight to the caller; returning a value here would capture the child's stdout.
& $FilePath @ArgumentList
} finally {
[Environment]::SetEnvironmentVariable($Key, $prior, 'Process')
$value = $null
}
}
if ($childPath) {
if (-not $credentialClient -or -not $credentialKey) { throw 'Client and key are required.' }
Invoke-WithManagedCredential $credentialClient $credentialKey $childPath $childArguments
exit $LASTEXITCODE
}