fix(bru-2079): validations for global environments get/set functions (#6009)

This commit is contained in:
lohit
2025-11-06 18:12:39 +05:30
committed by GitHub
parent 4510cc3414
commit 1ac128e35c
6 changed files with 51 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
const _ = require('lodash');
const Store = require('electron-store');
const { encryptStringSafe, decryptStringSafe } = require('../utils/encryption');
const { environmentSchema } = require('@usebruno/schema');
class GlobalEnvironmentsStore {
constructor() {
@@ -10,6 +11,28 @@ class GlobalEnvironmentsStore {
});
}
/**
* Validates and filters environments array, removing invalid entries
* @param {Array} environments - Array of environment objects to validate
* @returns {Array} - Array of valid environments
*/
filterValidEnvironments(environments) {
if (!Array.isArray(environments)) {
return [];
}
return environments.filter((env) => {
try {
environmentSchema.validateSync(env);
return true;
} catch (error) {
console.error('Invalid environment:', env);
console.error(error);
return false;
}
});
}
encryptGlobalEnvironmentVariables({ globalEnvironments }) {
return globalEnvironments?.map(env => {
const variables = env.variables?.map(v => ({
@@ -40,8 +63,7 @@ class GlobalEnvironmentsStore {
getGlobalEnvironments() {
let globalEnvironments = this.store.get('environments', []);
globalEnvironments = this.decryptGlobalEnvironmentVariables({ globalEnvironments });
// Previously, a bug caused environment variables to be saved without a type.
// Since that issue is now fixed, this code ensures that anyone who imported
// data before the fix will have the missing types added retroactively.
@@ -52,7 +74,11 @@ class GlobalEnvironmentsStore {
}
});
});
globalEnvironments = this.filterValidEnvironments(globalEnvironments);
globalEnvironments = this.decryptGlobalEnvironmentVariables({ globalEnvironments });
return globalEnvironments;
}
@@ -61,6 +87,8 @@ class GlobalEnvironmentsStore {
}
setGlobalEnvironments(globalEnvironments) {
globalEnvironments = this.filterValidEnvironments(globalEnvironments);
globalEnvironments = this.encryptGlobalEnvironmentVariables({ globalEnvironments });
return this.store.set('environments', globalEnvironments);
}

View File

@@ -12,11 +12,11 @@ describe('global environment variable type backward compatibility', () => {
// Mock global environments without type field
const mockGlobalEnvironments = [
{
uid: "env-1",
uid: 'yDlwWe3qgimPG20G7AbF7',
name: "Test Environment",
variables: [
{
uid: "var-1",
uid: 'b6BIHGaCrm4m97YA2dIdx',
name: "regular_var",
value: "regular_value",
enabled: true,
@@ -24,7 +24,7 @@ describe('global environment variable type backward compatibility', () => {
// Missing: type field
},
{
uid: "var-2",
uid: 'yQTqanPoMdRjKnHyIOZNc',
name: "secret_var",
value: "secret_value",
enabled: true,

View File

@@ -26,7 +26,6 @@ function getSystemCerts(): string[] {
return systemCertsCache;
} catch (error) {
console.error(error);
return [];
}
}