mirror of
https://github.com/Viren070/AIOStreams.git
synced 2025-12-01 23:14:04 +01:00
fix: improve IP handling in user data via schema and middleware (#461)
This commit is contained in:
@@ -298,7 +298,7 @@ export const UserDataSchema = z.object({
|
||||
encryptedPassword: z.string().min(1).optional(),
|
||||
trusted: z.boolean().optional(),
|
||||
addonPassword: z.string().optional(),
|
||||
ip: z.union([z.union([z.ipv4(), z.ipv6()])]).optional(),
|
||||
ip: z.union([z.ipv4(), z.ipv6()]).optional(),
|
||||
addonName: z.string().min(1).max(300).optional(),
|
||||
addonLogo: z.string().url().optional(),
|
||||
addonBackground: z.string().url().optional(),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { isIP } from 'net';
|
||||
import {
|
||||
createLogger,
|
||||
APIError,
|
||||
@@ -16,6 +17,13 @@ const logger = createLogger('server');
|
||||
// const VALID_RESOURCES = ['stream', 'configure'];
|
||||
const VALID_RESOURCES = [...constants.RESOURCES, 'manifest.json', 'configure'];
|
||||
|
||||
// Helper function to validate if a string is a valid IP address
|
||||
function isValidIp(ip: string | undefined): boolean {
|
||||
if (!ip) return false;
|
||||
// isIP returns 4 for IPv4, 6 for IPv6, and 0 for invalid
|
||||
return isIP(ip) !== 0;
|
||||
}
|
||||
|
||||
export const userDataMiddleware = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
@@ -99,7 +107,8 @@ export const userDataMiddleware = async (
|
||||
|
||||
userData.encryptedPassword = encryptedPassword;
|
||||
userData.uuid = uuid;
|
||||
userData.ip = req.userIp;
|
||||
// Only set IP if it's a valid IP address or undefined
|
||||
userData.ip = isValidIp(req.userIp) ? req.userIp : undefined;
|
||||
|
||||
if (resource !== 'configure') {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user