From 830b59af765a3a2c9b80ac54bdc9ca0613028561 Mon Sep 17 00:00:00 2001 From: Antonio Casagrande Date: Tue, 1 Sep 2026 12:37:52 -0300 Subject: [PATCH 1/2] Reject attribute-condition configuration that resolves to no entry or several parseKeyValuePair loads the configuration through Properties, which is the format the documentation specifies, but two shapes get past it and both make a condition match a resource set that was never configured: - No entry at all, when the input is empty or its key is one Properties reads as a '#' or '!' comment. stringPropertyNames().iterator().next() then raises a bare NoSuchElementException rather than the WorkflowInvalidStateException the method already throws a few lines earlier. - More than one entry, from multi-line input. Properties is a Hashtable, so the iterator picks an arbitrary entry and a stray newline silently selects a different attribute than the intended one. Require exactly one entry. No valid configuration changes meaning: both documented separators, presence-only keys, comma-separated values and escaped separators all parse as before, and only input that previously crashed or resolved arbitrarily is now rejected. Fixed in the shared helper rather than in one condition, so has-user-attribute and has-client-attribute keep identical configuration semantics. Signed-off-by: Antonio Casagrande --- ...serAttributeWorkflowConditionProvider.java | 11 +++ ...ttributeWorkflowConditionProviderTest.java | 94 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 model/jpa/src/test/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProviderTest.java diff --git a/model/jpa/src/main/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProvider.java b/model/jpa/src/main/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProvider.java index aae748f0648..ae58ac391c5 100644 --- a/model/jpa/src/main/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProvider.java @@ -152,6 +152,7 @@ public void close() { * * @param keyValuePair the key-value pair string to parse * @return a {@link String} array where the first element is the key and the second element is the value. + * @throws WorkflowInvalidStateException if the string does not parse to exactly one pair. */ public static String[] parseKeyValuePair(String keyValuePair) { Properties props = new Properties(); @@ -160,6 +161,16 @@ public static String[] parseKeyValuePair(String keyValuePair) { } catch (java.io.IOException e) { throw new WorkflowInvalidStateException("workflowConditionAttributeInvalid", keyValuePair); } + + // Input parsing to no entry, because it is empty or its key reads as a '#' or '!' + // comment, would reach the iterator below and raise NoSuchElementException instead of + // the exception above. Input parsing to several, from multiple lines, would resolve to + // an arbitrary one because Properties is a Hashtable. Both leave the condition + // matching a resource set that was never configured. + if (props.size() != 1) { + throw new WorkflowInvalidStateException("workflowConditionAttributeInvalid", keyValuePair); + } + String key = props.stringPropertyNames().iterator().next(); String value = props.getProperty(key); return new String[]{key, value}; diff --git a/model/jpa/src/test/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProviderTest.java b/model/jpa/src/test/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProviderTest.java new file mode 100644 index 00000000000..39484077834 --- /dev/null +++ b/model/jpa/src/test/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProviderTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2026 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.keycloak.models.workflow.conditions; + +import org.keycloak.models.workflow.WorkflowInvalidStateException; + +import org.junit.Assert; +import org.junit.Test; + +import static org.keycloak.models.workflow.conditions.UserAttributeWorkflowConditionProvider.parseKeyValuePair; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +/** + * Configuration parsing shared by the attribute conditions. Pins the properties format the + * documentation specifies, and pins that input resolving to no entry or to several is + * rejected rather than silently matching some attribute. + */ +public class UserAttributeWorkflowConditionProviderTest { + + private static void assertParsesTo(String configuration, String expectedKey, String expectedValue) { + String[] parsed = parseKeyValuePair(configuration); + + assertThat(parsed[0], is(expectedKey)); + assertThat(parsed[1], is(expectedValue)); + } + + private static String rejectionKeyOf(String configuration) { + return Assert.assertThrows(WorkflowInvalidStateException.class, + () -> parseKeyValuePair(configuration)).getMessage(); + } + + @Test + public void parsesBothDocumentedSeparators() { + // '=' is the separator used throughout the docs, ':' the one named in the admin messages + assertParsesTo("plan=gold", "plan", "gold"); + assertParsesTo("plan:gold", "plan", "gold"); + } + + @Test + public void parsesAPresenceOnlyKey() { + assertParsesTo("plan", "plan", ""); + assertParsesTo("plan:", "plan", ""); + } + + @Test + public void keepsMultipleValuesAsOneString() { + // Splitting on ',' belongs to the caller, not to the parser + assertParsesTo("plan:gold,silver", "plan", "gold,silver"); + } + + @Test + public void unescapesAnEscapedSeparatorInTheKey() { + // The escaped-separator handling this method delegates to Properties for + assertParsesTo("with\\:colon:gold", "with:colon", "gold"); + } + + @Test + public void rejectsInputWithNoEntry() { + // Previously fell through to NoSuchElementException from the iterator + assertThat(rejectionKeyOf(""), is("workflowConditionAttributeInvalid")); + assertThat(rejectionKeyOf(" "), is("workflowConditionAttributeInvalid")); + assertThat(rejectionKeyOf("#plan=gold"), is("workflowConditionAttributeInvalid")); + assertThat(rejectionKeyOf("!plan=gold"), is("workflowConditionAttributeInvalid")); + } + + @Test + public void rejectsInputWithMoreThanOneEntry() { + // Previously resolved to an arbitrary entry, Properties being a Hashtable + assertThat(rejectionKeyOf("plan=gold\ntier=silver"), is("workflowConditionAttributeInvalid")); + assertThat(rejectionKeyOf("plan=gold\r\ntier=silver"), is("workflowConditionAttributeInvalid")); + } + + @Test + public void toleratesATrailingNewline() { + assertParsesTo("plan=gold\n", "plan", "gold"); + } +} From 17859a91804da64344f9ba03241a80942411f60f Mon Sep 17 00:00:00 2001 From: Antonio Casagrande Date: Wed, 14 Oct 2026 15:02:29 -0300 Subject: [PATCH 2/2] Reject attribute-condition configuration that repeats a key Properties keeps one entry per key, so "plan=gold\nplan=silver" left size() at one and the guard accepted it, resolving silently to the last value. Count entries as they load instead, and pin the repeated-key case in the test. Signed-off-by: Antonio Casagrande --- ...serAttributeWorkflowConditionProvider.java | 20 +++++++++++++++---- ...ttributeWorkflowConditionProviderTest.java | 6 ++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProvider.java b/model/jpa/src/main/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProvider.java index ae58ac391c5..c79dfe7511e 100644 --- a/model/jpa/src/main/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProvider.java @@ -155,7 +155,7 @@ public void close() { * @throws WorkflowInvalidStateException if the string does not parse to exactly one pair. */ public static String[] parseKeyValuePair(String keyValuePair) { - Properties props = new Properties(); + EntryCountingProperties props = new EntryCountingProperties(); try { props.load(new StringReader(keyValuePair)); } catch (java.io.IOException e) { @@ -165,9 +165,10 @@ public static String[] parseKeyValuePair(String keyValuePair) { // Input parsing to no entry, because it is empty or its key reads as a '#' or '!' // comment, would reach the iterator below and raise NoSuchElementException instead of // the exception above. Input parsing to several, from multiple lines, would resolve to - // an arbitrary one because Properties is a Hashtable. Both leave the condition - // matching a resource set that was never configured. - if (props.size() != 1) { + // an arbitrary one because Properties is a Hashtable, or to the last value when a key + // repeats. Both leave the condition matching a resource set that was never configured. + // Entries are counted as they load because size() counts a repeated key once. + if (props.entries != 1) { throw new WorkflowInvalidStateException("workflowConditionAttributeInvalid", keyValuePair); } @@ -175,4 +176,15 @@ public static String[] parseKeyValuePair(String keyValuePair) { String value = props.getProperty(key); return new String[]{key, value}; } + + private static final class EntryCountingProperties extends Properties { + + private int entries; + + @Override + public synchronized Object put(Object key, Object value) { + entries++; + return super.put(key, value); + } + } } diff --git a/model/jpa/src/test/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProviderTest.java b/model/jpa/src/test/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProviderTest.java index 39484077834..68d16921d94 100644 --- a/model/jpa/src/test/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProviderTest.java +++ b/model/jpa/src/test/java/org/keycloak/models/workflow/conditions/UserAttributeWorkflowConditionProviderTest.java @@ -87,6 +87,12 @@ public void rejectsInputWithMoreThanOneEntry() { assertThat(rejectionKeyOf("plan=gold\r\ntier=silver"), is("workflowConditionAttributeInvalid")); } + @Test + public void rejectsTheSameKeyGivenTwice() { + // Previously resolved to the last value, Properties keeping one entry per key + assertThat(rejectionKeyOf("plan=gold\nplan=silver"), is("workflowConditionAttributeInvalid")); + } + @Test public void toleratesATrailingNewline() { assertParsesTo("plan=gold\n", "plan", "gold");