Skip to content

feat: sanitize all user inputs to prevent injection attacks#299

Merged
success-OG merged 2 commits intoDogStark:mainfrom
greatKhalifa-code:feat/input-sanitization
Apr 29, 2026
Merged

feat: sanitize all user inputs to prevent injection attacks#299
success-OG merged 2 commits intoDogStark:mainfrom
greatKhalifa-code:feat/input-sanitization

Conversation

@greatKhalifa-code
Copy link
Copy Markdown
Contributor

Implements global input sanitization across the backend API to prevent SQL injection and XSS attacks, with enforced
input length limits.

What's changed

  • backend/utils/sanitize.ts — Core sanitization utilities:

    • stripXss — removes HTML tags and dangerous patterns (javascript:, data:, on*= event handlers)
    • detectSqlInjection — regex detection of SQL keywords (SELECT, DROP, UNION, DELETE, --, /*, etc.)
    • truncate — enforces MAX_INPUT_LENGTH (10,000 chars) on any string
    • sanitizeValue — composes truncate → SQL check (throws) → XSS strip
    • sanitizeObject — recursively applies sanitization to all string values in objects and arrays
  • backend/middleware/sanitize.ts — sanitizeInputs Express middleware that sanitizes req.body, req.query, and
    req.params on every request; returns 400 INVALID_INPUT if SQL injection is detected.

  • backend/server/app.ts — sanitizeInputs wired globally after express.json(), before all route handlers. Also wired
    the pre-existing unused communityRouter.

  • backend/utils/tests/sanitize.test.ts — 27 passing unit tests.

Acceptance criteria

  • ✅ Inputs sanitized — all req.body, req.query, req.params strings are sanitized on every request
  • ✅ Attacks prevented — SQL injection returns 400, XSS tags are stripped before data reaches handlers
  • ✅ Input length limits — strings truncated to 10,000 characters maximum
    closes #90 Add input sanitization #159

- Add backend/utils/sanitize.ts with stripXss, detectSqlInjection,
  truncate, sanitizeValue, sanitizeObject utilities
- Add backend/middleware/sanitize.ts Express middleware sanitizing
  req.body, req.query, req.params; returns 400 on SQL injection
- Wire sanitizeInputs globally in app.ts after express.json()
- Wire communityRouter (pre-existing unused import fix)
- Add 27 unit tests covering XSS, SQL injection, length limits,
  recursive object sanitization, and middleware behavior
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 29, 2026

@greatKhalifa-code Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@success-OG success-OG merged commit 2340213 into DogStark:main Apr 29, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#90 Add input sanitization

2 participants