Skip to content

Commit 411d94b

Browse files
committed
Add Cache-Control no-transform to prevent Cloudflare from stripping Content-Length on HEAD requests
1 parent c96ed3d commit 411d94b

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/main/java/gov/nist/oar/distrib/web/CacheSecurityConfig.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,24 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
6969
*/
7070
public SecurityFilterChain configureStrictSecurity(HttpSecurity http) throws Exception {
7171
http.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
72-
.exceptionHandling(Customizer.withDefaults())
72+
.exceptionHandling(Customizer.withDefaults())
7373
.addFilterBefore(authenticationFilter(), AnonymousAuthenticationFilter.class)
7474
.authorizeHttpRequests(auth -> {
7575
// auth.requestMatchers("/ds/**").permitAll();
7676
auth.requestMatchers("/**").permitAll();
77-
auth.requestMatchers(SECURED_ENDPOINTS).authenticated();
77+
auth.requestMatchers(SECURED_ENDPOINTS).authenticated();
7878
})
79-
.csrf(csrf -> csrf.disable())
79+
.headers(headers -> headers
80+
.cacheControl(cache -> cache.disable())
81+
.addHeaderWriter((request, response) -> {
82+
response.setHeader("Cache-Control",
83+
"no-store, no-cache, no-transform, must-revalidate, proxy-revalidate, max-age=0");
84+
})
85+
)
86+
.csrf(csrf -> csrf.disable())
8087
.formLogin(form -> form.disable())
81-
.httpBasic(httpBasic -> httpBasic.disable())
82-
.logout(logout -> logout.disable());
88+
.httpBasic(httpBasic -> httpBasic.disable())
89+
.logout(logout -> logout.disable());
8390

8491
return http.build();
8592
}

src/test/java/gov/nist/oar/distrib/web/DatasetAccessControllerTest.java

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

1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1617
import static org.junit.jupiter.api.Assertions.assertNull;
1718
import static org.junit.jupiter.api.Assertions.assertTrue;
1819
import static org.mockito.Mockito.when;
@@ -384,6 +385,11 @@ public void testDownloadFileInfo() {
384385

385386
assertEquals(HttpStatus.OK, resp.getStatusCode());
386387
assertTrue(resp.getHeaders().getFirst("Content-Type").startsWith("application/json"));
388+
assertNotNull(resp.getHeaders().getFirst("Content-Length"));
389+
String cacheControl = resp.getHeaders().getFirst("Cache-Control");
390+
assertNotNull(cacheControl);
391+
assertTrue(cacheControl.contains("no-transform"),
392+
"Cache-Control must contain no-transform to prevent Cloudflare from stripping Content-Length");
387393
assertNull(resp.getBody());
388394
}
389395

0 commit comments

Comments
 (0)