From 0c13b5ddb15a90b5b8271c796327e43e3e7bdf2f Mon Sep 17 00:00:00 2001 From: Siddhu545 Date: Sat, 12 Apr 2025 17:01:53 +0100 Subject: [PATCH 1/3] Bug #2694 --- src/client.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index a50670ca3..f4cedef16 100644 --- a/src/client.ts +++ b/src/client.ts @@ -203,10 +203,32 @@ export default class Client extends API { if ((opts.cloud != null || opts.serverMode === 'serverless') && opts[kChild] === undefined) { if (opts.cloud != null) { const { id } = opts.cloud + if (typeof id !== 'string') { + throw new errors.ConfigurationError('Cloud ID must be a string.') + } + + const parts = id.split(':') + if (parts.length !== 2 || parts[1] === '') { + throw new errors.ConfigurationError( + 'Cloud ID must be in the format "name:base64string".' + ) + } + // the cloud id is `cluster-name:base64encodedurl` // the url is a string divided by two '$', the first is the cloud url // the second the elasticsearch instance, the third the kibana instance - const cloudUrls = Buffer.from(id.split(':')[1], 'base64').toString().split('$') + + let cloudUrls + try { + cloudUrls = Buffer.from(parts[1], 'base64').toString().split('$') + } catch (err) { + throw new errors.ConfigurationError('Cloud ID base64 decoding failed.') + } + if (cloudUrls.length < 2 || cloudUrls[0] === '' || cloudUrls[1] === '') { + throw new errors.ConfigurationError( + 'Cloud ID base64 must contain at least two "$" separated parts: "$[$]".' + ) + } opts.node = `https://${cloudUrls[1]}.${cloudUrls[0]}` } From 8366d0702ed56534c6810f1dbdb5a654557c7c78 Mon Sep 17 00:00:00 2001 From: Siddhu545 Date: Sat, 12 Apr 2025 21:41:33 +0100 Subject: [PATCH 2/3] added test cases --- src/client.ts | 2 +- test/unit/client.test.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index f4cedef16..348909721 100644 --- a/src/client.ts +++ b/src/client.ts @@ -35,7 +35,7 @@ import { RedactionOptions } from '@elastic/transport/lib/Transport' import BaseConnection, { prepareHeaders, ConnectionOptions } from '@elastic/transport/lib/connection/BaseConnection' import SniffingTransport from './sniffingTransport' import Helpers from './helpers' -import API from './api' +import API from '../src/api/index' import packageJson from '../package.json' import transportPackageJson from '@elastic/transport/package.json' diff --git a/test/unit/client.test.ts b/test/unit/client.test.ts index e57f4d092..7c4aa3339 100644 --- a/test/unit/client.test.ts +++ b/test/unit/client.test.ts @@ -287,9 +287,25 @@ test('Elastic Cloud config', t => { t.equal(connection?.url.hostname, 'abcd.localhost') t.equal(connection?.url.protocol, 'https:') + t.test('Invalid Cloud ID will throw ConfigurationError', t => { + t.throws(() => new Client({ + cloud : { + id : 'invalidCloudIdThatIsNotBase64' + }, + auth : { + username: 'elastic', + password: 'changeme' + } + + }), errors.ConfigurationError) + t.end() + }) + t.end() }) + + test('Override default Elastic Cloud options', t => { const client = new Client({ cloud: { From b10dade5d2ec763e91ac8b9e0d1dfc706fed84d6 Mon Sep 17 00:00:00 2001 From: Siddhu545 Date: Mon, 14 Apr 2025 19:33:56 +0100 Subject: [PATCH 3/3] revert import path to './api' --- src/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 348909721..f4cedef16 100644 --- a/src/client.ts +++ b/src/client.ts @@ -35,7 +35,7 @@ import { RedactionOptions } from '@elastic/transport/lib/Transport' import BaseConnection, { prepareHeaders, ConnectionOptions } from '@elastic/transport/lib/connection/BaseConnection' import SniffingTransport from './sniffingTransport' import Helpers from './helpers' -import API from '../src/api/index' +import API from './api' import packageJson from '../package.json' import transportPackageJson from '@elastic/transport/package.json'