From 1024bedae34f69b3282550eb6e0bc386df8be91f Mon Sep 17 00:00:00 2001 From: kroussou <58224693+kroussou@users.noreply.github.com> Date: Thu, 7 Oct 2021 20:08:48 +0300 Subject: [PATCH] Fix quoting and url-encoding (#153) * Replace '%2F' with '/' in relative dates Signed-off-by: Konstantin Roussou * Ensure proper quoting of converted dates in URL Signed-off-by: Konstantin Roussou * Use generic URL decoding Co-authored-by: Zhongnan Su * Use roundUp for toDate Co-authored-by: Zhongnan Su * Make date transformation same as in context_menu_helpers.js Co-authored-by: Zhongnan Su --- .../context_menu/context_menu_helpers.js | 25 +++++++++---------- .../main/report_details/report_details.tsx | 6 ++--- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/public/components/context_menu/context_menu_helpers.js b/public/components/context_menu/context_menu_helpers.js index 1c89007b..b9f615f4 100644 --- a/public/components/context_menu/context_menu_helpers.js +++ b/public/components/context_menu/context_menu_helpers.js @@ -48,15 +48,13 @@ export const getTimeFieldsFromUrl = () => { const url = unhashUrl(window.location.href); let [, fromDateString, toDateString] = url.match(timeRangeMatcher); - fromDateString = fromDateString.replace(/[']+/g, ''); + fromDateString = decodeURIComponent(fromDateString.replace(/[']+/g, '')); // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); - toDateString = toDateString.replace(/[']+/g, ''); - const toDateFormat = dateMath.parse(toDateString); + toDateString = decodeURIComponent(toDateString.replace(/[']+/g, '')); + const toDateFormat = dateMath.parse(toDateString, { roundUp: true }); - const timeDuration = moment.duration( - dateMath.parse(toDateString).diff(dateMath.parse(fromDateString)) - ); + const timeDuration = moment.duration(toDateFormat.diff(fromDateFormat)); return { time_from: fromDateFormat, @@ -141,22 +139,23 @@ export const replaceQueryURL = (pageUrl) => { // we unhash the url in case OpenSearch Dashboards advanced UI setting 'state:storeInSessionStorage' is turned on const unhashedUrl = new URL(unhashUrl(pageUrl)); let queryUrl = unhashedUrl.pathname + unhashedUrl.hash; - let [, fromDateString, toDateString] = queryUrl.match(timeRangeMatcher); - fromDateString = fromDateString.replace(/[']+/g, ''); + let [, fromDateStringMatch, toDateStringMatch] = + queryUrl.match(timeRangeMatcher); + fromDateString = decodeURIComponent(fromDateStringMatch.replace(/[']+/g, '')); // convert time range to from date format in case time range is relative const fromDateFormat = dateMath.parse(fromDateString); - toDateString = toDateString.replace(/[']+/g, ''); - const toDateFormat = dateMath.parse(toDateString); + toDateString = decodeURIComponent(toDateStringMatch.replace(/[']+/g, '')); + const toDateFormat = dateMath.parse(toDateString, { roundUp: true }); // replace to and from dates with absolute date queryUrl = queryUrl.replace( - fromDateString, + fromDateStringMatch, "'" + fromDateFormat.toISOString() + "'" ); queryUrl = queryUrl.replace( - toDateString + '))', - "'" + toDateFormat.toISOString() + "'))" + toDateStringMatch, + "'" + toDateFormat.toISOString() + "'" ); return queryUrl; }; diff --git a/public/components/main/report_details/report_details.tsx b/public/components/main/report_details/report_details.tsx index 280db93f..3fc01585 100644 --- a/public/components/main/report_details/report_details.tsx +++ b/public/components/main/report_details/report_details.tsx @@ -215,11 +215,11 @@ export function ReportDetails(props: { match?: any; setBreadcrumbs?: any; httpCl timeRangeMatcher ); - fromDateString = fromDateString.replace(/[']+/g, ''); - toDateString = toDateString.replace(/[']+/g, ''); + fromDateString = decodeURIComponent(fromDateString.replace(/[']+/g, '')); + toDateString = decodeURIComponent(toDateString.replace(/[']+/g, '')); let fromDateParsed = dateMath.parse(fromDateString); - let toDateParsed = dateMath.parse(toDateString); + let toDateParsed = dateMath.parse(toDateString, { roundUp: true }); const fromTimePeriod = fromDateParsed?.toDate(); const toTimePeriod = toDateParsed?.toDate();