Missing UNIQUE constraint on shorten column allows duplicate short codes and silent wrong redirects #8
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
The database migration creates the
shortencolumn without aUNIQUEindex. Therandom_str()method generates a random 9-character code but never checks for collisions before persisting the new row, and theLinksentity declares no unique constraint either.Location
migrations/Version20230703210818.php, line 22 —CREATE TABLEhas noUNIQUE KEYonshortensrc/Entity/Links.php, line 20 — no#[ORM\Column(unique: true)]src/Controller/Shortener.php, lines 29–35 — no collision check beforepersist/flushRisk
Under concurrent load (or with deliberate brute-force), two requests may independently generate the same code. Both would be inserted successfully (no constraint violation).
findOneBy(["shorten" => $code])inRedirectorreturns whichever row MySQL fetches first — silently sending users to the wrong destination with no error indication.Suggested fix direction
Add a
UNIQUEconstraint onshortenat both the database level (UNIQUE KEY) and in the Doctrine mapping (#[ORM\Column(unique: true)]). Wrap the insert in a retry loop that regenerates the code onUniqueConstraintViolationException.Severity
minor
Found by
Automated audit by Claude Code