Skip to content

validation: anchor duration regex to reject invalid tokens#619

Closed
bernot-dev wants to merge 1 commit into
kubernetes:masterfrom
bernot-dev:duration-format-fix
Closed

validation: anchor duration regex to reject invalid tokens#619
bernot-dev wants to merge 1 commit into
kubernetes:masterfrom
bernot-dev:duration-format-fix

Conversation

@bernot-dev

Copy link
Copy Markdown

I anchored the duration validation regex to ensure the entire string is validated.

Previously, the durationMatcher regex was unanchored. This allowed ParseDuration to match valid duration substrings embedded within invalid strings, ignoring the invalid text. For example, "1s invalid" would successfully validate and return a duration of 1s.

To fix this, I introduced a two-step validation:

  1. Check the candidate string against durationValidator, an anchored regex that ensures the input consists only of valid duration components.
  2. Sum the components using durationMatcher.

This allows composite durations like "1w2s" or "1w 2d 3h 4m" to be parsed while rejecting inputs with invalid tokens. I added positive and negative test cases to cover expected behavior.

I anchored the duration validation regex to ensure the entire string is validated.

Previously, the durationMatcher regex was unanchored. This allowed ParseDuration to match valid duration substrings embedded within invalid strings, ignoring the invalid text. For example, "1s invalid" would successfully validate and return a duration of 1s.

To fix this, I introduced a two-step validation:

1. Check the candidate string against durationValidator, an anchored regex that ensures the input consists only of valid duration components.
2. Sum the components using durationMatcher.

This allows composite durations like "1w2s" or "1w 2d 3h 4m" to be parsed while rejecting inputs with invalid tokens. I added positive and negative test cases to cover expected behavior.
@kubernetes-prow kubernetes-prow Bot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 26, 2026
@kubernetes-prow kubernetes-prow Bot requested a review from apelisse June 26, 2026 18:42
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: bernot-dev
Once this PR has been reviewed and has the lgtm label, please assign sttts for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @bernot-dev!

It looks like this is your first PR to kubernetes/kube-openapi 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kube-openapi has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot requested a review from sttts June 26, 2026 18:42
@kubernetes-prow kubernetes-prow Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 26, 2026
@bernot-dev

Copy link
Copy Markdown
Author

Closing this in favor of #621, which is a much more direct backport of the upstream go-openapi library.

@bernot-dev bernot-dev closed this Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant