From 4a7671e0cc97e123531f8c5fb515c8727b1a3e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6ran=20Sander?= Date: Fri, 20 Sep 2024 17:53:57 +0000 Subject: [PATCH] fix(config): Validate hostname, url and pasword fields in config file. --- src/lib/config-file-schema.js | 56 ++++++++++++++++++++++++++++------- src/lib/config-file-verify.js | 6 ++++ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/lib/config-file-schema.js b/src/lib/config-file-schema.js index b56fd51a..2ab15573 100755 --- a/src/lib/config-file-schema.js +++ b/src/lib/config-file-schema.js @@ -16,7 +16,10 @@ export const confifgFileSchema = { type: 'object', properties: { enable: { type: 'boolean' }, - host: { type: 'string' }, + host: { + type: 'string', + format: 'hostname', + }, port: { type: 'number' }, obfuscate: { type: 'boolean' }, }, @@ -27,7 +30,10 @@ export const confifgFileSchema = { type: 'object', properties: { enable: { type: 'boolean' }, - remoteURL: { type: 'string' }, + remoteURL: { + type: 'string', + format: 'uri', + }, frequency: { type: 'string' }, }, required: ['enable', 'remoteURL', 'frequency'], @@ -246,7 +252,10 @@ export const confifgFileSchema = { udpServerConfig: { type: 'object', properties: { - serverHost: { type: 'string' }, + serverHost: { + type: 'string', + format: 'hostname', + }, portUserActivityEvents: { type: 'number' }, }, required: ['serverHost', 'portUserActivityEvents'], @@ -363,7 +372,10 @@ export const confifgFileSchema = { udpServerConfig: { type: 'object', properties: { - serverHost: { type: 'string' }, + serverHost: { + type: 'string', + format: 'hostname', + }, portLogEvents: { type: 'number' }, }, required: ['serverHost', 'portLogEvents'], @@ -868,6 +880,7 @@ export const confifgFileSchema = { clientCertCA: { type: 'string' }, clientCertPassphrase: { type: ['string', 'null'], + format: 'password', }, }, required: ['clientCert', 'clientCertKey', 'clientCertCA'], @@ -877,7 +890,10 @@ export const confifgFileSchema = { type: 'object', properties: { enable: { type: 'boolean' }, - brokerHost: { type: 'string' }, + brokerHost: { + type: 'string', + format: 'hostname', + }, brokerPort: { type: 'number' }, baseTopic: { type: 'string' }, }, @@ -891,7 +907,10 @@ export const confifgFileSchema = { event: { type: 'object', properties: { - url: { type: 'string' }, + url: { + type: 'string', + format: 'uri', + }, header: { type: ['array', 'null'], items: { @@ -950,7 +969,10 @@ export const confifgFileSchema = { type: 'string', }, }, - url: { type: 'string' }, + url: { + type: 'string', + format: 'uri', + }, header: { type: ['array', 'null'], items: { @@ -1166,7 +1188,10 @@ export const confifgFileSchema = { type: 'object', properties: { enable: { type: 'boolean' }, - host: { type: 'string' }, + host: { + type: 'string', + format: 'hostname', + }, port: { type: 'number' }, }, required: ['enable', 'port'], @@ -1176,7 +1201,10 @@ export const confifgFileSchema = { type: 'object', properties: { enable: { type: 'boolean' }, - host: { type: 'string' }, + host: { + type: 'string', + format: 'hostname', + }, port: { type: 'number' }, version: { type: 'number' }, v2Config: { @@ -1205,7 +1233,10 @@ export const confifgFileSchema = { properties: { enable: { type: 'boolean' }, username: { type: 'string' }, - password: { type: 'string' }, + password: { + type: 'string', + format: 'password', + }, }, required: ['enable', 'username', 'password'], additionalProperties: false, @@ -1251,7 +1282,10 @@ export const confifgFileSchema = { properties: { enableAppNameExtract: { type: 'boolean' }, extractInterval: { type: 'number' }, - hostIP: { type: 'string' }, + hostIP: { + type: 'string', + format: 'hostname', + }, }, required: ['enableAppNameExtract', 'extractInterval', 'hostIP'], additionalProperties: false, diff --git a/src/lib/config-file-verify.js b/src/lib/config-file-verify.js index 76de3725..ad2b9469 100755 --- a/src/lib/config-file-verify.js +++ b/src/lib/config-file-verify.js @@ -21,6 +21,12 @@ export async function verifyConfigFile() { // Add keywords to ajv instance ajvKeywords.default(ajv); + // Dynamically import ajv-formats + const ajvFormats = await import('ajv-formats'); + + // Add formats to ajv instance + ajvFormats.default(ajv); + // Load the YAML schema file, identified by globals.configFile, from file const fileContent = await fs.readFile(globals.configFile, 'utf8');