Skip to content
Open
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
37 changes: 35 additions & 2 deletions server/src/main/java/com/cloud/api/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.servlet.ServletConfig;
Expand Down Expand Up @@ -78,7 +79,19 @@ public class ApiServlet extends HttpServlet {
protected static Logger LOGGER = LogManager.getLogger(ApiServlet.class);
private static final Logger ACCESSLOGGER = LogManager.getLogger("apiserver." + ApiServlet.class.getName());
private static final String REPLACEMENT = "_";
private static final String LOGGER_REPLACEMENTS = "[\n\r\t]";
private static final String REDACTED = "REDACTED";
private static final String LOGGER_REPLACEMENTS = "[\n\r\t]";

private static final Set<String> SENSITIVE_PARAMETER_KEYWORDS = Set.of(
"password",
"privatekey",
"accesskey",
"secretkey",
"apikey",
"signature",
"sessionkey",
"token"
);

@Inject
ApiServerService apiServer;
Expand Down Expand Up @@ -161,12 +174,32 @@ private void checkSingleQueryParameterValue(Map<String, String[]> params) {
params.forEach((k, v) -> {
if (v.length > 1) {
String message = String.format("Query parameter '%s' has multiple values %s. Only the last value will be respected." +
"It is advised to pass only a single parameter", k, Arrays.toString(v));
"It is advised to pass only a single parameter", k, formatValuesForLog(k, v));
LOGGER.warn(message);
}
});
}

static boolean isSensitiveParameter(String parameterName) {
if (parameterName == null) {
return false;
}

String lowerCaseParameter = parameterName.toLowerCase();
return SENSITIVE_PARAMETER_KEYWORDS.stream()
.anyMatch(lowerCaseParameter::contains);
}

static String formatValuesForLog(String parameterName, String[] values) {
if (!isSensitiveParameter(parameterName)) {
return Arrays.toString(values);
}

String[] masked = new String[values.length];
Arrays.fill(masked, REDACTED);
return Arrays.toString(masked);
}

void processRequestInContext(final HttpServletRequest req, final HttpServletResponse resp) {
InetAddress remoteAddress = null;
try {
Expand Down
25 changes: 25 additions & 0 deletions server/src/test/java/com/cloud/api/ApiServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,29 @@ public void testVerify2FAWhenExpectedCommandIsNotCalled() throws UnknownHostExce

Assert.assertEquals(false, result);
}
@Test
public void testFormatValuesForLogMasksSensitiveParameters() {
String result = ApiServlet.formatValuesForLog(
"password",
new String[]{"SECRET_ONE", "SECRET_TWO"});

Assert.assertEquals("[REDACTED, REDACTED]", result);
}

@Test
public void testFormatValuesForLogDoesNotMaskNormalParameters() {
String result = ApiServlet.formatValuesForLog(
"username",
new String[]{"alice", "bob"});

Assert.assertEquals("[alice, bob]", result);
}

@Test
public void testIsSensitiveParameter() {
Assert.assertTrue(ApiServlet.isSensitiveParameter("adminpassword"));
Assert.assertTrue(ApiServlet.isSensitiveParameter("usersecretkey"));
Assert.assertTrue(ApiServlet.isSensitiveParameter("sessiontoken"));
Assert.assertFalse(ApiServlet.isSensitiveParameter("username"));
}
}