fix: secure assistant generation telemetry
This commit is contained in:
1 parent
ca8163c061
commit
4060f9299f
11 files changed
+181
-10
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { createHmac, hkdfSync, timingSafeEqual } from "node:crypto";
|
||||
|
||||
import { env } from "@homarr/common/env";
|
||||
|
||||
const getGenerationTelemetrySecret = () =>
|
||||
Buffer.from(
|
||||
hkdfSync("sha256", Buffer.from(env.SECRET_ENCRYPTION_KEY, "hex"), "", "assistant-generation-telemetry", 32),
|
||||
);
|
||||
|
||||
const getGenerationTelemetryPayload = ({
|
||||
userId,
|
||||
threadId,
|
||||
generationId,
|
||||
}: {
|
||||
userId: string;
|
||||
threadId: string;
|
||||
generationId: string;
|
||||
}) => `${userId}\0${threadId}\0${generationId}`;
|
||||
|
||||
export const createAssistantGenerationAccessToken = (input: {
|
||||
userId: string;
|
||||
threadId: string;
|
||||
generationId: string;
|
||||
}) =>
|
||||
createHmac("sha256", getGenerationTelemetrySecret()).update(getGenerationTelemetryPayload(input)).digest("base64url");
|
||||
|
||||
export const verifyAssistantGenerationAccessToken = (
|
||||
input: { userId: string; threadId: string; generationId: string },
|
||||
accessToken: string,
|
||||
) => {
|
||||
const expected = Buffer.from(createAssistantGenerationAccessToken(input));
|
||||
const actual = Buffer.from(accessToken);
|
||||
return expected.length === actual.length && timingSafeEqual(expected, actual);
|
||||
};
|
||||
Reference in new issue
Block a user