Ansar Shaikh

A small URL shortener where every security control has a test

· secure-shortener on GitHub

A URL shortener is a good small target for security practice: it takes untrusted URLs, redirects people to them, and has per-user data. I kept secure-shortener to about 250 lines so every control could have a test named after its OWASP Top 10 (2021) category.

Controls

CategoryControl
A01 Access controlEvery link query uses WHERE owner=?; missing and not-yours both return the same 404.
A02 Cryptoscrypt password hashes; 256-bit random tokens stored as SHA-256; 8-character codes from secrets.
A03 InjectionSQLite parameter binding everywhere, plus a code-format check.
A04 DesignToken bucket of 30 burst at 1/s per IP, returning 429 with Retry-After.
A05 MisconfigurationCSP, HSTS, X-Frame-Options DENY, nosniff; /docs disabled; no server header.
A07 Auth12+ character passwords, 5 logins per minute per user and IP, the same error for unknown user and wrong password, and a dummy hash so both paths cost the same.
A10 SSRF / redirectsOnly http and https; blocks loopback, RFC 1918, link-local (cloud metadata), IPv6 loopback, user:pass@ and self-loops.

Testing it

pytest runs 24 tests in 1.3 seconds. I also ran a smoke script against a live uvicorn server:

Writing the test name first ("which OWASP category does this belong to?") made it obvious when a control had no test, which is the gap I wanted to close.

← All posts