Skip to content

Treat blank repeat fields as unset in time-based policies - #53517

Merged
pedroigor merged 1 commit into
keycloak:mainfrom
Ayush0612005:fix-53456-time-policy-blank-fields
Oct 6, 2026
Merged

pedroigor merged 1 commit into
keycloak:mainfrom
Ayush0612005:fix-53456-time-policy-blank-fields

Conversation

@Ayush0612005

Copy link
Copy Markdown

Closes #53456

A time-based policy saved from the Admin Console with only "Not before" / "Not on or after" set is persisted with empty strings for the unused repeat fields (month, monthEnd, dayMonth, hour, ...). TimePolicyProvider.isInvalid() only checked those values for null, so Integer.parseInt("") threw a NumberFormatException and every permission referencing the policy failed with HTTP 500.

Changes

  • TimePolicyProvider.isInvalid() now treats a null, empty or whitespace-only start value (trim().isEmpty(), as suggested by @muscla87) as "not configured", and a blank end value as "no range", so the start must match exactly. This matches the existing nbf/noa handling and covers policies created via the Admin Console, the Admin REST API and realm imports in one place.
  • Values are trimmed before parsing.

Tests

Added PolicyEvaluationTest#testCheckDateAndTimeWithBlankRepeatFields, next to the existing testCheckDateAndTime, with a fixed context time (kc.time.date_time):

  • all repeat fields blank, with nbf/noa range -> PERMIT (previously NumberFormatException)
  • month set, monthEnd blank -> exact match -> PERMIT
  • month set, monthEnd whitespace-only, no match -> DENY
  • hour range with the other fields blank -> PERMIT / DENY as expected

I also verified the change against the released 26.8.0 artifacts with a small standalone harness: the exact config from the issue throws NumberFormatException: For input string: "" before the fix and evaluates to PERMIT after it, and policies without blank fields behave the same before and after. I could not run the full Arquillian testsuite locally, so I'm relying on CI for PolicyEvaluationTest.

AI disclosure

This change (fix, tests and description) was prepared with the help of an AI coding agent (Claude Code). I have reviewed it, understand it, and will address review feedback myself.

cc @muscla87

The admin console persists empty strings for unused repeat fields of a
time policy (month, dayMonth, hour, ...). TimePolicyProvider only checked
these values for null before parsing them, so evaluating any permission
that referenced such a policy failed with a NumberFormatException.

Blank or whitespace-only start values are now ignored, and a blank end
value falls back to an exact match on the start value, consistent with
the existing nbf/noa handling.

Closes keycloak#53456

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Ayush0612005 <ayushkulshreshtha37@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 5, 2026 04:03
@Ayush0612005
Ayush0612005 requested review from a team as code owners October 5, 2026 04:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused fix addresses the reported failure and includes representative regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes time-policy evaluation when unused repeat fields contain blank values.

Changes:

  • Treats null, empty, and whitespace-only repeat fields as unset.
  • Trims configured repeat values before parsing.
  • Adds evaluation coverage for blank fields, exact matches, and ranges.
File Description
TimePolicyProvider.java Safely handles blank repeat fields.
PolicyEvaluationTest.java Tests blank-field and range behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@pedroigor

Copy link
Copy Markdown
Contributor

Thanks, @Ayush0612005.

@pedroigor
pedroigor merged commit 48e9b6e into keycloak:main Oct 6, 2026
158 of 165 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Time policy saved from the Admin UI stores empty strings for unused repeat fields; evaluation fails with NumberFormatException (HTTP 500)

3 participants