mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-11 09:10:13 +01:00
f4088d9348
* WIP integrity check between processes * Implement integrity checking on document processor payloads
43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
const crypto = require("crypto");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const keyPath =
|
|
process.env.NODE_ENV === "development"
|
|
? path.resolve(__dirname, `../../../server/storage/comkey`)
|
|
: path.resolve(process.env.STORAGE_DIR, `comkey`);
|
|
|
|
class CommunicationKey {
|
|
#pubKeyName = "ipc-pub.pem";
|
|
#storageLoc = keyPath;
|
|
|
|
constructor() {}
|
|
|
|
log(text, ...args) {
|
|
console.log(`\x1b[36m[CommunicationKeyVerify]\x1b[0m ${text}`, ...args);
|
|
}
|
|
|
|
#readPublicKey() {
|
|
return fs.readFileSync(path.resolve(this.#storageLoc, this.#pubKeyName));
|
|
}
|
|
|
|
// Given a signed payload from private key from /app/server/ this signature should
|
|
// decode to match the textData provided. This class does verification only in collector.
|
|
// Note: The textData is typically the JSON stringified body sent to the document processor API.
|
|
verify(signature = "", textData = "") {
|
|
try {
|
|
let data = textData;
|
|
if (typeof textData !== "string") data = JSON.stringify(data);
|
|
return crypto.verify(
|
|
"RSA-SHA256",
|
|
Buffer.from(data),
|
|
this.#readPublicKey(),
|
|
Buffer.from(signature, "hex")
|
|
);
|
|
} catch {}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = { CommunicationKey };
|