Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.15] Fix AggConfig backslash escaping (#193932) #194323

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getResponseAggId } from './get_response_agg_config_class';

describe('getResponseAggConfigClass', () => {
describe('getResponseAggId', () => {
it('should generate a dot-separated ID from parent/key', () => {
const id = getResponseAggId('parent', 'child');
expect(id).toBe('parent.child');
});

it('should use brackets/quotes if the value includes a dot', () => {
const id = getResponseAggId('parent', 'foo.bar');
expect(id).toBe(`parent['foo.bar']`);
});

it('should escape quotes', () => {
const id = getResponseAggId('parent', `foo.b'ar`);
expect(id).toBe(`parent['foo.b\\'ar']`);
});

it('should escape backslashes', () => {
const id = getResponseAggId('parent', `f\\oo.b'ar`);
expect(id).toBe(`parent['f\\\\oo.b\\'ar']`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export interface IResponseAggConfig extends IMetricAggConfig {
parentId: IMetricAggConfig['id'];
}

export function getResponseAggId(parentId: string, key: string) {
const subId = String(key);
if (subId.indexOf('.') > -1) {
return parentId + "['" + subId.replace(/[\\']/g, '\\$&') + "']"; // $& means the whole matched string
} else {
return parentId + '.' + subId;
}
}

export const create = (parentAgg: IMetricAggConfig, props: Partial<IMetricAggConfig>) => {
/**
* AggConfig "wrapper" for multi-value metric aggs which
Expand All @@ -40,17 +49,7 @@ export const create = (parentAgg: IMetricAggConfig, props: Partial<IMetricAggCon
*/
function ResponseAggConfig(this: IResponseAggConfig, key: string) {
const parentId = parentAgg.id;
let id;

const subId = String(key);

if (subId.indexOf('.') > -1) {
id = parentId + "['" + subId.replace(/'/g, "\\'") + "']";
} else {
id = parentId + '.' + subId;
}

this.id = id;
this.id = getResponseAggId(parentId, key);
this.key = key;
this.parentId = parentId;
}
Expand Down