Skip to content

Commit cfb3dee

Browse files
authored
[fix] fix align cors configuration with header based authentication (#4267)
1 parent 4feff12 commit cfb3dee

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/config/SecurityCorsConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public class SecurityCorsConfiguration {
3535
public FilterRegistrationBean corsFilter() {
3636
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
3737
CorsConfiguration corsConfiguration = new CorsConfiguration();
38-
corsConfiguration.setAllowCredentials(true);
38+
// Requests authenticate with a token in the Authorization header rather than a
39+
// cookie, so no request relies on ambient credentials being sent cross origin.
40+
corsConfiguration.setAllowCredentials(false);
3941
corsConfiguration.setAllowedOriginPatterns(Collections.singletonList(CorsConfiguration.ALL));
4042
corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
4143
corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hertzbeat.manager.config;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
23+
import jakarta.servlet.Filter;
24+
import org.junit.jupiter.api.Test;
25+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
26+
import org.springframework.mock.web.MockHttpServletRequest;
27+
import org.springframework.mock.web.MockHttpServletResponse;
28+
import org.springframework.mock.web.MockFilterChain;
29+
30+
/**
31+
* Test case for {@link SecurityCorsConfiguration}.
32+
*
33+
* <p>The filter answers every origin, which is intentional, and requests authenticate with
34+
* a token in the Authorization header rather than a cookie, so credentials do not need to
35+
* be allowed. Both halves are asserted: the credentials header is not sent, and a preflight
36+
* still succeeds so the api stays reachable cross origin.
37+
*/
38+
class SecurityCorsConfigurationTest {
39+
40+
private static final String OTHER_ORIGIN = "https://other.example";
41+
42+
@Test
43+
void testCredentialsAreNotAllowedForCrossOriginRequests() throws Exception {
44+
MockHttpServletResponse response = handlePreflight();
45+
46+
assertNotEquals("true", response.getHeader("Access-Control-Allow-Credentials"));
47+
}
48+
49+
@Test
50+
void testCrossOriginRequestsAreStillAnswered() throws Exception {
51+
MockHttpServletResponse response = handlePreflight();
52+
53+
assertNotNull(response.getHeader("Access-Control-Allow-Origin"),
54+
"the api is meant to stay reachable cross origin");
55+
assertEquals(200, response.getStatus());
56+
}
57+
58+
private MockHttpServletResponse handlePreflight() throws Exception {
59+
FilterRegistrationBean<?> registration = new SecurityCorsConfiguration().corsFilter();
60+
Filter filter = (Filter) registration.getFilter();
61+
62+
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/api/monitors");
63+
request.addHeader("Origin", OTHER_ORIGIN);
64+
request.addHeader("Access-Control-Request-Method", "GET");
65+
MockHttpServletResponse response = new MockHttpServletResponse();
66+
67+
filter.doFilter(request, response, new MockFilterChain());
68+
return response;
69+
}
70+
}

0 commit comments

Comments
 (0)