Skip to content

Commit 21348c4

Browse files
authored
chore(backend): replace insecure shortid usage for native filter migration with native uuid Python implementation (apache#32235)
Signed-off-by: hainenber <[email protected]>
1 parent af3589f commit 21348c4

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ dependencies = [
8787
"redis>=4.6.0, <5.0",
8888
"selenium>=4.14.0, <5.0",
8989
"shillelagh[gsheetsapi]>=1.2.18, <2.0",
90-
"shortid",
9190
"sshtunnel>=0.4.0, <0.5",
9291
"simplejson>=3.15.0",
9392
"slack_sdk>=3.19.0, <4",

requirements/base.txt

-2
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,6 @@ selenium==4.27.1
329329
# via apache-superset (pyproject.toml)
330330
shillelagh==1.2.18
331331
# via apache-superset (pyproject.toml)
332-
shortid==0.1.2
333-
# via apache-superset (pyproject.toml)
334332
simplejson==3.19.3
335333
# via apache-superset (pyproject.toml)
336334
six==1.16.0

requirements/development.txt

-4
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,6 @@ shillelagh==1.2.18
738738
# via
739739
# -c requirements/base.txt
740740
# apache-superset
741-
shortid==0.1.2
742-
# via
743-
# -c requirements/base.txt
744-
# apache-superset
745741
simplejson==3.19.3
746742
# via
747743
# -c requirements/base.txt

superset/migrations/shared/native_filters.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
from textwrap import dedent
1919
from typing import Any
2020

21-
from shortid import ShortId
22-
2321
from superset.models.dashboard import Dashboard
2422
from superset.models.slice import Slice
2523
from superset.utils import json
24+
from superset.utils.core import shortid
2625
from superset.utils.dashboard_filter_scopes_converter import convert_filter_scopes
2726

2827

@@ -49,7 +48,6 @@ def convert_filter_scopes_to_native_filters( # pylint: disable=invalid-name,too
4948
:see: convert_filter_scopes
5049
"""
5150

52-
shortid = ShortId()
5351
default_filters = json.loads(json_metadata.get("default_filters") or "{}")
5452
filter_scopes = json_metadata.get("filter_scopes", {})
5553
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
7674
}
7775

7876
# Construct the native filters.
77+
unique_short_ids = set()
7978
for filter_box in filter_boxes:
8079
key = str(filter_box.id)
8180
params = json.loads(filter_box.params or "{}")
8281

8382
for field, filter_scope in filter_scope_by_key_and_field[key].items():
8483
default = default_filters.get(key, {}).get(field)
84+
short_id = f"{shortid()}"[:9]
85+
86+
# Ensure uniqueness due to UUIDv4 truncation increasing
87+
# collision chance to infinitesimally small amount.
88+
while True:
89+
if short_id not in unique_short_ids:
90+
unique_short_ids.add(short_id)
91+
break
92+
else:
93+
short_id = f"{shortid()}"[:9]
8594

8695
fltr: dict[str, Any] = {
8796
"cascadeParentIds": [],
88-
"id": f"NATIVE_FILTER-{shortid.generate()}",
97+
"id": f"NATIVE_FILTER-{short_id}",
8998
"scope": {
9099
"rootPath": filter_scope["scope"],
91100
"excluded": [

0 commit comments

Comments
 (0)