Skip to content

Commit

Permalink
fix: prevent NPE when creating init parameters (#19856) (#19857)
Browse files Browse the repository at this point in the history
ServletConfig might contain init parameter with null values. This is however not
supported by java Properties class.
This change adds a null check and logs offending keys for debugging purpose.

Fixes #19855

Co-authored-by: Marco Collovati <marco@vaadin.com>
  • Loading branch information
vaadin-bot and mcollovati authored Sep 2, 2024
1 parent ef770fb commit 1f35b92
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Map;
import java.util.Properties;

import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.startup.AbstractConfigurationFactory;
Expand Down Expand Up @@ -94,8 +96,13 @@ protected Properties createInitParameters(Class<?> systemPropertyBaseClass,
for (final Enumeration<String> e = vaadinConfig
.getConfigParameterNames(); e.hasMoreElements();) {
final String name = e.nextElement();
initParameters.setProperty(name,
vaadinConfig.getConfigParameter(name));
String value = vaadinConfig.getConfigParameter(name);
if (value != null) {
initParameters.setProperty(name, value);
} else {
LoggerFactory.getLogger(DeploymentConfigurationFactory.class)
.debug("Ignoring NULL init parameter {}", name);
}
}

readBuildInfo(initParameters, vaadinConfig.getVaadinContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,29 @@ public void servletConfigParametersOverrideServletContextParameters()
config.getHeartbeatInterval());
}

@Test
public void servletConfigParameters_nullValues_ignored() throws Exception {
Class<NoSettings> servlet = NoSettings.class;

Map<String, String> servletConfigParams = new HashMap<>(
defaultServletParams);
servletConfigParams.put("someKey", null);
servletConfigParams.put("someNotNullKey", "NOT_NULL");

Map<String, String> servletContextParams = new HashMap<>();

DeploymentConfiguration config = new DeploymentConfigurationFactory()
.createDeploymentConfiguration(servlet, createVaadinConfigMock(
servletConfigParams, servletContextParams));

Assert.assertFalse(
"Expecting null parameter to be ignored, but was in configuration",
config.getInitParameters().containsKey("someKey"));
Assert.assertTrue(
"Expecting not null parameter to be in configuration, but was not",
config.getInitParameters().containsKey("someNotNullKey"));
}

@Test
public void should_readConfigurationFromTokenFile() throws Exception {
FileUtils.writeLines(tokenFile,
Expand Down

0 comments on commit 1f35b92

Please sign in to comment.