jamesfredley commented on code in PR #15967:
URL: https://github.com/apache/grails-core/pull/15967#discussion_r3591420165


##########
grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersFilter.java:
##########
@@ -0,0 +1,60 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.controllers;
+
+import java.io.IOException;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+
+import org.springframework.util.StringUtils;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+public class GrailsSecurityHeadersFilter extends OncePerRequestFilter {
+
+    private final GrailsSecurityHeadersProperties properties;
+
+    public GrailsSecurityHeadersFilter(GrailsSecurityHeadersProperties 
properties) {
+        this.properties = properties;
+    }
+
+    @Override
+    protected void doFilterInternal(HttpServletRequest request, 
HttpServletResponse response, FilterChain filterChain)
+            throws ServletException, IOException {
+        applyHeader(response, "X-Content-Type-Options", 
properties.getContentTypeOptions());
+        applyHeader(response, "X-Frame-Options", properties.getFrameOptions());
+        applyHeader(response, "Referrer-Policy", 
properties.getReferrerPolicy());
+        applyHeader(response, "X-XSS-Protection", 
properties.getXssProtection());
+        if (request.isSecure()) {
+            applyHeader(response, "Strict-Transport-Security", 
properties.getHsts());
+        }
+        applyHeader(response, "Content-Security-Policy", 
properties.getContentSecurityPolicy());
+        filterChain.doFilter(request, response);

Review Comment:
   Thanks - I evaluated moving header application into a `finally` block after 
the chain, but for **security** headers that regresses a critical case: on 
responses that commit *during* the chain (`sendRedirect`, `sendError`, 
`flushBuffer`, streaming), `response.isCommitted()` is already true when the 
chain returns, so a post-chain block would skip every header - redirects and 
error pages would ship with no hardening headers.
   
   Instead the filter keeps applying the defaults **eagerly before** the chain, 
but `applyHeader` only sets a header when it is not already present 
(`containsHeader`), so a value a downstream component set earlier still wins 
and an explicit downstream `setHeader` still replaces it. Added a regression 
test that a redirect-committed response still carries the headers, plus a 
`DispatcherType.ERROR` test after overriding `shouldNotFilterErrorDispatch()` 
so ERROR redispatches are covered too. See the commit comment for the follow-up 
note on Spring Security ordering.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to