cornucopia icon indicating copy to clipboard operation
cornucopia copied to clipboard

Raw Malicious Content Stored in Database Due to Missing Changeset Validation

Open khushal-winner opened this issue 8 months ago • 8 comments

Describe the bug
Stored malicious HTML/JS payloads (e.g. ) are accepted and saved in player/game names without any validation or sanitization.
Phoenix auto-escapes output so no XSS executes now, but raw dangerous content stays in the database.

Expected behavior
Player and game names should be validated/sanitized on save reject or strip HTML/JS tags and other unsafe characters.

Screenshot

Image

Desktop (please complete the following information):

  • OS: Windows / macOS / Linux
  • Browser: Chrome / Firefox / Edge
  • Version: Latest

Additional context

  • No content validation in changesets (only length/required checks).
  • Raw HTML/JS stored in DB → big risk if someone later uses raw() in templates, adds exports (CSV/JSON), emails, or admin views.
  • Current Phoenix escaping protects against immediate XSS, but this is a future-proofing / data integrity issue.

khushal-winner avatar Mar 07 '26 13:03 khushal-winner

@sydseter , if we expand in near future should we fix this?

khushal-winner avatar Mar 07 '26 13:03 khushal-winner

I've investigated this issue. The root cause is that both Player.changeset/2 and Game.changeset/2 in the Ecto schemas only validate presence and length of the name field, but do not reject HTML/JS content like <script>alert('XSS')</script>.

Fix: Add validate_format/4 to both changesets to reject names containing < or > characters. This prevents raw HTML/JS from being stored in the database while keeping the validation minimal and consistent with the existing changeset pattern.

The regex ~r/\A[^<>]*\z/ ensures the entire name string contains no angle brackets. Names like <script>alert('xss')</script> or <img src=x onerror=alert(1)> will be rejected with a clear error message.

Opening a PR shortly.

xovishnukosuri avatar Mar 14 '26 01:03 xovishnukosuri

Lets only allow characters based on this regexp:

"^[\u0600-\u06FF\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF\uFDF2\uFDF3\uFDF4\uFDFD\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF\uFF66-\uFF9Fー々〆〤\u3400-\u4DBF\uF900-\uFAFF\u0900-\u097F\u0621-\u064A\u0660-\u0669\u4E00-\u9FFF\u0E00-\u0E7F«»฿ฯ๏๚๛\u0400-\u04FF\u0500-\u052F\u2DE0-\u2DFF\uA640-\uA69FЮ́ю́Я́я́\u0370-\u03FF\u1F00-\u1FFFA-Za-zÀ-ÖØ-öø-ÿĀ-ž0-9._\--ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوي ًٌٍَُِّْٰﷲﷴﷺﷻ ٠١٢٣٤٥٦٧٨٩ \s]+$"

sydseter avatar Mar 19 '26 14:03 sydseter

This issue is for newcomers, I'm un-assigning myself from #2555 and #2557 right now. Both are super beginner-friendly (small changeset fixes), so perfect for someone new to jump in and learn Ecto/Phoenix security/best practices.

I'll still be around to review/help if anyone picks them up, but won't claim them myself, if anyone needs help ping me up

khushal-winner avatar Mar 19 '26 16:03 khushal-winner

@sydseter, can you assign this to me?

Suresh-Krishna-P avatar Mar 20 '26 12:03 Suresh-Krishna-P

@Suresh-Krishna-P You are assigned.

sydseter avatar Mar 20 '26 13:03 sydseter

The task here should be to implement input validation. I have supplied the regexp above.

sydseter avatar Apr 07 '26 17:04 sydseter

There is no need to clean the content from the db.

sydseter avatar Apr 07 '26 17:04 sydseter

Unasigning due to inactivity. Feel free to open a pull-request.

sydseter avatar May 29 '26 13:05 sydseter

It still lacks name-format validation in both player.ex and game.ex

sydseter avatar Jul 15 '26 22:07 sydseter

hii @sydseter, can i take this issue?

Adarshkumar0509 avatar Jul 22 '26 05:07 Adarshkumar0509

@Adarshkumar0509 Sure. Thank you for your time!

sydseter avatar Jul 22 '26 08:07 sydseter

Update :- I just started working on this sorry for delay.

Adarshkumar0509 avatar Aug 15 '26 12:08 Adarshkumar0509