-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBASE-23303 Add security headers to REST server/info page (#843)
Signed-off-by: Toshihiro Suzuki <brfrn169@gmail.com> Signed-off-by: Sean Busbey <busbey@apache.org>
- Loading branch information
Showing
6 changed files
with
355 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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.