Skip to content

Commit cc7d78e

Browse files
authored
Merge pull request #50 from spectriclabs/dev
Fix long file name bug and clear fastapi reg ex DOS CVE-2024-24762
2 parents cdb9d34 + 3bf7868 commit cc7d78e

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

elastic_datashader/cache.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import OrderedDict
33
from datetime import datetime, timedelta, timezone
44
from os import scandir
5+
from hashlib import sha256
56
import os
67
from contextlib import suppress
78
from pathlib import Path
@@ -23,14 +24,37 @@ def path_age(now: datetime, path: Path) -> timedelta:
2324

2425
return now - path_dt
2526

27+
index_hash_map = {}
28+
29+
def get_index_hash(idx: str) -> str:
30+
'''
31+
Calculates a hash value for the specific index set
32+
On some OS's the pathname becomes too long and causes errors when
33+
creating files if multiple CCS indexes have been explicitly defined
34+
*:my-data-* listed as
35+
mysite-1:my-data-*,mysite-2:my-data-*,mysite-3:my-data-*,mysite-4:my-data-*,mysite-5:my-data-*
36+
'''
37+
idx_hash = index_hash_map.get(idx, None)
38+
if idx_hash is not None:
39+
return idx_hash
40+
idx_hash = sha256()
41+
idx_hash.update(str(idx).encode("utf-8"))
42+
idx_hash = idx_hash.hexdigest()[0:20]
43+
index_hash_map[idx] = idx_hash
44+
return idx_hash
45+
2646
def tile_name(idx, x, y, z, parameter_hash) -> str:
27-
return f"{idx}/{parameter_hash}/{z}/{x}/{y}.png"
47+
idx_hash = get_index_hash(idx)
48+
return f"{idx_hash}/{parameter_hash}/{z}/{x}/{y}.png"
2849

2950
def rendering_tile_name(idx, x, y, z, parameter_hash) -> str:
30-
return f"{idx}/{parameter_hash}/{z}/{x}/{y}.rendering"
51+
idx_hash = get_index_hash(idx)
52+
53+
return f"{idx_hash}/{parameter_hash}/{z}/{x}/{y}.rendering"
3154

3255
def tile_id(idx, x, y, z, parameter_hash) -> str:
33-
return f"{idx}_{parameter_hash}_{z}_{x}_{y}"
56+
idx_hash = get_index_hash(idx)
57+
return f"{idx_hash}_{parameter_hash}_{z}_{x}_{y}"
3458

3559
def directory_size(path: Path) -> int:
3660
'''
@@ -134,14 +158,14 @@ def release_cache_placeholder(cache_path: Path, tile: str) -> None:
134158
if tile_path.exists():
135159
tile_path.unlink(missing_ok=True)
136160

137-
def check_cache_dir(cache_path: Path, layer_name: str) -> None:
161+
def check_cache_dir(cache_path: Path, idx: str) -> None:
138162
"""
139163
Ensure the folder ``cache_path``/``layer_name`` exists
140164
141165
:param cache_path: Top level directory
142166
:param layer_name: Specific layer in cache
143167
"""
144-
tile_cache_path = cache_path / layer_name
168+
tile_cache_path = cache_path / get_index_hash(idx)
145169
tile_cache_path.mkdir(parents=True, exist_ok=True)
146170

147171
def clear_hash_cache(cache_path: Path, idx_name: str, param_hash: Optional[str]) -> None:

elastic_datashader/parameters.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def get_parameter_hash(params: Dict[str, Any]) -> str:
267267
p = p.isoformat()
268268
parameter_hash.update(str(p).encode("utf-8"))
269269

270-
return parameter_hash.hexdigest()
270+
return parameter_hash.hexdigest()[0:30]
271271

272272
def extract_parameters(headers: Dict[Any, Any], query_params: Dict[Any, Any]) -> Tuple[str, Dict[str, Any]]:
273273
"""

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ numpy = "^1.23"
3737
PyYAML = "*"
3838
humanize = "*"
3939
uvicorn = {extras = ["standard"], version = "0.24.0", optional = true}
40-
fastapi = "^0.96"
40+
fastapi = ">=0.109.1"
4141
georgio = "2023.156.924"
4242
jinja2 = "3.1.2"
4343

tests/test_cache.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def test_du(tmp_path):
2626

2727

2828
def test_tile_name():
29-
assert cache.tile_name("abc", 1, 2, 3, "somehash") == "abc/somehash/3/1/2.png"
29+
assert cache.tile_name("abc", 1, 2, 3, "somehash") == "ba7816bf8f01cfea4141/somehash/3/1/2.png"
3030

3131

3232
def test_tile_id():
33-
assert cache.tile_id("abc", 1, 2, 3, "somehash") == "abc_somehash_3_1_2"
33+
assert cache.tile_id("abc", 1, 2, 3, "somehash") == "ba7816bf8f01cfea4141_somehash_3_1_2"
3434

3535

3636
def test_get_cache_none():
@@ -57,8 +57,8 @@ def test_set_cache(tmp_path):
5757

5858

5959
def test_check_cache_dir(tmp_path):
60-
cache.check_cache_dir(tmp_path, "foo")
61-
assert (tmp_path / "foo").exists()
60+
cache.check_cache_dir(tmp_path, "abc")
61+
assert (tmp_path / "ba7816bf8f01cfea4141").exists()
6262

6363

6464
def test_clear_hash_cache(tmp_path):

tests/test_parameters.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def test_get_category_field():
119119
assert parameters.get_category_field("banana") == "banana"
120120

121121
def test_get_parameter_hash():
122-
assert parameters.get_parameter_hash({"foo": "bar", "baz": 1}) == "a6488297eb1cdaa23e196800b1c399b13d9a5c86cfecaf8f9fd0fbe9af7533db"
123-
assert parameters.get_parameter_hash({"foo": "bar", "baz": 1, "abc": datetime(2022, 2, 17, 11, 0, 0, tzinfo=timezone.utc)}) == "88ade56886a8099e6fd3c25525a0fb9ab3938be64374569480ed3bfd7f3d70e9"
124-
assert parameters.get_parameter_hash({}) == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
122+
assert parameters.get_parameter_hash({"foo": "bar", "baz": 1}) == "a6488297eb1cdaa23e196800b1c399"
123+
assert parameters.get_parameter_hash({"foo": "bar", "baz": 1, "abc": datetime(2022, 2, 17, 11, 0, 0, tzinfo=timezone.utc)}) == "88ade56886a8099e6fd3c25525a0fb"
124+
assert parameters.get_parameter_hash({}) == "e3b0c44298fc1c149afbf4c8996fb9"
125125

126126
def test_get_time_bounds_already_quantized():
127127
now = datetime(2022, 6, 14, 12, 15, 0, tzinfo=timezone.utc)

0 commit comments

Comments
 (0)