Skip to content

Commit cd6d7e3

Browse files
authored
[ISSUE #7359] Add xss filter (#7364)
- Set response header 'Content-Security-Policy'
1 parent 13032a2 commit cd6d7e3

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

console/src/main/java/com/alibaba/nacos/console/config/ConsoleConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.alibaba.nacos.console.config;
1818

19+
import com.alibaba.nacos.console.filter.XssFilter;
1920
import com.alibaba.nacos.core.code.ControllerMethodsCache;
2021
import org.springframework.beans.factory.annotation.Autowired;
2122
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
@@ -69,6 +70,11 @@ public CorsFilter corsFilter() {
6970
return new CorsFilter(source);
7071
}
7172

73+
@Bean
74+
public XssFilter xssFilter() {
75+
return new XssFilter();
76+
}
77+
7278
@Bean
7379
public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
7480
return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(ZoneId.systemDefault().toString());
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.console.filter;
18+
19+
import org.springframework.web.filter.OncePerRequestFilter;
20+
21+
import javax.servlet.FilterChain;
22+
import javax.servlet.ServletException;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
import java.io.IOException;
26+
27+
/**
28+
* XSS filter.
29+
* @author onewe
30+
*/
31+
public class XssFilter extends OncePerRequestFilter {
32+
33+
private static final String CONTENT_SECURITY_POLICY_HEADER = "Content-Security-Policy";
34+
35+
private static final String CONTENT_SECURITY_POLICY = "script-src 'self'";
36+
37+
@Override
38+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
39+
throws ServletException, IOException {
40+
41+
response.setHeader(CONTENT_SECURITY_POLICY_HEADER, CONTENT_SECURITY_POLICY);
42+
filterChain.doFilter(request, response);
43+
}
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.console.filter;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.mockito.Mock;
22+
import org.mockito.Mockito;
23+
import org.mockito.junit.MockitoJUnitRunner;
24+
25+
import javax.servlet.FilterChain;
26+
import javax.servlet.ServletException;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
import java.io.IOException;
30+
31+
@RunWith(MockitoJUnitRunner.class)
32+
public class XssFilterTest {
33+
34+
private static final String CONTENT_SECURITY_POLICY_HEADER = "Content-Security-Policy";
35+
36+
private static final String CONTENT_SECURITY_POLICY = "script-src 'self'";
37+
38+
@Mock
39+
private HttpServletRequest request;
40+
41+
@Mock
42+
private HttpServletResponse response;
43+
44+
@Mock
45+
private FilterChain filterChain;
46+
47+
@Test
48+
public void testSetResponseHeader() throws ServletException, IOException {
49+
XssFilter xssFilter = new XssFilter();
50+
xssFilter.doFilterInternal(request, response, filterChain);
51+
Mockito.verify(response).setHeader(CONTENT_SECURITY_POLICY_HEADER, CONTENT_SECURITY_POLICY);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)