Skip to content

Commit

Permalink
[Fleet] Accept socks5 proxy url (elastic#174338)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Jan 8, 2024
1 parent 3e9fd19 commit d672936
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export const FLEET_SERVER_HOST_SAVED_OBJECT_TYPE = 'fleet-fleet-server-host';
export const DEFAULT_FLEET_SERVER_HOST_ID = 'fleet-default-fleet-server-host';

export const FLEET_PROXY_SAVED_OBJECT_TYPE = 'fleet-proxy';

export const PROXY_URL_REGEX = /^(http[s]?|socks5):\/\/[^\s$.?#].[^\s]*$/gm;
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { FLYOUT_MAX_WIDTH } from '../../constants';
import type { FleetProxy } from '../../../../types';
import { TextInput, TextAreaInput } from '../form';

import { useFleetProxyForm } from './user_fleet_proxy_form';
import { useFleetProxyForm } from './use_fleet_proxy_form';

export interface FleetProxyFlyoutProps {
onClose: () => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { createFleetTestRendererMock } from '../../../../../../mock';

import { useFleetProxyForm } from './use_fleet_proxy_form';

describe('useFleetProxyForm', () => {
describe('validate url', () => {
it('should accept http url', async () => {
const testRenderer = createFleetTestRendererMock();
const { result } = testRenderer.renderHook(() => useFleetProxyForm(undefined, () => {}));
result.current.inputs.urlInput.setValue('http://test.fr:8080');
expect(result.current.inputs.urlInput.validate()).toBeTruthy();
expect(result.current.inputs.urlInput.errors).toBeUndefined();
});

it('should accept https url', async () => {
const testRenderer = createFleetTestRendererMock();
const { result } = testRenderer.renderHook(() => useFleetProxyForm(undefined, () => {}));
result.current.inputs.urlInput.setValue('https://test.fr:8080');
expect(result.current.inputs.urlInput.validate()).toBeTruthy();
expect(result.current.inputs.urlInput.errors).toBeUndefined();
});
it('should accept socks5 url', async () => {
const testRenderer = createFleetTestRendererMock();
const { result } = testRenderer.renderHook(() => useFleetProxyForm(undefined, () => {}));
result.current.inputs.urlInput.setValue('socks5://test.fr:8080');
expect(result.current.inputs.urlInput.validate()).toBeTruthy();
expect(result.current.inputs.urlInput.errors).toBeUndefined();
});

it('should not accept invalid url', async () => {
const testRenderer = createFleetTestRendererMock();
const { result } = testRenderer.renderHook(() => useFleetProxyForm(undefined, () => {}));
result.current.inputs.urlInput.setValue('iamnotavaliderror');
expect(result.current.inputs.urlInput.validate()).toBeFalsy();

expect(result.current.inputs.urlInput.errors).toEqual(['Invalid URL']);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import {

import { useConfirmModal } from '../../hooks/use_confirm_modal';
import type { FleetProxy } from '../../../../types';

const URL_REGEX = /^(http)(s)?:\/\/[^\s$.?#].[^\s]*$/gm;
import { PROXY_URL_REGEX } from '../../../../../../../common/constants';

const ConfirmTitle = () => (
<FormattedMessage
Expand All @@ -46,7 +45,7 @@ function validateUrl(value: string) {
];
}

if (!value.match(URL_REGEX)) {
if (!value.match(PROXY_URL_REGEX)) {
return [
i18n.translate('xpack.fleet.settings.fleetProxyFlyoutUrlError', {
defaultMessage: 'Invalid URL',
Expand Down
14 changes: 12 additions & 2 deletions x-pack/plugins/fleet/server/types/rest_spec/fleet_proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@

import { schema } from '@kbn/config-schema';

import { PROXY_URL_REGEX } from '../../../common/constants';

function validateUrl(value: string) {
if (!value.match(PROXY_URL_REGEX)) {
return 'Invalid URL';
}
}

export const PostFleetProxyRequestSchema = {
body: schema.object({
id: schema.maybe(schema.string()),
url: schema.string(),
url: schema.string({
validate: validateUrl,
}),
name: schema.string(),
proxy_headers: schema.maybe(
schema.recordOf(
Expand All @@ -28,7 +38,7 @@ export const PutFleetProxyRequestSchema = {
params: schema.object({ itemId: schema.string() }),
body: schema.object({
name: schema.maybe(schema.string()),
url: schema.maybe(schema.string()),
url: schema.maybe(schema.string({ validate: validateUrl })),
proxy_headers: schema.nullable(
schema.recordOf(
schema.string(),
Expand Down

0 comments on commit d672936

Please sign in to comment.