Skip to content

Commit 871f4e8

Browse files
authored
fix(copilot): env key validation (#1017)
* Fix v1 * Use env var * Lint * Fix env key validation * Remove logger * Fix agent url * Fix tests
1 parent 091343a commit 871f4e8

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

apps/sim/app/api/copilot/methods/route.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('Copilot Methods API Route', () => {
6060
vi.doMock('@/lib/env', () => ({
6161
env: {
6262
INTERNAL_API_SECRET: 'test-secret-key',
63+
COPILOT_API_KEY: 'test-copilot-key',
6364
},
6465
}))
6566

@@ -123,17 +124,16 @@ describe('Copilot Methods API Route', () => {
123124

124125
expect(response.status).toBe(401)
125126
const responseData = await response.json()
126-
expect(responseData).toEqual({
127-
success: false,
128-
error: 'Invalid API key',
129-
})
127+
expect(responseData.success).toBe(false)
128+
expect(typeof responseData.error).toBe('string')
130129
})
131130

132131
it('should return 401 when internal API key is not configured', async () => {
133132
// Mock environment with no API key
134133
vi.doMock('@/lib/env', () => ({
135134
env: {
136135
INTERNAL_API_SECRET: undefined,
136+
COPILOT_API_KEY: 'test-copilot-key',
137137
},
138138
}))
139139

@@ -154,10 +154,9 @@ describe('Copilot Methods API Route', () => {
154154

155155
expect(response.status).toBe(401)
156156
const responseData = await response.json()
157-
expect(responseData).toEqual({
158-
success: false,
159-
error: 'Internal API key not configured',
160-
})
157+
expect(responseData.status).toBeUndefined()
158+
expect(responseData.success).toBe(false)
159+
expect(typeof responseData.error).toBe('string')
161160
})
162161

163162
it('should return 400 for invalid request body - missing methodId', async () => {

apps/sim/app/api/copilot/methods/route.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,21 @@ export async function POST(req: NextRequest) {
232232
const startTime = Date.now()
233233

234234
try {
235-
// Check authentication (internal API key)
236-
const authResult = checkInternalApiKey(req) || checkCopilotApiKey(req)
237-
if (!authResult.success) {
238-
return NextResponse.json(createErrorResponse(authResult.error || 'Authentication failed'), {
235+
// Evaluate both auth schemes; pass if either is valid
236+
const internalAuth = checkInternalApiKey(req)
237+
const copilotAuth = checkCopilotApiKey(req)
238+
const isAuthenticated = !!(internalAuth?.success || copilotAuth?.success)
239+
if (!isAuthenticated) {
240+
const errorMessage = copilotAuth.error || internalAuth.error || 'Authentication failed'
241+
return NextResponse.json(createErrorResponse(errorMessage), {
239242
status: 401,
240243
})
241244
}
242245

243246
const body = await req.json()
244247
const { methodId, params, toolCallId } = MethodExecutionSchema.parse(body)
245248

246-
logger.info(`[${requestId}] Method execution request: ${methodId}`, {
249+
logger.info(`[${requestId}] Method execution request`, {
247250
methodId,
248251
toolCallId,
249252
hasParams: !!params && Object.keys(params).length > 0,

0 commit comments

Comments
 (0)