Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Dec 26, 2023
1 parent c9097ec commit 5e6ece3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion posthog/queries/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,16 @@ def is_truthy_or_falsy_property_value(value: Any) -> bool:


def relative_date_parse_for_feature_flag_matching(value: str) -> Optional[datetime.datetime]:
regex = r"(?P<number>[0-9]+)(?P<interval>[a-z])"
regex = r"^(?P<number>[0-9]+)(?P<interval>[a-z])$"
match = re.search(regex, value)
parsed_dt = datetime.datetime.now(tz=ZoneInfo("UTC"))
if match:
number = int(match.group("number"))

if number >= 10_000:
# Guard against overflow, disallow numbers greater than 10_000
return None

interval = match.group("interval")
if interval == "h":
parsed_dt = parsed_dt - relativedelta(hours=number)
Expand Down
19 changes: 19 additions & 0 deletions posthog/queries/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,25 @@ def test_sanitize_keys(key, expected):


class TestRelativeDateParsing(unittest.TestCase):
def test_invalid_input(self):
with freeze_time("2020-01-01T12:01:20.1340Z"):
assert relative_date_parse_for_feature_flag_matching("1") is None
assert relative_date_parse_for_feature_flag_matching("1x") is None
assert relative_date_parse_for_feature_flag_matching("1.2y") is None
assert relative_date_parse_for_feature_flag_matching("1z") is None
assert relative_date_parse_for_feature_flag_matching("1s") is None
assert relative_date_parse_for_feature_flag_matching("123344000.134m") is None
assert relative_date_parse_for_feature_flag_matching("bazinga") is None
assert relative_date_parse_for_feature_flag_matching("000bello") is None
assert relative_date_parse_for_feature_flag_matching("000hello") is None

assert relative_date_parse_for_feature_flag_matching("000h") is not None
assert relative_date_parse_for_feature_flag_matching("1000h") is not None

def test_overflow(self):
assert relative_date_parse_for_feature_flag_matching("1000000h") is None
assert relative_date_parse_for_feature_flag_matching("100000000000000000y") is None

def test_hour_parsing(self):
with freeze_time("2020-01-01T12:01:20.1340Z"):
assert relative_date_parse_for_feature_flag_matching("1h") == datetime.datetime(
Expand Down

0 comments on commit 5e6ece3

Please sign in to comment.