𝗦𝗲𝗰𝘂𝗿𝗶𝘁𝘆 𝗔𝘂𝗱𝗶𝘁 𝗥𝗲𝘀𝘂𝗹𝘁𝘀: 𝗪𝗵𝘆 𝗜 𝗳𝗲𝗹 𝗲𝗺𝗯𝗮𝗿𝗿𝗮𝘀𝘀𝗲𝗱
I recently ran a security audit on all my side projects. This includes my FastAPI backend, Telegram bots, PWA, and Streamlit apps.
I thought my code was safe because I was careful. I was wrong.
I am sharing these real production bugs to help you avoid them. These are not theoretical checklists. These are mistakes I actually made.
The Conditional Authentication Trap I wrote code that checked if an API secret existed before verifying it. If the environment variable was missing, the check skipped entirely. This meant my entire API was open to the public. Rule: If a secret is missing, fail hard with a 500 error. Never skip authentication.
The Git History Leak I once hardcoded an API key for a quick test. I moved it to a .env file later and thought it was fixed. But Git remembers everything. Anyone can find that key in my commit history. Rule: If you commit a key, assume it is stolen. Rotate it immediately. Use git-filter-repo to clean your history.
The Debug Endpoint Leak I left a /debug/config endpoint in production to help me troubleshoot. It exposed my database URLs and environment settings. Rule: Remove all debug endpoints before deployment. Use logs instead.
Leaking System Info via Errors I used str(e) in my error responses. This sent database errors and file paths directly to the user. Attackers use this to map your infrastructure. Rule: Log the detailed error for yourself. Send a generic "Internal Server Error" to the client.
The XSS Risk in Frontend I used innerHTML to render user content. This allowed scripts to run in other users' browsers. Rule: Always escape HTML. Treat innerHTML as a way to execute arbitrary code.
Missing Rate Limits I had endpoints that called expensive AI models without any limits. A single loop or a stolen key could cost me hundreds of dollars. Rule: Authentication stops unauthorized users. Rate limiting stops authorized users from abusing your system.
Permissive CORS Policy I used allow_origins=["*"] in production. This allows any site to make requests to your API. Rule: Only allow your specific frontend domain.
عارضی فائل کا رساؤ اگر فائل پروسیس کرتے وقت میرا کوڈ کریش ہو جاتا، تو عارضی فائل ہمیشہ کے لیے ڈسک پر رہ جاتی۔ اس سے جگہ ضائع ہوتی ہے اور حساس ڈیٹا لیک ہو سکتا ہے۔ اصول:
try-finallyبلاکس کا استعمال کریں تاکہ اس بات کو یقینی بنایا جا سکے کہ ایرر (error) ہونے کی صورت میں بھی فائلیں ڈیلیٹ ہو جائیں۔
میری نئی لازمی چیک لسٹ:
کوڈنگ سے پہلے:
• .gitignore بنائیں
• .env.example بنائیں
ہر endpoint کے لیے: • Authentication شامل کریں • Generic error messages استعمال کریں • بھاری کاموں (expensive tasks) کے لیے rate limits لگائیں
Commit کرنے سے پہلے: • اپنے diff میں secrets کے لیے اسکین کریں
Deploy کرنے سے پہلے: • اپنی dependencies پر سیکیورٹی آڈٹ چلائیں
سیکیورٹی کے مسائل اتفاقیہ طور پر نہیں ہوتے۔ یہ "TODO" کمنٹس اور "عارضی" اصلاحات کی وجہ سے ہوتے ہیں جو ہمیشہ کے لیے پروڈکشن میں رہ جاتی ہیں۔
بگ (bug) کو ٹھیک کرنا بورنگ ہے۔ ڈیٹا کی خلاف ورزی (breach) کو ٹھیک کرنا مہنگا ہے۔