Skip to content

Commit 45aac21

Browse files
refactor: move encryption key coercion into crypto.ts
Move the 33-zeros -> 32-zeros default key normalization out of the env schema and into encrypt()/decrypt() in crypto.ts, where the key is actually used. SOURCEBOT_ENCRYPTION_KEY is now a plain string in the env schema. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 1ba8c53 commit 45aac21

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

packages/shared/src/crypto.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ const generateIV = (): Buffer => {
1515
return crypto.randomBytes(ivLength);
1616
};
1717

18+
// @hack in our docker-compose.yml, we mistakenly used an encryption key with
19+
// _33_ zeros. As a hacky mechanism to fix peoples deployments without requiring
20+
// them to update their encryption key, we look for keys with this pattern and
21+
// coerce them into _32_ zeros (AES-256 requires a 32-byte key).
22+
// @see https://github.com/sourcebot-dev/sourcebot/commit/e30e75e7af96308b3b063bb3aed8369f5b15aa2e
23+
const coerceEncryptionKey = (key: string): string => {
24+
return key === "0".repeat(33) ? "0".repeat(32) : key;
25+
};
26+
1827
export function encrypt(text: string): { iv: string; encryptedData: string } {
19-
const encryptionKey = Buffer.from(env.SOURCEBOT_ENCRYPTION_KEY, 'ascii');
28+
const encryptionKey = Buffer.from(coerceEncryptionKey(env.SOURCEBOT_ENCRYPTION_KEY), 'ascii');
2029

2130
const iv = generateIV();
2231
const cipher = crypto.createCipheriv(algorithm, encryptionKey, iv);
@@ -28,7 +37,7 @@ export function encrypt(text: string): { iv: string; encryptedData: string } {
2837
}
2938

3039
export function decrypt(iv: string, encryptedText: string): string {
31-
const encryptionKey = Buffer.from(env.SOURCEBOT_ENCRYPTION_KEY, 'ascii');
40+
const encryptionKey = Buffer.from(coerceEncryptionKey(env.SOURCEBOT_ENCRYPTION_KEY), 'ascii');
3241

3342
const ivBuffer = Buffer.from(iv, 'hex');
3443
const encryptedBuffer = Buffer.from(encryptedText, 'hex');

packages/shared/src/env.server.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,8 @@ const options = {
341341
// The key is read as ASCII (1 char = 1 byte), so AES-256's 32-byte key
342342
// requirement means this must be exactly 32 characters. Generate one with
343343
// `openssl rand -base64 24` (24 random bytes => a 32-character base64 string).
344-
SOURCEBOT_ENCRYPTION_KEY: z.preprocess(
345-
// @hack in our docker-compose.yml, we mistakenly used a
346-
// encryption key with _33_ zeros. As a hacky mechanism to
347-
// fix peoples deployments without requiring them to update
348-
// their encryption key, we look for keys with this pattern
349-
// and coerce them into _32_ zeros.
350-
// @see https://github.com/sourcebot-dev/sourcebot/commit/e30e75e7af96308b3b063bb3aed8369f5b15aa2e
351-
(value) => value === "0".repeat(33) ? "0".repeat(32) : value,
352-
z.string().length(32, {
353-
message: "SOURCEBOT_ENCRYPTION_KEY must be exactly 32 characters (a 256-bit AES key). Generate one with `openssl rand -base64 24`.",
354-
}),
355-
),
344+
// @note: the key is normalized in shared/src/crypto.ts before use.
345+
SOURCEBOT_ENCRYPTION_KEY: z.string(),
356346
SOURCEBOT_INSTALL_ID: z.string().default("unknown"),
357347
SOURCEBOT_LIGHTHOUSE_URL: z.string().url().default("https://deployments.sourcebot.dev"),
358348

0 commit comments

Comments
 (0)