Skip to content

Commit 029fb54

Browse files
committed
HDFS-17591. RBF: Router should follow X-FRAME-OPTIONS protection setting (#6963)
(cherry picked from commit 059e996)
1 parent 071440c commit 029fb54

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterHttpServer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.InetSocketAddress;
2121

2222
import org.apache.hadoop.conf.Configuration;
23+
import org.apache.hadoop.hdfs.DFSConfigKeys;
2324
import org.apache.hadoop.hdfs.DFSUtil;
2425
import org.apache.hadoop.hdfs.server.common.JspHelper;
2526
import org.apache.hadoop.hdfs.server.namenode.NameNodeHttpServer;
@@ -86,6 +87,16 @@ protected void serviceStart() throws Exception {
8687
RBFConfigKeys.DFS_ROUTER_KERBEROS_INTERNAL_SPNEGO_PRINCIPAL_KEY,
8788
RBFConfigKeys.DFS_ROUTER_KEYTAB_FILE_KEY);
8889

90+
final boolean xFrameEnabled = conf.getBoolean(
91+
DFSConfigKeys.DFS_XFRAME_OPTION_ENABLED,
92+
DFSConfigKeys.DFS_XFRAME_OPTION_ENABLED_DEFAULT);
93+
94+
final String xFrameOptionValue = conf.getTrimmed(
95+
DFSConfigKeys.DFS_XFRAME_OPTION_VALUE,
96+
DFSConfigKeys.DFS_XFRAME_OPTION_VALUE_DEFAULT);
97+
98+
builder.configureXFrame(xFrameEnabled).setXFrameOption(xFrameOptionValue);
99+
89100
this.httpServer = builder.build();
90101

91102
NameNodeHttpServer.initWebHdfs(conf, httpServer,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* <p>
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package org.apache.hadoop.hdfs.server.federation.router;
19+
20+
import java.io.IOException;
21+
import java.net.HttpURLConnection;
22+
import java.net.InetSocketAddress;
23+
import java.net.URI;
24+
import java.net.URL;
25+
26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
29+
import org.apache.hadoop.conf.Configuration;
30+
import org.apache.hadoop.hdfs.DFSConfigKeys;
31+
import org.apache.hadoop.hdfs.HdfsConfiguration;
32+
33+
import static org.apache.hadoop.http.HttpServer2.XFrameOption.SAMEORIGIN;
34+
35+
/**
36+
* A class to test the XFrame options of Router HTTP Server.
37+
*/
38+
public class TestRouterHttpServerXFrame {
39+
40+
@Test
41+
public void testRouterXFrame() throws IOException {
42+
Configuration conf = new HdfsConfiguration();
43+
conf.setBoolean(DFSConfigKeys.DFS_XFRAME_OPTION_ENABLED, true);
44+
conf.set(DFSConfigKeys.DFS_XFRAME_OPTION_VALUE, SAMEORIGIN.toString());
45+
46+
Router router = new Router();
47+
try {
48+
router.init(conf);
49+
router.start();
50+
51+
InetSocketAddress httpAddress = router.getHttpServerAddress();
52+
URL url =
53+
URI.create("http://" + httpAddress.getHostName() + ":" + httpAddress.getPort()).toURL();
54+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
55+
conn.connect();
56+
57+
String xfoHeader = conn.getHeaderField("X-FRAME-OPTIONS");
58+
Assert.assertNotNull("X-FRAME-OPTIONS is absent in the header", xfoHeader);
59+
Assert.assertTrue(xfoHeader.endsWith(SAMEORIGIN.toString()));
60+
} finally {
61+
router.stop();
62+
router.close();
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)