Skip to content

fix: add credentials to API key save/reveal calls for CSRF protection#1435

Open
jecruz wants to merge 1 commit intoagent0ai:mainfrom
jecruz:fix/api-keys-credentials
Open

fix: add credentials to API key save/reveal calls for CSRF protection#1435
jecruz wants to merge 1 commit intoagent0ai:mainfrom
jecruz:fix/api-keys-credentials

Conversation

@jecruz
Copy link
Copy Markdown

@jecruz jecruz commented Apr 3, 2026

Summary

The saveApiKeys and revealApiKey functions in api-keys-mixin.js were missing credentials: 'same-origin' in their fetchApi calls. Without this, the browser doesn't send the Flask session cookie, causing CSRF validation to fail with "CSRF token missing or invalid" (403).

Root Cause

fetchApi() passes options through to fetch(), but the options object passed to these state-mutating POST calls didn't include credentials: 'same-origin'. As a result:

  1. Browser makes POST request to /api/plugins/_model_config/api_keys
  2. Flask's CSRF protection checks: session.get("csrf_token") vs X-CSRF-Token header
  3. Since no session cookie was sent, the CSRF validation fails

Fix

Add credentials: 'same-origin' to both saveApiKeys() and revealApiKey() to ensure the session cookie is sent with the request.

Test plan

  1. Open the API Keys modal in the Agent Zero UI
  2. Enter an API key and click Save
  3. Verify the key persists to agent-zero-data/.env
  4. Verify no 403 CSRF errors in browser console

These API calls were not sending the session cookie, causing the
Flask CSRF protection to reject the requests with 403 'CSRF token
missing or invalid'. Adding credentials: 'same-origin' ensures
the browser includes the session cookie with the request.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant