mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
Remove dollar key stripping from search requests (#30483)
This commit is contained in:
parent
d4022d8ddf
commit
1e65ad5ce4
5 changed files with 2 additions and 166 deletions
|
@ -19,7 +19,6 @@
|
|||
|
||||
import _ from 'lodash';
|
||||
import { noWhiteSpace } from '../../utils/no_white_space';
|
||||
import { toJson } from '../../utils/aggressive_parse';
|
||||
import { shortenDottedString } from '../../utils/shorten_dotted_string';
|
||||
|
||||
const templateHtml = `
|
||||
|
@ -46,7 +45,7 @@ export function createSourceFormat(FieldFormat) {
|
|||
}
|
||||
|
||||
SourceFormat.prototype._convert = {
|
||||
text: (value) => toJson(value),
|
||||
text: (value) => JSON.stringify(value),
|
||||
html: function sourceToHtml(source, field, hit) {
|
||||
if (!field) return _.escape(this.getConverterFor('text')(source));
|
||||
|
||||
|
|
|
@ -1,101 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import _ from 'lodash';
|
||||
import expect from 'expect.js';
|
||||
import sinon from 'sinon';
|
||||
import * as aggressiveParse from '../aggressive_parse';
|
||||
|
||||
describe('aggressiveParse', () => {
|
||||
|
||||
let object;
|
||||
let jsonFn;
|
||||
let result;
|
||||
|
||||
beforeEach(() => {
|
||||
object = Object.freeze({
|
||||
foo: 'bar',
|
||||
nums: { two: 2, $three: 3 },
|
||||
another: { level: { $deep: 'inception' } },
|
||||
$no: 'wai'
|
||||
});
|
||||
jsonFn = sinon.stub().returns('{"foo":"bar","$foo":"bar"}');
|
||||
});
|
||||
|
||||
describe('#toJson()', () => {
|
||||
it('returns serialized version of object', () => {
|
||||
result = aggressiveParse.toJson(object);
|
||||
result = JSON.parse(result);
|
||||
|
||||
expect(_.get(result, 'foo')).to.equal(object.foo);
|
||||
expect(_.get(result, 'nums.two')).to.equal(object.nums.two);
|
||||
expect(_.has(result, 'another.level')).to.be(true);
|
||||
});
|
||||
|
||||
it('does not include any properties that begin with $', () => {
|
||||
result = aggressiveParse.toJson(object);
|
||||
result = JSON.parse(result);
|
||||
|
||||
expect(_.has(result, '$no')).to.be(false);
|
||||
expect(_.has(result, 'nums.$three')).to.be(false);
|
||||
expect(_.has(result, 'another.level.$deep')).to.be(false);
|
||||
});
|
||||
|
||||
describe('with arity of 2', () => {
|
||||
beforeEach(() => {
|
||||
result = aggressiveParse.toJson(object, jsonFn);
|
||||
result = JSON.parse(result);
|
||||
});
|
||||
|
||||
it('sends first argument to custom json function', () => {
|
||||
expect(jsonFn.calledWith(object)).to.be(true);
|
||||
});
|
||||
|
||||
it('serializes the json returned by jsonFn', () => {
|
||||
expect(_.get(result, 'foo')).to.equal('bar');
|
||||
});
|
||||
|
||||
it('still does not include any properties that begin with $', () => {
|
||||
expect(result).not.to.have.property('$foo');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with arity of 3', () => {
|
||||
beforeEach(() => {
|
||||
result = aggressiveParse.toJson({ foo: 'bar' }, undefined, 2);
|
||||
});
|
||||
|
||||
it('formats the json string with the number of spaces given', () => {
|
||||
const formattedJson = JSON.stringify({ foo: 'bar' }, null, 2);
|
||||
expect(result).to.be(formattedJson);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#replacer()', () => {
|
||||
it('returns undefined if key begins with $', () => {
|
||||
result = aggressiveParse.replacer('$foo', 'bar');
|
||||
expect(result).to.be(undefined);
|
||||
});
|
||||
it('returns value if key does not being with $', () => {
|
||||
result = aggressiveParse.replacer('foo', 'bar');
|
||||
expect(result).to.equal('bar');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,46 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import { isString, startsWith } from 'lodash';
|
||||
|
||||
/**
|
||||
* Serializes the given object into a JSON string
|
||||
*
|
||||
* All properties that begin with $ throughout the entire object are omitted.
|
||||
* If a custom JSON serializer function is passed, then the given object is
|
||||
* passed through it before being re-stringified with the native stringify.
|
||||
*
|
||||
* The space argument is passed unaltered to the native stringify.
|
||||
*/
|
||||
export function toJson(object, jsonFn, space) {
|
||||
if (jsonFn) {
|
||||
// We reparse the stringified json so that we can lean on JSON.stringify's
|
||||
// avoiding-infinite-recursion capabilities when stripping out any
|
||||
// remaining properties that begin with a dollar sign ($)
|
||||
object = JSON.parse(jsonFn(object));
|
||||
}
|
||||
return JSON.stringify(object, replacer, space);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the given value if the key does not begin with a dollar sign ($)
|
||||
*/
|
||||
export function replacer(key, value) {
|
||||
return isString(key) && startsWith(key, '$') ? undefined : value;
|
||||
}
|
|
@ -18,7 +18,6 @@
|
|||
*/
|
||||
|
||||
import _ from 'lodash';
|
||||
import { toJson } from '../../../../../../core_plugins/kibana/common/utils/aggressive_parse';
|
||||
|
||||
function emptySearch() {
|
||||
return {
|
||||
|
@ -106,7 +105,7 @@ export function serializeFetchParams(
|
|||
header.preference = config.get('courier:customRequestPreference');
|
||||
}
|
||||
|
||||
return `${JSON.stringify(header)}\n${toJson(body, JSON.stringify)}`;
|
||||
return `${JSON.stringify(header)}\n${JSON.stringify(body)}`;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -51,21 +51,6 @@ function serializeFetchParamsWithDefaults(paramOverrides) {
|
|||
);
|
||||
}
|
||||
|
||||
test('filters out any body properties that begin with $', () => {
|
||||
const requestFetchParams = [
|
||||
{
|
||||
index: ['logstash-123'],
|
||||
type: 'blah',
|
||||
search_type: 'blah2',
|
||||
body: { foo: 'bar', $foo: 'bar' }
|
||||
}
|
||||
];
|
||||
return serializeFetchParamsWithDefaults({ requestFetchParams }).then(value => {
|
||||
expect(_.includes(value, 'foo')).toBe(true);
|
||||
expect(_.includes(value, '$foo')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when indexList is not empty', () => {
|
||||
test('includes the index', () => {
|
||||
const requestFetchParams = [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue