001_hardening.sql 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. -- Migration 001 – brute-force throttling + collision-safe short links.
  2. -- Run once against an existing PackIt database:
  3. -- mysql -u <user> -p <your-database> < migrations/001_hardening.sql
  4. -- New installs get all of this from schema.sql and can skip it.
  5. -- 1. Failed-login log backing the throttle in admin/login.php.
  6. CREATE TABLE IF NOT EXISTS login_attempts (
  7. attemptID INT AUTO_INCREMENT PRIMARY KEY,
  8. ip VARCHAR(45) NOT NULL,
  9. username VARCHAR(50) NOT NULL,
  10. attempted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  11. INDEX idx_ip_time (ip, attempted_at),
  12. INDEX idx_user_time (username, attempted_at)
  13. );
  14. -- 2. Short links.
  15. --
  16. -- Rows whose hash is longer than 16 characters are leftovers from the original
  17. -- 64-character scheme. packing.php has rejected those links since the switch to
  18. -- 5-character hashes, so they are already dead; deleting them lets the column
  19. -- shrink. The table is only a cache of selection -> hash: any such link is
  20. -- recreated by re-picking the activities.
  21. DELETE FROM selection_hashes WHERE CHAR_LENGTH(hash) > 16;
  22. ALTER TABLE selection_hashes
  23. MODIFY hash VARCHAR(16) NOT NULL,
  24. MODIFY activity_ids VARCHAR(500) NOT NULL;
  25. -- Every new list looks its selection up by activity_ids; without this it is a
  26. -- full table scan.
  27. ALTER TABLE selection_hashes ADD INDEX idx_activity_ids (activity_ids);