Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,4 @@ http://localhost:8080/PortalExample/do/Entity/search.action?entityManagerName=ja

</wpsa:subset>

</s:form>
</s:form>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%@ taglib prefix="wp" uri="/aps-core" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<wp:headInfo type="JS_EXT" info="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" />
<wp:headInfo type="JS_EXT" info="https://code.jquery.com/ui/1.10.3/jquery-ui.min.js" />

<jacms:contentList listName="contentList" titleVar="titleVar"
pageLinkVar="pageLinkVar" pageLinkDescriptionVar="pageLinkDescriptionVar" userFilterOptionsVar="userFilterOptionsVar" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jQuery(function($){
});
</c:set>
<wp:headInfo type="JS" info="entando-misc-html5-essentials/modernizr-2.5.3-full.js" />
<wp:headInfo type="JS_EXT" info="http://code.jquery.com/ui/1.10.0/jquery-ui.min.js" />
<wp:headInfo type="CSS_EXT" info="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.min.css" />
<wp:headInfo type="JS_EXT" info="https://code.jquery.com/ui/1.10.0/jquery-ui.min.js" />
<wp:headInfo type="CSS_EXT" info="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.min.css" />
<wp:headInfo type="JS_RAW" info="${js_for_datepicker}" />

<fieldset>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@
public static final String PAR_FILEUPLOAD_MAXSIZE = "fileUploadMaxSize";

public static final String PAR_CSP_ENABLED = "cspEnabled";

public static final String PAR_CSP_HEADER_EXTRA_CONFIG = "cspHeaderExtraConfig";

public static final String PAR_CSP_BACKOFFICE_CONFIG = "cspBackofficeConfig";

public static final String PAR_CSP_BACKOFFICE_REPORT_ONLY = "cspBackofficeReportOnly";

/**
* Nome parametro extra per requestContext: lingua corrente
*/
Expand Down Expand Up @@ -317,7 +321,15 @@
public static final String ENTITY_EVENT_CHANNEL = "core-entity";

public static final String CSP_HEADER_ENABLED = "CSP_HEADER_ENABLED";
public static final String CSP_HEADER_PORTAL_EXTRACONFIG = "CSP_HEADER_PORTAL_EXTRACONFIG";
/**
* @deprecated use {@link #CSP_HEADER_PORTAL_EXTRACONFIG}, which makes it explicit that the
* variable configures the portal (front-end) policy; kept for backward compatibility.
*/
@Deprecated

Check warning on line 329 in engine/src/main/java/com/agiletec/aps/system/SystemConstants.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add 'since' and/or 'forRemoval' arguments to the @Deprecated annotation.

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3G73bFWqtoRklLK&open=AZ9wS3G73bFWqtoRklLK&pullRequest=349
public static final String CSP_HEADER_EXTRACONFIG = "CSP_HEADER_EXTRACONFIG";

Check warning on line 330 in engine/src/main/java/com/agiletec/aps/system/SystemConstants.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not forget to remove this deprecated code someday.

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3G73bFWqtoRklLL&open=AZ9wS3G73bFWqtoRklLL&pullRequest=349
public static final String CSP_HEADER_BACKOFFICE_CONFIG = "CSP_HEADER_BACKOFFICE_CONFIG";
public static final String CSP_HEADER_BACKOFFICE_REPORT_ONLY = "CSP_HEADER_BACKOFFICE_REPORT_ONLY";
public static final String ENTANDO_CSRF_PROTECTION = "ENTANDO_CSRF_PROTECTION";
public static final String ENTANDO_CSRF_ALLOWED_DOMAINS = "ENTANDO_CSRF_ALLOWED_DOMAINS";
public static final String SEPARATOR_DOMAINS = ",";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
private static final EntLogger LOGGER = EntLogFactory.getSanitizedLogger(StartupListener.class);

