Skip to content

Commit

Permalink
fix: ensure sessionId on Sessions documents (#31487)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto committed Jan 23, 2024
1 parent 30183fd commit 36d793a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-parrots-count.md
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixed a bug where some sessions were being saved without a sessionId
32 changes: 18 additions & 14 deletions apps/meteor/app/api/server/api.ts
Expand Up @@ -108,6 +108,22 @@ const getRequestIP = (req: Request): string | null => {
return forwardedFor[forwardedFor.length - httpForwardedCount];
};

const generateConnection = (
ipAddress: string,
httpHeaders: Record<string, any>,
): {
id: string;
close: () => void;
clientAddress: string;
httpHeaders: Record<string, any>;
} => ({
id: Random.id(),
// eslint-disable-next-line @typescript-eslint/no-empty-function
close() {},
httpHeaders,
clientAddress: ipAddress,
});

let prometheusAPIUserAgent = false;

export class APIClass<TBasePath extends string = ''> extends Restivus {
Expand Down Expand Up @@ -569,14 +585,7 @@ export class APIClass<TBasePath extends string = ''> extends Restivus {

let result;

const connection = {
id: Random.id(),
// eslint-disable-next-line @typescript-eslint/no-empty-function
close() {},
token: this.token,
httpHeaders: this.request.headers,
clientAddress: this.requestIp,
};
const connection = { ...generateConnection(this.requestIp, this.request.headers), token: this.token };

try {
if (options.deprecationVersion) {
Expand Down Expand Up @@ -761,12 +770,7 @@ export class APIClass<TBasePath extends string = ''> extends Restivus {
const args = loginCompatibility(this.bodyParams, request);

const invocation = new DDPCommon.MethodInvocation({
connection: {
// eslint-disable-next-line @typescript-eslint/no-empty-function
close() {},
httpHeaders: this.request.headers,
clientAddress: getRequestIP(request) || '',
},
connection: generateConnection(getRequestIP(request) || '', this.request.headers),
});

let auth;
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/statistics/server/lib/SAUMonitor.ts
Expand Up @@ -146,7 +146,7 @@ export class SAUMonitorClass {

const searchTerm = this._getSearchTerm(data);

await Sessions.insertOne({ ...data, searchTerm, createdAt: new Date() });
await Sessions.createOrUpdate({ ...data, searchTerm });
}

private async _finishSessionsFromDate(yesterday: Date, today: Date): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/definition/externals/meteor/ddp-common.d.ts
Expand Up @@ -5,6 +5,7 @@ declare module 'meteor/ddp-common' {
class MethodInvocation {
constructor(options: {
connection: {
id: string;
close: () => void;
clientAddress: string;
httpHeaders: Record<string, any>;
Expand Down
25 changes: 15 additions & 10 deletions apps/meteor/server/models/raw/Sessions.ts
Expand Up @@ -1431,11 +1431,15 @@ export class SessionsRaw extends BaseRaw<ISession> implements ISessionsModel {
};
}

private isValidData(data: Omit<ISession, '_id' | 'createdAt' | '_updatedAt'>): boolean {
return Boolean(data.year && data.month && data.day && data.sessionId && data.instanceId);
}

async createOrUpdate(data: Omit<ISession, '_id' | 'createdAt' | '_updatedAt'>): Promise<UpdateResult | undefined> {
// TODO: check if we should create a session when there is no loginToken or not
const { year, month, day, sessionId, instanceId } = data;

if (!year || !month || !day || !sessionId || !instanceId) {
if (!this.isValidData(data)) {
return;
}

Expand Down Expand Up @@ -1588,16 +1592,17 @@ export class SessionsRaw extends BaseRaw<ISession> implements ISessionsModel {
sessions.forEach((doc) => {
const { year, month, day, sessionId, instanceId } = doc;
delete doc._id;

ops.push({
updateOne: {
filter: { year, month, day, sessionId, instanceId },
update: {
$set: doc,
if (this.isValidData(doc)) {
ops.push({
updateOne: {
filter: { year, month, day, sessionId, instanceId },
update: {
$set: doc,
},
upsert: true,
},
upsert: true,
},
});
});
}
});

return this.col.bulkWrite(ops, { ordered: false });
Expand Down

0 comments on commit 36d793a

Please sign in to comment.