I’m facing a serious issue and need help identifying the cause.
My application setup is:
Backend: Python FastAPI
Frontend: Next.js
Auth: JWT (Bearer token)
Hosting: Nginx on a Linux server
OpenAI API Key: Stored in a
.envfile on the server
The API keys are not sent to the frontend, and they are never exposed to users. The .env file is owned by www-data and has permission rw-r--------, so only the application user can read it.
However, the OpenAI API key gets leaked within a day after recharging it, even though it’s used only on the backend. I have no logs or code paths that expose the key, and no one else has server access.
I’m trying to understand:
What could cause a backend-only API key to be leaked?
Could this be due to a server misconfiguration, a dependency vulnerability, or some kind of intrusion?
What steps should I take to diagnose and secure my server and application?
Any guidance or similar experiences would be highly appreciated. This issue is becoming critical.
www-data, and the application is running as that user, are you 100% certain the file cannot simply be requested through a simple HTTP request? What do your logfiles indicate?