@Override
public void contextInitialized(ServletContextEvent event) {

Check failure on line 48 in engine/src/main/java/org/entando/entando/aps/servlet/StartupListener.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3Co3bFWqtoRklLJ&open=AZ9wS3Co3bFWqtoRklLJ&pullRequest=349
long startMs = System.currentTimeMillis();
ServletContext svCtx = event.getServletContext();
String msg = this.getClass().getName() + ": INIT " + svCtx.getServletContextName();
Expand All @@ -72,9 +72,16 @@
String cspEnabled = System.getenv(SystemConstants.CSP_HEADER_ENABLED);
if (StringUtils.isEmpty(cspEnabled) || Boolean.TRUE.toString().equalsIgnoreCase(cspEnabled)) {
LOGGER.info("Content Security Policy (CSP) header is enabled");
String cspExtraConfig = System.getenv(SystemConstants.CSP_HEADER_EXTRACONFIG);
String cspExtraConfig = System.getenv(SystemConstants.CSP_HEADER_PORTAL_EXTRACONFIG);
if (StringUtils.isEmpty(cspExtraConfig)) {
cspExtraConfig = System.getenv(SystemConstants.CSP_HEADER_EXTRACONFIG);
if (!StringUtils.isEmpty(cspExtraConfig)) {
LOGGER.warn("The CSP_HEADER_EXTRACONFIG environment variable is deprecated:"
+ " use CSP_HEADER_PORTAL_EXTRACONFIG instead");
}
}
if (!StringUtils.isEmpty(cspExtraConfig)) {
LOGGER.info("Content Security Policy (CSP) extra-config set to: " + cspExtraConfig);
LOGGER.info("Content Security Policy (CSP) portal extra-config set to: " + cspExtraConfig);

Check warning on line 84 in engine/src/main/java/org/entando/entando/aps/servlet/StartupListener.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3Co3bFWqtoRklLI&open=AZ9wS3Co3bFWqtoRklLI&pullRequest=349
}
} else {
LOGGER.warn("Content Security Policy (CSP) header is not enabled");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@
<entry key="cspEnabled">
<value>${csp.header.enabled:true}</value>
</entry>
<entry key="cspBackofficeConfig">
<value>${csp.header.backoffice.config:default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'; connect-src 'self'}</value>
</entry>
<entry key="cspBackofficeReportOnly">
<value>${csp.header.backoffice.reportOnly:false}</value>
</entry>
</map>
</property>
<property name="configDAO" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<entry key="cspEnabled">
<value>${csp.header.enabled:true}</value>
</entry>
<entry key="cspBackofficeConfig">
<value>${csp.header.backoffice.config:default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'; connect-src 'self'}</value>
</entry>
<entry key="cspBackofficeReportOnly">
<value>${csp.header.backoffice.reportOnly:false}</value>
</entry>
</map>
</property>
<property name="configDAO" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.security.SecureRandom;
import java.util.List;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -108,15 +107,7 @@ protected RequestContext initRequestContext(HttpServletRequest request,
}

public String createSecureRandomString() {
int leftLimit = 48;
int rightLimit = 122;
int targetStringLength = 64;
SecureRandom rand = new SecureRandom();
return rand.ints(leftLimit, rightLimit + 1)
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
.limit(targetStringLength)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
return CspNonceGenerator.createNonce();
}

protected int controlRequest(HttpServletRequest request,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2026-Present Entando Inc. (http://www.entando.com) All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package org.entando.entando.aps.servlet;

import com.agiletec.aps.system.SystemConstants;
import com.agiletec.aps.system.services.baseconfig.ConfigInterface;
import com.agiletec.aps.util.ApsWebApplicationUtils;
import java.io.IOException;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

/**
* Applies the Content-Security-Policy header to back-office requests
* (/do/*, /struts/*, /ExtStr2/do/*), which do not route through the
* {@link ControllerServlet} and would otherwise get no CSP at all.
* <p>
* The policy is read from the "cspBackofficeConfig" system parameter and honors
* the same "cspEnabled" switch used for the portal. An optional "{nonce}"
* placeholder in the policy is replaced with a per-request secure random token,
* also exposed as the "cspNonceToken" request attribute for use in JSPs.
* When the "cspBackofficeReportOnly" parameter is true, the policy is sent as
* Content-Security-Policy-Report-Only, allowing violation monitoring without
* enforcement during rollout.
*/
public class CspHeaderFilter implements Filter {

public static final String CSP_HEADER_NAME = "Content-Security-Policy";
public static final String CSP_REPORT_ONLY_HEADER_NAME = "Content-Security-Policy-Report-Only";
public static final String NONCE_PLACEHOLDER = "{nonce}";
public static final String REQUEST_ATTR_CSP_NONCE = "cspNonceToken";

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {

Check warning on line 52 in portal-ui/src/main/java/org/entando/entando/aps/servlet/CspHeaderFilter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this instanceof check and cast with 'instanceof HttpServletRequest httpservletrequest'

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3KN3bFWqtoRklLM&open=AZ9wS3KN3bFWqtoRklLM&pullRequest=349
this.addCspHeader((HttpServletRequest) request, (HttpServletResponse) response);
}
chain.doFilter(request, response);
}

protected void addCspHeader(HttpServletRequest request, HttpServletResponse response) {
ConfigInterface configManager = this.getConfigManager(request);
if (!Boolean.parseBoolean(configManager.getParam(SystemConstants.PAR_CSP_ENABLED))) {
return;
}
String policy = configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_CONFIG);
if (StringUtils.isBlank(policy)) {
return;
}
if (policy.contains(NONCE_PLACEHOLDER)) {
String nonce = CspNonceGenerator.createNonce();
request.setAttribute(REQUEST_ATTR_CSP_NONCE, nonce);
policy = policy.replace(NONCE_PLACEHOLDER, nonce);
}
boolean reportOnly = Boolean.parseBoolean(configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_REPORT_ONLY));
response.setHeader(reportOnly ? CSP_REPORT_ONLY_HEADER_NAME : CSP_HEADER_NAME, policy);
}

protected ConfigInterface getConfigManager(HttpServletRequest request) {
return (ConfigInterface) ApsWebApplicationUtils.getBean(SystemConstants.BASE_CONFIG_MANAGER, request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2026-Present Entando Inc. (http://www.entando.com) All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package org.entando.entando.aps.servlet;

import java.security.SecureRandom;

/**
* Generates the per-request secure random tokens used as Content-Security-Policy nonces,
* shared between the portal front-controller and the back-office CSP filter.
*/
public final class CspNonceGenerator {

private static final SecureRandom RANDOM = new SecureRandom();

private CspNonceGenerator() {
// utility class
}

public static String createNonce() {
int leftLimit = 48;
int rightLimit = 122;
int targetStringLength = 64;
return RANDOM.ints(leftLimit, rightLimit + 1)
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
.limit(targetStringLength)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@
});
</c:set>
<wp:headInfo type="JS" info="entando-misc-html5-essentials/modernizr-2.5.3-full.js" />
<wp:headInfo type="JS_EXT" info="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js" />
<wp:headInfo type="CSS_EXT" info="http://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.min.css" />
<wp:headInfo type="JS_EXT" info="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" />
<wp:headInfo type="CSS_EXT" info="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.min.css" />
<wp:headInfo type="JS_RAW" info="${js_for_datepicker}" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2026-Present Entando Inc. (http://www.entando.com) All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package org.entando.entando.aps.servlet;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;

import com.agiletec.aps.system.SystemConstants;
import com.agiletec.aps.system.services.baseconfig.ConfigInterface;
import jakarta.servlet.FilterChain;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class CspHeaderFilterTest {

private static final String POLICY = "default-src 'self'; object-src 'none'";

@Mock
private ConfigInterface configManager;

@Mock
private HttpServletRequest request;

@Mock
private HttpServletResponse response;

@Mock
private FilterChain filterChain;

private CspHeaderFilter filter;

@BeforeEach
void setUp() {
this.filter = new CspHeaderFilter() {
@Override
protected ConfigInterface getConfigManager(HttpServletRequest request) {
return configManager;
}
};
}

@Test
void shouldSetEnforcedHeaderWhenCspIsEnabled() throws Exception {
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_ENABLED)).thenReturn("true");

Check warning on line 65 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLN&open=AZ9wS3K_3bFWqtoRklLN&pullRequest=349
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_CONFIG)).thenReturn(POLICY);

Check warning on line 66 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLO&open=AZ9wS3K_3bFWqtoRklLO&pullRequest=349
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_REPORT_ONLY)).thenReturn("false");

Check warning on line 67 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLP&open=AZ9wS3K_3bFWqtoRklLP&pullRequest=349
filter.doFilter(request, response, filterChain);
Mockito.verify(response).setHeader(CspHeaderFilter.CSP_HEADER_NAME, POLICY);

Check warning on line 69 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLQ&open=AZ9wS3K_3bFWqtoRklLQ&pullRequest=349
Mockito.verify(filterChain).doFilter(request, response);

Check warning on line 70 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLR&open=AZ9wS3K_3bFWqtoRklLR&pullRequest=349
}

@Test
void shouldSetReportOnlyHeaderWhenConfigured() throws Exception {
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_ENABLED)).thenReturn("true");

Check warning on line 75 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLS&open=AZ9wS3K_3bFWqtoRklLS&pullRequest=349
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_CONFIG)).thenReturn(POLICY);

