Use message age to expire old incoming calls

This commit is contained in:
Jim Gustafson 2020-06-25 14:42:32 -07:00 committed by Scott Nonnenberg
parent 870a0d2b65
commit e87a0103cc
3 changed files with 35 additions and 5 deletions

View File

@ -165,11 +165,7 @@ export class CallingClass {
return;
}
const now = new Date();
const serverTimestamp = envelope.serverTimestamp
? envelope.serverTimestamp
: now.valueOf();
const messageAgeSec = Math.floor((now.valueOf() - serverTimestamp) / 1000);
const messageAgeSec = envelope.messageAgeSec ? envelope.messageAgeSec : 0;
RingRTC.handleCallingMessage(
remoteUserId,

1
ts/textsecure.d.ts vendored
View File

@ -372,6 +372,7 @@ export declare class EnvelopeClass {
// Note: these additional properties are added in the course of processing
id: string;
unidentifiedDeliveryReceived?: boolean;
messageAgeSec?: number;
}
// Note: we need to use namespaces to express nested classes in Typescript

View File

@ -369,6 +369,12 @@ class MessageReceiverInner extends EventTarget {
? envelope.serverTimestamp.toNumber()
: null;
// Calculate the message age (time on server).
envelope.messageAgeSec = this.calculateMessageAge(
headers,
envelope.serverTimestamp
);
this.cacheAndQueue(envelope, plaintext, request);
} catch (e) {
request.respond(500, 'Bad encrypted websocket message');
@ -385,6 +391,33 @@ class MessageReceiverInner extends EventTarget {
// tslint:disable-next-line no-floating-promises
this.incomingQueue.add(job);
}
calculateMessageAge(
headers: Array<string>,
serverTimestamp?: number
): number {
let messageAgeSec = 0; // Default to 0 in case of unreliable parameters.
if (serverTimestamp) {
// The 'X-Signal-Timestamp' is usually the last item, so start there.
let it = headers.length;
while (--it >= 0) {
const match = headers[it].match(/^X-Signal-Timestamp:\s*(\d+)\s*$/);
if (match && match.length === 2) {
const timestamp = Number(match[1]);
// One final sanity check, the timestamp when a message is pulled from
// the server should be later than when it was pushed.
if (timestamp > serverTimestamp) {
messageAgeSec = Math.floor((timestamp - serverTimestamp) / 1000);
}
break;
}
}
}
return messageAgeSec;
}
async addToQueue(task: () => Promise<void>) {
this.count += 1;