smolnar82 commented on code in PR #1021:
URL: https://github.com/apache/knox/pull/1021#discussion_r2042168938


##########
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<>();

Review Comment:
   A better class member name would be more helpful.



##########
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:
   You may want to create another constant for `enabled` in 
WebAppSeciContributor and re-use it here.



##########
gateway-provider-security-webappsec/src/main/java/org/apache/knox/gateway/webappsec/deploy/WebAppSecContributor.java:
##########
@@ -55,6 +55,10 @@ public class WebAppSecContributor extends 
ProviderDeploymentContributorBase {
   private static final String RATE_LIMITING_PREFIX = "rate.limiting";
   private static final String RATE_LIMITING_SUFFIX = "_RATE.LIMITING";
   private static final String RATE_LIMITING_ENABLED = RATE_LIMITING_PREFIX + 
".enabled";
+  private static final String SECURITY_HEADER_PREFIX = "security.header";

Review Comment:
   I think the prefix should be `"security.header."` (note the `.` at the end). 



##########
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:
   See my recommendation above of using a constant here.



-- 
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: dev-unsubscr...@knox.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to