Skip to content

Commit

Permalink
Fix AggConfig backslash escaping (#193932)
Browse files Browse the repository at this point in the history
## Summary

Fixes `AggConfig` handling to properly escape backslash as well as the
single quote it already escapes.

(I believe this has been around ever since
#2486.)

(cherry picked from commit 9916dd0)
  • Loading branch information
lukasolson committed Sep 27, 2024
1 parent 7a872dc commit 6f44eae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
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 @@ -31,6 +31,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 @@ -41,17 +50,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

0 comments on commit 6f44eae

Please sign in to comment.