Skip to content

Error in datetime-regex #1260

@Agh42

Description

@Agh42

Describe the bug

I believe there to be a bug in the "datetime with timezone" regex as described in https://github.com/usnistgov/OSCAL/edit/main/docs/content/reference/datatypes.md

As it stands, the regex will match dates 2017-06-04T00:00:00Z and 1999-04-03T12:13:45Z but it will NOT match i.e. 2400-02-29T00:00:00Z 2022-05-03T02:00:00Z.

This is because the T separating the date from the time part of the regex is only applicable after the fourth of the major matching groups. There has to be an additional group containing the first three major blocks (the ones that are present to differentiate between leap years and normal years).

I think to make the regex work like it's supposed to it needs to be this:

(((2000|2400|2800|(19|20-9))-02-29)|(((19|2[0-9])[0-9]{2})-02-(0[1-9]|1[0-9]|2[0-8]))|(((19|2[0-9])[0-9]{2})-(0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01]))|(((19|2[0-9])[0-9]{2})-(0[469]|11)-(0[1-9]|[12][0-9]|30)))T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})

instead of the current

((2000|2400|2800|(19|20-9))-02-29)|(((19|2[0-9])[0-9]{2})-02-(0[1-9]|1[0-9]|2[0-8]))|(((19|2[0-9])[0-9]{2})-(0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01]))|(((19|2[0-9])[0-9]{2})-(0[469]|11)-(0[1-9]|[12][0-9]|30))T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})

It becomes a bit clearer when you run it through a regex visualizer (using debuggex.com here):

Current regex (notice how T only matches for the last possible year-month-day pattern):
image

Corrected regex: (T matches after all possible variations of year-month-day):
image

Who is the bug affecting?

Anyone trying to use the regex pattern defined in the OSCAL (JSON) data types to determine if a date/date-time input is considered a valid date in the OSCAL format. (For instance I tripped over this bug because one of my entered dates was not accepted by an autogenerated REST API stub.)

What is affected by this bug?

See description above, only some valid dates will match the regex currently.

When does this occur?

The regex is present in the documentation, but it is also present in example content and generated OpenAPI specification. It is repeated 36 times in 9 files in the OSCAL repo (mostly XSD schemas)

How do we replicate the issue?

See above.

Expected behavior (i.e. solution)

See suggested change to the regex above.

Other Comments

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions