[ https://issues.apache.org/jira/browse/KNOX-3124?focusedWorklogId=966292&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-966292 ]
ASF GitHub Bot logged work on KNOX-3124: ---------------------------------------- Author: ASF GitHub Bot Created on: 15/Apr/25 23:05 Start Date: 15/Apr/25 23:05 Worklog Time Spent: 10m Work Description: lmccay commented on code in PR #1021: URL: https://github.com/apache/knox/pull/1021#discussion_r2045681362 ########## gateway-provider-security-webappsec/src/main/java/org/apache/knox/gateway/webappsec/filter/SecurityHeaderFilter.java: ########## @@ -0,0 +1,123 @@ +/* + * 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 + * + * http://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.apache.knox.gateway.webappsec.filter; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; +import java.io.IOException; +import java.util.Collection; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class SecurityHeaderFilter implements Filter { + + private Map<String, String> map = new HashMap<>(); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + // Dynamically add headers based on init parameters + Enumeration<String> initParamNames = filterConfig.getInitParameterNames(); + while (initParamNames.hasMoreElements()) { + String headerName = initParamNames.nextElement(); + if (!"enabled".equals(headerName)) { Review Comment: agreed ########## gateway-provider-security-webappsec/src/main/java/org/apache/knox/gateway/webappsec/filter/SecurityHeaderFilter.java: ########## @@ -0,0 +1,123 @@ +/* + * 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 + * + * http://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.apache.knox.gateway.webappsec.filter; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; +import java.io.IOException; +import java.util.Collection; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class SecurityHeaderFilter implements Filter { + + private Map<String, String> map = new HashMap<>(); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + // Dynamically add headers based on init parameters + Enumeration<String> initParamNames = filterConfig.getInitParameterNames(); + while (initParamNames.hasMoreElements()) { + String headerName = initParamNames.nextElement(); + if (!"enabled".equals(headerName)) { + String headerValue = filterConfig.getInitParameter(headerName); + map.put(headerName, headerValue); + } + } + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + + HttpServletResponse httpResponse = new SecurityHeaderResponseWrapper((HttpServletResponse) response); + + // Dynamically add headers based on init parameters + for (Map.Entry<String, String> entry : map.entrySet()) { + String headerName = entry.getKey(); + String headerValue = entry.getValue(); + httpResponse.setHeader(headerName, headerValue); + } + + // Continue the filter chain + chain.doFilter(request, httpResponse); + } + + @Override + public void destroy() { + // Cleanup logic if needed + } + + class SecurityHeaderResponseWrapper extends HttpServletResponseWrapper { + + SecurityHeaderResponseWrapper(HttpServletResponse res) { + super(res); + } + + @Override + public void addHeader(String name, String value) { + if (!"enabled".equals(name)) { Review Comment: agreed Issue Time Tracking ------------------- Worklog Id: (was: 966292) Time Spent: 40m (was: 0.5h) > Add Generic Security Header Filter to WebAppSec Provider > -------------------------------------------------------- > > Key: KNOX-3124 > URL: https://issues.apache.org/jira/browse/KNOX-3124 > Project: Apache Knox > Issue Type: Improvement > Components: Server > Reporter: Larry McCay > Assignee: Larry McCay > Priority: Major > Fix For: 2.2.0 > > Time Spent: 40m > Remaining Estimate: 0h > > In order to add various security headers to a response, we can add a generic > filter for which init params with the param name and value indicating the > header name and string representing the directives for the header > respectively. > This will allow admins to configure things like Content-Security-Policy, > Cache-Control, etc. without the need to add separate filters for each one. -- This message was sent by Atlassian Jira (v8.20.10#820010)