From 10535f0b90488b4e630b0fb071b34a66b47ce39c Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Thu, 14 Nov 2019 20:20:22 -0700 Subject: [PATCH] [Maps] prevent users from overflowing URL when filtering by shape --- src/legacy/ui/public/url/index.js | 2 +- .../geometry_filter_form.test.js.snap | 82 +++++++++++++++++++ .../public/components/geometry_filter_form.js | 8 ++ .../components/geometry_filter_form.test.js | 19 +++++ .../feature_geometry_filter_form.js | 19 +++++ 5 files changed, 129 insertions(+), 1 deletion(-) diff --git a/src/legacy/ui/public/url/index.js b/src/legacy/ui/public/url/index.js index a8f7b5fed350c..f3beb05e577c1 100644 --- a/src/legacy/ui/public/url/index.js +++ b/src/legacy/ui/public/url/index.js @@ -19,4 +19,4 @@ export { KbnUrlProvider } from './url'; export { RedirectWhenMissingProvider } from './redirect_when_missing'; -export { modifyUrl } from '../../../../core/public/utils'; +export { modifyUrl } from '../../../../core/utils'; diff --git a/x-pack/legacy/plugins/maps/public/components/__snapshots__/geometry_filter_form.test.js.snap b/x-pack/legacy/plugins/maps/public/components/__snapshots__/geometry_filter_form.test.js.snap index 0205519674be3..98e6fa0517f58 100644 --- a/x-pack/legacy/plugins/maps/public/components/__snapshots__/geometry_filter_form.test.js.snap +++ b/x-pack/legacy/plugins/maps/public/components/__snapshots__/geometry_filter_form.test.js.snap @@ -188,6 +188,88 @@ exports[`should not show "within" relation when filter geometry is not closed 1` `; +exports[`should render error message 1`] = ` + + + + + + + + + My index + + +
+ my geo field + , + "value": "My index/my geo field", + }, + ] + } + valueOfSelected="My index/my geo field" + /> +
+ + + Simulated error + + + + Create filter + + +
+`; + exports[`should render relation select when geo field is geo_shape 1`] = ` {this.props.errorMsg}; + } return ( + {error} + { + const component = shallow( + + ); + + expect(component).toMatchSnapshot(); +}); diff --git a/x-pack/legacy/plugins/maps/public/connected_components/map/features_tooltip/feature_geometry_filter_form.js b/x-pack/legacy/plugins/maps/public/connected_components/map/features_tooltip/feature_geometry_filter_form.js index b5029407d4786..cfc8221a64c5e 100644 --- a/x-pack/legacy/plugins/maps/public/connected_components/map/features_tooltip/feature_geometry_filter_form.js +++ b/x-pack/legacy/plugins/maps/public/connected_components/map/features_tooltip/feature_geometry_filter_form.js @@ -11,11 +11,14 @@ import { i18n } from '@kbn/i18n'; import { createSpatialFilterWithGeometry } from '../../../elasticsearch_geo_utils'; import { GEO_JSON_TYPE } from '../../../../common/constants'; import { GeometryFilterForm } from '../../../components/geometry_filter_form'; +import { UrlOverflowService } from 'ui/error_url_overflow'; +import rison from 'rison-node'; export class FeatureGeometryFilterForm extends Component { state = { isLoading: false, + errorMsg: undefined, } componentDidMount() { @@ -46,6 +49,7 @@ export class FeatureGeometryFilterForm extends Component { } _createFilter = async ({ geometryLabel, indexPatternId, geoFieldName, geoFieldType, relation }) => { + this.setState({ errorMsg: undefined }); const preIndexedShape = await this._loadPreIndexedShape(); if (!this._isMounted) { // do not create filter if component is unmounted @@ -61,6 +65,20 @@ export class FeatureGeometryFilterForm extends Component { geoFieldType, relation, }); + + // When filter contains geometry, ensure filter will not overflow URL + if (!preIndexedShape) { + const urlOverflow = new UrlOverflowService(); + if (window.location.href.length + rison.encode(filter).length > urlOverflow.failLength()) { + this.setState({ + errorMsg: i18n.translate('xpack.maps.tooltip.geometryFilterForm.filterTooLargeMessage', { + defaultMessage: 'Unable to create filter, too many vertices in shape.' + }) + }); + return; + } + } + this.props.addFilters([filter]); this.props.onClose(); } @@ -102,6 +120,7 @@ export class FeatureGeometryFilterForm extends Component { isFilterGeometryClosed={this.props.geometry.type !== GEO_JSON_TYPE.LINE_STRING && this.props.geometry.type !== GEO_JSON_TYPE.MULTI_LINE_STRING} isLoading={this.state.isLoading} + errorMsg={this.state.errorMsg} /> ); }