Skip to content

Commit

Permalink
Fix quoting and url-encoding (opensearch-project#153)
Browse files Browse the repository at this point in the history
* Replace '%2F' with '/' in relative dates

Signed-off-by: Konstantin Roussou <[email protected]>

* Ensure proper quoting of converted dates in URL

Signed-off-by: Konstantin Roussou <[email protected]>

* Use generic URL decoding

Co-authored-by: Zhongnan Su <[email protected]>

* Use roundUp for toDate

Co-authored-by: Zhongnan Su <[email protected]>

* Make date transformation same as in context_menu_helpers.js

Co-authored-by: Zhongnan Su <[email protected]>
  • Loading branch information
kroussou and zhongnansu authored Oct 7, 2021
1 parent 59fc708 commit 1024bed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
25 changes: 12 additions & 13 deletions public/components/context_menu/context_menu_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};
6 changes: 3 additions & 3 deletions public/components/main/report_details/report_details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1024bed

Please sign in to comment.