Skip to content

Commit 6ffb343

Browse files
Copilotunixfox
andcommitted
Fix config environment variable evaluation and secret key validation tests
Co-authored-by: unixfox <[email protected]>
1 parent b0c1e2f commit 6ffb343

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/lib/helpers/config.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ export const ConfigSchema = z.object({
1212
Deno.env.get("SERVER_UNIX_SOCKET_PATH") ||
1313
"/tmp/invidious-companion.sock",
1414
),
15-
secret_key: z.string().length(16).regex(
16-
/^[a-zA-Z0-9]+$/,
17-
"SERVER_SECRET_KEY contains invalid characters. Only alphanumeric characters (a-z, A-Z, 0-9) are allowed. Please generate a valid key using 'pwgen 16 1' or ensure your key contains only letters and numbers.",
18-
).default(
19-
Deno.env.get("SERVER_SECRET_KEY") || "",
20-
),
15+
secret_key: z.preprocess(
16+
(val) =>
17+
val === undefined
18+
? Deno.env.get("SERVER_SECRET_KEY") || ""
19+
: val,
20+
z.string().length(16).regex(
21+
/^[a-zA-Z0-9]+$/,
22+
"SERVER_SECRET_KEY contains invalid characters. Only alphanumeric characters (a-z, A-Z, 0-9) are allowed. Please generate a valid key using 'pwgen 16 1' or ensure your key contains only letters and numbers.",
23+
),
24+
).default(undefined),
2125
verify_requests: z.boolean().default(
2226
Deno.env.get("SERVER_VERIFY_REQUESTS") === "true" || false,
2327
),
@@ -133,7 +137,13 @@ export async function parseConfig() {
133137
console.log(errorMessage);
134138
if (err instanceof ZodError) {
135139
console.log(err.issues);
136-
throw new Error("Failed to parse configuration file");
140+
// Include detailed error information in the thrown error for testing
141+
const detailedMessage = err.issues.map((issue) =>
142+
`${issue.path.join(".")}: ${issue.message}`
143+
).join("; ");
144+
throw new Error(
145+
`Failed to parse configuration file: ${detailedMessage}`,
146+
);
137147
}
138148
// rethrow error if not Zod
139149
throw err;

src/tests/secret_key_validation_test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { parseConfig } from "../lib/helpers/config.ts";
99
Deno.test("Secret key validation in Invidious companion config", async (t) => {
1010
// Clean up any existing environment variables that might interfere
1111
Deno.env.delete("SERVER_SECRET_KEY");
12-
12+
1313
// Disable PO token generation for testing to avoid network calls
1414
Deno.env.set("JOBS_YOUTUBE_SESSION_PO_TOKEN_ENABLED", "false");
1515

@@ -25,10 +25,6 @@ Deno.test("Secret key validation in Invidious companion config", async (t) => {
2525
for (const key of validKeys) {
2626
// Set the environment variable for each test
2727
Deno.env.set("SERVER_SECRET_KEY", key);
28-
29-
// Debug: Check what environment variable is actually set
30-
const actualEnvValue = Deno.env.get("SERVER_SECRET_KEY");
31-
console.log(`Debug: Setting key "${key}", actual env value: "${actualEnvValue}"`);
3228

3329
try {
3430
const config = await parseConfig();

0 commit comments

Comments
 (0)