fix(core): Normalize quotes in strings in env vars (#18219)

This commit is contained in:
Iván Ovejero
2025-08-12 13:31:30 +02:00
committed by GitHub
parent aeef79df53
commit 71f51519de
2 changed files with 124 additions and 1 deletions

View File

@@ -75,7 +75,7 @@ export const Config: ClassDecorator = (ConfigClass: Class) => {
config[key] = new Date(timestamp); config[key] = new Date(timestamp);
} }
} else if (type === String) { } else if (type === String) {
config[key] = value; config[key] = value.trim().replace(/^(['"])(.*)\1$/, '$2');
} else { } else {
config[key] = new (type as Constructable)(value); config[key] = new (type as Constructable)(value);
} }

View File

@@ -0,0 +1,123 @@
import { Container } from '@n8n/di';
import { GlobalConfig } from '../src/index';
beforeEach(() => {
Container.reset();
jest.clearAllMocks();
});
const originalEnv = process.env;
afterEach(() => {
process.env = originalEnv;
});
it('should strip double quotes from string values', () => {
process.env = {
GENERIC_TIMEZONE: '"America/Bogota"',
N8N_HOST: '"localhost"',
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('America/Bogota');
expect(config.host).toBe('localhost');
});
it('should strip single quotes from string values', () => {
process.env = {
GENERIC_TIMEZONE: "'America/Bogota'",
N8N_HOST: "'localhost'",
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('America/Bogota');
expect(config.host).toBe('localhost');
});
it('should trim whitespace from quoted values', () => {
process.env = {
GENERIC_TIMEZONE: ' "America/Bogota" ',
N8N_HOST: " 'localhost' ",
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('America/Bogota');
expect(config.host).toBe('localhost');
});
it('should trim whitespace from unquoted values', () => {
process.env = {
GENERIC_TIMEZONE: ' America/Bogota ',
N8N_HOST: ' localhost ',
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('America/Bogota');
expect(config.host).toBe('localhost');
});
it('should leave mismatched quotes unchanged', () => {
process.env = {
GENERIC_TIMEZONE: '"America/Bogota\'',
N8N_HOST: '\'localhost"',
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('"America/Bogota\'');
expect(config.host).toBe('\'localhost"');
});
it('should handle empty quotes', () => {
process.env = {
GENERIC_TIMEZONE: '""',
N8N_HOST: "''",
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('');
expect(config.host).toBe('');
});
it('should handle single character in quotes', () => {
process.env = {
GENERIC_TIMEZONE: '"A"',
N8N_HOST: "'B'",
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('A');
expect(config.host).toBe('B');
});
it('should handle values with spaces in quotes', () => {
process.env = {
GENERIC_TIMEZONE: '"America/New York"',
N8N_HOST: "'my host name'",
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('America/New York');
expect(config.host).toBe('my host name');
});
it('should handle nested quotes', () => {
process.env = {
GENERIC_TIMEZONE: '"America/\'Bogota\'"',
N8N_HOST: '\'"localhost"\'',
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe("America/'Bogota'");
expect(config.host).toBe('"localhost"');
});
it('should handle only opening or closing quotes', () => {
process.env = {
GENERIC_TIMEZONE: '"America/Bogota',
N8N_HOST: 'localhost"',
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('"America/Bogota');
expect(config.host).toBe('localhost"');
});
it('should handle multiple quote pairs', () => {
process.env = {
GENERIC_TIMEZONE: '""America/Bogota""',
N8N_HOST: "''localhost''",
};
const config = Container.get(GlobalConfig);
expect(config.generic.timezone).toBe('"America/Bogota"'); // should strip only outer quotes
expect(config.host).toBe("'localhost'");
});