8000 Fix null handling in XMLTimeUtil by nihalritgithub · Pull Request #53839 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2272,9 +2272,8 @@ public void samlSecurityTokenNotFoundInRegistry(String id) {
*@see org.picketlink.identity.federation.PicketLinkLogger#samlMetaDataFailedToCreateCacheDuration(java.lang.String)
*/
@Override
public IllegalArgumentException samlMetaDataFailedToCreateCacheDuration(String timeValue) {
return new IllegalArgumentException("Cache duration could not be created using '" + timeValue
+ "'. This value must be an ISO-8601 period or a numeric value representing the duration in milliseconds.");
public IllegalArgumentException samlMetaDataFailedToCreateCacheDuration() {
return new IllegalArgumentException("Cache duration could not be created. This value must be an ISO-8601 period or a numeric value representing the duration in milliseconds.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,15 @@ public interface PicketLinkLogger {
/** @param id */
void samlSecurityTokenNotFoundInRegistry(String id);

IllegalArgumentException samlMetaDataFailedToCreateCacheDuration(String timeValue);
/**
* Do not use this method as it will log user-provided data in the exception.
*/
@Deprecated(forRemoval = true, since = "27.0")
default IllegalArgumentException samlMetaDataFailedToCreateCacheDuration(String timeValue) {
return samlMetaDataFailedToCreateCacheDuration();
}

IllegalArgumentException samlMetaDataFailedToCreateCacheDuration();
Comment thread
ahus1 marked this conversation as resolved.
Resolved

ConfigurationException samlMetaDataNoIdentityProviderDefined();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static boolean isValid(XMLGregorianCalendar now, XMLGregorianCalendar not
*/
public static Duration parseAsDuration(String timeValue) {
if (timeValue == null) {
PicketLinkLoggerFactory.getLogger().nullArgumentError("duration time");
throw logger.nullArgumentError("duration time");
}

DatatypeFactory factory = DATATYPE_FACTORY.get();
Expand All @@ -193,7 +193,7 @@ public static Duration parseAsDuration(String timeValue) {
return factory.newDuration(Long.parseLong(timeValue));
}
} catch (Exception e) {
throw logger.samlMetaDataFailedToCreateCacheDuration(timeValue);
throw logger.samlMetaDataFailedToCreateCacheDuration();
}
}

Expand All @@ -205,6 +205,10 @@ public static Duration parseAsDuration(String timeValue) {
* @return
*/
public static XMLGregorianCalendar parse(String timeString) {
if (timeString == null) {
throw logger.nullArgumentError("timeString");
}

DatatypeFactory factory = DATATYPE_FACTORY.get();
return factory.newXMLGregorianCalendar(timeString);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.saml.processing.core.saml.v2.util;

import java.util.Date;

import org.keycloak.saml.common.ErrorCodes;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

public class XMLTimeUtilTest {

@Test
public void testParse() {
assertEquals("2009-06-17T18:55:10.738Z", XMLTimeUtil.parse("2009-06-17T18:55:10.738Z").toXMLFormat());
assertEquals("2002-05-30T09:30:10-06:00", XMLTimeUtil.parse("2002-05-30T09:30:10-06:00").toXMLFormat());
}

@Test
public void testParseNull() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> XMLTimeUtil.parse(null));
assertEquals(ErrorCodes.NULL_ARGUMENT + "timeString", e.getMessage());
}

@Test
public void testParseInvalid() {
assertThrows(IllegalArgumentException.class, () -> XMLTimeUtil.parse("not-a-date"));
}

@Test
public void testParseAsDuration() {
Date start = new Date(0);
assertEquals(600000L, XMLTimeUtil.parseAsDuration("PT10M").getTimeInMillis(start));
assertEquals(1000L, XMLTimeUtil.parseAsDuration("1000").getTimeInMillis(start));
}

@Test
public void testParseAsDurationNull() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> XMLTimeUtil.parseAsDuration(null));
assertEquals(ErrorCodes.NULL_ARGUMENT + "duration time", e.getMessage());
}

@Test
public void testParseAsDurationInvalid() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> XMLTimeUtil.parseAsDuration("ten minutes"));
assertTrue(e.getMessage().startsWith("Cache duration could not be created"));
}
}
Loading
0