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 [];
}
}

View File

@@ -1,11 +1,11 @@
{
"environments": [
{
"uid": "local-env-export-test",
"uid": "FfmX1qYW2EaOpLWeNPLKM",
"name": "local",
"variables": [
{
"uid": "local-env-export-test-host",
"uid": "PYtEmbl0WSSE7NQcYeGx7",
"name": "host",
"value": "http://localhost:3000",
"type": "text",
@@ -13,7 +13,7 @@
"secret": false
},
{
"uid": "local-env-export-test-secret-token",
"uid": "FExjpMlo6zg3egKcgpfop",
"name": "secretToken",
"value": "",
"type": "text",
@@ -23,11 +23,11 @@
]
},
{
"uid": "prod-env-export-test",
"uid": "YCuMlcT9KEWf3cSn3lWOq",
"name": "prod",
"variables": [
{
"uid": "prod-env-export-test-host",
"uid": "cdfGL8VF46DrJtBYkiR8i",
"name": "host",
"value": "https://echo.usebruno.com",
"type": "text",
@@ -35,7 +35,7 @@
"secret": false
},
{
"uid": "prod-env-export-test-secret-token",
"uid": "hTqi6z7CLLWLHxjQ3bwtt",
"name": "secretToken",
"value": "",
"type": "text",
@@ -45,5 +45,5 @@
]
}
],
"activeGlobalEnvironmentUid": "local-env-export-test"
"activeGlobalEnvironmentUid": "FfmX1qYW2EaOpLWeNPLKM"
}

View File

@@ -1,11 +1,11 @@
{
"environments": [
{
"uid": "local-env-export-test",
"uid": "FfmX1qYW2EaOpLWeNPLKM",
"name": "local",
"variables": [
{
"uid": "local-env-export-test-host",
"uid": "rfrIqlfmuyFD4560ciK04",
"name": "host",
"value": "http://localhost:3000",
"type": "text",
@@ -13,7 +13,7 @@
"secret": false
},
{
"uid": "local-env-export-test-secret-token",
"uid": "3WEAWueN0Ov99uOX0uuuM",
"name": "secretToken",
"value": "",
"type": "text",
@@ -23,11 +23,11 @@
]
},
{
"uid": "prod-env-export-test",
"uid": "PYtEmbl0WSSE7NQcYeGx7",
"name": "prod",
"variables": [
{
"uid": "prod-env-export-test-host",
"uid": "tdqX6Yln9DYQYNievEJR1",
"name": "host",
"value": "https://echo.usebruno.com",
"type": "text",
@@ -35,7 +35,7 @@
"secret": false
},
{
"uid": "prod-env-export-test-secret-token",
"uid": "fSxTRpngl8fxkhrl3z7hA",
"name": "secretToken",
"value": "",
"type": "text",
@@ -45,5 +45,5 @@
]
}
],
"activeGlobalEnvironmentUid": "local-env-export-test"
"activeGlobalEnvironmentUid": "FfmX1qYW2EaOpLWeNPLKM"
}

View File

@@ -5,7 +5,7 @@
"name": "Development Environment",
"variables": [
{
"uid": "dev-var-1",
"uid": "lflBDSYBdHkUedYhBF4Ty",
"name": "env_type",
"value": "development",
"type": "text",
@@ -19,7 +19,7 @@
"name": "Production Environment",
"variables": [
{
"uid": "prod-var-1",
"uid": "TZljXLErzW1nUWoozntZE",
"name": "env_type",
"value": "production",
"type": "text",
@@ -33,7 +33,7 @@
"name": "Staging Environment",
"variables": [
{
"uid": "staging-var-1",
"uid": "FwoWhHvu9eLhA8H4brG6f",
"name": "env_type",
"value": "staging",
"type": "text",