Skip to content

Commit 8dbd668

Browse files
committed
fix escape vars
1 parent 8761641 commit 8dbd668

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

x-pack/solutions/observability/plugins/synthetics/server/synthetics_service/formatters/formatting_utils.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* 2.0; you may not use this file except in compliance with the Elastic License
55
* 2.0.
66
*/
7-
import { replaceStringWithParams } from './formatting_utils';
7+
import { inlineSourceFormatter, replaceStringWithParams } from './formatting_utils';
88
import { loggerMock } from '@kbn/logging-mocks';
9+
import { ConfigKey } from '../../../common/constants/monitor_management';
910

1011
describe('replaceStringWithParams', () => {
1112
const logger = loggerMock.create();
@@ -238,4 +239,14 @@ describe('replaceStringWithParams', () => {
238239

239240
expect(result).toEqual({});
240241
});
242+
243+
it(`should replace $ {} with adding $ in start for template literal`, () => {
244+
const value = inlineSourceFormatter(
245+
{
246+
[ConfigKey.SOURCE_INLINE]: 'const a = ${b}; const c = ${d}',
247+
},
248+
ConfigKey.SOURCE_INLINE
249+
);
250+
expect(value).toEqual('const a = $${b}; const c = $${d}');
251+
});
241252
});

x-pack/solutions/observability/plugins/synthetics/server/synthetics_service/formatters/formatting_utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,19 @@ export const formatMWs = (mws?: MaintenanceWindow[], strRes = true) => {
122122
}
123123
return JSON.stringify(formatted);
124124
};
125+
126+
function escapeTemplateLiterals(script: string): string {
127+
return script.replace(/\$\{/g, '$$${');
128+
}
129+
130+
export const inlineSourceFormatter: FormatterFn = (fields, key) => {
131+
const value = fields[key] as string;
132+
if (!value?.trim()) return value;
133+
134+
// Re-indent with 2 spaces, but don't include `|-` here
135+
try {
136+
return escapeTemplateLiterals(value);
137+
} catch (e) {
138+
return value;
139+
}
140+
};

x-pack/solutions/observability/plugins/synthetics/server/synthetics_service/formatters/private_formatters/formatting_utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* 2.0.
66
*/
77

8+
import { inlineSourceFormatter } from '../formatting_utils';
89
import { ConfigKey, MonitorFields } from '../../../../common/runtime_types';
910

1011
export type FormatterFn = (fields: Partial<MonitorFields>, key: ConfigKey) => string | null;
@@ -53,8 +54,12 @@ export const tlsArrayToYamlFormatter: FormatterFn = (fields, key) => {
5354
};
5455

5556
export const stringToJsonFormatter: FormatterFn = (fields, key) => {
56-
const value = (fields[key] as string) ?? '';
57+
if (key === ConfigKey.SOURCE_INLINE) {
58+
const value = inlineSourceFormatter(fields, key);
5759

60+
return value ? JSON.stringify(value) : null;
61+
}
62+
const value = (fields[key] as string) ?? '';
5863
return value ? JSON.stringify(value) : null;
5964
};
6065

x-pack/solutions/observability/plugins/synthetics/server/synthetics_service/formatters/public_formatters/browser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* 2.0.
66
*/
77

8+
import { inlineSourceFormatter } from '../formatting_utils';
89
import { DEFAULT_BROWSER_ADVANCED_FIELDS } from '../../../../common/constants/monitor_defaults';
910
import { BrowserFields, ConfigKey } from '../../../../common/runtime_types';
1011
import { Formatter, commonFormatters } from './common';
@@ -42,7 +43,7 @@ export const browserFormatters: BrowserFormatMap = {
4243
[ConfigKey.PORT]: null,
4344
[ConfigKey.URLS]: null,
4445
[ConfigKey.METADATA]: objectFormatter,
45-
[ConfigKey.SOURCE_INLINE]: null,
46+
[ConfigKey.SOURCE_INLINE]: inlineSourceFormatter,
4647
[ConfigKey.THROTTLING_CONFIG]: throttlingFormatter,
4748
[ConfigKey.JOURNEY_FILTERS_MATCH]: null,
4849
[ConfigKey.SYNTHETICS_ARGS]: arrayFormatter,

0 commit comments

Comments
 (0)