RocketChat / RocketChat/Rocket.Chat

Fix unsafe username cast in prepareMessageObject

Open Beginner friendly
#39,296 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug
Dominant language
TypeScript
Stars
46.1k
Forks
13.9k
Avg merge
3d 3h
Merged PRs (30d)
130

Description

Describe the bug / problem

In apps/meteor/app/lib/server/functions/sendMessage.ts, the helper prepareMessageObject currently accepts a user argument where username is optional, but then casts it to string:

export function prepareMessageObject(> message: Partial,> rid: IRoom['_id'],> user: { _id: string; username?: string; name?: string },> ): asserts message is IMessage {> // ...> const { _id, username, name } = user;> message.u = {> _id,> username: username as string, // unsafe cast> name,> };> }>

This breaks type safety and can lead to message.u.username being undefined at runtime if callers ever omit username.

Proposed solution

  • Require username in the user parameter type and remove the unsafe cast:
  • Change the signature to user: { _id: string; username: string; name?: string }.
  • Assign username directly in message.u.
  • Existing call sites already pass username, so this should not change runtime behavior.

Why this is safe

  • insertMessage already calls prepareMessageObject with user: Pick<IUser, '_id' | 'username'>.
  • sendMessage passes user as any, which remains assignable.
  • The change tightens types and removes a // FIXME without altering logic.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open apps/meteor/app/lib/server/functions/sendMessage.ts and inspect prepareMessageObject, then check the insertMessage and sendMessage call sites mentioned in the issue. The work is complete when the user parameter requires username as a string, the unsafe cast is gone, and TypeScript still accepts the existing callers.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.