Skip to content

Commit d1975ff

Browse files
committed
Enhance request body check
Closes gh-733
1 parent 0182e72 commit d1975ff

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
@@ -84,8 +84,7 @@ public WebGraphQlRequest(
8484
URI uri, HttpHeaders headers, @Nullable MultiValueMap<String, HttpCookie> cookies,
8585
Map<String, Object> attributes, Map<String, Object> body, String id, @Nullable Locale locale) {
8686

87-
super(getKey("query", body), getKey("operationName", body), getKey("variables", body),
88-
getKey("extensions", body), id, locale);
87+
super(getQuery(body), getOperation(body), getMap("variables", body), getMap("extensions", body), id, locale);
8988

9089
Assert.notNull(uri, "URI is required'");
9190
Assert.notNull(headers, "HttpHeaders is required'");
@@ -96,12 +95,31 @@ public WebGraphQlRequest(
9695
this.attributes = Collections.unmodifiableMap(attributes);
9796
}
9897

98+
private static String getQuery(Map<String, Object> body) {
99+
Object value = body.get("query");
100+
if (!(value instanceof String query) || !StringUtils.hasText(query)) {
101+
throw new ServerWebInputException("Invalid value for 'query'");
102+
}
103+
return (String) value;
104+
}
105+
106+
@Nullable
107+
private static String getOperation(Map<String, Object> body) {
108+
Object value = body.get("operation");
109+
if (value != null && !(value instanceof String)) {
110+
throw new ServerWebInputException("Invalid value for 'operation'");
111+
}
112+
return (String) value;
113+
}
114+
99115
@SuppressWarnings("unchecked")
100-
private static <T> T getKey(String key, Map<String, Object> body) {
101-
if (key.equals("query") && !StringUtils.hasText((String) body.get(key))) {
102-
throw new ServerWebInputException("No \"query\" in the request document");
116+
@Nullable
117+
private static Map<String, Object> getMap(String key, Map<String, Object> body) {
118+
Object value = body.get(key);
119+
if (value != null && !(value instanceof Map)) {
120+
throw new ServerWebInputException("Invalid value for '" + key + "'");
103121
}
104-
return (T) body.get(key);
122+
return (Map<String, Object>) value;
105123
}
106124

107125

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)