Skip to content

Commit

Permalink
fix: prevent NPE when creating init parameters (#19856)
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
  • Loading branch information
mcollovati committed Sep 2, 2024
1 parent 0d27e33 commit d97beba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 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 @@ -2,6 +2,7 @@

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
Expand All @@ -20,12 +21,6 @@
import java.util.Properties;
import java.util.Set;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.di.ResourceProvider;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.frontend.FrontendUtils;
import com.vaadin.flow.server.startup.ApplicationConfiguration;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.AfterClass;
Expand All @@ -38,13 +33,19 @@
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;

import static java.util.Collections.emptyMap;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.di.ResourceProvider;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.frontend.FrontendUtils;
import com.vaadin.flow.server.startup.ApplicationConfiguration;

import static com.vaadin.flow.server.Constants.VAADIN_SERVLET_RESOURCES;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE;
import static com.vaadin.flow.server.frontend.FrontendUtils.PARAM_TOKEN_FILE;
import static com.vaadin.flow.server.frontend.FrontendUtils.TOKEN_FILE;
import static com.vaadin.flow.server.startup.AbstractConfigurationFactory.DEV_FOLDER_MISSING_MESSAGE;
import static java.util.Collections.emptyMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -186,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 d97beba

Please sign in to comment.