惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
IT之家
IT之家
博客园 - 聂微东
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
小众软件
小众软件
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
量子位
月光博客
月光博客
博客园 - 【当耐特】
博客园 - 叶小钗

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building an E2EE Chat App in Flask - Part 3: Keeping File...
Avash Karn · 2026-05-23 · via DEV Community

Okay hi, so imagine you have a mailbox at your house. Anyone can put things in it, am I right or am I right? What if someone puts a bomb in there? Or trash? You need to check what goes in before it causes problems.

That's what file uploads are like. Users can upload anything. We need to stop the bad stuff.

The Problem: Bad Files

When I first built my chat app, I didn't think about what users could upload. They could upload:

  • Files with viruses hidden inside
  • Really huge files that break the app
  • Weird file types that cause problems
  • Files with tricky names designed to hack the system

It's like leaving your house door open and hoping bad people don't come in. Spoiler: they will.

My Solution: Check Everything

I learned to be a security guard for my app. Here's what I do:

1. Only Accept Certain File Types

First, I made a python Set of file types I actually want:

ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'mp4', 'mov'}

Enter fullscreen mode Exit fullscreen mode

I only allow images (png, jpg, gif) and videos (mp4, mov). That's it. No .exe files. No .zip files. Nothing unsafe.

Then I check every file:

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

Enter fullscreen mode Exit fullscreen mode

All that code just says: "Does the file have a dot? Is it one of our allowed types? If yes, cool. If no, reject it."

2. Clean Up Bad Filenames

Here's something sneaky: attackers might upload a file named something like "../../../admin.php" to try to escape the upload folder and hack the system.

So I use a function that removes all the dangerous stuff:

from werkzeug.utils import secure_filename

filename = secure_filename(file.filename)

Enter fullscreen mode Exit fullscreen mode

If someone uploads "../../admin.php", this function turns it into "admin.php" (harmless).

If someone uploads "file (1) [2023].jpg", it cleans it up too.

3. Organize Files by Type

After the file is safe, I check what type it is:

def get_file_type(filename):
    ext = filename.rsplit('.', 1)[1].lower()
    if ext in {'mp4', 'mov'}:
        return 'video'
    elif ext in {'png', 'jpg', 'jpeg', 'gif'}:
        return 'image'
    return 'file'

Enter fullscreen mode Exit fullscreen mode

So if someone uploads a video, I store it in the videos folder. Images go in the images folder. Everything stays organized and safe.

Why This Actually Works

Think about it like airport security:

  1. Whitelist = Only let through what's allowed (like a passenger list)
  2. Clean names = Remove anything suspicious (like checking luggage)
  3. Organize = Put things in the right place (like baggage claim)

If you don't do these checks, bad stuff gets through.

What Actually Happened

I built this without thinking about security. Then I realized: what if someone uploads a virus? What if they upload a 1GB file? What if they try to hack the system with a weird filename?

So I added these checks. Now my app is safer.

The Real Lesson

Never, ever trust what users do. Always assume someone is trying to break your app. Check everything.

What's Next

Part 4 is about real-time messaging. How do messages update instantly without refreshing? WebSockets.

Let me know what you think!