A secure programmatic advertising demo using Flask and HTTPS. This project focuses on implementing robust web security practices, including HTTPS (SSL/TLS), Signed Cookies, and persistent Session Tracking.
- Local HTTPS: Uses
mkcertfor a trusted local development environment. - Secure Sessions: Implements Flask
sessionfor tamper-proof visitor counting. - User Profiles: Persistent storage of user interests and visit analytics.
- Targeted Ads: Basic ad targeting logic based on user data and loyalty.
- Database Persistence: SQLite-backed user management with hashed passwords and profile tracking.
- Secure=True: Ensures cookies are only sent over encrypted HTTPS connections.
- HttpOnly=True: Prevents client-side scripts (JavaScript) from accessing session cookies, mitigating XSS attacks.
- SameSite=Lax: Protects against Cross-Site Request Forgery (CSRF) while allowing essential functionality.
- Session Signing: Uses a
SECRET_KEYto sign data, preventing users from manually editing their visit counts in the browser console.
git clone https://github.com/toxicbishop/Cookies-in-Python.git
cd Cookies-in-Pythonpython3 -m venv .venv
source .venv/bin/activatepython -m venv .venv
.venv\Scripts\activate.batpython -m venv .venv
source .venv/Scripts/activatepython -m venv .venv
.venv\Scripts\Activate.ps1Upgrade pip (recommended):
python -m pip install --upgrade pipInstall the required packages:
pip install -r requirements.txtYou'll need mkcert to create trusted local SSL certificates.
choco install mkcertbrew install mkcertsudo apt install mkcert libnss3-toolsInstall the local Certificate Authority:
mkcert -installRun:
mkcert localhost 127.0.0.1 ::1This generates:
localhost+2.pemlocalhost+2-key.pem
These certificates are used by Flask to serve HTTPS locally.
The application uses SQLite.
To manually initialize the database:
sqlite3 users.db < schema.sqlNote: If
users.dbdoes not exist, the application automatically creates the required database and tables on first launch.
Start the Flask application:
python cookies.pyOpen your browser and visit:
https://127.0.0.1:5001
Accept the local certificate if prompted (this should only happen if mkcert is not installed correctly).
.
├── cookies.py # Main Flask application
├── requirements.txt # Python dependencies
├── schema.sql # SQLite schema
├── users.db # SQLite database (generated locally)
├── static/
│ └── profile_pics/ # Uploaded profile images
├── .gitignore
├── LICENSE
└── README.md
- HTTPS using locally trusted SSL certificates.
- Secure, signed Flask session cookies.
HttpOnlysession cookies.Securecookie flag.SameSite=Laxcookie protection.- Password hashing using Werkzeug.
- SQLite-backed persistent user storage.
- Protected profile image uploads using
secure_filename().
This project is licensed under the MIT License. See the LICENSE file for details.