Skip to content

Commit 276c281

Browse files
waleedlatif1claude
andcommitted
refactor: clean up crypto modules
- Fix error: any → error: unknown with proper type guard in encryption.ts - Eliminate duplicate iv.toString('hex') calls in both encrypt functions - Remove redundant string split in decryptApiKey (was splitting twice) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9dcb2ac commit 276c281

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

apps/sim/lib/api-key/crypto.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ export async function encryptApiKey(apiKey: string): Promise<{ encrypted: string
4141
encrypted += cipher.final('hex')
4242

4343
const authTag = cipher.getAuthTag()
44+
const ivHex = iv.toString('hex')
4445

4546
// Format: iv:encrypted:authTag
4647
return {
47-
encrypted: `${iv.toString('hex')}:${encrypted}:${authTag.toString('hex')}`,
48-
iv: iv.toString('hex'),
48+
encrypted: `${ivHex}:${encrypted}:${authTag.toString('hex')}`,
49+
iv: ivHex,
4950
}
5051
}
5152

@@ -55,8 +56,10 @@ export async function encryptApiKey(apiKey: string): Promise<{ encrypted: string
5556
* @returns A promise that resolves to an object containing the decrypted API key
5657
*/
5758
export async function decryptApiKey(encryptedValue: string): Promise<{ decrypted: string }> {
59+
const parts = encryptedValue.split(':')
60+
5861
// Check if this is actually encrypted (contains colons)
59-
if (!encryptedValue.includes(':') || encryptedValue.split(':').length !== 3) {
62+
if (parts.length !== 3) {
6063
// This is a plain text key, return as-is
6164
return { decrypted: encryptedValue }
6265
}
@@ -68,10 +71,9 @@ export async function decryptApiKey(encryptedValue: string): Promise<{ decrypted
6871
return { decrypted: encryptedValue }
6972
}
7073

71-
const parts = encryptedValue.split(':')
7274
const ivHex = parts[0]
73-
const authTagHex = parts[parts.length - 1]
74-
const encrypted = parts.slice(1, -1).join(':')
75+
const authTagHex = parts[2]
76+
const encrypted = parts[1]
7577

7678
if (!ivHex || !encrypted || !authTagHex) {
7779
throw new Error('Invalid encrypted API key format. Expected "iv:encrypted:authTag"')

apps/sim/lib/core/security/encryption.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ export async function encryptSecret(secret: string): Promise<{ encrypted: string
2626
encrypted += cipher.final('hex')
2727

2828
const authTag = cipher.getAuthTag()
29+
const ivHex = iv.toString('hex')
2930

3031
// Format: iv:encrypted:authTag
3132
return {
32-
encrypted: `${iv.toString('hex')}:${encrypted}:${authTag.toString('hex')}`,
33-
iv: iv.toString('hex'),
33+
encrypted: `${ivHex}:${encrypted}:${authTag.toString('hex')}`,
34+
iv: ivHex,
3435
}
3536
}
3637

@@ -61,8 +62,10 @@ export async function decryptSecret(encryptedValue: string): Promise<{ decrypted
6162
decrypted += decipher.final('utf8')
6263

6364
return { decrypted }
64-
} catch (error: any) {
65-
logger.error('Decryption error:', { error: error.message })
65+
} catch (error: unknown) {
66+
logger.error('Decryption error:', {
67+
error: error instanceof Error ? error.message : 'Unknown error',
68+
})
6669
throw error
6770
}
6871
}

0 commit comments

Comments
 (0)