Skip to content

Bug: SCHEDULE_BETWEEN_TIME_RANGE mishandles same-hour midnight rollover and accepts malformed HH:MM #8423

Description

@dreamorosi

Expected Behaviour

A time range whose end is earlier than its start on the clock should be treated as crossing midnight regardless of whether the hour digits differ, and only strings that are exactly HH:MM should pass schema validation.

Current Behaviour

Two related defects in the same code path.

  1. Rollover is detected by comparing hours only (if int(end_hour) < int(start_hour) in comparators.py). A range like START: "23:30", END: "23:00" has equal hours, so it takes the same-day branch start <= now <= end, which can never be true. The rule silently never matches.
  2. TIME_RANGE_PATTERN is 2[0-3]:[0-5]\d|[0-1]\d:[0-5]\d with no anchors and is used with re.match. "10:00abc" passes validation. At evaluation time int("00abc") raises, _match_by_action swallows the exception and returns False, so the misconfiguration is invisible.

Code snippet

from aws_lambda_powertools.utilities.feature_flags import FeatureFlags, RuleAction
from aws_lambda_powertools.utilities.feature_flags.schema import SchemaValidator

# 1. same-hour rollover: never matches, at any time of day
features = {
    "late_night": {
        "default": False,
        "rules": {
            "almost all day": {
                "when_match": True,
                "conditions": [{
                    "action": RuleAction.SCHEDULE_BETWEEN_TIME_RANGE.value,
                    "key": "CURRENT_TIME",
                    "value": {"START": "23:30", "END": "23:00"},
                }],
            }
        },
    }
}

# 2. malformed time passes validation
SchemaValidator({
    "f": {"default": False, "rules": {"r": {"when_match": True, "conditions": [{
        "action": RuleAction.SCHEDULE_BETWEEN_TIME_RANGE.value,
        "key": "CURRENT_TIME",
        "value": {"START": "10:00abc", "END": "12:00"},
    }]}}}
}).validate()  # no error raised

Possible Solution

Compare full minutes-since-midnight (start_h*60+start_m vs end_h*60+end_m) to decide rollover, and anchor the pattern (^(?:2[0-3]|[01]\d):[0-5]\d$) or use fullmatch.

Context: we're porting this utility to Powertools for TypeScript (aws-powertools/powertools-lambda-typescript#5614) and want both runtimes to evaluate the same document identically.

Steps to Reproduce

Run the snippet. For (1), evaluate late_night at any time and observe False. For (2), observe validate() returns without error.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions