fix(security): harden hashing in image-proxy (#5188)
This commit is contained in:
1 parent
a9085399c0
commit
5723da21aa
1 file changed
+18
-26
@@ -1,6 +1,7 @@
|
|||||||
import { createHash, randomBytes } from "node:crypto";
|
import { createHmac, hkdfSync } from "node:crypto";
|
||||||
|
|
||||||
import { createId } from "@homarr/common";
|
import { createId } from "@homarr/common";
|
||||||
|
import { env } from "@homarr/common/env";
|
||||||
import { decryptSecret, encryptSecret } from "@homarr/common/server";
|
import { decryptSecret, encryptSecret } from "@homarr/common/server";
|
||||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
||||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||||
@@ -9,29 +10,22 @@ import { createGetSetChannel } from "@homarr/redis";
|
|||||||
|
|
||||||
const logger = createLogger({ module: "imageProxy" });
|
const logger = createLogger({ module: "imageProxy" });
|
||||||
|
|
||||||
const createHashChannel = (hash: `${string}.${string}`) => createGetSetChannel<string>(`image-proxy:hash:${hash}`);
|
const createHmacChannel = (hmac: `${string}.${string}`) => createGetSetChannel<string>(`image-proxy:hmac:${hmac}`);
|
||||||
const createUrlByIdChannel = (id: string) =>
|
const createUrlByIdChannel = (id: string) =>
|
||||||
createGetSetChannel<{
|
createGetSetChannel<{
|
||||||
url: `${string}.${string}`;
|
url: `${string}.${string}`;
|
||||||
headers: `${string}.${string}`;
|
headers: `${string}.${string}`;
|
||||||
}>(`image-proxy:url:${id}`);
|
}>(`image-proxy:url:${id}`);
|
||||||
const saltChannel = createGetSetChannel<string>("image-proxy:salt");
|
|
||||||
|
|
||||||
export class ImageProxy {
|
export class ImageProxy {
|
||||||
private static salt: string | null = null;
|
private static hmacKey: Buffer<ArrayBuffer> | null = null;
|
||||||
private async getOrCreateSaltAsync(): Promise<string> {
|
|
||||||
if (ImageProxy.salt) return ImageProxy.salt;
|
|
||||||
const existingSalt = await saltChannel.getAsync();
|
|
||||||
if (existingSalt) {
|
|
||||||
ImageProxy.salt = existingSalt;
|
|
||||||
return existingSalt;
|
|
||||||
}
|
|
||||||
|
|
||||||
const salt = randomBytes(32).toString("hex");
|
/**
|
||||||
logger.debug("Generated new salt for image proxy", { salt });
|
* Generate secure key for image-proxy from encryption key
|
||||||
ImageProxy.salt = salt;
|
*/
|
||||||
await saltChannel.setAsync(salt);
|
private static getHmacKey(): Buffer<ArrayBuffer> {
|
||||||
return salt;
|
this.hmacKey ??= Buffer.from(hkdfSync("sha256", env.SECRET_ENCRYPTION_KEY, "", "image-proxy", 32));
|
||||||
|
return this.hmacKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createImageAsync(url: string, headers?: Record<string, string>): Promise<string> {
|
public async createImageAsync(url: string, headers?: Record<string, string>): Promise<string> {
|
||||||
@@ -106,20 +100,18 @@ export class ImageProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async getExistingIdAsync(url: string, headers: Record<string, string> | undefined): Promise<string | null> {
|
private async getExistingIdAsync(url: string, headers: Record<string, string> | undefined): Promise<string | null> {
|
||||||
const salt = await this.getOrCreateSaltAsync();
|
const urlHash = this.hashSecret(url);
|
||||||
const urlHash = this.createDeterministicHash(url, salt);
|
const headerHash = this.hashSecret(JSON.stringify(headers ?? null));
|
||||||
const headerHash = this.createDeterministicHash(JSON.stringify(headers ?? null), salt);
|
|
||||||
|
|
||||||
const channel = createHashChannel(`${urlHash}.${headerHash}`);
|
const channel = createHmacChannel(`${urlHash}.${headerHash}`);
|
||||||
return await channel.getAsync();
|
return await channel.getAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async storeImageAsync(id: string, url: string, headers: Record<string, string> | undefined): Promise<void> {
|
private async storeImageAsync(id: string, url: string, headers: Record<string, string> | undefined): Promise<void> {
|
||||||
const salt = await this.getOrCreateSaltAsync();
|
const urlHash = this.hashSecret(url);
|
||||||
const urlHash = this.createDeterministicHash(url, salt);
|
const headerHash = this.hashSecret(JSON.stringify(headers ?? null));
|
||||||
const headerHash = this.createDeterministicHash(JSON.stringify(headers ?? null), salt);
|
|
||||||
|
|
||||||
const hashChannel = createHashChannel(`${urlHash}.${headerHash}`);
|
const hashChannel = createHmacChannel(`${urlHash}.${headerHash}`);
|
||||||
const urlHeaderChannel = createUrlByIdChannel(id);
|
const urlHeaderChannel = createUrlByIdChannel(id);
|
||||||
await urlHeaderChannel.setAsync({
|
await urlHeaderChannel.setAsync({
|
||||||
url: encryptSecret(url),
|
url: encryptSecret(url),
|
||||||
@@ -134,8 +126,8 @@ export class ImageProxy {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private createDeterministicHash(value: string, salt: string): string {
|
private hashSecret(value: string): string {
|
||||||
return createHash("sha256").update(`${salt}:${value}`).digest("hex");
|
return createHmac("sha256", ImageProxy.getHmacKey()).update(value).digest("hex");
|
||||||
}
|
}
|
||||||
|
|
||||||
private redactUrl(url: string): string {
|
private redactUrl(url: string): string {
|
||||||
|
|||||||
Reference in new issue
Block a user