@nectarcrm/ccx 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/ccx.js +14 -3
  2. package/package.json +1 -1
package/bin/ccx.js CHANGED
@@ -45,13 +45,24 @@ const fingerprint = t => createHash('sha256').update(t).digest('hex').slice(0, 1
45
45
  // macOS: Keychain. Windows: DPAPI (só o usuário do Windows abre). Linux: cofre do sistema
46
46
  // (secret-tool/libsecret) ou, sem ele (servidor, WSL), arquivo que só o usuário lê.
47
47
  // O segredo sempre passa por stdin, nunca pela linha de comando.
48
- const ps = (script, input) => execFileSync('powershell', ['-NoProfile', '-NonInteractive', '-Command', script], { input, stdio: ['pipe', 'pipe', 'ignore'] }).toString().trim()
48
+ // PSModulePath herdado de um PowerShell 7 impede o 5.1 de carregar módulos (ConvertTo-SecureString some),
49
+ // então o DPAPI é chamado direto pelo .NET e o PSModulePath é descartado.
50
+ const ps = (script, input) => {
51
+ const { PSModulePath, ...env } = process.env
52
+ return execFileSync('powershell', ['-NoProfile', '-NonInteractive', '-Command', script], { input, env, stdio: ['pipe', 'pipe', 'ignore'] }).toString().trim()
53
+ }
54
+ const DPAPI = "Add-Type -AssemblyName System.Security; $v = [Console]::In.ReadToEnd().Trim(); "
55
+ const DPAPI_PREFIX = 'dpapi:'
49
56
  const KEY_FILE = join(DIR, 'chave') // Windows (DPAPI) e Linux sem cofre
50
57
  const SECRET_TOOL = ['service', SERVICE, 'account', 'chave']
51
58
 
52
59
  function secretGet() {
53
60
  try {
54
- if (WIN) return ps('$s = [Console]::In.ReadToEnd().Trim() | ConvertTo-SecureString; [Runtime.InteropServices.Marshal]::PtrToStringBSTR([Runtime.InteropServices.Marshal]::SecureStringToBSTR($s))', readFileSync(KEY_FILE, 'utf8'))
61
+ if (WIN) {
62
+ const stored = readFileSync(KEY_FILE, 'utf8').trim()
63
+ if (!stored.startsWith(DPAPI_PREFIX)) return ps('$s = [Console]::In.ReadToEnd().Trim() | ConvertTo-SecureString; [Runtime.InteropServices.Marshal]::PtrToStringBSTR([Runtime.InteropServices.Marshal]::SecureStringToBSTR($s))', stored) // formato antigo
64
+ return ps(DPAPI + '[Text.Encoding]::UTF8.GetString([Security.Cryptography.ProtectedData]::Unprotect([Convert]::FromBase64String($v), $null, "CurrentUser"))', stored.slice(DPAPI_PREFIX.length))
65
+ }
55
66
  if (MAC) return execFileSync('security', ['find-generic-password', '-s', SERVICE, '-a', 'chave', '-w'], { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim()
56
67
  if (existsSync(KEY_FILE)) return readFileSync(KEY_FILE, 'utf8').trim()
57
68
  return execFileSync('secret-tool', ['lookup', ...SECRET_TOOL], { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim() || null
@@ -61,7 +72,7 @@ function secretGet() {
61
72
  function secretSet(value) {
62
73
  if (WIN) {
63
74
  mkdirSync(DIR, { recursive: true })
64
- writeFileSync(KEY_FILE, ps('[Console]::In.ReadToEnd().Trim() | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString', value))
75
+ writeFileSync(KEY_FILE, DPAPI_PREFIX + ps(DPAPI + '[Convert]::ToBase64String([Security.Cryptography.ProtectedData]::Protect([Text.Encoding]::UTF8.GetBytes($v), $null, "CurrentUser"))', value))
65
76
  return
66
77
  }
67
78
  if (MAC) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nectarcrm/ccx",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Escolhe qual conta Claude este computador usa, conectado ao app Contas Claude da Nectar.",
5
5
  "type": "module",
6
6
  "bin": { "ccx": "bin/ccx.js" },