Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(connections): an empty OAuth2 token reply no longer breaks connection refresh (#16490)

Bartosz Majewskib043d7409298
diff --git a/packages/server/api/src/app/app-connection/app-connection-service/oauth2/oauth2-util.ts b/packages/server/api/src/app/app-connection/app-connection-service/oauth2/oauth2-util.ts
index d239bdf1650..945c7418fc5 100644
--- a/packages/server/api/src/app/app-connection/app-connection-service/oauth2/oauth2-util.ts
+++ b/packages/server/api/src/app/app-connection/app-connection-service/oauth2/oauth2-util.ts
@@ -49,7 +49,7 @@ export const oauth2Util = (log: FastifyBaseLogger) => ({
},
isUserError: (e: unknown): boolean => {
if (isAxiosError(e)) {
- const error = e.response?.data.error
+ const error = e.response?.data?.error
switch (error) {
case 'invalid_grant':
return true
diff --git a/packages/server/api/test/unit/app/app-connection/oauth2-util.test.ts b/packages/server/api/test/unit/app/app-connection/oauth2-util.test.ts
index 4886c38ac91..ca333cb4723 100644
--- a/packages/server/api/test/unit/app/app-connection/oauth2-util.test.ts
+++ b/packages/server/api/test/unit/app/app-connection/oauth2-util.test.ts
@@ -1,5 +1,6 @@
import { PropertyType } from '@activepieces/pieces-framework'
import { OAuth2GrantType } from '@activepieces/shared'
+import { AxiosError, AxiosHeaders } from 'axios'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
const mockGetOrThrow = vi.fn()
@@ -17,6 +18,17 @@ import { oauth2Util } from '../../../../src/app/app-connection/app-connection-se
const util = oauth2Util({} as never)
const nowSeconds = Math.round(Date.now() / 1000)
+function tokenEndpointError(data: unknown): AxiosError {
+ const config = { headers: new AxiosHeaders() }
+ return new AxiosError('stream has been aborted', AxiosError.ERR_BAD_RESPONSE, config, {}, {
+ status: 200,
+ statusText: 'OK',
+ headers: {},
+ config,
+ data,
+ })
+}
+
function connectionWith({ claimedAt, expiresIn }: { claimedAt: number | string, expiresIn?: number | string }): never {
return {
access_token: 'token',
@@ -105,6 +117,25 @@ describe('oauth2Util.formatOAuth2Response', () => {
})
})
+describe('oauth2Util.isUserError', () => {
+ it('treats a token-endpoint reply with no body as a non-user error instead of throwing', () => {
+ expect(util.isUserError(tokenEndpointError(undefined))).toBe(false)
+ expect(util.isUserError(tokenEndpointError(null))).toBe(false)
+ })
+
+ it('recognises invalid_grant as a user error', () => {
+ expect(util.isUserError(tokenEndpointError({ error: 'invalid_grant' }))).toBe(true)
+ })
+
+ it('treats other OAuth2 error codes as non-user errors', () => {
+ expect(util.isUserError(tokenEndpointError({ error: 'invalid_client' }))).toBe(false)
+ })
+
+ it('treats an axios error with no response as a non-user error', () => {
+ expect(util.isUserError(new AxiosError('socket hang up', AxiosError.ECONNABORTED))).toBe(false)
+ })
+})
+
describe('oauth2Util.buildAuthorizationUrl', () => {
it('omits scope from the authorize url rather than sending the literal placeholder when an optional scope prop is not provided', async () => {
mockGetOrThrow.mockResolvedValue({