Skip to content

Write HTML. Render video. Built for agents.

fix(media-use): skip a .env folder when loading HeyGen credentials (#4988)

Miguel Ángel5f3191bdc593
diff --git a/skills-manifest.json b/skills-manifest.json
index 6e2ef014cc..339bfe34a8 100644
--- a/skills-manifest.json
+++ b/skills-manifest.json
@@ -54,7 +54,7 @@
"files": 1
},
"media-use": {
- "hash": "e2e523d50ce6baec",
+ "hash": "1e767e3b7e2992a2",
"files": 104
},
"motion-graphics": {
diff --git a/skills/media-use/audio/scripts/lib/heygen.mjs b/skills/media-use/audio/scripts/lib/heygen.mjs
index 0886aead98..f1ec536f4a 100644
--- a/skills/media-use/audio/scripts/lib/heygen.mjs
+++ b/skills/media-use/audio/scripts/lib/heygen.mjs
@@ -17,13 +17,24 @@ export const HEYGEN_CLI_SOURCE_HEADERS = { "X-HeyGen-Source": "cli" };
// OAuth-only cli-source header above, which also gates the free allowance.
export const HEYGEN_CLIENT_SOURCE_HEADERS = { "X-HeyGen-Client-Source": "media-use" };
+// A missing `.env`, or a `.env` folder (some home dirs have one), is no env file: null. Read without checking first,
+// so the file cannot change between a check and the read.
+function envFileText(path) {
+ try {
+ return readFileSync(path, "utf8");
+ } catch (error) {
+ if (["ENOENT", "ENOTDIR", "EISDIR"].includes(error.code)) return null;
+ throw error;
+ }
+}
+
// Walk up ≤5 dirs from startDir; load the first .env (shell env always wins).
export function loadEnvFromDir(startDir) {
let dir = resolve(startDir);
for (let i = 0; i < 5; i++) {
- const envPath = join(dir, ".env");
- if (existsSync(envPath)) {
- for (const raw of readFileSync(envPath, "utf8").split("\n")) {
+ const text = envFileText(join(dir, ".env"));
+ if (text != null) {
+ for (const raw of text.split("\n")) {
let line = raw.trim();
if (!line || line.startsWith("#")) continue;
if (line.startsWith("export ")) line = line.slice(7).trim();
diff --git a/skills/media-use/audio/scripts/lib/heygen.test.mjs b/skills/media-use/audio/scripts/lib/heygen.test.mjs
index 22542f3552..0098240c8e 100644
--- a/skills/media-use/audio/scripts/lib/heygen.test.mjs
+++ b/skills/media-use/audio/scripts/lib/heygen.test.mjs
@@ -1,9 +1,9 @@
import { test } from "node:test";
import assert from "node:assert/strict";
-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { heygenAuthHeaders, heygenAuthMethod } from "./heygen.mjs";
+import { heygenAuthHeaders, heygenAuthMethod, loadEnvFromDir } from "./heygen.mjs";
function withCleanHeygenEnv(fn) {
const previousApiKey = process.env.HEYGEN_API_KEY;
@@ -101,3 +101,17 @@ test("heygenAuthMethod returns null with no credential at all", () => {
}
});
});
+
+test("loadEnvFromDir skips a .env folder and loads the .env file above it", () => {
+ const root = mkdtempSync(join(tmpdir(), "heygen-env-"));
+ const project = join(root, "project");
+ mkdirSync(join(project, ".env"), { recursive: true });
+ writeFileSync(join(root, ".env"), "MEDIA_USE_ENV_DIR_TEST=from-parent\n");
+ try {
+ loadEnvFromDir(project);
+ assert.equal(process.env.MEDIA_USE_ENV_DIR_TEST, "from-parent");
+ } finally {
+ delete process.env.MEDIA_USE_ENV_DIR_TEST;
+ rmSync(root, { recursive: true, force: true });
+ }
+});