Skip to content

Commit 8d58e8d

Browse files
committed
Enhance request body check
Backport of 1406ca2 Closes gh-735
1 parent 265fa29 commit 8d58e8d

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

spring-graphql/src/main/java/org/springframework/graphql/server/WebGraphQlRequest.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public class WebGraphQlRequest extends DefaultExecutionGraphQlRequest implements
5858
public WebGraphQlRequest(
5959
URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale) {
6060

61-
super(getKey("query", body), getKey("operationName", body), getKey("variables", body),
62-
getKey("extensions", body), id, locale);
61+
super(getQuery(body), getOperation(body), getMap("variables", body), getMap("extensions", body), id, locale);
6362

6463
Assert.notNull(uri, "URI is required'");
6564
Assert.notNull(headers, "HttpHeaders is required'");
@@ -68,12 +67,31 @@ public WebGraphQlRequest(
6867
this.headers = headers;
6968
}
7069

70+
private static String getQuery(Map<String, Object> body) {
71+
Object value = body.get("query");
72+
if (!(value instanceof String query) || !StringUtils.hasText(query)) {
73+
throw new ServerWebInputException("Invalid value for 'query'");
74+
}
75+
return (String) value;
76+
}
77+
78+
@Nullable
79+
private static String getOperation(Map<String, Object> body) {
80+
Object value = body.get("operation");
81+
if (value != null && !(value instanceof String)) {
82+
throw new ServerWebInputException("Invalid value for 'operation'");
83+
}
84+
return (String) value;
85+
}
86+
7187
@SuppressWarnings("unchecked")
72-
private static <T> T getKey(String key, Map<String, Object> body) {
73-
if (key.equals("query") && !StringUtils.hasText((String) body.get(key))) {
74-
throw new ServerWebInputException("No \"query\" in the request document");
88+
@Nullable
89+
private static Map<String, Object> getMap(String key, Map<String, Object> body) {
90+
Object value = body.get(key);
91+
if (value != null && !(value instanceof Map)) {
92+
throw new ServerWebInputException("Invalid value for '" + key + "'");
7593
}
76-
return (T) body.get(key);
94+
return (Map<String, Object>) value;
7795
}
7896

7997

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.server;
18+
19+
import java.net.URI;
20+
import java.util.Collections;
21+
import java.util.Map;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.util.LinkedMultiValueMap;
27+
import org.springframework.web.server.ServerWebInputException;
28+
29+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
30+
31+
/**
32+
* Unit tests for {@link WebGraphQlRequest}.
33+
*
34+
* @author Rossen Stoyanchev
35+
*/
36+
public class WebGraphQlRequestTests {
37+
38+
@Test // gh-726
39+
void invalidBody() {
40+
testInvalidBody(Map.of());
41+
testInvalidBody(Map.of("query", Collections.emptyMap()));
42+
testInvalidBody(Map.of("query", "query { foo }", "operation", Collections.emptyMap()));
43+
testInvalidBody(Map.of("query", "query { foo }", "variables", "not-a-map"));
44+
testInvalidBody(Map.of("query", "query { foo }", "extensions", "not-a-map"));
45+
}
46+
47+
private void testInvalidBody(Map<String, Object> body) {
48+
assertThatThrownBy(() ->
49+
new WebGraphQlRequest(
50+
URI.create("/graphql"), new HttpHeaders(), new LinkedMultiValueMap<>(),
51+
Collections.emptyMap(), body, "1", null))
52+
.isInstanceOf(ServerWebInputException.class);
53+
}
54+
55+
56+
}

0 commit comments

Comments
 (0)