RocketChat / RocketChat/Rocket.Chat
Fix unsafe username cast in prepareMessageObject
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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