Skip to content

[ZSvirt] bug: TimeUtils timestamp validation accepts invalid dates and trailing input #33

Description

@tllovesxs

Component

utils / time

Problem description

Both TimeUtils.isValidTimestampFormat overloads use SimpleDateFormat.parse(String) with the default lenient mode and without checking that the entire input was consumed.

Relevant code:
https://github.com/ZSvirt/zsvirt/blob/main/utils/src/main/java/org/zstack/utils/TimeUtils.java#L144-L161

As a result, values such as 2024-02-30 10:00:00 and values with trailing characters can be accepted as valid.

Steps to reproduce

  1. Call TimeUtils.isValidTimestampFormat("2024-02-30 10:00:00").
  2. Call TimeUtils.isValidTimestampFormat("2024-01-01 10:00:00xyz").
  3. Observe that the method returns true.

Expected behavior

Validation should reject calendar-invalid dates and any unparsed trailing input.

Proposed fix

Set setLenient(false) and use a ParsePosition or equivalent full-input check for both overloads. Add regression tests for leap-day boundaries, invalid dates, and trailing characters.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions