Skip to content

Commit a85d94e

Browse files
authored
Add usedforsecurity for sha1 algorithm (apache#44081)
SHA1 is cryptographically weak and some restricted environments (FIPS compliant) are blocking weak algorithms. You can use them (as of Python 3.9) in those environments by specifically stating that the algorithm is not used for security.
1 parent 80a2f10 commit a85d94e

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

airflow/models/dagcode.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,9 @@ def dag_fileloc_hash(full_filepath: str) -> int:
163163
import hashlib
164164

165165
# Only 7 bytes because MySQL BigInteger can hold only 8 bytes (signed).
166-
return struct.unpack(">Q", hashlib.sha1(full_filepath.encode("utf-8")).digest()[-8:])[0] >> 8
166+
return (
167+
struct.unpack(
168+
">Q", hashlib.sha1(full_filepath.encode("utf-8"), usedforsecurity=False).digest()[-8:]
169+
)[0]
170+
>> 8
171+
)

airflow/models/taskinstance.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,8 @@ def next_retry_datetime(self):
24562456
# deterministic per task instance
24572457
ti_hash = int(
24582458
hashlib.sha1(
2459-
f"{self.dag_id}#{self.task_id}#{self.logical_date}#{self.try_number}".encode()
2459+
f"{self.dag_id}#{self.task_id}#{self.logical_date}#{self.try_number}".encode(),
2460+
usedforsecurity=False,
24602461
).hexdigest(),
24612462
16,
24622463
)

airflow/sensors/base.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ def _get_next_poke_interval(
365365
# Calculate the jitter
366366
run_hash = int(
367367
hashlib.sha1(
368-
f"{self.dag_id}#{self.task_id}#{started_at}#{estimated_poke_count}".encode()
368+
f"{self.dag_id}#{self.task_id}#{started_at}#{estimated_poke_count}".encode(),
369+
usedforsecurity=False,
369370
).hexdigest(),
370371
16,
371372
)
@@ -384,7 +385,9 @@ def _get_next_poke_interval(
384385
min_backoff = max(int(self.poke_interval * (2 ** (poke_count - 2))), 1)
385386

386387
run_hash = int(
387-
hashlib.sha1(f"{self.dag_id}#{self.task_id}#{started_at}#{poke_count}".encode()).hexdigest(),
388+
hashlib.sha1(
389+
f"{self.dag_id}#{self.task_id}#{started_at}#{poke_count}".encode(), usedforsecurity=False
390+
).hexdigest(),
388391
16,
389392
)
390393
modded_hash = min_backoff + run_hash % min_backoff

airflow/utils/file.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def iter_airflow_imports(file_path: str) -> Generator[str, None, None]:
356356
def get_unique_dag_module_name(file_path: str) -> str:
357357
"""Return a unique module name in the format unusual_prefix_{sha1 of module's file path}_{original module name}."""
358358
if isinstance(file_path, str):
359-
path_hash = hashlib.sha1(file_path.encode("utf-8")).hexdigest()
359+
path_hash = hashlib.sha1(file_path.encode("utf-8"), usedforsecurity=False).hexdigest()
360360
org_mod_name = re2.sub(r"[.-]", "_", Path(file_path).stem)
361361
return MODIFIED_DAG_MODULE_NAME.format(path_hash=path_hash, module_name=org_mod_name)
362362
raise ValueError("file_path should be a string to generate unique module name")

0 commit comments

Comments
 (0)