From 0712affa9b23ae5a5ddeb53e63a289a99a15596c Mon Sep 17 00:00:00 2001 From: Kersom <9053044+nixocio@users.noreply.github.com> Date: Fri, 8 Apr 2022 13:08:32 -0400 Subject: [PATCH] Escape name__regex and name__iregex (#11964) Escape name__regex and name__iregex. Escaping the value for those keys when creating a smart inventory is a work around for the pyparsing code on the API side for special characters. This will just display an extra escape when showing the host_filter on details page. --- .../src/components/Lookup/shared/HostFilterUtils.js | 11 +++++++---- .../components/Lookup/shared/HostFilterUtils.test.js | 11 +++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/awx/ui/src/components/Lookup/shared/HostFilterUtils.js b/awx/ui/src/components/Lookup/shared/HostFilterUtils.js index 986df2c901..8d6f9fd049 100644 --- a/awx/ui/src/components/Lookup/shared/HostFilterUtils.js +++ b/awx/ui/src/components/Lookup/shared/HostFilterUtils.js @@ -68,11 +68,12 @@ export function toQueryString(config, searchParams = {}) { /** * Escape a string with double quote in case there was a white space + * @param {string} key The key of the value to be parsed * @param {string} value A string to be parsed * @return {string} string */ -const escapeString = (value) => { - if (verifySpace(value)) { +const escapeString = (key, value) => { + if (verifySpace(value) || key.includes('regex')) { return `"${value}"`; } return value; @@ -95,9 +96,11 @@ export function toHostFilter(searchParams = {}) { .sort() .flatMap((key) => { if (Array.isArray(searchParams[key])) { - return searchParams[key].map((val) => `${key}=${escapeString(val)}`); + return searchParams[key].map( + (val) => `${key}=${escapeString(key, val)}` + ); } - return `${key}=${escapeString(searchParams[key])}`; + return `${key}=${escapeString(key, searchParams[key])}`; }); const filteredSearchParams = flattenSearchParams.filter( diff --git a/awx/ui/src/components/Lookup/shared/HostFilterUtils.test.js b/awx/ui/src/components/Lookup/shared/HostFilterUtils.test.js index 93a491d662..79475fe614 100644 --- a/awx/ui/src/components/Lookup/shared/HostFilterUtils.test.js +++ b/awx/ui/src/components/Lookup/shared/HostFilterUtils.test.js @@ -136,6 +136,17 @@ describe('toHostFilter', () => { ); }); + test('should escape name__regex and name__iregex', () => { + const object = { + or__name__regex: '(t|e)st', + or__name__iregex: '(f|o)', + or__name: 'foo', + }; + expect(toHostFilter(object)).toEqual( + 'name=foo or name__iregex="(f|o)" or name__regex="(t|e)st"' + ); + }); + test('should return a host filter with or conditional when value is array', () => { const object = { or__groups__id: ['1', '2'],