Skip to content

Commit

Permalink
TKSS-883: Backport JDK-8319332: Security properties files inclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshajiang committed Oct 15, 2024
1 parent f51e110 commit 2f45e14
Showing 1 changed file with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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<String> propertiesGetter) throws ExpandException {
if (value == null)
return null;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2f45e14

Please sign in to comment.