Server: Disallow loading env file that contains duplicate keys

pull/8623/head
Laurent Cozic 2023-08-05 18:33:26 +01:00
parent c0501fc4e0
commit 9d16dd22be
2 changed files with 12 additions and 1 deletions

View File

@ -99,7 +99,14 @@ async function main() {
const envFilePath = await getEnvFilePath(env, argv);
const envFromFile = envFilePath ? parseEnvFile(envFilePath) : {};
let envFromFile: Record<string, string> = {};
try {
if (envFilePath) envFromFile = parseEnvFile(envFilePath);
} catch (error) {
error.message = `Could not parse env file at ${envFilePath}: ${error.message}`;
throw error;
}
const fullEnv = {
...envFromFile,

View File

@ -9,6 +9,7 @@ export interface Options {
overwrite?: boolean;
raise?: boolean;
logger?: typeof console;
allowDuplicateKeys?: boolean;
}
export const parseEnvFile = (env_file: string, options: Options = {}) => {
@ -17,6 +18,7 @@ export const parseEnvFile = (env_file: string, options: Options = {}) => {
overwrite: false,
raise: true,
verbose: false,
allowDuplicateKeys: false,
...options,
};
@ -101,6 +103,8 @@ export const parseEnvFile = (env_file: string, options: Options = {}) => {
// remove ' and " characters if right side of = is quoted
const env_value = key_value[2].match(/^(['"]?)([^\n]*)\1$/m)[2];
if ((env_key in data[env_file]) && !options.allowDuplicateKeys) throw new Error(`Found duplicate key: ${env_key}`);
if (options.overwrite) {
data[env_file][env_key] = env_value;