mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Meteor 3's server Accounts.createUser is createUserAsync. inviteUserToBoard
called it without await, so the new user's id was a Promise; findOneAsync
read the Promise as the empty selector {} and returned the FIRST user,
usually the site administrator. That user was added to the board and got
the invitee's language and invitedBoards, while the invitee was created but
not added. It was not FerretDB: any database did the same.
createUser and sendEnrollmentEmail are awaited (an enrollment email that
fails is logged, as the invitation email is), and the user found must be
the one just created. Every server ReactiveCache getter now refuses a
Promise as its id or selector, so a missing await fails loudly instead of
picking the first document. Tests: the guard and the reporter's case in
memory, a scan that no server file calls an async Accounts API without
await, and a browser test that inviting a new address adds exactly that new
account - it fails on the previous code.
Thanks to momosari and xet7 !
Fixes #6831.
40 lines
1.5 KiB
JavaScript
40 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
// A query given a Promise where an id or a selector belongs. findOneAsync()
|
|
// reads a Promise as the empty selector {}, so it answers with the FIRST
|
|
// document of the collection: #6831 invited a new user by email, missed the
|
|
// `await` on Meteor 3's async Accounts.createUser, and added the first user -
|
|
// the site administrator - to the board in their place. A missing `await`
|
|
// fails here, loudly, instead of quietly picking a document nobody asked for.
|
|
|
|
function isThenable(value) {
|
|
return !!value && (typeof value === 'object' || typeof value === 'function') &&
|
|
typeof value.then === 'function';
|
|
}
|
|
|
|
function assertNotPromiseSelector(selector, where) {
|
|
if (isThenable(selector)) {
|
|
throw new Error(`${where}: a Promise was given as the id or selector - a missing await`);
|
|
}
|
|
}
|
|
|
|
// Every get* of an object (ReactiveCacheServer, whose getters are all async)
|
|
// checks its first argument, and rejects as the getter itself would.
|
|
function guardSelectorMethods(api) {
|
|
for (const name of Object.keys(api)) {
|
|
const method = api[name];
|
|
if (!/^get[A-Z]/.test(name) || typeof method !== 'function') continue;
|
|
api[name] = function guardedGetter(selector, ...rest) {
|
|
try {
|
|
assertNotPromiseSelector(selector, `ReactiveCache.${name}`);
|
|
} catch (e) {
|
|
return Promise.reject(e);
|
|
}
|
|
return method.call(this, selector, ...rest);
|
|
};
|
|
}
|
|
return api;
|
|
}
|
|
|
|
module.exports = { isThenable, assertNotPromiseSelector, guardSelectorMethods };
|