diff --git a/src/main/java/graphql/execution/preparsed/persisted/PersistedQuerySupport.java b/src/main/java/graphql/execution/preparsed/persisted/PersistedQuerySupport.java index b65e20b78a..5ff1120314 100644 --- a/src/main/java/graphql/execution/preparsed/persisted/PersistedQuerySupport.java +++ b/src/main/java/graphql/execution/preparsed/persisted/PersistedQuerySupport.java @@ -45,6 +45,11 @@ public CompletableFuture getDocumentAsync(ExecutionInput try { if (queryIdOption.isPresent()) { Object persistedQueryId = queryIdOption.get(); + // query text sent along with the id has to match it, even when the id is already cached + String requestQueryText = executionInput.getQuery(); + if (hasQueryText(requestQueryText) && persistedQueryIdIsInvalid(persistedQueryId, requestQueryText)) { + throw new PersistedQueryIdInvalid(persistedQueryId); + } return persistedQueryCache.getPersistedQueryDocumentAsync(persistedQueryId, executionInput, (queryText) -> { // we have a miss and they gave us nothing - bah! if (queryText == null || queryText.isBlank()) { @@ -79,6 +84,10 @@ protected boolean persistedQueryIdIsInvalid(Object persistedQueryId, String quer return false; } + private static boolean hasQueryText(String queryText) { + return queryText != null && !queryText.isBlank() && !queryText.equals(PERSISTED_QUERY_MARKER); + } + /** * Allows you to customize the graphql error that is sent back on a missing persisted query * diff --git a/src/test/groovy/graphql/execution/preparsed/persisted/ApolloPersistedQuerySupportTest.groovy b/src/test/groovy/graphql/execution/preparsed/persisted/ApolloPersistedQuerySupportTest.groovy index 7b4a4cc168..3d2331110d 100644 --- a/src/test/groovy/graphql/execution/preparsed/persisted/ApolloPersistedQuerySupportTest.groovy +++ b/src/test/groovy/graphql/execution/preparsed/persisted/ApolloPersistedQuerySupportTest.groovy @@ -1,8 +1,11 @@ package graphql.execution.preparsed.persisted import graphql.ExecutionInput +import graphql.GraphQL +import graphql.TestUtil import graphql.execution.preparsed.PreparsedDocumentEntry import graphql.parser.Parser +import graphql.schema.DataFetcher import spock.lang.Specification import java.util.concurrent.CompletableFuture @@ -100,12 +103,12 @@ class ApolloPersistedQuerySupportTest extends Specification { persistedQueryCache.parseCount.size() == 0 } - def "will use query hash in preference to query text"() { + def "will use query hash when the query text matches it"() { CacheImplementation persistedQueryCache = new CacheImplementation() def apolloSupport = new ApolloPersistedQuerySupport(persistedQueryCache) when: - def ei = mkEI(hashOne, "{normal}") + def ei = mkEI(hashOne, "query { oneTwoThree }") def documentEntry = apolloSupport.getDocumentAsync(ei, engineParser).join() def doc = documentEntry.getDocument() then: @@ -115,6 +118,42 @@ class ApolloPersistedQuerySupportTest extends Specification { } + def "will have error if the query text does not match the query hash"() { + CacheImplementation persistedQueryCache = new CacheImplementation() + def apolloSupport = new ApolloPersistedQuerySupport(persistedQueryCache) + + when: + def ei = mkEI(hashOne, "{normal}") + def documentEntry = apolloSupport.getDocumentAsync(ei, engineParser).join() + then: + documentEntry.getDocument() == null + def error = documentEntry.getErrors()[0] + error.message == "PersistedQueryIdInvalid" + error.getExtensions()["persistedQueryId"] == hashOne + persistedQueryCache.keyCount[hashOne] == null + } + + def "will have error if the query text does not match a query hash that is already cached"() { + def schema = TestUtil.schema("type Query { public : String secret : String }", + [Query: [public: { env -> "public" } as DataFetcher, secret: { env -> "secret" } as DataFetcher]]) + def cache = new InMemoryPersistedQueryCache([:]) + def graphQL = GraphQL.newGraphQL(schema).preparsedDocumentProvider(new ApolloPersistedQuerySupport(cache)).build() + // sha256 of "{ secret }" + def secretHash = "d039e560fffc8b5c8f910b1e30d0ede1df97a362ad5f0c25738f6d4ffd431dc4" + + when: + def er = graphQL.execute(mkEI(secretHash, "{ secret }")) + then: + er.errors.isEmpty() + er.data == [secret: "secret"] + + when: + er = graphQL.execute(mkEI(secretHash, "{ public }")) + then: + er.data == null + er.errors[0].message == "PersistedQueryIdInvalid" + } + def "will have error if we dont return any query text on cache miss"() { CacheImplementation persistedQueryCache = new CacheImplementation()