Skip to content

Commit

Permalink
fix runtime type risk in parse_timelock_info() (#16345)
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky authored Sep 18, 2023
1 parent 046ef0b commit 40cddbd
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions chia/wallet/conditions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from dataclasses import dataclass, replace
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Type, TypeVar, Union, final
from dataclasses import dataclass, fields, replace
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Type, TypeVar, Union, final, get_type_hints

from blspy import G1Element
from clvm.casts import int_to_bytes
Expand Down Expand Up @@ -1195,6 +1195,14 @@ def to_conditions(self) -> List[Condition]:
return final_condition_list


condition_valid_times_hints = get_type_hints(ConditionValidTimes)
condition_valid_times_types: Dict[str, Type[int]] = {}
for field in fields(ConditionValidTimes):
hint = condition_valid_times_hints[field.name]
[type_] = [type_ for type_ in hint.__args__ if type_ is not type(None)]
condition_valid_times_types[field.name] = type_


# Properties of the dataclass above, grouped by their property
SECONDS_PROPERTIES: Set[str] = {"min_secs_since_created", "min_time", "max_secs_after_created", "max_time"}
HEIGHT_PROPERTIES: Set[str] = {"min_blocks_since_created", "min_height", "max_blocks_after_created", "max_height"}
Expand Down Expand Up @@ -1252,6 +1260,8 @@ def parse_timelock_info(conditions: Iterable[Condition]) -> ConditionValidTimes:
else:
new_value = timelock.timestamp

valid_times = replace(valid_times, **{final_property: new_value})
final_type = condition_valid_times_types[final_property]
replacement: Dict[str, int] = {final_property: final_type(new_value)}
valid_times = replace(valid_times, **replacement)

return valid_times

0 comments on commit 40cddbd

Please sign in to comment.