-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9e32437
HBASE-23303. Added new HTTP filter to include missing security headers
anmolnar 3080054
HBASE-23303. Added configurable X-Frame-Options header
anmolnar b28af74
HBASE-23303. Use the same filter for both http and rest server for co…
anmolnar 519afd1
HBASE-23303. Added HSTS security header
anmolnar dfc3fdd
HBASE-23303. Checkstyle fixes
anmolnar ef700ce
HBASE-23303. Added new (last) security header: Content-Security-Policy
anmolnar fab1bf9
HBASE-23303. Reverted changes to ClickjackingPreventionFilter, create…
anmolnar 158d607
HBASE-23303. Checkstyle fix
anmolnar 19e9cb1
HBASE-23303. Added unit tests
anmolnar 47ec91b
HBASE-23303. Added unit tests to hbase-http project
anmolnar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
hbase-http/src/main/java/org/apache/hadoop/hbase/http/SecurityHeadersFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * 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 java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| 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 org.apache.commons.lang3.StringUtils; | ||
| 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; | ||
|
|
||
| @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) | ||
| public class SecurityHeadersFilter implements Filter { | ||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(SecurityHeadersFilter.class); | ||
| private static final String DEFAULT_HSTS = ""; | ||
| private static final String DEFAULT_CSP = ""; | ||
| 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"); | ||
| String hsts = filterConfig.getInitParameter("hsts"); | ||
| if (StringUtils.isNotBlank(hsts)) { | ||
| httpResponse.addHeader("Strict-Transport-Security", hsts); | ||
| } | ||
| String csp = filterConfig.getInitParameter("csp"); | ||
| if (StringUtils.isNotBlank(csp)) { | ||
| httpResponse.addHeader("Content-Security-Policy", 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("hsts", conf.get("hbase.http.filter.hsts.value", | ||
| DEFAULT_HSTS)); | ||
| params.put("csp", conf.get("hbase.http.filter.csp.value", | ||
| DEFAULT_CSP)); | ||
| return params; | ||
| } | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestSecurityHeadersFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * 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.hadoop.hbase.http; | ||
|
|
||
| import static org.apache.hadoop.hbase.http.HttpServerFunctionalTest.createTestServer; | ||
| import static org.apache.hadoop.hbase.http.HttpServerFunctionalTest.getServerURL; | ||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.CoreMatchers.not; | ||
| import static org.junit.Assert.assertThat; | ||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URL; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.hbase.HBaseClassTestRule; | ||
| import org.apache.hadoop.hbase.testclassification.MediumTests; | ||
| import org.hamcrest.core.Is; | ||
| import org.hamcrest.core.IsEqual; | ||
| import org.junit.After; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
|
|
||
| @Category({HttpServerFunctionalTest.class, MediumTests.class}) | ||
| public class TestSecurityHeadersFilter { | ||
| private static URL baseUrl; | ||
| private HttpServer http; | ||
|
|
||
| @ClassRule | ||
| public static final HBaseClassTestRule CLASS_RULE = | ||
| HBaseClassTestRule.forClass(TestSecurityHeadersFilter.class); | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| http.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDefaultValues() throws Exception { | ||
| http = createTestServer(); | ||
| http.start(); | ||
| baseUrl = getServerURL(http); | ||
|
|
||
| URL url = new URL(baseUrl, "/"); | ||
| HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
| assertThat(conn.getResponseCode(), equalTo(HttpURLConnection.HTTP_OK)); | ||
|
|
||
| assertThat("Header 'X-Content-Type-Options' is missing", | ||
| conn.getHeaderField("X-Content-Type-Options"), is(not((String)null))); | ||
| assertThat(conn.getHeaderField("X-Content-Type-Options"), equalTo("nosniff")); | ||
| assertThat("Header 'X-XSS-Protection' is missing", | ||
| conn.getHeaderField("X-XSS-Protection"), is(not((String)null))); | ||
| assertThat("Header 'X-XSS-Protection' has invalid value", | ||
| conn.getHeaderField("X-XSS-Protection"), equalTo("1; mode=block")); | ||
|
|
||
| assertThat("Header 'Strict-Transport-Security' should be missing from response," + | ||
| "but it's present", | ||
| conn.getHeaderField("Strict-Transport-Security"), is((String)null)); | ||
| assertThat("Header 'Content-Security-Policy' should be missing from response," + | ||
| "but it's present", | ||
| conn.getHeaderField("Content-Security-Policy"), is((String)null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHstsAndCspSettings() throws IOException { | ||
| Configuration conf = new Configuration(); | ||
| conf.set("hbase.http.filter.hsts.value", | ||
| "max-age=63072000;includeSubDomains;preload"); | ||
| conf.set("hbase.http.filter.csp.value", | ||
| "default-src https: data: 'unsafe-inline' 'unsafe-eval'"); | ||
| http = createTestServer(conf); | ||
| http.start(); | ||
| baseUrl = getServerURL(http); | ||
|
|
||
| URL url = new URL(baseUrl, "/"); | ||
| HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
| assertThat(conn.getResponseCode(), equalTo(HttpURLConnection.HTTP_OK)); | ||
|
|
||
| assertThat("Header 'Strict-Transport-Security' is missing from Rest response", | ||
| conn.getHeaderField("Strict-Transport-Security"), Is.is(not((String)null))); | ||
| assertThat("Header 'Strict-Transport-Security' has invalid value", | ||
| conn.getHeaderField("Strict-Transport-Security"), | ||
| IsEqual.equalTo("max-age=63072000;includeSubDomains;preload")); | ||
|
|
||
| assertThat("Header 'Content-Security-Policy' is missing from Rest response", | ||
| conn.getHeaderField("Content-Security-Policy"), Is.is(not((String)null))); | ||
| assertThat("Header 'Content-Security-Policy' has invalid value", | ||
| conn.getHeaderField("Content-Security-Policy"), | ||
| IsEqual.equalTo("default-src https: data: 'unsafe-inline' 'unsafe-eval'")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.