diff --git a/app/api/scheduled/execute/route.ts b/app/api/scheduled/execute/route.ts index 965d5bc0ea..dd8fe41133 100644 --- a/app/api/scheduled/execute/route.ts +++ b/app/api/scheduled/execute/route.ts @@ -178,9 +178,15 @@ export async function POST(req: NextRequest) { throw new Error('No environment variables found for this user') } + // Add debug logging + console.log('Raw variables from DB:', userEnv.variables) + // Parse and validate environment variables const variables = EnvVarsSchema.parse(userEnv.variables) + // Add more debug logging + console.log('Parsed variables:', Object.keys(variables)) + // Replace environment variables in the block states const currentBlockStates = await Object.entries(mergedStates).reduce( async (accPromise, [id, block]) => { @@ -194,16 +200,34 @@ export async function POST(req: NextRequest) { if (typeof value === 'string' && value.includes('{{') && value.includes('}}')) { const matches = value.match(/{{([^}]+)}}/g) if (matches) { + console.log('Processing block:', id, 'subBlock:', key) + console.log('Found variable matches:', matches) + // Process all matches sequentially for (const match of matches) { const varName = match.slice(2, -2) // Remove {{ and }} + console.log('Looking for variable:', varName) + console.log('Available variables:', Object.keys(variables)) + const encryptedValue = variables[varName] + console.log('Found encrypted value:', encryptedValue) + if (!encryptedValue) { throw new Error(`Environment variable "${varName}" was not found`) } - // Decrypt the value - const { decrypted } = await decryptSecret(encryptedValue) - value = (value as string).replace(match, decrypted) + + try { + console.log('Attempting to decrypt value for:', varName) + const { decrypted } = await decryptSecret(encryptedValue) + console.log('Successfully decrypted value for:', varName) + value = (value as string).replace(match, decrypted) + console.log('Successfully replaced value for:', varName) + } catch (error: any) { + console.error('Error decrypting value:', error) + throw new Error( + `Failed to decrypt environment variable "${varName}": ${error.message}` + ) + } } } } @@ -320,9 +344,15 @@ export async function GET(req: NextRequest) { throw new Error('No environment variables found for this user') } + // Add debug logging + console.log('Raw variables from DB:', userEnv.variables) + // Parse and validate environment variables const variables = EnvVarsSchema.parse(userEnv.variables) + // Add more debug logging + console.log('Parsed variables:', Object.keys(variables)) + // Replace environment variables in the block states const currentBlockStates = await Object.entries(mergedStates).reduce( async (accPromise, [id, block]) => { @@ -336,16 +366,34 @@ export async function GET(req: NextRequest) { if (typeof value === 'string' && value.includes('{{') && value.includes('}}')) { const matches = value.match(/{{([^}]+)}}/g) if (matches) { + console.log('Processing block:', id, 'subBlock:', key) + console.log('Found variable matches:', matches) + // Process all matches sequentially for (const match of matches) { const varName = match.slice(2, -2) // Remove {{ and }} + console.log('Looking for variable:', varName) + console.log('Available variables:', Object.keys(variables)) + const encryptedValue = variables[varName] + console.log('Found encrypted value:', encryptedValue) + if (!encryptedValue) { throw new Error(`Environment variable "${varName}" was not found`) } - // Decrypt the value - const { decrypted } = await decryptSecret(encryptedValue) - value = (value as string).replace(match, decrypted) + + try { + console.log('Attempting to decrypt value for:', varName) + const { decrypted } = await decryptSecret(encryptedValue) + console.log('Successfully decrypted value for:', varName) + value = (value as string).replace(match, decrypted) + console.log('Successfully replaced value for:', varName) + } catch (error: any) { + console.error('Error decrypting value:', error) + throw new Error( + `Failed to decrypt environment variable "${varName}": ${error.message}` + ) + } } } } diff --git a/lib/utils.ts b/lib/utils.ts index 1173902d0a..d3c61061a2 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -42,7 +42,18 @@ export async function encryptSecret(secret: string): Promise<{ encrypted: string * @returns A promise that resolves to an object containing the decrypted secret */ export async function decryptSecret(encryptedValue: string): Promise<{ decrypted: string }> { - const [ivHex, encrypted, authTagHex] = encryptedValue.split(':') + console.log('Decrypting value:', encryptedValue) + const parts = encryptedValue.split(':') + console.log('Split parts:', parts) + + // Handle case where encrypted part might contain colons + const ivHex = parts[0] + const authTagHex = parts[parts.length - 1] + // Join any middle parts back together as they might be part of the encrypted value + const encrypted = parts.slice(1, -1).join(':') + + console.log('Extracted parts:', { ivHex, encrypted: encrypted?.slice(0, 20) + '...', authTagHex }) + if (!ivHex || !encrypted || !authTagHex) { throw new Error('Invalid encrypted value format. Expected "iv:encrypted:authTag"') } @@ -51,13 +62,18 @@ export async function decryptSecret(encryptedValue: string): Promise<{ decrypted const iv = Buffer.from(ivHex, 'hex') const authTag = Buffer.from(authTagHex, 'hex') - const decipher = createDecipheriv('aes-256-gcm', key, iv) - decipher.setAuthTag(authTag) + try { + const decipher = createDecipheriv('aes-256-gcm', key, iv) + decipher.setAuthTag(authTag) - let decrypted = decipher.update(encrypted, 'hex', 'utf8') - decrypted += decipher.final('utf8') + let decrypted = decipher.update(encrypted, 'hex', 'utf8') + decrypted += decipher.final('utf8') - return { decrypted } + return { decrypted } + } catch (error: any) { + console.error('Decryption error:', error.message) + throw error + } } export function convertScheduleOptionsToCron(