From 4285a05d19a8b246fbdcbad2ef66f186ed0b1ed7 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 31 Dec 2025 07:39:49 +0000 Subject: [PATCH] Fix getEnvArray to parse JSON when '[' present, CSV otherwise Simplifies the comma-separated parsing logic to: - If value contains '[', parse as JSON array - Otherwise, parse as comma-separated values This prevents incorrect splitting of arguments containing internal commas when there's only one argument. For arguments with commas, users should use JSON format: CHROME_ARGS='["--arg1,val", "--arg2"]' Also exports getEnvArray in module.exports for consistency. Co-authored-by: Nick Sweeting --- archivebox/plugins/chrome/chrome_utils.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/archivebox/plugins/chrome/chrome_utils.js b/archivebox/plugins/chrome/chrome_utils.js index def11874..263f2cbf 100755 --- a/archivebox/plugins/chrome/chrome_utils.js +++ b/archivebox/plugins/chrome/chrome_utils.js @@ -58,6 +58,15 @@ function getEnvInt(name, defaultValue = 0) { /** * Get array environment variable (JSON array or comma-separated string). + * + * Parsing strategy: + * - If value contains '[' anywhere, parse as JSON array + * - Otherwise, parse as comma-separated values + * + * This prevents incorrect splitting of arguments that contain internal commas. + * For arguments with commas, use JSON format: + * CHROME_ARGS='["--user-data-dir=/path/with,comma", "--window-size=1440,900"]' + * * @param {string} name - Environment variable name * @param {string[]} [defaultValue=[]] - Default value if not set * @returns {string[]} - Array of strings @@ -66,23 +75,18 @@ function getEnvArray(name, defaultValue = []) { const val = getEnv(name, ''); if (!val) return defaultValue; - // Try parsing as JSON array first - if (val.startsWith('[')) { + // If contains '[', parse as JSON array + if (val.includes('[')) { try { const parsed = JSON.parse(val); if (Array.isArray(parsed)) return parsed; } catch (e) { + console.error(`[!] Failed to parse ${name} as JSON array: ${e.message}`); // Fall through to comma-separated parsing } } - // Parse as comma-separated (but be careful with args that contain commas) - // For Chrome args, we split on comma followed by '--' to be safe - if (val.includes(',--')) { - return val.split(/,(?=--)/).map(s => s.trim()).filter(Boolean); - } - - // Simple comma-separated + // Parse as comma-separated values return val.split(',').map(s => s.trim()).filter(Boolean); } @@ -1314,6 +1318,7 @@ module.exports = { getEnv, getEnvBool, getEnvInt, + getEnvArray, parseResolution, // PID file management writePidWithMtime,