Skip to content

Commit 2d972ca

Browse files
committed
Stop echoing the request Origin with allow-credentials in logviewer JSON responses
1 parent 76a59d2 commit 2d972ca

2 files changed

Lines changed: 51 additions & 12 deletions

File tree

storm-webapp/src/main/java/org/apache/storm/daemon/logviewer/utils/LogviewerResponseBuilder.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import java.io.InputStream;
3636
import java.io.OutputStream;
3737
import java.nio.file.Files;
38-
import java.util.HashMap;
39-
import java.util.Map;
4038

4139
import org.apache.storm.daemon.common.JsonResponseBuilder;
4240
import org.apache.storm.daemon.ui.UIHelpers;
@@ -61,11 +59,13 @@ public static Response buildSuccessHtmlResponse(String content) {
6159
*
6260
* @param entity entity object to represent it as JSON
6361
* @param callback callbackParameterName for JSONP
64-
* @param origin origin
62+
* @param origin origin of the request, not echoed back in the response
6563
*/
6664
public static Response buildSuccessJsonResponse(Object entity, String callback, String origin) {
67-
return new JsonResponseBuilder().setData(entity).setCallback(callback)
68-
.setHeaders(LogviewerResponseBuilder.getHeadersForSuccessResponse(origin)).build();
65+
// The request origin is deliberately not reflected back: pairing a caller supplied
66+
// Access-Control-Allow-Origin with Access-Control-Allow-Credentials would let browsers
67+
// hand the response to any site. Keep the default Access-Control-Allow-Origin: * instead.
68+
return new JsonResponseBuilder().setData(entity).setCallback(callback).build();
6969
}
7070

7171
/**
@@ -136,13 +136,6 @@ public static Response buildExceptionJsonResponse(Exception ex, String callback)
136136
.setCallback(callback).setStatus(statusCode).build();
137137
}
138138

139-
private static Map<String, Object> getHeadersForSuccessResponse(String origin) {
140-
Map<String, Object> headers = new HashMap<>();
141-
headers.put("Access-Control-Allow-Origin", origin);
142-
headers.put("Access-Control-Allow-Credentials", "true");
143-
return headers;
144-
}
145-
146139
private static String buildUnauthorizedUserHtml(String user) {
147140
String content = "User '" + escapeHtml4(user) + "' is not authorized.";
148141
return body(h2(content)).render();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.storm.daemon.logviewer.utils;
20+
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
import static org.hamcrest.Matchers.is;
23+
import static org.hamcrest.Matchers.nullValue;
24+
25+
import jakarta.ws.rs.core.Response;
26+
27+
import java.util.Collections;
28+
29+
import org.junit.jupiter.api.Test;
30+
31+
public class LogviewerResponseBuilderTest {
32+
33+
/**
34+
* A success response must keep the documented Access-Control-Allow-Origin: * and must not
35+
* echo the request origin back, nor allow credentials.
36+
*/
37+
@Test
38+
public void testSuccessJsonResponseDoesNotEchoRequestOrigin() {
39+
Response response = LogviewerResponseBuilder.buildSuccessJsonResponse(
40+
Collections.singletonMap("someKey", "someValue"), null, "http://other.example.com");
41+
42+
assertThat(response.getStatus(), is(200));
43+
assertThat(response.getHeaderString("Access-Control-Allow-Origin"), is("*"));
44+
assertThat(response.getHeaderString("Access-Control-Allow-Credentials"), is(nullValue()));
45+
}
46+
}

0 commit comments

Comments
 (0)