From 2f45e1421a62e7d455c8a302af44179794ed0a83 Mon Sep 17 00:00:00 2001 From: John Jiang Date: Tue, 15 Oct 2024 10:59:33 +0800 Subject: [PATCH] TKSS-883: Backport JDK-8319332: Security properties files inclusion --- .../sun/security/util/PropertyExpander.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/kona-pkix/src/main/java/com/tencent/kona/sun/security/util/PropertyExpander.java b/kona-pkix/src/main/java/com/tencent/kona/sun/security/util/PropertyExpander.java index 44fad579..6d720e4a 100644 --- a/kona-pkix/src/main/java/com/tencent/kona/sun/security/util/PropertyExpander.java +++ b/kona-pkix/src/main/java/com/tencent/kona/sun/security/util/PropertyExpander.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,6 +30,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.security.GeneralSecurityException; +import java.util.function.UnaryOperator; /** * A utility class to expand properties embedded in a string. @@ -52,15 +53,31 @@ public ExpandException(String msg) { } } - public static String expand(String value) - throws ExpandException - { + public static String expand(String value) throws ExpandException { return expand(value, false); } - public static String expand(String value, boolean encodeURL) - throws ExpandException - { + public static String expand(String value, boolean encodeURL) + throws ExpandException { + return expand(value, encodeURL, System::getProperty); + } + + /* + * In non-strict mode an undefined property is replaced by an empty string. + */ + public static String expandNonStrict(String value) { + try { + return expand(value, false, key -> System.getProperty(key, "")); + } catch (ExpandException e) { + // should not happen + throw new AssertionError("unexpected expansion error: when " + + "expansion is non-strict, undefined properties should " + + "be replaced by an empty string", e); + } + } + + private static String expand(String value, boolean encodeURL, + UnaryOperator propertiesGetter) throws ExpandException { if (value == null) return null; @@ -106,7 +123,7 @@ public static String expand(String value, boolean encodeURL) if (prop.equals("/")) { sb.append(java.io.File.separatorChar); } else { - String val = System.getProperty(prop); + String val = propertiesGetter.apply(prop); if (val != null) { if (encodeURL) { // encode 'val' unless it's an absolute URI