Skip to content

Commit 24bc8a8

Browse files
committed
fix(producer): secure Lambda font cache directory
1 parent 7862809 commit 24bc8a8

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

packages/producer/src/services/deterministicFonts.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createHash } from "node:crypto";
2-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
33
import { homedir, tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { defaultLogger } from "../logger.js";
@@ -581,17 +581,21 @@ function warnUnresolvedFonts(unresolved: string[]): void {
581581
// Google Fonts on-demand fetch + local cache
582582
// ---------------------------------------------------------------------------
583583

584-
// On AWS Lambda `$HOME` resolves to a `/home/sbx_*` tree that's
585-
// read-only; only `/tmp` is writable. Route the cache there when
586-
// running inside Lambda, and honor `HYPERFRAMES_FONT_CACHE_DIR` as
587-
// an explicit override for any environment.
584+
let lambdaFontCacheRoot: string | undefined;
585+
586+
// On AWS Lambda `$HOME` resolves to a `/home/sbx_*` tree that's read-only;
587+
// only `/tmp` is writable. Create one private, unguessable cache directory per
588+
// warm process and reuse it across invocations. Honor HYPERFRAMES_FONT_CACHE_DIR
589+
// as an explicit override for any environment.
588590
function resolveFontCacheRoot(): string {
589-
return (
590-
process.env.HYPERFRAMES_FONT_CACHE_DIR ??
591-
(process.env.AWS_LAMBDA_FUNCTION_NAME
592-
? join(tmpdir(), "hyperframes", "fonts")
593-
: join(homedir(), ".cache", "hyperframes", "fonts"))
594-
);
591+
if (process.env.HYPERFRAMES_FONT_CACHE_DIR) {
592+
return process.env.HYPERFRAMES_FONT_CACHE_DIR;
593+
}
594+
if (process.env.AWS_LAMBDA_FUNCTION_NAME) {
595+
lambdaFontCacheRoot ??= mkdtempSync(join(tmpdir(), "hyperframes-fonts-"));
596+
return lambdaFontCacheRoot;
597+
}
598+
return join(homedir(), ".cache", "hyperframes", "fonts");
595599
}
596600

597601
// Chrome UA triggers woff2 responses from Google Fonts CSS API

0 commit comments

Comments
 (0)