Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/graphql/src/tools/graphql.tools.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { afterEach, describe, expect, it, vi } from 'vitest'

import { fetchTypeDetails } from './graphql.tools'

describe('fetchTypeDetails', () => {
afterEach(() => {
vi.restoreAllMocks()
})

it('sends the type name as a GraphQL variable instead of interpolating it into the query', async () => {
const maliciousTypeName = '") { name } } # injected'
const fetchMock = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
json: async () => ({
data: {
__type: {
name: 'safe',
kind: 'OBJECT',
description: null,
fields: [],
inputFields: [],
interfaces: [],
enumValues: [],
possibleTypes: [],
},
},
errors: null,
}),
statusText: 'OK',
} as Response)

await fetchTypeDetails(maliciousTypeName, 'test-token')

expect(fetchMock).toHaveBeenCalledTimes(1)
const [, init] = fetchMock.mock.calls[0]
const body = JSON.parse(String(init?.body))

expect(body.query).toContain('query TypeDetails($typeName: String!)')
expect(body.query).toContain('__type(name: $typeName)')
expect(body.query).not.toContain(maliciousTypeName)
expect(body.variables).toEqual({ typeName: maliciousTypeName })
})
})
23 changes: 17 additions & 6 deletions apps/graphql/src/tools/graphql.tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ async function fetchSchemaOverview(apiToken: string): Promise<SchemaOverviewResp
* @param apiToken Cloudflare API token
* @returns Detailed type information
*/
async function fetchTypeDetails(typeName: string, apiToken: string): Promise<TypeDetailsResponse> {
export async function fetchTypeDetails(
typeName: string,
apiToken: string
): Promise<TypeDetailsResponse> {
const typeDetailsQuery = `
query TypeDetails {
__type(name: "${typeName}") {
query TypeDetails($typeName: String!) {
__type(name: $typeName) {
name
kind
description
Expand Down Expand Up @@ -174,7 +177,11 @@ async function fetchTypeDetails(typeName: string, apiToken: string): Promise<Typ
}
`

const response = await executeGraphQLRequest<TypeDetailsResponse>(typeDetailsQuery, apiToken)
const response = await executeGraphQLQuery<TypeDetailsResponse>(
typeDetailsQuery,
{ typeName },
apiToken
)
return response
}

Expand Down Expand Up @@ -221,7 +228,11 @@ async function executeGraphQLRequest<T>(query: string, apiToken: string): Promis
* @param apiToken Cloudflare API token
* @returns The query results
*/
async function executeGraphQLQuery(query: string, variables: any, apiToken: string) {
async function executeGraphQLQuery<T>(
query: string,
variables: Record<string, unknown>,
apiToken: string
): Promise<T> {
// Clone the variables to avoid modifying the original
const queryVariables = { ...variables }

Expand Down Expand Up @@ -249,7 +260,7 @@ async function executeGraphQLQuery(query: string, variables: any, apiToken: stri
console.warn(`GraphQL query errors: ${errorMessages}`)
}

return result
return result as T
}

/**
Expand Down
7 changes: 7 additions & 0 deletions apps/graphql/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
environment: 'node',
},
})