Check warning on line 76 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLT&open=AZ9wS3K_3bFWqtoRklLT&pullRequest=349
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_REPORT_ONLY)).thenReturn("true");

Check warning on line 77 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLU&open=AZ9wS3K_3bFWqtoRklLU&pullRequest=349
filter.doFilter(request, response, filterChain);
Mockito.verify(response).setHeader(CspHeaderFilter.CSP_REPORT_ONLY_HEADER_NAME, POLICY);

Check warning on line 79 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLV&open=AZ9wS3K_3bFWqtoRklLV&pullRequest=349
Mockito.verify(response, never()).setHeader(eq(CspHeaderFilter.CSP_HEADER_NAME), anyString());

Check warning on line 80 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLW&open=AZ9wS3K_3bFWqtoRklLW&pullRequest=349
Mockito.verify(filterChain).doFilter(request, response);

Check warning on line 81 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLX&open=AZ9wS3K_3bFWqtoRklLX&pullRequest=349
}

@Test
void shouldNotSetHeaderWhenCspIsDisabled() throws Exception {
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_ENABLED)).thenReturn("false");

Check warning on line 86 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLY&open=AZ9wS3K_3bFWqtoRklLY&pullRequest=349
filter.doFilter(request, response, filterChain);
Mockito.verify(response, never()).setHeader(anyString(), anyString());

