fix: validate public IP response

This commit is contained in:
Viren070
2025-10-12 20:24:09 +01:00
parent 98f3d0203d
commit 3d08959121
2 changed files with 22 additions and 0 deletions
+11
View File
@@ -1,3 +1,4 @@
import z from 'zod';
import { StreamProxyConfig } from '../db/schemas.js';
import {
Cache,
@@ -107,6 +108,16 @@ export abstract class BaseProxy {
const data = await response.json();
const publicIp = this.getPublicIpFromResponse(data);
const { error, success } = z
.union([z.ipv4(), z.ipv6()])
.safeParse(publicIp);
if (error || !success) {
logger.error(
`IP Response of ${publicIp} could not be parsed as a valid IP`
);
throw new Error(`Proxy did not respond with a valid public IP`);
}
if (publicIp && cache) {
await cache.set(cacheKey, publicIp, Env.PROXY_IP_CACHE_TTL);
} else {
+11
View File
@@ -9,6 +9,7 @@ import {
Cache,
} from '../utils/index.js';
import path from 'path';
import z from 'zod';
const logger = createLogger('builtin');
@@ -85,6 +86,16 @@ export class BuiltinProxy extends BaseProxy {
const publicIp = await response.text();
const { error, success } = z
.union([z.ipv4(), z.ipv6()])
.safeParse(publicIp);
if (error || !success) {
logger.error(
`IP Response of ${publicIp} could not be parsed as a valid IP`
);
throw new Error(`Proxy did not respond with a valid public IP`);
}
if (publicIp && cache) {
await cache.set(cacheKey, publicIp, Env.PROXY_IP_CACHE_TTL);
} else {