From 050036898287ff0fdcf3720cec30123101bf5c1f Mon Sep 17 00:00:00 2001 From: Alex Christoffer Rasmussen Date: Sat, 12 Oct 2024 10:52:14 +0200 Subject: [PATCH] Revert http test (#1257) * Revert "refactor: allow messages not starting with http" This reverts commit 887e5c448e427a5166a17067850a5689e683f410. * Revert "refactor: allow messages not starting with http" This reverts commit 6ce275da7ad34575eacad8b94a06d227531d3b7f. --- .../src/common/utils/__tests__/regex.test.ts | 14 +++++++++++++- apps/client/src/common/utils/regex.ts | 1 + .../panel/integrations-panel/HttpIntegrations.tsx | 5 +++++ .../src/utils/__tests__/parserFunctions.test.ts | 1 + apps/server/src/utils/parserFunctions.ts | 6 +++++- 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/apps/client/src/common/utils/__tests__/regex.test.ts b/apps/client/src/common/utils/__tests__/regex.test.ts index e9206110f2..7e34a7fad7 100644 --- a/apps/client/src/common/utils/__tests__/regex.test.ts +++ b/apps/client/src/common/utils/__tests__/regex.test.ts @@ -1,4 +1,4 @@ -import { isAlphanumeric, isIPAddress, isNotEmpty, isOnlyNumbers, startsWithSlash } from '../regex'; +import { isAlphanumeric, isIPAddress, isNotEmpty, isOnlyNumbers, startsWithHttp, startsWithSlash } from '../regex'; describe('simple tests for regex', () => { test('isOnlyNumbers', () => { @@ -25,6 +25,18 @@ describe('simple tests for regex', () => { }); }); + test('startsWithHttp', () => { + const right = ['http://test']; + const wrong = ['https://test', 'testing', '123.0.1']; + + right.forEach((t) => { + expect(startsWithHttp.test(t)).toBe(true); + }); + wrong.forEach((t) => { + expect(startsWithHttp.test(t)).toBe(false); + }); + }); + test('startsWithSlash', () => { const right = ['//test']; const wrong = ['testing', '123.0.1']; diff --git a/apps/client/src/common/utils/regex.ts b/apps/client/src/common/utils/regex.ts index 2681f443fd..3f4848350b 100644 --- a/apps/client/src/common/utils/regex.ts +++ b/apps/client/src/common/utils/regex.ts @@ -5,6 +5,7 @@ export const isOnlyNumbers = /^\d+$/; export const isIPAddress = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/; +export const startsWithHttp = /^http:\/\//; export const startsWithSlash = /^\//; export const isAlphanumeric = /^[a-z0-9]+$/i; export const isASCII = /^[ -~]+$/; //https://catonmat.net/my-favorite-regex diff --git a/apps/client/src/features/app-settings/panel/integrations-panel/HttpIntegrations.tsx b/apps/client/src/features/app-settings/panel/integrations-panel/HttpIntegrations.tsx index 391f095fc0..b12635d977 100644 --- a/apps/client/src/features/app-settings/panel/integrations-panel/HttpIntegrations.tsx +++ b/apps/client/src/features/app-settings/panel/integrations-panel/HttpIntegrations.tsx @@ -8,6 +8,7 @@ import { generateId } from 'ontime-utils'; import { maybeAxiosError } from '../../../../common/api/utils'; import { useHttpSettings, usePostHttpSettings } from '../../../../common/hooks-query/useHttpSettings'; import { isKeyEscape } from '../../../../common/utils/keyEvent'; +import { startsWithHttp } from '../../../../common/utils/regex'; import * as Panel from '../PanelUtils'; import { cycles } from './integrationUtils'; @@ -157,6 +158,10 @@ export default function HttpIntegrations() { placeholder='http://third-party/vt1/{{timer.current}}' {...register(`subscriptions.${index}.message`, { required: { value: true, message: 'Required field' }, + pattern: { + value: startsWithHttp, + message: 'HTTP messages should start with http://', + }, })} /> {maybeError && {maybeError}} diff --git a/apps/server/src/utils/__tests__/parserFunctions.test.ts b/apps/server/src/utils/__tests__/parserFunctions.test.ts index a6983d2be2..a2515c1d66 100644 --- a/apps/server/src/utils/__tests__/parserFunctions.test.ts +++ b/apps/server/src/utils/__tests__/parserFunctions.test.ts @@ -128,6 +128,7 @@ describe('parseHttp()', () => { { id: '1', cycle: 'onLoad', message: 'http://', enabled: true }, // OK {}, // no data { id: '2', cycle: 'onStart', enabled: true }, // no message + { id: '3', cycle: 'onLoad', message: '/test', enabled: true }, // doesnt start with http ], } as HttpSettings; const result = parseHttp({ http }, errorEmitter); diff --git a/apps/server/src/utils/parserFunctions.ts b/apps/server/src/utils/parserFunctions.ts index 65959a188d..df4091e907 100644 --- a/apps/server/src/utils/parserFunctions.ts +++ b/apps/server/src/utils/parserFunctions.ts @@ -222,7 +222,11 @@ export function sanitiseHttpSubscriptions(subscriptions?: HttpSubscription[]): H return subscriptions.filter( ({ id, cycle, message, enabled }) => - typeof id === 'string' && isOntimeCycle(cycle) && typeof message === 'string' && typeof enabled === 'boolean', + typeof id === 'string' && + isOntimeCycle(cycle) && + typeof message === 'string' && + message.startsWith('http://') && + typeof enabled === 'boolean', ); }