Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,12 @@ private void initializeWebServer(String name, String hostName,

addGlobalFilter("safety", QuotingInputFilter.class.getName(), null);
Map<String, String> params = new HashMap<>();
params.put("xframeoptions", conf.get("hbase.http.filter.xframeoptions.mode", "DENY"));
addGlobalFilter("clickjackingprevention",
ClickjackingPreventionFilter.class.getName(), params);
params.put("xframeoptions", conf.get("hbase.http.filter.xframeoptions.mode",
"DENY"));
params.put("hsts", conf.get("hbase.http.filter.hsts.value",
"max-age=31536000"));
addGlobalFilter("securityheaders",
SecurityHeadersFilter.class.getName(), params);
final FilterInitializer[] initializers = getFilterInitializers(conf);
if (initializers != null) {
conf = new Configuration(conf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.hadoop.hbase.http;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
Expand All @@ -28,24 +28,33 @@
import javax.servlet.http.HttpServletResponse;

import org.apache.hadoop.hbase.HBaseInterfaceAudience;

import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change it Private as this class is used internal only. It looks to me that the original class ClickjackingPreventionFilter was already used internal only. So I think it's okay to rename this class. What do you think? @petersomogyi

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to rename the class? Best if we don't then the patch can safely go back to shipping branches.

This seems like only sticking point. Otherwise patch looks nice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LP(Config) is supposed to mean the class name is exposed in user configs. If the user can no longer specify which filters then it'd be fine to make it Private. Otherwise we'd need a layer of indirection so that folks don't give classnames.

I think it'd also be fine for this to go to shipping branches of we expressly checked for the old classname in configs and did the appropriate thing to put the same protection in place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rechecked the source code and I found that implementations of Servlet filters in the hbase project are basically LP(Config). It seems to me that we assume that the implementations are used from "hbase.rest.filter.classes". If so, we should not rename the class name or we need a layer of indirection as Sean mentioned.

public class ClickjackingPreventionFilter implements Filter {
public class SecurityHeadersFilter implements Filter {
private static final Logger LOG =
LoggerFactory.getLogger(SecurityHeadersFilter.class);
private FilterConfig filterConfig;

@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
LOG.info("Added security headers filter");
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpRes = (HttpServletResponse) res;
httpRes.addHeader("X-Frame-Options", filterConfig.getInitParameter("xframeoptions"));
chain.doFilter(req, res);
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.addHeader("X-Content-Type-Options", "nosniff");
httpResponse.addHeader("X-XSS-Protection", "1; mode=block");
httpResponse.addHeader("X-Frame-Options",
filterConfig.getInitParameter("xframeoptions"));
httpResponse.addHeader("Strict-Transport-Security",
filterConfig.getInitParameter("hsts"));
chain.doFilter(request, response);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.EnumSet;
import java.util.concurrent.ArrayBlockingQueue;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.hadoop.hbase.http.SecurityHeadersFilter;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
Expand Down Expand Up @@ -137,6 +139,19 @@ void addCSRFFilter(ServletContextHandler ctxHandler, Configuration conf) {
}
}

private void addSecurityHeadersFilter(ServletContextHandler ctxHandler, Configuration conf) {
FilterHolder holder = new FilterHolder();
holder.setName("security");
holder.setClassName(SecurityHeadersFilter.class.getName());
Map<String, String> params = new HashMap<>();
params.put("xframeoptions", conf.get("hbase.http.filter.xframeoptions.mode",
"DENY"));
params.put("hsts", conf.get("hbase.http.filter.hsts.value",
"max-age=31536000"));
holder.setInitParameters(params);
ctxHandler.addFilter(holder, PATH_SPEC_ANY, EnumSet.allOf(DispatcherType.class));
}

// login the server principal (if using secure Hadoop)
private static Pair<FilterHolder, Class<? extends ServletContainer>> loginServerPrincipal(
UserProvider userProvider, Configuration conf) throws Exception {
Expand Down Expand Up @@ -349,6 +364,7 @@ public synchronized void run() throws Exception {
ctxHandler.addFilter(filter, PATH_SPEC_ANY, EnumSet.of(DispatcherType.REQUEST));
}
addCSRFFilter(ctxHandler, conf);
addSecurityHeadersFilter(ctxHandler, conf);
HttpServerUtil.constrainHttpMethods(ctxHandler, servlet.getConfiguration()
.getBoolean(REST_HTTP_ALLOW_OPTIONS_METHOD, REST_HTTP_ALLOW_OPTIONS_METHOD_DEFAULT));

Expand Down