Skip to content

fix(sdk): add userId length limit to mitigate DoS attack risk - #7378

Merged
mattinannt merged 3 commits into
formbricks:mainfrom
bharathkumar39293:fix/userId-length-limit
Mar 3, 2026
Merged

mattinannt merged 3 commits into
formbricks:mainfrom
bharathkumar39293:fix/userId-length-limit

Conversation

@bharathkumar39293

Copy link
Copy Markdown
Contributor

Fixes #7375

Description

The userId parameter in endpoints like POST /api/v1/client/{environmentId}/displays and the JS SDK lacked explicit length validation. This could potentially allow arbitrarily large payloads (up to Next.js's 4MB limit), creating a DoS risk.

This PR adds a maximum length limit of 255 characters to the userId in both the Zod schemas and the JS SDK early rejection logic.

Changes

  • Added .max(255) to ZDisplayCreateInput in packages/types/displays.ts
  • Added .max(255) to ZResponseInput in packages/types/responses.ts
  • Added .max(255) to ZJsUserIdentifyInput and ZJsPersonSyncParams in packages/types/js.ts
  • Added an early rejection guard in setUserId within packages/js-core/src/lib/user/user.ts to prevent excessive payload sizes before the network call.
  • Added a comprehensive unit test in user.test.ts to verify the early rejection behavior.

Testing

  • Added a new test case: should reject userId longer than 255 characters and not send updates
  • Verified that when a userId exceeds 255 characters, an error is logged and UpdateQueue is not triggered.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 33a4423bba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/js-core/src/lib/user/user.ts Resolved
@coderabbitai

coderabbitai Bot commented Feb 26, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This pull request implements a maximum length constraint of 255 characters for userId fields across the codebase. Changes include adding runtime validation logic in the user module that checks userId length and returns early if exceeded, adding a corresponding test case to verify this behavior, and updating validation schemas in the types package to enforce the 255-character limit at the schema level for multiple user identification input types. These changes ensure consistent userId length validation throughout the application.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly describes the main change: adding a userId length limit to prevent DoS attacks, which aligns with all file modifications.
Description check ✅ Passed The PR description is mostly complete with issue reference, clear motivation, specific changes listed, and testing approach described. However, the 'How should this be tested?' section lacks detailed step-by-step instructions and the checklist is not filled out.
Linked Issues check ✅ Passed The PR successfully implements all key requirements from issue #7375: adds .max(255) constraints to ZDisplayCreateInput and ZJsUserIdentifyInput/ZJsPersonSyncParams Zod schemas, implements early rejection logic in setUserId, and includes comprehensive unit tests verifying the validation behavior.
Out of Scope Changes check ✅ Passed All changes directly support the DoS mitigation objective: userId validation in schemas, early rejection guard in SDK, and test coverage. No unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/js-core/src/lib/user/user.ts`:
- Around line 13-17: Currently the userId max length literal (255) is
duplicated; replace the local const MAX_USER_ID_LENGTH in user.ts with an
exported constant (export const MAX_USER_ID_LENGTH = 255) and update the check
in the function that references userId to use that exported symbol; update the
logger.error message to interpolate the exported constant rather than a
hardcoded number; then update any schema/validator modules to import
MAX_USER_ID_LENGTH instead of using their own literal so the runtime check
(user.ts), schema validators, and messages remain in sync.

In `@packages/types/js.ts`:
- Around line 101-104: ZJsActionInput currently allows an unbounded userId
(z.string().optional()), leaving a DoS vector; change the schema to enforce the
same cap as ZJsUserIdentifyInput by replacing the unbounded userId with
z.string().max(255).optional(). Also scan this file for any other schemas that
declare a userId (the other occurrences referenced around lines 147-149) and
apply the same max(255) constraint to them so all userId fields consistently
enforce the length limit.

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c6ebaea and 33a4423.

📒 Files selected for processing (4)
  • packages/js-core/src/lib/user/tests/user.test.ts
  • packages/js-core/src/lib/user/user.ts
  • packages/types/displays.ts
  • packages/types/js.ts

Comment thread packages/js-core/src/lib/user/user.ts Resolved
Comment thread packages/types/js.ts Resolved
@bharathkumar39293

Copy link
Copy Markdown
Contributor Author

Hey @Dhruwang! 👋

I've picked up #7375 and implemented the length limit fixes for userId.

Included in this PR:

Added .max(255) to the Zod schemas (ZDisplayCreateInput, ZResponseInput, ZJsUserIdentifyInput, ZJsPersonSyncParams).
Added an early-rejection guard directly in the JS SDK (user.ts) so we don't even make the network call if the ID is too long.
Added a test in user.test.ts to verify the early rejection.
The fix is ready for review.

Let me know if you need any adjustments or if I missed any other schemas!

@mattinannt
mattinannt self-requested a review February 27, 2026 15:04
mattinannt
mattinannt previously approved these changes Mar 3, 2026

@mattinannt mattinannt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bharathkumar39293 Thanks you for the PR :-)

The additional validation makes sense and the code looks good 💪

@mattinannt
mattinannt merged commit d939263 into formbricks:main Mar 3, 2026
6 of 10 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.

Add length limit to userId parameter to mitigate DoS attack risk

2 participants