Skip to content

Commit 6cee49c

Browse files
Allow disabling the host validation via env var (#1637)
Resolves OPS-3078.
1 parent a8a8c27 commit 6cee49c

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

packages/server/shared/src/lib/host-validation/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { promises as dns } from 'dns';
22
import ipRangeCheck from 'ip-range-check';
33
import { isIPv4, isIPv6 } from 'net';
4+
import { SharedSystemProp, system } from '../system';
45

56
const internalV4Cidrs = [
67
'127.0.0.0/8', // Loopback addresses
@@ -55,7 +56,10 @@ async function isInternalHost(host: string): Promise<boolean> {
5556
}
5657

5758
export async function validateHost(host: string | undefined): Promise<void> {
58-
if (!host) return;
59+
const isHostValidationEnabled = system.getBoolean(
60+
SharedSystemProp.ENABLE_HOST_VALIDATION,
61+
);
62+
if (!isHostValidationEnabled || !host) return;
5963
const isPrivate = await isInternalHost(host);
6064
if (isPrivate) throw new Error('Host must not be an internal address');
6165
}

packages/server/shared/src/lib/system/system-prop.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ export enum SharedSystemProp {
148148
LANGFUSE_SECRET_KEY = 'LANGFUSE_SECRET_KEY',
149149
LANGFUSE_PUBLIC_KEY = 'LANGFUSE_PUBLIC_KEY',
150150
LANGFUSE_HOST = 'LANGFUSE_HOST',
151+
152+
ENABLE_HOST_VALIDATION = 'ENABLE_HOST_VALIDATION',
151153
}
152154

153155
export enum WorkerSystemProps {

packages/server/shared/src/lib/system/system.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const systemPropDefaultValues: Partial<Record<SystemProp, string>> = {
9898
[AppSystemProp.LLM_CHAT_EXPIRE_TIME_SECONDS]: '86400', // 24 hours
9999
[AppSystemProp.TELEMETRY_MODE]: 'COLLECTOR',
100100
[AppSystemProp.TELEMETRY_COLLECTOR_URL]: 'https://telemetry.openops.com/save',
101+
[SharedSystemProp.ENABLE_HOST_VALIDATION]: 'true',
101102
};
102103

103104
export const system = {

packages/server/shared/test/host-validation/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
const resolve4 = jest.fn();
22
const resolve6 = jest.fn();
3+
const getBoolean = jest.fn().mockReturnValue(true);
34

45
jest.mock('dns', () => ({ promises: { resolve4, resolve6 } }));
6+
jest.mock('../../src/lib/system', () => ({
7+
system: { getBoolean },
8+
SharedSystemProp: { ENABLE_HOST_VALIDATION: 'ENABLE_HOST_VALIDATION' },
9+
}));
510

611
import { validateHost } from '../../src/lib/host-validation';
712

813
describe('Host Validation', () => {
14+
beforeEach(() => {
15+
getBoolean.mockReturnValue(true);
16+
});
17+
918
afterEach(() => {
1019
jest.clearAllMocks();
1120
});
1221

22+
test('should skip if host validation is disabled', async () => {
23+
getBoolean.mockReturnValue(false);
24+
const host = '127.0.0.1';
25+
await expect(validateHost(host)).resolves.toBeUndefined();
26+
});
27+
28+
test('should skip for empty host', async () => {
29+
const host = '';
30+
await expect(validateHost(host)).resolves.toBeUndefined();
31+
});
32+
1333
test('should throw for private IPv4 address', async () => {
1434
const host = '192.168.1.1';
1535
await expect(validateHost(host)).rejects.toThrow(

0 commit comments

Comments
 (0)