diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c6e11f2bf..b55f545cac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to the Wazuh app project will be documented in this file. - Added validation to the plugin settings in the form of `Settings/Configuration` and the endpoint to update the plugin configuration [#4503](https://github.com/wazuh/wazuh-kibana-app/pull/4503)[#4785](https://github.com/wazuh/wazuh-kibana-app/pull/4785) - Added new plugin settings to customize the header and footer on the PDF reports [#4505](https://github.com/wazuh/wazuh-kibana-app/pull/4505)[#4798](https://github.com/wazuh/wazuh-kibana-app/pull/4798)[#4805](https://github.com/wazuh/wazuh-kibana-app/pull/4805) - Add a new plugin setting to enable or disable the customization [#4507](https://github.com/wazuh/wazuh-kibana-app/pull/4507) +- Added a centralized service to handle the requestrs [#4758](https://github.com/wazuh/wazuh-kibana-app/pull/4758) ### Changed @@ -41,9 +42,11 @@ commands of Start the agent in the deploy new agent section [#4458](https://gith - Fixed nested field rendering in security alerts table details [#4428](https://github.com/wazuh/wazuh-kibana-app/pull/4428) - Fixed a bug where the Wazuh logo was used instead of the custom one [#4539](https://github.com/wazuh/wazuh-kibana-app/pull/4539) - Fixed rendering problems of the `Agent Overview` section in low resolutions [#4516](https://github.com/wazuh/wazuh-kibana-app/pull/4516) -- Fixed issue when logging out from Wazuh when SAML is enabled [#4595](https://github.com/wazuh/wazuh-kibana-app/issues/4595) +- Fixed issue when logging out from Wazuh when SAML is enabled [#4664](https://github.com/wazuh/wazuh-kibana-app/pull/4664) - Fixed server errors with code 500 when the Wazuh API is not reachable / up. [#4710](https://github.com/wazuh/wazuh-kibana-app/pull/4710) [#4728](https://github.com/wazuh/wazuh-kibana-app/pull/4728) - Fixed pagination to SCA table [#4653](https://github.com/wazuh/wazuh-kibana-app/issues/4653) +- Fixed a bug that caused the flyouts to close when clicking inside them [#4638](https://github.com/wazuh/wazuh-kibana-app/pull/4638) +- Fixed a bug that caused the main Office 365 dashboard to display an incorrect Max rule level [#4508](https://github.com/wazuh/wazuh-kibana-app/pull/4508) ## Wazuh v4.3.9 - Kibana 7.10.2, 7.16.x, 7.17.x - Revision 4310 diff --git a/public/plugin.ts b/public/plugin.ts index 18e3d57be3..6478fc290c 100644 --- a/public/plugin.ts +++ b/public/plugin.ts @@ -31,6 +31,7 @@ import { getThemeAssetURL, getAssetURL } from './utils/assets'; import { WzRequest } from './react-services/wz-request'; import store from './redux/store'; import { updateAppConfig } from './redux/actions/appConfigActions'; +import { initializeInterceptor } from './services/request-handler'; const SIDEBAR_LOGO = 'customization.logo.sidebar'; const innerAngularName = 'app/wazuh'; @@ -164,6 +165,7 @@ export class WazuhPlugin implements Plugin> { try { @@ -172,8 +168,8 @@ export class WzRequest { * @param message * @returns error */ - static returnErrorInstance(error, message){ - if(!error || typeof error === 'string'){ + static returnErrorInstance(error, message) { + if (!error || typeof error === 'string') { return new Error(message || error); } error.message = message diff --git a/public/services/request-handler.js b/public/services/request-handler.js new file mode 100644 index 0000000000..ca5386abea --- /dev/null +++ b/public/services/request-handler.js @@ -0,0 +1,89 @@ +import { getCore } from '../kibana-services'; + +let allow = true; +let aborts = []; +let currentid = 0; + +const removeController = (id) => { + const index = aborts.findIndex(object => { + return object.id === id; + }); + if (!id) { + return; + } + aborts.splice(index); + return; +} + +export const disableRequests = () => { + allow = false; + aborts.forEach(item => { + item.controller.abort(); + }) + return; +} + +export const initializeInterceptor = () => { + const core = getCore(); + core.http.intercept({ + responseError: (httpErrorResponse, controller) => { + if ( + httpErrorResponse.response?.status === 401 + ) { + disableRequests(); + setTimeout(() => window.location.reload(), 1000); + } + }, + }); +} + +export const request = async (info = '') => { + if (!allow) { + return Promise.reject('Requests are disabled'); + } + + + if (!info.method | !info.path) { + return Promise.reject("Missing parameters") + } + + let { method, path, headers, data, timeout } = info; + const core = getCore(); + const url = path.split('?')[0] + + const query = Object.fromEntries([... new URLSearchParams(path.split('?')[1])]) + const abort = new AbortController(); + let options = { + method: method, + headers: headers, + query: query, + signal: abort.signal, + id: currentid + } + currentid++; + + if (method !== 'GET') { + options = { ...options, body: JSON.stringify(data) } + } + + if (allow) { + try { + aborts.push({ id: options.id, controller: abort }) + if (timeout && timeout !== 0) { + const id = setTimeout(() => abort.abort(), timeout); + const requestData = await core.http.fetch(url, options); + clearTimeout(id); + removeController(options.id); + return Promise.resolve({ data: requestData, timeout: timeout }); + } + else { + const requestData = await core.http.fetch(url, options); + removeController(options.id); + return Promise.resolve({ data: requestData }); + } + } + catch (e) { + return Promise.reject(e); + } + } +} \ No newline at end of file diff --git a/public/services/request-handler.test.js b/public/services/request-handler.test.js new file mode 100644 index 0000000000..870f588a7e --- /dev/null +++ b/public/services/request-handler.test.js @@ -0,0 +1,72 @@ +import { request, disableRequests } from "./request-handler"; +import * as kibanaServices from '../kibana-services'; + +function waitTimeout(ms) { + return new Promise((resolve) => setTimeout(() => resolve(true), ms)); +} + +const fetchFunction = async (url, options) => { + if (url === '/invalidurl') { + return Promise.reject('URL Not found') + } + else { + return Promise.resolve('data') + } +} +const coreExpected = { + http: { + fetch: fetchFunction + } +} + + +describe('test request-handler', () => { + beforeAll(() => { + jest.spyOn(kibanaServices, 'getCore').mockReturnValue(coreExpected) + }) + + it('should resolve data', () => { + const info = { + method: 'GET', + path: '/validurl', + } + expect(request(info)).resolves.toMatchObject({ "data": "data" }) + }); + + it('should reject "URL Not found" error', () => { + const info = { + method: 'GET', + path: '/invalidurl', + } + expect(request(info)).rejects.toBe('URL Not found') + }); + + it('should reject "Missing parameters" (no path)', () => { + const info = { + method: 'GET' + } + expect(request(info)).rejects.toBe('Missing parameters') + }); + + it('should reject "Missing parameters" (no method)', () => { + const info = { + path: '/validurl', + } + expect(request(info)).rejects.toBe('Missing parameters') + }); + + it('should reject "Missing parameters" (no parameters)', () => { + expect(request()).rejects.toBe('Missing parameters') + }); + + it('should reject "Requests are disabled"', () => { + disableRequests(); + const info = { + method: 'GET', + path: '/invalidurl', + } + expect(request(info)).rejects.toBe('Requests are disabled') + + }); + +}); diff --git a/server/plugin.ts b/server/plugin.ts index 8fcd37351b..1e4b400ea1 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -35,6 +35,7 @@ import * as ApiInterceptor from './lib/api-interceptor'; import { schema, TypeOf } from '@kbn/config-schema'; import type { Observable } from 'rxjs'; import { first } from 'rxjs/operators'; +import {disableRequests} from '../public/services/request-handler' declare module 'kibana/server' { interface RequestHandlerContext { @@ -73,6 +74,9 @@ export class WazuhPlugin implements Plugin { let wazuhSecurity; core.http.registerRouteHandlerContext('wazuh', async(context, request) => { !wazuhSecurity && (wazuhSecurity = await SecurityObj(plugins, context)); + if(!request.auth.isAuthenticated){ + disableRequests(); + } return { logger: this.logger, server: {