Skip to content

Fix DotEnv class turning export to empty string - #6625

Merged
paulbalandan merged 2 commits into
codeigniter4:developfrom
paulbalandan:dotenv-export
Oct 7, 2022
Merged

Fix DotEnv class turning export to empty string#6625
paulbalandan merged 2 commits into
codeigniter4:developfrom
paulbalandan:dotenv-export

Conversation

@paulbalandan

Copy link
Copy Markdown
Member

Description
Fixes #6621

I think the bug is on the incorrect sanitization of exported env variables with the syntax export varname=value.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 4, 2022
Comment thread system/Config/DotEnv.php
$value = trim($value);

// Sanitize the name
$name = str_replace(['export', '\'', '"'], '', $name);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the purpose of deleting these strings?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not entirely sure, but I think this was meant to make 'foo' or "foo" to just foo

@kenjis

kenjis commented Oct 4, 2022

Copy link
Copy Markdown
Member

Is export varname=value valid syntax for .env?
It seems no such explanation in the user guide.

@paulbalandan

Copy link
Copy Markdown
Member Author

Is export varname=value valid syntax for .env? It seems no such explanation in the user guide.

I think that was the intention.

@paulbalandan

Copy link
Copy Markdown
Member Author

Okay. Since there was mention that the code comes mostly from vlucas/phpdotenv, I checked the repo and found this code in v2.0.0
https://github.com/vlucas/phpdotenv/blob/058b9b03b945b6c3eced241935c45822fd2bf4e8/src/Loader.php#L249-L254

It seems our DotEnv class is based on an old code.

@MGatner MGatner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, an edge case I would like to see covered.

Comment thread tests/system/Config/fixtures/.env
@MGatner

MGatner commented Oct 4, 2022

Copy link
Copy Markdown
Member

Just saw your comment - should we use an actual package for this, either via Composer or unlined like we do with Kint? Seems like a high-risk security area to be based off old third-party code.

Co-authored-by: MGatner <mgatner@icloud.com>
@paulbalandan

Copy link
Copy Markdown
Member Author

Just saw your comment - should we use an actual package for this, either via Composer or unlined like we do with Kint? Seems like a high-risk security area to be based off old third-party code.

Well I'm in favor. Less maintenance burden on us.

@kenjis

kenjis commented Oct 4, 2022

Copy link
Copy Markdown
Member

What security concern is in DotEnv?
It seems a few methods were borrowed from vlucas/phpdotenv when we see the code comment.

Config is a part that has slowed down considerably with CI4, and I don't want to see it get any heavier.

@kenjis

kenjis commented Oct 4, 2022

Copy link
Copy Markdown
Member

It seems vlucas/phpdotenv support export varname=value:
https://github.com/vlucas/phpdotenv/blob/master/tests/fixtures/env/exported.env

But for what?

@MGatner MGatner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm good with this fix for now. I do agree that Config has gotten rather bloated, but I think DotEnv is distinct enough (and necessary) that I would be in favor of some research at least about outsourcing it.

@kenjis

kenjis commented Oct 6, 2022

Copy link
Copy Markdown
Member

This is not related to this PR, but I've found this PHP extention:
https://github.com/sjinks/php-ext-dotenv

The parser is compatible with that of Node.js dotenv version 10.0 (test cases use the same environment file to test parsing).

character.export.var=variable
export char.var=character
export char.exports=imports
fruit.export = "banana"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Before: $_ENV

    'char.expo.foo' => string (12) "exported foo"
    'character..var' => string (8) "variable"
    ' char.var' => string (9) "character"
    ' char.s' => string (7) "imports"
    'fruit.' => string (6) "banana"

After: $_ENV

    'char.expo.foo' => string (12) "exported foo"
    'character.export.var' => string (8) "variable"
    'char.var' => string (9) "character"
    'char.exports' => string (7) "imports"
    'fruit.export' => string (6) "banana"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Node.js dotenv@16.0.3:

  'char.expo.foo': 'exported foo',
  'character.export.var': 'variable',
  'char.var': 'character',
  'char.exports': 'imports',
  'fruit.export': 'banana'

It seems that export is handled the same way in Node dotenv as in this PR.

@kenjis kenjis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As the bug fix, this has no problem.

@paulbalandan
paulbalandan merged commit a5de551 into codeigniter4:develop Oct 7, 2022
@paulbalandan
paulbalandan deleted the dotenv-export branch October 7, 2022 01:34
@paulbalandan

Copy link
Copy Markdown
Member Author

Thanks for the reviews

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

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Unable to connect to the database when name contains word export

3 participants