github-advanced-security[bot] commented on code in PR #43870:
URL: https://github.com/apache/superset/pull/43870#discussion_r4340504016


##########
superset-frontend/src/components/Datasource/components/DatasourceEditor/components/PartitionFilterMapping/utils.ts:
##########
@@ -0,0 +1,738 @@
+/**
+ * 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 { t } from '@apache-superset/core/translation';
+import type {
+  PartitionMappingColumn,
+  PartitionMappingDatasource,
+  PartitionMappingIssue,
+  PartitionRowState,
+} from './types';
+
+/**
+ * Jinja delimiters.
+ *
+ * A transform is evaluated in a different context and at a different time from
+ * the chart query, so a template would not render the same way -- the backend
+ * rejects one outright. Shared so the check that declines to preview a Jinja
+ * transform and the check that blocks saving it cannot drift apart.
+ */
+const JINJA_PATTERN = /\{\{|\{%|\{#/;
+
+/**
+ * The column whose filters are mirrored.
+ *
+ * `partition_mapped_column` is an explicit override; `null` means "follow the
+ * default datetime column", so re-pointing that column moves the mapping with
+ * it. Mirrors `SqlaTable.partition_filter_mapping_summary` on the backend.
+ */
+export function resolveMappedColumn(
+  datasource: PartitionMappingDatasource,
+): string | null {
+  if (!datasource.partition_column) {
+    return null;
+  }
+  return datasource.partition_mapped_column || datasource.main_dttm_col || 
null;
+}
+
+/** Whether the mapped column came from `main_dttm_col` rather than an 
override. */
+export function mappedColumnIsImplicit(
+  datasource: PartitionMappingDatasource,
+): boolean {
+  return Boolean(
+    datasource.partition_column &&
+    !datasource.partition_mapped_column &&
+    datasource.main_dttm_col,
+  );
+}
+
+/**
+ * Whether a transform is worth spending a preview request on.
+ *
+ * A necessary condition, never a sufficient one: the server stays the 
authority
+ * on whether a transform is usable, and anything this cannot see -- an
+ * unparseable expression, a non-deterministic function -- still comes back 
from
+ * it. What this catches is the cases that are knowably hopeless while the 
owner
+ * is still typing, which would otherwise spend the per-user preview budget and
+ * leave none for the transform they eventually finish writing.
+ */
+export function transformCanPreview(
+  transform: string | null | undefined,
+  mappedColumn?: string | null,
+  partitionColumn?: string | null,
+): boolean {
+  const trimmed = transform?.trim();
+  if (!trimmed) {
+    return false;
+  }
+  // Without the placeholder there is no value to substitute, so the transform
+  // is inert however well-formed it is.
+  if (!/:value\b/.test(trimmed)) {
+    return false;
+  }
+  // Jinja is rejected on save, so previewing it is pointless.
+  if (JINJA_PATTERN.test(trimmed)) {
+    return false;
+  }
+  // A column cannot stand in for itself; the backend rejects this outright.
+  if (mappedColumn && partitionColumn && mappedColumn === partitionColumn) {
+    return false;
+  }
+  return true;
+}
+
+/**
+ * A declared type naming an array, as the server's `declares_array_type` reads
+ * it: PostgreSQL's `TEXT[]`, and the `ARRAY<...>` / `ARRAY(...)` / 
`Array(...)`
+ * spellings.
+ */
+const ARRAY_TYPE_RE = /(\[\s*\d*\s*\])+\s*$|^\s*ARRAY\b/i;

Review Comment:
   ## CodeQL / Inefficient regular expression
   
   This part of the regular expression may cause exponential backtracking on 
strings starting with '\[' and containing many repetitions of '\t\]\['.
   
   [Show more 
details](https://github.com/apache/superset/security/code-scanning/3969)



-- 
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