-
Notifications
You must be signed in to change notification settings - Fork 3.4k
HBASE-23303 Add security headers to REST server/info page #843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
9e32437
3080054
b28af74
519afd1
dfc3fdd
ef700ce
fab1bf9
158d607
19e9cb1
47ec91b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** | ||
| * 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 | ||
| * <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 org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.hbase.HBaseInterfaceAudience; | ||
| import org.apache.yetus.audience.InterfaceAudience; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| 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 java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) | ||
| public class SecurityHeadersFilter implements Filter { | ||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(SecurityHeadersFilter.class); | ||
| private static final String DEFAULT_XFRAMEOPTIONS = "DENY"; | ||
| private static final String DEFAULT_HSTS = "max-age=63072000;includeSubDomains;preload"; | ||
| private static final String DEFAULT_CSP = | ||
| "default-src https: data: 'unsafe-inline' 'unsafe-eval'"; | ||
| 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 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")); | ||
| httpResponse.addHeader("Content-Security-Policy", | ||
|
||
| filterConfig.getInitParameter("csp")); | ||
| chain.doFilter(request, response); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| } | ||
|
|
||
| public static Map<String, String> getDefaultParameters(Configuration conf) { | ||
| Map<String, String> params = new HashMap<>(); | ||
| params.put("xframeoptions", conf.get("hbase.http.filter.xframeoptions.mode", | ||
| DEFAULT_XFRAMEOPTIONS)); | ||
| params.put("hsts", conf.get("hbase.http.filter.hsts.value", | ||
| DEFAULT_HSTS)); | ||
| params.put("csp", conf.get("hbase.http.filter.csp.value", | ||
| DEFAULT_CSP)); | ||
| return params; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can change it
Privateas this class is used internal only. It looks to me that the original classClickjackingPreventionFilterwas already used internal only. So I think it's okay to rename this class. What do you think? @petersomogyiThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.