Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Guard against invalid sorting fields in report generation #471

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions server/routes/utils/dataReportHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
} from '../../../../../src/plugins/data/common';
import { ExcelBuilder } from './excelBuilder';

export var metaData = {

Check failure on line 18 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected var, use let or const instead
saved_search_id: <string>null,

Check failure on line 19 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as string' instead of '<string>'
report_format: <string>null,

Check failure on line 20 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as string' instead of '<string>'
start: <string>null,

Check failure on line 21 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as string' instead of '<string>'
end: <string>null,

Check failure on line 22 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as string' instead of '<string>'
fields: <string>null,

Check failure on line 23 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as string' instead of '<string>'
type: <string>null,

Check failure on line 24 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as string' instead of '<string>'
timeFieldName: <string>null,

Check failure on line 25 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as string' instead of '<string>'
sorting: <string>null,

Check failure on line 26 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as string' instead of '<string>'
fields_exist: <boolean>false,

Check failure on line 27 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Use 'as boolean' instead of '<boolean>'
selectedFields: <any>[],

Check warning on line 28 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
paternName: <string>null,
searchSourceJSON: <any>[],

Check warning on line 30 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
dateFields: <any>[],

Check warning on line 31 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
};

// Get the selected columns by the user.
Expand All @@ -51,7 +51,7 @@
// Build the OpenSearch query from the meta data
// is_count is set to 1 if we building the count query but 0 if we building the fetch data query
export const buildRequestBody = (
report: any,

Check warning on line 54 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
allowLeadingWildcards: boolean,
is_count: number
) => {
Expand Down Expand Up @@ -90,11 +90,21 @@
.query(esbBoolQuery)
.version(true);

if (report._source.sorting.length > 0) {
const sortings: Sort[] = report._source.sorting.map((element: string[]) => {
let sorting: string[][] = report._source.sorting;

// We expect a list of [field, order] pairs for sorting. In some migration paths, though it's not
// clear why, this list can get unnested in the case of one sort, [["field", "asc"]] becomes
// ["field", "asc"]. The true root cause remains a mystery, so we work around it.
// See: https://github.com/opensearch-project/dashboards-reporting/issues/371
if (sorting.length > 0 && typeof sorting[0] === "string") {
sorting = [(sorting as unknown as string[])];
}

if (sorting.length > 0) {
const sorts: Sort[] = sorting.map((element: string[]) => {
return esb.sort(element[0], element[1]);
});
esbSearchQuery.sorts(sortings);
esbSearchQuery.sorts(sorts);
}

// add selected fields to query
Expand All @@ -118,13 +128,13 @@

// Fetch the data from OpenSearch
export const getOpenSearchData = (
arrayHits: any,

Check warning on line 131 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
report: { _source: any },

Check warning on line 132 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
params: { excel: any; limit: number },

Check warning on line 133 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
dateFormat: string,
timezone: string
) => {
let hits: any = [];

Check warning on line 137 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
for (let valueRes of arrayHits) {
for (let data of valueRes.hits) {
const fields = data.fields;
Expand Down Expand Up @@ -214,7 +224,7 @@

// Convert the data to Csv format
export const convertToCSV = async (dataset, csvSeparator) => {
let convertedData: any = [];

Check warning on line 227 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const options = {
delimiter: { field: csvSeparator, eol: '\n' },
emptyFieldValue: ' ',
Expand Down Expand Up @@ -242,7 +252,7 @@
return result;
}

function flattenObject(obj = {}, parentKey = '', result: any = {}) {

Check warning on line 255 in server/routes/utils/dataReportHelpers.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
for (const [key, value] of Object.entries(obj)) {
const newKey = parentKey ? `${parentKey}.${key}` : key;

Expand Down
Loading