From d6db74d5b15c09800acfcaa4806518c9a671f8e8 Mon Sep 17 00:00:00 2001 From: Mike Johanson Date: Tue, 26 Aug 2025 14:58:52 -0700 Subject: [PATCH] refactor: uses built-in express validator functions instead of custom --- src/routes/admin/proxy/proxyValidator.ts | 68 +++++++------------ .../collections/rps.postman_collection.json | 2 +- 2 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/routes/admin/proxy/proxyValidator.ts b/src/routes/admin/proxy/proxyValidator.ts index 9156a9c83..dfbbc5958 100644 --- a/src/routes/admin/proxy/proxyValidator.ts +++ b/src/routes/admin/proxy/proxyValidator.ts @@ -5,67 +5,45 @@ import { check } from 'express-validator' -const ipv4 = - '^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$' -const ipv6 = - '^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$' -const fqdn = '^(?=.{1,254}$)((?=[a-z0-9-]{1,63}\\.)(xn--+)?[a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,63}$' - export const proxyValidator = (): any => [ - check('accessInfo') - .not() - .isEmpty() - .withMessage('Server address is required') - .custom((value, { req }) => { - if (accessInfoFormatValidator(value, req)) { - return true - } - return false - }), + // Validate and normalize infoFormat first so conditional checks can rely on a number check('infoFormat') .not() .isEmpty() .withMessage('Server address format is required') + .isInt() + .toInt() .isIn([ 3, 4, 201 ]) .withMessage('Server address format should be either 3(IPV4), 4(IPV6) or 201(FQDN)'), - check('port') - .not() - .isEmpty() - .withMessage('Port is required') - .isInt({ min: 0, max: 65535 }) - .withMessage('Port value should range between 0 and 65535'), + + // accessInfo presence + check('accessInfo').not().isEmpty().withMessage('Server address is required'), + + // accessInfo format based on infoFormat + check('accessInfo') + .if((_, { req }) => req.body.infoFormat === 3) + .isIP(4) + .withMessage('infoFormat 3 requires IPV4 server address'), + check('accessInfo') + .if((_, { req }) => req.body.infoFormat === 4) + .isIP(6) + .withMessage('infoFormat 4 requires IPV6 server address'), + check('accessInfo') + .if((_, { req }) => req.body.infoFormat === 201) + .isFQDN({ require_tld: true, allow_underscores: false, allow_numeric_tld: false }) + .withMessage('infoFormat 201 requires FQDN server address'), + + check('port').exists().isPort().withMessage('Port value should range between 1 and 65535'), check('networkDnsSuffix') .not() .isEmpty() .withMessage('Domain name of the network is required') - .matches('^(?=.{1,192}$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z]{2,})+$') + .isFQDN({ require_tld: true, allow_underscores: false }) .withMessage('Domain name of the network should contain alphanumeric and hyphens in the middle') .isLength({ max: 192 }) .withMessage('Domain name of the network maximum length is 192') ] - -function accessInfoFormatValidator(value, req): boolean { - if (req.body.infoFormat == null) { - throw new Error('accessInfo is required') - } - if (value != null) { - if (req.body.infoFormat === 3) { - if (!value.match(ipv4)) { - throw new Error('infoFormat 3 requires IPV4 server address') - } - } else if (req.body.infoFormat === 4) { - if (!value.match(ipv6)) { - throw new Error('infoFormat 4 requires IPV6 server address') - } - } else if (req.body.infoFormat === 201) { - if (!value.match(fqdn)) { - throw new Error('infoFormat 201 requires FQDN server address') - } - } - } - return true -} diff --git a/src/test/collections/rps.postman_collection.json b/src/test/collections/rps.postman_collection.json index b6e449377..b5045a6a7 100644 --- a/src/test/collections/rps.postman_collection.json +++ b/src/test/collections/rps.postman_collection.json @@ -12811,7 +12811,7 @@ "});\r", "pm.test(\"Result should contain an error and message\", function () {\r", " var jsonData = pm.response.json();\r", - " pm.expect(jsonData.errors[0].msg).to.eql(\"Port value should range between 0 and 65535\");\r", + " pm.expect(jsonData.errors[0].msg).to.eql(\"Port value should range between 1 and 65535\");\r", " pm.expect(jsonData.errors[0].path).to.eql(\"port\");\r", "});" ],