Skip to content

Commit d0020cf

Browse files
adinauerclaude
andcommitted
fix(graphql): Filter batched GraphQL request bodies
Apply document and variable collection policies to every operation in a batched GraphQL request. Fail closed when a batch contains non-object entries instead of attaching partially filtered content. Refs #5666 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3a6fa4f commit d0020cf

2 files changed

Lines changed: 77 additions & 12 deletions

File tree

‎sentry/src/main/java/io/sentry/util/GraphqlUtils.java‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import io.sentry.SentryLevel;
66
import io.sentry.SentryOptions;
77
import java.io.StringReader;
8+
import java.io.StringWriter;
9+
import java.util.ArrayList;
810
import java.util.LinkedHashMap;
11+
import java.util.List;
912
import java.util.Map;
1013
import org.jetbrains.annotations.ApiStatus;
1114
import org.jetbrains.annotations.NotNull;
@@ -31,23 +34,45 @@ private GraphqlUtils() {}
3134

3235
try (JsonObjectReader reader = new JsonObjectReader(new StringReader(body))) {
3336
final @Nullable Object value = reader.nextObjectOrNull();
34-
if (!(value instanceof Map)) {
37+
final @NotNull Object filtered;
38+
if (value instanceof Map) {
39+
@SuppressWarnings("unchecked")
40+
final @NotNull Map<String, Object> requestBody = (Map<String, Object>) value;
41+
filtered = filterRequest(requestBody, includeDocument, includeVariables);
42+
} else if (value instanceof List) {
43+
final @NotNull List<Map<String, Object>> filteredBatch = new ArrayList<>();
44+
for (final @Nullable Object item : (List<?>) value) {
45+
if (!(item instanceof Map)) {
46+
return null;
47+
}
48+
@SuppressWarnings("unchecked")
49+
final @NotNull Map<String, Object> requestBody = (Map<String, Object>) item;
50+
filteredBatch.add(filterRequest(requestBody, includeDocument, includeVariables));
51+
}
52+
filtered = filteredBatch;
53+
} else {
3554
return null;
3655
}
37-
38-
@SuppressWarnings("unchecked")
39-
final @NotNull Map<String, Object> requestBody = (Map<String, Object>) value;
40-
final @NotNull Map<String, Object> filtered = new LinkedHashMap<>(requestBody);
41-
if (!includeDocument) {
42-
filtered.remove("query");
43-
}
44-
if (!includeVariables) {
45-
filtered.remove("variables");
46-
}
47-
return options.getSerializer().serialize(filtered);
56+
final @NotNull StringWriter writer = new StringWriter();
57+
options.getSerializer().serialize(filtered, writer);
58+
return writer.toString();
4859
} catch (Throwable e) {
4960
options.getLogger().log(SentryLevel.ERROR, "Failed to filter GraphQL request body.", e);
5061
return null;
5162
}
5263
}
64+
65+
private static @NotNull Map<String, Object> filterRequest(
66+
final @NotNull Map<String, Object> request,
67+
final boolean includeDocument,
68+
final boolean includeVariables) {
69+
final @NotNull Map<String, Object> filtered = new LinkedHashMap<>(request);
70+
if (!includeDocument) {
71+
filtered.remove("query");
72+
}
73+
if (!includeVariables) {
74+
filtered.remove("variables");
75+
}
76+
return filtered;
77+
}
5378
}

‎sentry/src/test/java/io/sentry/util/GraphqlUtilsTest.kt‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,48 @@ class GraphqlUtilsTest {
3535
}
3636
}
3737

38+
@Test
39+
fun `filters documents from a batched GraphQL request body`() {
40+
val options = SentryOptions().also { it.dataCollection.graphql.setDocument(false) }
41+
42+
val result = GraphqlUtils.filterRequestBody(BATCH_REQUEST_BODY, options)
43+
44+
assertThat(result).isNotNull()
45+
JsonObjectReader(StringReader(result)).use { reader ->
46+
@Suppress("UNCHECKED_CAST") val body = reader.nextObjectOrNull() as List<Map<String, Any>>
47+
assertThat(body).hasSize(2)
48+
assertThat(body[0]).containsEntry("operationName", "GetUser")
49+
assertThat(body[0]).containsEntry("variables", mapOf("id" to "123"))
50+
assertThat(body[0]).doesNotContainKey("query")
51+
assertThat(body[1]).containsEntry("operationName", "GetTeam")
52+
assertThat(body[1]).containsEntry("variables", mapOf("slug" to "sdk"))
53+
assertThat(body[1]).doesNotContainKey("query")
54+
}
55+
}
56+
57+
@Test
58+
fun `filters variables from a batched GraphQL request body`() {
59+
val options = SentryOptions().also { it.dataCollection.graphql.setVariables(false) }
60+
61+
val result = GraphqlUtils.filterRequestBody(BATCH_REQUEST_BODY, options)
62+
63+
assertThat(result).isNotNull()
64+
JsonObjectReader(StringReader(result)).use { reader ->
65+
@Suppress("UNCHECKED_CAST") val body = reader.nextObjectOrNull() as List<Map<String, Any>>
66+
assertThat(body).hasSize(2)
67+
assertThat(body[0]).containsEntry("operationName", "GetUser")
68+
assertThat(body[0]).containsEntry("query", "query { viewer { name } }")
69+
assertThat(body[0]).doesNotContainKey("variables")
70+
assertThat(body[1]).containsEntry("operationName", "GetTeam")
71+
assertThat(body[1]).containsEntry("query", "query { team { name } }")
72+
assertThat(body[1]).doesNotContainKey("variables")
73+
}
74+
}
75+
3876
private companion object {
3977
const val REQUEST_BODY =
4078
"""{"operationName":"GetUser","variables":{"id":"123"},"query":"query { viewer { name } }"}"""
79+
const val BATCH_REQUEST_BODY =
80+
"""[{"operationName":"GetUser","variables":{"id":"123"},"query":"query { viewer { name } }"},{"operationName":"GetTeam","variables":{"slug":"sdk"},"query":"query { team { name } }"}]"""
4181
}
4282
}

0 commit comments

Comments
 (0)