Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced getenv/putenv with $_ENV and unset #429

Closed
wants to merge 1 commit into from

Conversation

DracoBlue
Copy link

@DracoBlue DracoBlue commented Jan 16, 2023

The current version of google auth library php uses getenv/putenv to read and set environment variables in php. But the function putenv is not threadsafe, which made libraries (Dotenv https://github.com/symfony/dotenv/blob/6.2/Dotenv.php#L61 and phpdotenv https://github.com/vlucas/phpdotenv#putenv-and-getenv) to disable putenv support by default. Frameworks like symfony (deprecated in 4.3 symfony/symfony#31062 and removed in 5.0) and also in laravel there was a similiar solution to not use it anymore directly (vlucas/phpdotenv#76).

Thus here is a pull request to change all the ocurences of:

  • getenv($variable) to array_key_exists($variable, $_ENV) ? $_ENV[$variable] : false
  • putenv($variable) to unset($_ENV[$variable])
  • putenv($variable . "=") to $_ENV[$variable]=''
  • putenv($variable . "=" . $value) to $_ENV[$variable]=$value

Threads about this:

@DracoBlue DracoBlue requested a review from a team as a code owner January 16, 2023 14:29
@DracoBlue DracoBlue force-pushed the replace-getenv-with-env branch 2 times, most recently from 808a700 to 444a468 Compare January 16, 2023 15:05
@vishwarajanand vishwarajanand changed the title Replaced getenv/putenv with $_ENV and unset fix: migrating getenv/putenv to threadsafe alternatives Jan 17, 2023
@conventional-commit-lint-gcf
Copy link

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@vishwarajanand
Copy link
Contributor

@DracoBlue thanks for making this PR. I want to check once whether there was any reason for using getenv/putenv across all libraries.
If you can, pls fix the commit message according to conventional commit guidelines.

@DracoBlue DracoBlue changed the title fix: migrating getenv/putenv to threadsafe alternatives Replaced getenv/putenv with $_ENV and unset Jan 17, 2023
@DracoBlue
Copy link
Author

Done!

Copy link
Contributor

@bshaffer bshaffer left a comment

Choose a reason for hiding this comment

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

@DracoBlue Sorry for the confusing bot message, your previous PR title was correct, and it was just giving a warning to us (the administrators) in case we merged with the commit message instead of the PR title, which was in the incorrect format. I fixed this in #430, so this message will no longer happen.

As for this PR, unfortunately, the changes here break existing implementations. If someone is setting their credentials using putenv (as we've instructed them to do), this library would no longer work as expected. This is unacceptable.
I can see three options:

  1. Wait to make these changes in a v2 version
  2. Check getenv first and _ENV second. (This seems like it would cause even more issues)
  3. Provide a way (similar to symphony's usePutenv function, perhaps even use an environment variable as a flag 😛) to use the threadsafe version, e.g.:
$_ENV['GOOGLE_AUTH_USE_PUTENV']=false
$_ENV['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/credentials.json'';

There are some other implications of switching from getenv/putenv that aren't discussed in this PR, along with the implications of these functions not being threadsafe. Do you have any links to discussions of the implications?

@DracoBlue
Copy link
Author

DracoBlue commented Jan 18, 2023

Thanks for your reply! You are right.

There are some other implications of switching from getenv/putenv that aren't discussed in this PR, along with the implications of these functions not being threadsafe. Do you have any links to discussions of the implications?

I added some more threads related to this to the initial post.

Check getenv first and _ENV second. (This seems like it would cause even more issues)

I think so, too.

Provide a way (similar to symphony's usePutenv function, perhaps even use an environment variable as a flag 😛) to use the threadsafe version, e.g.:

I guess for this to be interesting enough to implement, I would suggest I am supplying a test case (I don't have a proper one, yet!) which suffers from the getenv/putenv threadsafety issue - and then we can think about a way to make it possible for those using the frameworks possible to supply the credentials variable in a safe way.

@bshaffer
Copy link
Contributor

I've moved this into a feature request here: #432

supplying a test case... which suffers from the getenv/putenv threadsafety issue

Yes! If you have one, or find one, please update the above issue!

I'm closing this for now since we will not be using this implementation.

@bshaffer bshaffer closed this Jan 25, 2023
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.

3 participants