T-14: Dev QA-Mongo guard misses short or numeric IPv4 loopback spellings (127.1, 2130706433, 0x7f000001, 0177.0.0.1)
This commit is contained in:
parent
5a23e862af
commit
88022a0790
2 changed files with 417 additions and 2 deletions
|
|
@ -47,9 +47,88 @@ function parseIpv4Octets(host: string): [number, number, number, number] | null
|
|||
return [a, b, c, d];
|
||||
}
|
||||
|
||||
function parseIpv4Part(part: string): number | null {
|
||||
let value: number;
|
||||
const hexMatch = /^0x([0-9a-f]*)$/i.exec(part);
|
||||
if (hexMatch) {
|
||||
const digits = hexMatch[1];
|
||||
if (digits === undefined) {
|
||||
return null;
|
||||
}
|
||||
value = digits === '' ? 0 : parseInt(digits, 16);
|
||||
} else if (/^0[0-7]+$/.test(part)) {
|
||||
value = parseInt(part, 8);
|
||||
} else if (/^(?:0|[1-9]\d*)$/.test(part)) {
|
||||
value = parseInt(part, 10);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!Number.isSafeInteger(value) || value < 0 || value > 0xffffffff) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function parseNumericIpv4Octets(host: string): [number, number, number, number] | null {
|
||||
const parts = host.split('.');
|
||||
if (parts.length < 1 || parts.length > 4) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed: number[] = [];
|
||||
for (const part of parts) {
|
||||
const value = parseIpv4Part(part);
|
||||
if (value === null) {
|
||||
return null;
|
||||
}
|
||||
parsed.push(value);
|
||||
}
|
||||
|
||||
const [p0, p1, p2, p3] = parsed;
|
||||
if (p0 === undefined) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (parsed.length === 1) {
|
||||
return [(p0 >>> 24) & 0xff, (p0 >>> 16) & 0xff, (p0 >>> 8) & 0xff, p0 & 0xff];
|
||||
}
|
||||
|
||||
if (parsed.length === 2) {
|
||||
if (p1 === undefined || p0 > 0xff || p1 > 0xffffff) {
|
||||
return null;
|
||||
}
|
||||
return [p0, (p1 >>> 16) & 0xff, (p1 >>> 8) & 0xff, p1 & 0xff];
|
||||
}
|
||||
|
||||
if (parsed.length === 3) {
|
||||
if (p1 === undefined || p2 === undefined || p0 > 0xff || p1 > 0xff || p2 > 0xffff) {
|
||||
return null;
|
||||
}
|
||||
return [p0, p1, (p2 >>> 8) & 0xff, p2 & 0xff];
|
||||
}
|
||||
|
||||
if (
|
||||
p1 === undefined ||
|
||||
p2 === undefined ||
|
||||
p3 === undefined ||
|
||||
p0 > 0xff ||
|
||||
p1 > 0xff ||
|
||||
p2 > 0xff ||
|
||||
p3 > 0xff
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return [p0, p1, p2, p3];
|
||||
}
|
||||
|
||||
function isIpv4Loopback(host: string): boolean {
|
||||
const octets = parseIpv4Octets(host);
|
||||
return octets !== null && octets[0] === 127;
|
||||
const octets = parseNumericIpv4Octets(host);
|
||||
return (
|
||||
octets !== null &&
|
||||
(octets[0] === 127 ||
|
||||
(octets[0] === 0 && octets[1] === 0 && octets[2] === 0 && octets[3] === 0))
|
||||
);
|
||||
}
|
||||
|
||||
function parseIpv6Side(side: string, allowIpv4Tail: boolean): number[] | null {
|
||||
|
|
|
|||
336
test/unit/env-safety-ipv4-loopback.test.ts
Normal file
336
test/unit/env-safety-ipv4-loopback.test.ts
Normal file
|
|
@ -0,0 +1,336 @@
|
|||
import { describe, expect, test } from 'bun:test';
|
||||
import { setLoggerConfig } from '@stock-bot/logger';
|
||||
import {
|
||||
assertEnvironmentSafety,
|
||||
initializeStockConfig,
|
||||
resetStockConfig,
|
||||
stockAppSchema,
|
||||
type StockAppConfig,
|
||||
} from '@stock-bot/stock-config';
|
||||
|
||||
setLoggerConfig({ logLevel: 'fatal', logConsole: false });
|
||||
|
||||
function makeConfig(
|
||||
overrides: {
|
||||
environment?: 'development' | 'qa' | 'test' | 'production';
|
||||
pgHost?: string;
|
||||
pgPort?: number;
|
||||
pgDatabase?: string;
|
||||
mongoUri?: string;
|
||||
mongoReadUri?: string;
|
||||
mongoReadOnly?: boolean;
|
||||
mongoDatabase?: string;
|
||||
} = {}
|
||||
): StockAppConfig {
|
||||
const defaultMongoUri =
|
||||
'mongodb://dev_admin:dev_mongo_pass@localhost:27018/stock_dev?authSource=admin';
|
||||
const defaultMongoReadUri =
|
||||
'mongodb://studio_stock_bot_read:secret@localhost:27017/stock?authSource=stock';
|
||||
|
||||
return stockAppSchema.parse({
|
||||
name: 'stock-bot',
|
||||
version: '1.0.0',
|
||||
environment: overrides.environment ?? 'development',
|
||||
service: {
|
||||
name: 'stock-bot',
|
||||
port: 2002,
|
||||
host: '127.0.0.1',
|
||||
},
|
||||
log: {
|
||||
level: 'info',
|
||||
format: 'json',
|
||||
},
|
||||
database: {
|
||||
postgres: {
|
||||
enabled: true,
|
||||
host: overrides.pgHost ?? 'localhost',
|
||||
port: overrides.pgPort ?? 5433,
|
||||
database: overrides.pgDatabase ?? 'trading_bot_dev',
|
||||
user: 'dev_user',
|
||||
password: 'dev_pass',
|
||||
ssl: false,
|
||||
},
|
||||
mongodb: {
|
||||
enabled: true,
|
||||
uri: overrides.mongoUri ?? defaultMongoUri,
|
||||
readUri: overrides.mongoReadUri ?? defaultMongoReadUri,
|
||||
readOnly: overrides.mongoReadOnly ?? false,
|
||||
database: overrides.mongoDatabase ?? 'stock_dev',
|
||||
},
|
||||
questdb: {
|
||||
enabled: false,
|
||||
host: 'localhost',
|
||||
ilpPort: 9010,
|
||||
httpPort: 9100,
|
||||
pgPort: 8813,
|
||||
},
|
||||
dragonfly: {
|
||||
enabled: true,
|
||||
host: 'localhost',
|
||||
port: 6380,
|
||||
},
|
||||
},
|
||||
providers: {
|
||||
yahoo: {
|
||||
name: 'yahoo',
|
||||
enabled: false,
|
||||
priority: 1,
|
||||
timeout: 30000,
|
||||
baseUrl: 'https://query1.finance.yahoo.com',
|
||||
},
|
||||
qm: {
|
||||
name: 'qm',
|
||||
enabled: false,
|
||||
priority: 2,
|
||||
username: '',
|
||||
password: '',
|
||||
baseUrl: 'https://app.quotemedia.com/quotetools',
|
||||
webmasterId: '',
|
||||
},
|
||||
ib: {
|
||||
name: 'ib',
|
||||
enabled: false,
|
||||
priority: 3,
|
||||
gateway: { host: 'localhost', port: 5000, clientId: 1 },
|
||||
marketDataType: 'delayed',
|
||||
},
|
||||
eod: {
|
||||
name: 'eod',
|
||||
enabled: false,
|
||||
priority: 4,
|
||||
apiKey: '',
|
||||
baseUrl: 'https://eodhistoricaldata.com/api',
|
||||
tier: 'free',
|
||||
},
|
||||
},
|
||||
features: {},
|
||||
});
|
||||
}
|
||||
|
||||
describe('Environment safety: short and numeric IPv4 loopback spellings (T-14)', () => {
|
||||
const reportedLoopbackHosts = ['127.1', '2130706433', '0x7f000001', '0177.0.0.1'] as const;
|
||||
|
||||
test('blocks reported short/numeric IPv4 loopback spellings on :27017 for dev dataIngestion and dataPipeline', () => {
|
||||
for (const host of reportedLoopbackHosts) {
|
||||
const ingestionCfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoUri: `mongodb://trading_admin:trading_mongo_dev@${host}:27017/stock?authSource=admin`,
|
||||
mongoDatabase: 'stock',
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(ingestionCfg, 'dataIngestion', {})).toThrow(
|
||||
/cannot open a writable MongoDB connection to QA MongoDB/
|
||||
);
|
||||
expect(() =>
|
||||
assertEnvironmentSafety(ingestionCfg, 'dataIngestion', {
|
||||
STOCK_BOT_ALLOW_DEV_QA_MONGO_WRITE: 'true',
|
||||
})
|
||||
).not.toThrow();
|
||||
|
||||
const pipelineNonReadOnlyCfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoReadUri: `mongodb://studio_stock_bot_read:secret@${host}:27017/stock?authSource=stock`,
|
||||
mongoReadOnly: false,
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(pipelineNonReadOnlyCfg, 'dataPipeline', {})).toThrow(
|
||||
/MONGODB_READ_URI.*studio_stock_bot_read.*mongoReadOnly: true/
|
||||
);
|
||||
|
||||
const pipelineAdminCfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoReadUri: `mongodb://trading_admin:trading_mongo_dev@${host}:27017/stock?authSource=admin`,
|
||||
mongoReadOnly: true,
|
||||
});
|
||||
expect(() =>
|
||||
assertEnvironmentSafety(pipelineAdminCfg, 'dataPipeline', {
|
||||
STOCK_BOT_ALLOW_DEV_QA_MONGO_WRITE: 'true',
|
||||
})
|
||||
).toThrow(/MONGODB_READ_URI.*studio_stock_bot_read.*mongoReadOnly: true/);
|
||||
}
|
||||
});
|
||||
|
||||
test('blocks numeric IPv4 loopback and 0.0.0.0 shorthand variants across ports, seed lists, encoding, trailing dots, and srv', () => {
|
||||
const loopbackHosts = [
|
||||
'127.1',
|
||||
'127.0.1',
|
||||
'127.65537',
|
||||
'2130706432', // 127.0.0.0
|
||||
'2130706433', // 127.0.0.1
|
||||
'2147483647', // 127.255.255.255
|
||||
'0x7f000001',
|
||||
'0X7F000001',
|
||||
'0x7f.1',
|
||||
'0x7f.0.0.1',
|
||||
'0177.0.0.1',
|
||||
'00177.0.0.1',
|
||||
'0177.1',
|
||||
'0177.0.1',
|
||||
'017700000001',
|
||||
'0x7f.0.01',
|
||||
'127.0x000001',
|
||||
'0',
|
||||
'0.0',
|
||||
'0.0.0',
|
||||
'0x0',
|
||||
'0000',
|
||||
];
|
||||
|
||||
for (const host of loopbackHosts) {
|
||||
const uris = [
|
||||
`mongodb://u:p@${host}:27017/stock`,
|
||||
`mongodb://u:p@${host}/stock`,
|
||||
`mongodb://u:p@${host}:/stock`,
|
||||
`mongodb://u:p@localhost:27018,${host}:27017/stock_dev`,
|
||||
`mongodb://u:p@${host}./stock`,
|
||||
`mongodb+srv://u:p@${host}/stock`,
|
||||
];
|
||||
|
||||
for (const mongoUri of uris) {
|
||||
const cfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoUri,
|
||||
mongoDatabase: 'stock_dev',
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(cfg, 'dataIngestion', {})).toThrow(
|
||||
/cannot open a writable MongoDB connection to QA MongoDB/
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const encodedCfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoUri: 'mongodb://u:p@%31%32%37%2e%31:27017/stock',
|
||||
mongoDatabase: 'stock',
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(encodedCfg, 'dataIngestion', {})).toThrow(
|
||||
/cannot open a writable MongoDB connection to QA MongoDB/
|
||||
);
|
||||
});
|
||||
|
||||
test('blocks development QA PostgreSQL (5432/trading_bot) when pgHost uses short or numeric IPv4 loopback spellings', () => {
|
||||
const pgLoopbackHosts = [...reportedLoopbackHosts, '0', '0.0', '0x0', '0000'];
|
||||
|
||||
for (const pgHost of pgLoopbackHosts) {
|
||||
const cfg = makeConfig({
|
||||
environment: 'development',
|
||||
pgHost,
|
||||
pgPort: 5432,
|
||||
pgDatabase: 'trading_bot',
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(cfg, 'dataPipeline', {})).toThrow(
|
||||
/development environment cannot target QA PostgreSQL/
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('initializeStockConfig blocks numeric loopback MONGODB_URI spellings after schema validation', () => {
|
||||
const envKeys = [
|
||||
'NODE_ENV',
|
||||
'POSTGRES_HOST',
|
||||
'POSTGRES_PORT',
|
||||
'POSTGRES_DB',
|
||||
'POSTGRES_USER',
|
||||
'POSTGRES_PASSWORD',
|
||||
'MONGODB_URI',
|
||||
'MONGODB_READ_URI',
|
||||
'MONGODB_DATABASE',
|
||||
'HTTP_PROXY',
|
||||
'HTTPS_PROXY',
|
||||
'http_proxy',
|
||||
'https_proxy',
|
||||
] as const;
|
||||
const savedEnv: Partial<Record<(typeof envKeys)[number], string | undefined>> = {};
|
||||
for (const key of envKeys) {
|
||||
savedEnv[key] = process.env[key];
|
||||
}
|
||||
|
||||
try {
|
||||
process.env.NODE_ENV = 'development';
|
||||
process.env.POSTGRES_HOST = 'localhost';
|
||||
process.env.POSTGRES_PORT = '5433';
|
||||
process.env.POSTGRES_DB = 'trading_bot_dev';
|
||||
process.env.POSTGRES_USER = 'dev_user';
|
||||
process.env.POSTGRES_PASSWORD = 'dev_pass';
|
||||
process.env.MONGODB_READ_URI =
|
||||
'mongodb://studio_stock_bot_read:secret@localhost:27017/stock?authSource=stock';
|
||||
process.env.MONGODB_DATABASE = 'stock_dev';
|
||||
delete process.env.HTTP_PROXY;
|
||||
delete process.env.HTTPS_PROXY;
|
||||
delete process.env.http_proxy;
|
||||
delete process.env.https_proxy;
|
||||
|
||||
for (const host of reportedLoopbackHosts) {
|
||||
process.env.MONGODB_URI = `mongodb://u:p@${host}:27017/stock`;
|
||||
resetStockConfig();
|
||||
expect(() => initializeStockConfig('dataIngestion')).toThrow(
|
||||
/cannot open a writable MongoDB connection to QA MongoDB/
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
for (const key of envKeys) {
|
||||
if (savedEnv[key] === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = savedEnv[key];
|
||||
}
|
||||
}
|
||||
resetStockConfig();
|
||||
}
|
||||
});
|
||||
|
||||
test('allows dev port 27018, read-only QA dataPipeline, ordinary hostnames, non-loopback numeric IPs, and overflow/invalid octal inputs', () => {
|
||||
// Dev localhost:27018/stock_dev
|
||||
const devLocalhostCfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoUri: 'mongodb://dev_admin:dev_mongo_pass@localhost:27018/stock_dev?authSource=admin',
|
||||
mongoDatabase: 'stock_dev',
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(devLocalhostCfg, 'dataIngestion', {})).not.toThrow();
|
||||
|
||||
// Numeric loopback spellings on dev port 27018
|
||||
for (const host of reportedLoopbackHosts) {
|
||||
const devPortCfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoUri: `mongodb://dev_admin:dev_mongo_pass@${host}:27018/stock_dev?authSource=admin`,
|
||||
mongoDatabase: 'stock_dev',
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(devPortCfg, 'dataIngestion', {})).not.toThrow();
|
||||
}
|
||||
|
||||
// Read-only dataPipeline with studio_stock_bot_read on 127.1:27017/stock
|
||||
const readOnlyPipelineCfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoReadUri: 'mongodb://studio_stock_bot_read:secret@127.1:27017/stock?authSource=stock',
|
||||
mongoReadOnly: true,
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(readOnlyPipelineCfg, 'dataPipeline', {})).not.toThrow();
|
||||
|
||||
// Ordinary hostnames, non-loopback numeric IPs, exact 127.0.0.0/8 boundaries, and overflow/invalid octal inputs
|
||||
const allowedHostsOn27017 = [
|
||||
'mongo.dev.internal',
|
||||
'127.example.com',
|
||||
'2130706433.example.com',
|
||||
'0x7f.internal',
|
||||
'10.0.0.51',
|
||||
'10.1',
|
||||
'167772161', // 10.0.0.1
|
||||
'2130706431', // 126.255.255.255
|
||||
'2147483648', // 128.0.0.0
|
||||
'0176.0.0.1', // 126.0.0.1
|
||||
'0200.0.0.1', // 128.0.0.1
|
||||
'0177', // 0.0.0.127
|
||||
'0x17f000001', // overflow > 0xffffffff (must not wrap into 127.0.0.1)
|
||||
'4294967296', // 2^32 overflow (must not wrap into 0.0.0.0)
|
||||
'127.16777216', // 2-part overflow > 0xffffff
|
||||
'0178.0.0.1', // invalid octal digit
|
||||
];
|
||||
|
||||
for (const host of allowedHostsOn27017) {
|
||||
const cfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoUri: `mongodb://dev_admin:dev_mongo_pass@${host}:27017/stock_dev`,
|
||||
mongoDatabase: 'stock_dev',
|
||||
});
|
||||
expect(() => assertEnvironmentSafety(cfg, 'dataIngestion', {})).not.toThrow();
|
||||
}
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue