bito-code-review[bot] commented on code in PR #39795:
URL: https://github.com/apache/superset/pull/39795#discussion_r3207153104


##########
superset-frontend/src/dashboard/util/risonFilters.ts:
##########
@@ -0,0 +1,497 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import {
+  QueryObjectFilterClause,
+  PartialFilters,
+  DataMaskStateWithId,
+} from '@superset-ui/core';
+import rison from 'rison';
+
+export interface RisonFilter {
+  subject: string;
+  operator: string;
+  comparator: string | number | boolean | (string | number)[];
+}
+
+export interface IntelligentRisonInjectionResult {
+  updatedDataMask: DataMaskStateWithId;
+  unmatchedFilters: RisonFilter[];
+}
+
+/**
+ * Parse individual filter condition
+ */
+function parseFilterCondition(key: string, value: unknown): RisonFilter {
+  // Handle comparison operators: (gt:100), (between:!(1,10))
+  if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
+    const [operator, operatorValue] = Object.entries(
+      value as Record<string, unknown>,
+    )[0];
+
+    switch (operator) {
+      case 'gt':
+        return {
+          subject: key,
+          operator: '>',
+          comparator: operatorValue as string | number,
+        };
+      case 'gte':
+        return {
+          subject: key,
+          operator: '>=',
+          comparator: operatorValue as string | number,
+        };
+      case 'lt':
+        return {
+          subject: key,
+          operator: '<',
+          comparator: operatorValue as string | number,
+        };
+      case 'lte':
+        return {
+          subject: key,
+          operator: '<=',
+          comparator: operatorValue as string | number,
+        };
+      case 'between':
+        return {
+          subject: key,
+          operator: 'BETWEEN',
+          comparator: operatorValue as (string | number)[],
+        };
+      case 'like':
+        return {
+          subject: key,
+          operator: 'LIKE',
+          comparator: operatorValue as string,
+        };
+      default:
+        return {
+          subject: key,
+          operator: '==',
+          comparator: value as unknown as string | number,
+        };
+    }
+  }
+
+  // Handle IN operator: !(value1,value2)
+  if (Array.isArray(value)) {
+    return {
+      subject: key,
+      operator: 'IN',
+      comparator: value as (string | number)[],
+    };
+  }
+
+  // Handle simple equality
+  return {
+    subject: key,
+    operator: '==',
+    comparator: value as string | number | boolean,
+  };
+}
+
+/**
+ * Parse Rison filter syntax from URL parameter.
+ * Supports formats like: (country:USA,year:2024)
+ */
+export function parseRisonFilters(risonString: string): RisonFilter[] {
+  try {
+    const parsed = rison.decode(risonString);
+    const filters: RisonFilter[] = [];
+
+    if (!parsed || typeof parsed !== 'object') {
+      return filters;
+    }
+
+    const parsedObj = parsed as Record<string, unknown>;
+
+    // Handle OR operator: OR:!(condition1,condition2)
+    if (parsedObj.OR && Array.isArray(parsedObj.OR)) {
+      (parsedObj.OR as Record<string, unknown>[]).forEach(condition => {
+        if (typeof condition === 'object') {
+          Object.entries(condition).forEach(([key, value]) => {
+            filters.push(parseFilterCondition(key, value));
+          });
+        }
+      });
+      return filters;
+    }
+
+    // Handle NOT operator: NOT:(condition)
+    if (parsedObj.NOT && typeof parsedObj.NOT === 'object') {
+      Object.entries(parsedObj.NOT as Record<string, unknown>).forEach(
+        ([key, value]) => {
+          const filter = parseFilterCondition(key, value);
+          if (filter.operator === '==') {
+            filter.operator = '!=';
+          } else if (filter.operator === 'IN') {
+            filter.operator = 'NOT IN';
+          }
+          filters.push(filter);
+        },
+      );
+      return filters;
+    }
+
+    // Handle regular filters
+    Object.entries(parsedObj).forEach(([key, value]) => {
+      if (key !== 'OR' && key !== 'NOT') {
+        filters.push(parseFilterCondition(key, value));
+      }
+    });
+
+    return filters;
+  } catch (error) {
+    console.warn('Failed to parse Rison filters:', error);
+    return [];
+  }
+}
+
+/**
+ * Convert Rison filters to Superset adhoc filter format
+ */
+export function risonToAdhocFilters(
+  risonFilters: RisonFilter[],
+): QueryObjectFilterClause[] {
+  return risonFilters.map(
+    filter =>
+      ({
+        expressionType: 'SIMPLE' as const,
+        clause: 'WHERE' as const,
+        subject: filter.subject,
+        operator: filter.operator,
+        comparator: filter.comparator,
+      }) as unknown as QueryObjectFilterClause,
+  );
+}
+
+/**
+ * Prettify Rison filter URL by replacing encoded characters.
+ * Uses browser history API to update URL without page reload.
+ */
+export function prettifyRisonFilterUrl(): void {
+  try {
+    const currentUrl = window.location.href;
+
+    if (!currentUrl.includes('&f=') && !currentUrl.includes('?f=')) {
+      return;
+    }
+
+    const urlMatch = currentUrl.match(/([?&])f=([^&]*)/);
+    if (!urlMatch) {
+      return;
+    }
+
+    const separator = urlMatch[1];
+    let risonValue = urlMatch[2];
+
+    if (!risonValue.includes('%') && !risonValue.includes('+')) {
+      return;
+    }
+
+    let previousValue = '';
+    let decodeAttempts = 0;
+    while (risonValue !== previousValue && decodeAttempts < 5) {
+      previousValue = risonValue;
+      try {
+        if (risonValue.includes('%')) {
+          risonValue = decodeURIComponent(risonValue);
+        }
+      } catch {
+        break;
+      }
+      decodeAttempts += 1;
+    }
+
+    risonValue = risonValue.replace(/\+/g, ' ');
+
+    const matchIndex = urlMatch.index ?? 0;
+    const beforeRison = currentUrl.substring(0, matchIndex);
+    const afterRison = currentUrl.substring(matchIndex + urlMatch[0].length);
+    const prettifiedUrl = 
`${beforeRison}${separator}f=${risonValue}${afterRison}`;
+
+    if (prettifiedUrl !== currentUrl) {
+      window.history.replaceState(window.history.state, '', prettifiedUrl);
+    }
+  } catch (error) {
+    console.warn('Failed to prettify Rison URL:', error);
+  }
+}
+
+/**
+ * Get Rison filter parameter from URL
+ */
+export function getRisonFilterParam(): string | null {
+  const params = new URLSearchParams(window.location.search);
+  return params.get('f');
+}
+
+/**
+ * Convert an array of RisonFilter back to Rison string format
+ */
+export function risonFiltersToString(filters: RisonFilter[]): string {
+  if (filters.length === 0) {
+    return '';
+  }
+
+  const risonObject: Record<
+    string,
+    string | number | boolean | (string | number)[] | Record<string, unknown>
+  > = {};
+
+  filters.forEach(filter => {
+    if (filter.operator === 'IN' && Array.isArray(filter.comparator)) {
+      risonObject[filter.subject] = filter.comparator;
+    } else if (filter.operator === '==') {
+      risonObject[filter.subject] = filter.comparator;
+    } else {
+      const operatorMap: Record<string, string> = {
+        '>': 'gt',
+        '>=': 'gte',
+        '<': 'lt',
+        '<=': 'lte',
+        BETWEEN: 'between',
+        LIKE: 'like',
+      };
+
+      const risonOp = operatorMap[filter.operator] || filter.operator;
+      risonObject[filter.subject] = { [risonOp]: filter.comparator };
+    }
+  });
+
+  try {
+    return rison.encode(risonObject);
+  } catch (error) {
+    console.warn('Failed to encode Rison filters:', error);
+    return '';
+  }
+}
+
+/**
+ * Update the URL to remove successfully matched filters, keeping only 
unmatched ones
+ */
+export function updateUrlWithUnmatchedFilters(
+  unmatchedFilters: RisonFilter[],
+): void {
+  try {
+    const currentUrl = new URL(window.location.href);
+
+    if (unmatchedFilters.length === 0) {
+      currentUrl.searchParams.delete('f');
+    } else {
+      const newRisonString = risonFiltersToString(unmatchedFilters);
+      if (newRisonString) {
+        currentUrl.searchParams.set('f', newRisonString);
+      } else {
+        currentUrl.searchParams.delete('f');
+      }
+    }
+
+    window.history.replaceState(
+      window.history.state,
+      '',
+      currentUrl.toString(),
+    );
+  } catch (error) {
+    console.warn('Failed to update URL with unmatched filters:', error);
+  }
+}
+
+/**
+ * Find a native filter that matches a Rison filter by column name.
+ * Uses case-insensitive, trimmed comparison to handle column names with 
spaces.
+ */
+function findMatchingNativeFilter(
+  risonFilter: RisonFilter,
+  nativeFilters: PartialFilters,
+): string | null {
+  const normalizedSubject = risonFilter.subject.trim().toLowerCase();
+
+  for (const [filterId, nativeFilter] of Object.entries(nativeFilters)) {
+    if (!nativeFilter?.targets) continue;
+
+    const hasMatchingTarget = nativeFilter.targets.some(target => {
+      if (typeof target === 'object' && target && 'column' in target) {
+        return target.column?.name?.trim().toLowerCase() === normalizedSubject;
+      }
+      return false;
+    });
+
+    if (hasMatchingTarget) {
+      return filterId;
+    }
+  }
+
+  return null;
+}
+
+/**
+ * Build extraFormData filters for a given rison filter and column name
+ */
+function buildExtraFormDataFilters(
+  risonFilter: RisonFilter,
+  columnName: string,
+): { col: string; op: string; val: unknown }[] {
+  const { operator, comparator } = risonFilter;
+
+  if (operator === 'IN' || (operator === '==' && Array.isArray(comparator))) {
+    return [
+      {
+        col: columnName,
+        op: 'IN',
+        val: Array.isArray(comparator) ? comparator : [comparator],
+      },
+    ];
+  }
+
+  if (operator === '==' && !Array.isArray(comparator)) {
+    return [{ col: columnName, op: 'IN', val: [comparator] }];
+  }
+
+  if (
+    operator === 'BETWEEN' &&
+    Array.isArray(comparator) &&
+    comparator.length === 2
+  ) {
+    return [
+      { col: columnName, op: '>=', val: comparator[0] },
+      { col: columnName, op: '<=', val: comparator[1] },
+    ];
+  }
+
+  return [{ col: columnName, op: operator, val: comparator }];
+}
+
+/**
+ * Convert a Rison filter value to the format expected by a native filter.
+ * Also returns extraFormData for auto-application.
+ */
+function convertRisonToNativeValue(
+  risonFilter: RisonFilter,
+  nativeFilter: { filterType?: string },
+): unknown {
+  const { comparator, operator } = risonFilter;
+  const filterType = nativeFilter?.filterType;
+
+  switch (filterType) {
+    case 'filter_select':
+      if (operator === 'IN' || Array.isArray(comparator)) {
+        return Array.isArray(comparator) ? comparator : [comparator];
+      }
+      return [comparator];
+
+    case 'filter_range':
+      if (
+        operator === 'BETWEEN' &&
+        Array.isArray(comparator) &&
+        comparator.length === 2
+      ) {
+        return { min: comparator[0], max: comparator[1] };
+      }
+      return comparator;
+
+    case 'filter_time_range':
+    case 'filter_timecolumn':
+      return comparator;
+
+    default:
+      return Array.isArray(comparator) ? comparator : [comparator];
+  }
+}
+
+/**
+ * Build a complete DataMask entry for a rison filter matched to a native 
filter.
+ * Sets both filterState.value AND extraFormData so the filter auto-applies.
+ */
+function buildDataMaskForFilter(
+  risonFilter: RisonFilter,
+  nativeFilter: {
+    id: string;
+    filterType?: string;
+    targets?: { column?: { name?: string } }[];
+  },
+  columnName: string,
+) {
+  const convertedValue = convertRisonToNativeValue(risonFilter, nativeFilter);
+
+  return {
+    id: nativeFilter.id,
+    filterState: {
+      value: convertedValue,
+    },
+    extraFormData: {
+      filters: buildExtraFormDataFilters(risonFilter, columnName),
+    },
+    ownState: {},
+  };
+}
+
+/**
+ * Intelligently inject Rison filters into native filters where possible,
+ * falling back to brute-force injection for unmatched filters
+ */
+export function injectRisonFiltersIntelligently(
+  risonFilters: RisonFilter[],
+  nativeFilters: PartialFilters,
+  currentDataMask: DataMaskStateWithId,
+): IntelligentRisonInjectionResult {
+  const updatedDataMask = { ...currentDataMask };
+  const unmatchedFilters: RisonFilter[] = [];
+
+  risonFilters.forEach(risonFilter => {
+    const matchingFilterId = findMatchingNativeFilter(
+      risonFilter,
+      nativeFilters,
+    );
+
+    if (matchingFilterId) {
+      const matchedFilter = nativeFilters[matchingFilterId];
+      const filterId = matchedFilter?.id ?? matchingFilterId;
+      if (matchedFilter && filterId) {
+        const columnName =
+          matchedFilter.targets?.[0]?.column?.name ?? risonFilter.subject;
+
+        const dataMaskEntry = buildDataMaskForFilter(
+          risonFilter,
+          { ...matchedFilter, id: filterId } as {
+            id: string;
+            filterType?: string;
+            targets?: { column?: { name?: string } }[];
+          },
+          columnName,
+        );
+
+        updatedDataMask[filterId] = {
+          ...updatedDataMask[filterId],

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>CWE-1321: Object injection sink in updatedDataMask 
property access</b></div>
   <div id="fix">
   
   Lines 482-483 use dynamic property access on `updatedDataMask[filterId]`. 
The PR fix properly validates `filterId` before use, mitigating the object 
injection risk ([CWE-1321](https://cwe.mitre.org/data/definitions/1321.html)).
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #e64a1d</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to