Skip to content

Commit

Permalink
Handle waf result in graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlesDD committed May 24, 2024
1 parent bcc3fd4 commit 45055a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/dd-trace/src/appsec/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ function onGraphqlStartResolve ({ context, resolverInfo }) {
if (!resolverInfo || typeof resolverInfo !== 'object') return

const actions = waf.run({ ephemeral: { [addresses.HTTP_INCOMING_GRAPHQL_RESOLVER]: resolverInfo } }, req)
if (actions?.includes('block')) {
if (
actions &&
(Object.keys(actions).includes('block_request') || Object.keys(actions).includes('redirect_request'))
) {
const requestData = graphqlRequestData.get(req)
if (requestData?.isInGraphqlRequest) {
requestData.blocked = true
requestData.wafAction = actions.block_request || actions.redirect_request
context?.abortController?.abort()
}
}
Expand Down Expand Up @@ -87,7 +91,7 @@ function beforeWriteApolloGraphqlResponse ({ abortController, abortData }) {
const rootSpan = web.root(req)
if (!rootSpan) return

const blockingData = getBlockingData(req, specificBlockingTypes.GRAPHQL, rootSpan)
const blockingData = getBlockingData(req, specificBlockingTypes.GRAPHQL, rootSpan, requestData.wafAction)
abortData.statusCode = blockingData.statusCode
abortData.headers = blockingData.headers
abortData.message = blockingData.body
Expand Down
12 changes: 10 additions & 2 deletions packages/dd-trace/test/appsec/graphql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,17 @@ describe('GraphQL', () => {
user: [{ id: '1234' }]
}

const blockParameters = {
status_code: '401',
type: 'auto',
grpc_status_code: '10'
}

const abortController = context.abortController

sinon.stub(waf, 'run').returns(['block'])
sinon.stub(waf, 'run').returns({
block_request: blockParameters
})
sinon.stub(web, 'root').returns({})

startGraphqlResolve.publish({ context, resolverInfo })
Expand All @@ -231,7 +239,7 @@ describe('GraphQL', () => {
const abortData = {}
apolloChannel.asyncEnd.publish({ abortController, abortData })

expect(blocking.getBlockingData).to.have.been.calledOnceWithExactly(req, 'graphql', {})
expect(blocking.getBlockingData).to.have.been.calledOnceWithExactly(req, 'graphql', {}, blockParameters)
})
})
})

0 comments on commit 45055a9

Please sign in to comment.