Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Fix getEnvFromShell() to correctly handle newlines in env vars #17628

Merged
merged 7 commits into from
Sep 3, 2021

Conversation

brettle
Copy link
Contributor

@brettle brettle commented Jul 5, 2018

Description of the Change

Fix #17627 by using awk to portably separate env vars with \0 instead of \n.

getEnvFromShell() was supposed to copy the user's environment as it exists in a new shell. However, it did that by running env in that shell and using \n to split the output into var=value pairs. However, values can contain \n (e.g. exported bash functions) and such values will break the parsing. The fix is to have the var=value pairs separated by \0s. GNU's env -0 does that but won't work on Mac or BSD. Instead, we get the same result using a one line POSIX-compliant awk script.

Alternate Designs

I was going to just use env -0 until I realized that it was GNU-specific.

Why Should This Be In Core?

It fixes a bug in core.

Benefits

It fixes and prevents issues where a subprocess is started with an incorrect environment due to multiline environment variables.

Possible Drawbacks

If awk corresponds to GNU's awk, it will add AWKLIBPATH and AWKPATH to the environment (with default values) if they don't already exist.

Verification Process

I modified the existing spec to include multiline values and confirmed it worked.
I also confirmed that multiline values set in .bashrc are actually showing up correctly in process.env after the change was made.

Applicable Issues

Issue #17627

Fix atom#17627 by using awk to portably separate env vars with \0 instead
of \n.
@winstliu
Copy link
Contributor

winstliu commented Jul 5, 2018

Looks like you have linting errors @brettle.

@thomasjo
Copy link
Contributor

thomasjo commented Jul 5, 2018

@brettle Looks like the tests are failing in a consistent manner, and the failures definitely seem to be related to the changes in this PR. Let us know if you need any assistance with resolving the problems.

Use printf("...%c...", 0) instead of printf("...\0...") to inject \0.
@brettle
Copy link
Contributor Author

brettle commented Jul 5, 2018

@thomasjo Thanks for the offer, but it looks like I've got it fixed.

@drguthals drguthals mentioned this pull request Jul 12, 2018
23 tasks
@sadick254
Copy link
Contributor

Hey @brettle. Apologies for not looking into this on time. I know this PR has been here for a very long time, during that time Atom has seen a number of changes. Due to the changes, this PR has now become outdated. Is it possible for you to resolve the conflicts?

Copy link
Contributor

@sadick254 sadick254 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

@sadick254 sadick254 merged commit d04abd6 into atom:master Sep 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getEnvFromShell() does not handle newlines in environment variables correctly
4 participants