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


##########
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:
   The filter applies security headers *before* calling 
`filterChain.doFilter(...)`. This means downstream filters/controllers that 
only add headers when they are missing will see these defaults already present 
and may back off, causing Grails defaults to take precedence even when other 
components intended to supply their own values. Applying the defaults after the 
chain runs (in a `finally` block) lets downstream code set headers first while 
still ensuring Grails fills any gaps.



-- 
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