Check warning on line 88 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLZ&open=AZ9wS3K_3bFWqtoRklLZ&pullRequest=349
Mockito.verify(filterChain).doFilter(request, response);

Check warning on line 89 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLa&open=AZ9wS3K_3bFWqtoRklLa&pullRequest=349
}

@Test
void shouldNotSetHeaderWhenPolicyIsBlank() throws Exception {
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_ENABLED)).thenReturn("true");

Check warning on line 94 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLb&open=AZ9wS3K_3bFWqtoRklLb&pullRequest=349
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_CONFIG)).thenReturn(" ");

Check warning on line 95 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLc&open=AZ9wS3K_3bFWqtoRklLc&pullRequest=349
filter.doFilter(request, response, filterChain);
Mockito.verify(response, never()).setHeader(anyString(), anyString());

Check warning on line 97 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLd&open=AZ9wS3K_3bFWqtoRklLd&pullRequest=349
Mockito.verify(filterChain).doFilter(request, response);

Check warning on line 98 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLe&open=AZ9wS3K_3bFWqtoRklLe&pullRequest=349
}

@Test
void shouldReplaceNoncePlaceholderAndExposeRequestAttribute() throws Exception {
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_ENABLED)).thenReturn("true");

Check warning on line 103 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLf&open=AZ9wS3K_3bFWqtoRklLf&pullRequest=349
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_CONFIG))

Check warning on line 104 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLg&open=AZ9wS3K_3bFWqtoRklLg&pullRequest=349
.thenReturn("script-src 'nonce-{nonce}'; object-src 'none'");
Mockito.when(configManager.getParam(SystemConstants.PAR_CSP_BACKOFFICE_REPORT_ONLY)).thenReturn("false");

Check warning on line 106 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLh&open=AZ9wS3K_3bFWqtoRklLh&pullRequest=349
filter.doFilter(request, response, filterChain);
ArgumentCaptor<String> nonceCaptor = ArgumentCaptor.forClass(String.class);
Mockito.verify(request).setAttribute(eq(CspHeaderFilter.REQUEST_ATTR_CSP_NONCE), nonceCaptor.capture());

Check warning on line 109 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLi&open=AZ9wS3K_3bFWqtoRklLi&pullRequest=349
String nonce = nonceCaptor.getValue();
Assertions.assertEquals(64, nonce.length());
Mockito.verify(response).setHeader(CspHeaderFilter.CSP_HEADER_NAME,

Check warning on line 112 in portal-ui/src/test/java/org/entando/entando/aps/servlet/CspHeaderFilterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ9wS3K_3bFWqtoRklLj&open=AZ9wS3K_3bFWqtoRklLj&pullRequest=349
"script-src 'nonce-" + nonce + "'; object-src 'none'");
}

@Test
void shouldGenerateDifferentNoncesPerRequest() {
String first = CspNonceGenerator.createNonce();
String second = CspNonceGenerator.createNonce();
Assertions.assertNotEquals(first, second);
Assertions.assertTrue(first.matches("[0-9A-Za-z]{64}"));
}

}
Loading
Loading