Skip to content

fix: filter object env vars to prevent [object Object] in subprocess env#6080

Open
aviu16 wants to merge 1 commit intoUnitech:masterfrom
aviu16:fix/object-env-vars
Open

fix: filter object env vars to prevent [object Object] in subprocess env#6080
aviu16 wants to merge 1 commit intoUnitech:masterfrom
aviu16:fix/object-env-vars

Conversation

@aviu16
Copy link

@aviu16 aviu16 commented Feb 13, 2026

Summary

  • Fixes Bug: PM2 passes object properties as "[object Object]" environment variables to subprocesses #6073: PM2 passes object properties as [object Object] environment variables to subprocesses
  • Adds Utility.sanitizeEnv() to filter pm2_env before passing to child_process.spawn() in ForkMode, keeping only primitive values (string, number, boolean)
  • Adds Utility.isEnvSafeValue() helper to check if a value is safe for env var usage
  • The 6 polluted env vars (env, axm_monitor, axm_options, axm_dynamic, axm_actions, node_args) are now properly excluded from the spawn environment

Root Cause

In lib/God/ForkMode.js, the entire pm2_env object (which contains nested objects) was passed directly as the env option to child_process.spawn(). Node.js spawn() calls .toString() on all env values, converting objects to the string "[object Object]".

Fix

Filter the environment object before passing it to spawn(), keeping only string, number, and boolean values. Numbers and booleans are explicitly converted to strings via String(). Objects, arrays, null, undefined, and functions are excluded.

Note: ClusterMode.js already handles this correctly by using JSON.stringify(env_copy) (line 48), so no changes were needed there.

Test plan

  • Added 38 tests in test/programmatic/env_sanitization.mocha.js
    • Unit tests for Utility.sanitizeEnv() covering all value types
    • Unit tests for Utility.isEnvSafeValue() helper
    • PM2-specific scenario tests verifying the 6 known object keys are filtered
    • Edge case tests (null input, empty objects, NaN, Infinity, Date, RegExp)
    • Integration tests verifying forked processes start correctly with sanitized env
  • All 38 tests passing
  • Verified no [object Object] values appear in sanitized output

@CLAassistant
Copy link

CLAassistant commented Feb 13, 2026

CLA assistant check
All committers have signed the CLA.

@aviu16 aviu16 force-pushed the fix/object-env-vars branch from 5f03fd9 to 3f21d4d Compare February 13, 2026 19:30
@aviu16
Copy link
Author

aviu16 commented Feb 13, 2026

recheck

When PM2 spawns processes via ForkMode, the entire pm2_env object
(containing nested objects like axm_monitor, axm_options, axm_dynamic,
axm_actions, env, node_args) is passed directly to child_process.spawn()
as the env option. Node.js spawn() calls .toString() on all env values,
converting these objects to "[object Object]" strings.

This adds a Utility.sanitizeEnv() function that filters the environment
object to only include primitive values (string, number, boolean) before
passing it to spawn(). Numbers and booleans are explicitly converted to
strings, while objects, arrays, null, undefined, and functions are
excluded.

Fixes Unitech#6073
@aviu16 aviu16 force-pushed the fix/object-env-vars branch from 3f21d4d to 5f18cb2 Compare February 15, 2026 23:43
@aviu16
Copy link
Author

aviu16 commented Feb 17, 2026

quick ping here. if this needs adjustments, i can update fast.

@aviu16
Copy link
Author

aviu16 commented Feb 17, 2026

validated this branch with the dedicated sanitization suite:\n./node_modules/.bin/mocha test/programmatic/env_sanitization.mocha.js\n\nresult: 38 passing.\n\nthis includes integration assertions that forked processes start without [object Object] env pollution while preserving user-defined primitive env vars.

@aviu16
Copy link
Author

aviu16 commented Feb 22, 2026

Friendly bump on this PR. When maintainers have a moment, could someone please take a look? Happy to make any requested changes quickly. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: PM2 passes object properties as "[object Object]" environment variables to subprocesses

2 participants