Rewrite keys/upload OTK-collision 400 into synthetic 200
Rust crypto uses an in-memory store (no IndexedDB in Node), so after a restart it re-uploads one-time keys it already published. Synapse rejects the duplicate with a 400; the SDK never marks the request as sent, so it retries forever and the bot can never decrypt. Intercept that specific 400 and return a fake success with empty one_time_key_counts, which makes the machine mint fresh OTK ids that upload cleanly.
This commit is contained in:
28
index.js
28
index.js
@@ -14,6 +14,34 @@ const client = new MatrixClient({
|
|||||||
deviceId,
|
deviceId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The Rust crypto backend uses an in-memory store here (no IndexedDB in Node),
|
||||||
|
// so after a restart it 'forgets' one-time keys it already uploaded. Re-uploading
|
||||||
|
// an existing OTK id makes Synapse reject the request with a 400, the SDK never
|
||||||
|
// marks it as sent, and it retries forever — which blocks decryption entirely.
|
||||||
|
// Rewrite that specific 400 into a synthetic success: the empty count makes the
|
||||||
|
// Rust machine mint fresh OTK ids on the next tick, which then upload cleanly.
|
||||||
|
function installOtkCollisionWorkaround(client) {
|
||||||
|
const http = client.http;
|
||||||
|
const authedRequest = http.authedRequest.bind(http);
|
||||||
|
http.authedRequest = async function (method, path, queryParams, body, opts) {
|
||||||
|
try {
|
||||||
|
return await authedRequest(method, path, queryParams, body, opts);
|
||||||
|
} catch (err) {
|
||||||
|
if (
|
||||||
|
method === 'POST' &&
|
||||||
|
/\/keys\/upload$/.test(path) &&
|
||||||
|
err?.httpStatus === 400 &&
|
||||||
|
/already exists/.test(err?.message || '')
|
||||||
|
) {
|
||||||
|
console.warn('⚙️ One-time-key collision on keys/upload; telling Rust crypto to mint fresh keys');
|
||||||
|
return '{"one_time_key_counts":{}}';
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
installOtkCollisionWorkaround(client);
|
||||||
|
|
||||||
function loadQuotes() {
|
function loadQuotes() {
|
||||||
try {
|
try {
|
||||||
const quotesFile = './quotes.json';
|
const quotesFile = './quotes.json';
|
||||||
|
|||||||
Reference in New Issue
Block a user