This is an automated email from the ASF dual-hosted git repository.

michaelsmolina pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 21348c418a chore(backend): replace insecure `shortid` usage for native 
filter migration with native `uuid` Python implementation (#32235)
21348c418a is described below

commit 21348c418ad25b33591d05e48da74c17bc33d35c
Author: Đỗ Trọng Hải <[email protected]>
AuthorDate: Wed Feb 12 23:15:51 2025 +0700

    chore(backend): replace insecure `shortid` usage for native filter 
migration with native `uuid` Python implementation (#32235)
    
    Signed-off-by: hainenber <[email protected]>
---
 pyproject.toml                               |  1 -
 requirements/base.txt                        |  2 --
 requirements/development.txt                 |  4 ----
 superset/migrations/shared/native_filters.py | 17 +++++++++++++----
 4 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/pyproject.toml b/pyproject.toml
index 892a2fea5c..fa7069bf12 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -87,7 +87,6 @@ dependencies = [
     "redis>=4.6.0, <5.0",
     "selenium>=4.14.0, <5.0",
     "shillelagh[gsheetsapi]>=1.2.18, <2.0",
-    "shortid",
     "sshtunnel>=0.4.0, <0.5",
     "simplejson>=3.15.0",
     "slack_sdk>=3.19.0, <4",
diff --git a/requirements/base.txt b/requirements/base.txt
index 3ba7e3f984..e94d77e270 100644
--- a/requirements/base.txt
+++ b/requirements/base.txt
@@ -329,8 +329,6 @@ selenium==4.27.1
     # via apache-superset (pyproject.toml)
 shillelagh==1.2.18
     # via apache-superset (pyproject.toml)
-shortid==0.1.2
-    # via apache-superset (pyproject.toml)
 simplejson==3.19.3
     # via apache-superset (pyproject.toml)
 six==1.16.0
diff --git a/requirements/development.txt b/requirements/development.txt
index 276ca4e20e..96ef635670 100644
--- a/requirements/development.txt
+++ b/requirements/development.txt
@@ -738,10 +738,6 @@ shillelagh==1.2.18
     # via
     #   -c requirements/base.txt
     #   apache-superset
-shortid==0.1.2
-    # via
-    #   -c requirements/base.txt
-    #   apache-superset
 simplejson==3.19.3
     # via
     #   -c requirements/base.txt
diff --git a/superset/migrations/shared/native_filters.py 
b/superset/migrations/shared/native_filters.py
index 1fa097c23b..20cda901c0 100644
--- a/superset/migrations/shared/native_filters.py
+++ b/superset/migrations/shared/native_filters.py
@@ -18,11 +18,10 @@ from collections import defaultdict
 from textwrap import dedent
 from typing import Any
 
-from shortid import ShortId
-
 from superset.models.dashboard import Dashboard
 from superset.models.slice import Slice
 from superset.utils import json
+from superset.utils.core import shortid
 from superset.utils.dashboard_filter_scopes_converter import 
convert_filter_scopes
 
 
@@ -49,7 +48,6 @@ def convert_filter_scopes_to_native_filters(  # pylint: 
disable=invalid-name,too
     :see: convert_filter_scopes
     """
 
-    shortid = ShortId()
     default_filters = json.loads(json_metadata.get("default_filters") or "{}")
     filter_scopes = json_metadata.get("filter_scopes", {})
     filter_box_ids = {filter_box.id for filter_box in filter_boxes}
@@ -76,16 +74,27 @@ def convert_filter_scopes_to_native_filters(  # pylint: 
disable=invalid-name,too
         }
 
     # Construct the native filters.
+    unique_short_ids = set()
     for filter_box in filter_boxes:
         key = str(filter_box.id)
         params = json.loads(filter_box.params or "{}")
 
         for field, filter_scope in filter_scope_by_key_and_field[key].items():
             default = default_filters.get(key, {}).get(field)
+            short_id = f"{shortid()}"[:9]
+
+            # Ensure uniqueness due to UUIDv4 truncation increasing
+            # collision chance to infinitesimally small amount.
+            while True:
+                if short_id not in unique_short_ids:
+                    unique_short_ids.add(short_id)
+                    break
+                else:
+                    short_id = f"{shortid()}"[:9]
 
             fltr: dict[str, Any] = {
                 "cascadeParentIds": [],
-                "id": f"NATIVE_FILTER-{shortid.generate()}",
+                "id": f"NATIVE_FILTER-{short_id}",
                 "scope": {
                     "rootPath": filter_scope["scope"],
                     "excluded": [

Reply via email to