Skip to content

fix(js-express): resolve CodeQL rate limit, regex, and query findings#119

Merged
cbullinger merged 3 commits into
mainfrom
fix/codeql-js-express
Jun 8, 2026
Merged

fix(js-express): resolve CodeQL rate limit, regex, and query findings#119
cbullinger merged 3 commits into
mainfrom
fix/codeql-js-express

Conversation

@cbullinger

Copy link
Copy Markdown
Collaborator

Summary

Resolves 20 open CodeQL findings in the JS Express MFlix server by adding API rate limiting, escaping user genre input for regex queries, and validating MongoDB filter/update payloads before database writes.

Changes

  • Rate limiting (15 alerts): express-rate-limit middleware on all /api/movies routes via moviesRateLimiter
  • Regex injection (1 alert): escape user-supplied genre values before building $regex filters
  • NoSQL query injection (4 alerts): whitelist allowed filter fields/operators and update fields in batch and single update handlers via mongoQuery.ts

Testing

Related

Made with Cursor

Add rate limiting on movie routes, escape user genre input for regex
filters, and validate batch filter/update fields before MongoDB writes.

Co-authored-by: Cursor <cursoragent@cursor.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses CodeQL findings in the JS Express MFlix server by hardening request handling around /api/movies through rate limiting, safer regex construction, and sanitization of MongoDB filter/update payloads before DB writes.

Changes:

  • Added an express-rate-limit middleware applied to all /api/movies routes.
  • Escaped user-supplied genre input before building a case-insensitive $regex filter.
  • Introduced mongoQuery utilities to whitelist allowed MongoDB filter fields/operators and update fields, and applied them in single/batch update and batch delete handlers.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
mflix/server/js-express/tests/controllers/movieController.test.ts Updates batch update test data; (recommended) add a test covering newly rejected update operators.
mflix/server/js-express/src/utils/mongoQuery.ts New query/update sanitization helpers + regex escaping utility.
mflix/server/js-express/src/routes/movies.ts Applies the movies rate limiter middleware to all movie routes.
mflix/server/js-express/src/middleware/rateLimiter.ts New rate limiter configuration for movie routes.
mflix/server/js-express/src/controllers/movieController.ts Uses regex escaping for genre and sanitizes batch/single update + batch delete filters/updates.
mflix/server/js-express/package.json Adds express-rate-limit dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mflix/server/js-express/src/middleware/rateLimiter.ts
Comment thread mflix/server/js-express/src/utils/mongoQuery.ts
Comment thread mflix/server/js-express/src/controllers/movieController.ts Outdated
Comment thread mflix/server/js-express/src/controllers/movieController.ts Outdated
Comment thread mflix/server/js-express/src/controllers/movieController.ts
Comment thread mflix/server/js-express/tests/controllers/movieController.test.ts
Use createErrorResponse for rate limits, validate update payload shape,
return 400 for invalid ObjectId filters, skip whitespace-only genres,
and add tests for rejected batch update payloads.

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread mflix/server/js-express/src/utils/mongoQuery.ts Outdated
Comment thread mflix/server/js-express/src/utils/mongoQuery.ts Outdated
Comment thread mflix/server/js-express/src/middleware/rateLimiter.ts Outdated
Comment thread mflix/server/js-express/src/utils/mongoQuery.ts Outdated

@dacharyc dacharyc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of small suggested fixups for JS idiomaticity and consistency with the other apps, but otherwise LGTM ✅

Consolidate movie field allowlists, use generic filter/update error
messages, replace as-never assignment, and configure rate limit via
RATE_LIMIT_MAX env var in test setup.

Co-authored-by: Cursor <cursoragent@cursor.com>
@cbullinger

Copy link
Copy Markdown
Collaborator Author

Addressed review feedback in 204389a:

  1. Single field source of truthMOVIE_FIELDS drives both ALLOWED_FILTER_FIELDS (+ _id) and UPDATE_FIELDS
  2. Generic error messages — filter/update validation no longer echoes rejected field or operator names
  3. as never removed — uses (sanitized as Record<string, unknown>)[key] = update[key]
  4. Rate limit via envRATE_LIMIT_MAX (set to 10000 in unit + integration test setup) replaces NODE_ENV === "test" branch

@cbullinger cbullinger merged commit 6218d39 into main Jun 8, 2026
6 checks passed
@cbullinger cbullinger deleted the fix/codeql-js-express branch June 8, 2026 11:16
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.

3 participants