debug icon indicating copy to clipboard operation
debug copied to clipboard

Fix exposing of .env variables in Create React App

Open moos opened this issue 7 years ago • 8 comments

Create React Apps have the nifty feature of getting environment variables either from the shell or .env files. These are then resolved and baked into the JS bundle by webpack.

When using debug, the entirety of your .env file gets exposed in the bundle, e.g.:

    function u() {
      var e;
      try {
        e = t.storage.debug
      } catch (e) {
      }
      return !e && "undefined" !== typeof r && "env" in r && (e = Object({
        NODE_ENV: "production",
        PUBLIC_URL: "",
        REACT_APP_FOO: "1"
      }).DEBUG), e
    }

This PR fixes that situation by moving the env access to a separate file that isn't accessed when in browser mode.

Also should resolve https://github.com/visionmedia/debug/issues/467#issuecomment-324054986.

moos avatar Apr 18 '18 06:04 moos

Coverage Status

Coverage increased (+1.2%) to 75.0% when pulling 3d42235dac35b1fb4f3b77c00589128759bd1202 on moos:fix-cra-env into 22f993216dcdcee07eb0601ea71a917e4925a30a on visionmedia:master.

coveralls avatar Apr 18 '18 06:04 coveralls

Coverage Status

Coverage increased (+1.2%) to 75.0% when pulling 3d42235dac35b1fb4f3b77c00589128759bd1202 on moos:fix-cra-env into 22f993216dcdcee07eb0601ea71a917e4925a30a on visionmedia:master.

coveralls avatar Apr 18 '18 06:04 coveralls

Coverage Status

Coverage increased (+2.2%) to 89.862% when pulling 86c9684b10dd508ac544ed70bf22350efdfa39c2 on moos:fix-cra-env into 5c7c61dc0df0db4eb5de25707d8cd1b9be1add4f on visionmedia:master.

coveralls avatar Apr 18 '18 06:04 coveralls

I'm :-1: on this. It's adding very specific checks where this is really a shortcoming in electron. Is this even still an issue? I know this is kind of an old PR.

Qix- avatar Jun 20 '18 19:06 Qix-

Yup - definitely still an issue. It's not an electron issue -- the code referenced above ends up in CRA bundle that's consumed by the browser, thereby exposing the entirety of CRA app's various .env files to the web site visitor -- clearly, an unintended (and hard-to-detect, unless you analyze the bundle -- I bet most app devs don't do that!) side-effect of .env + debug.

I'd be happy to submit an update if there is interest.

moos avatar Aug 30 '18 05:08 moos

Hi, sorry it took so long to get back. Could you rebase please? I'm okay with adding this check.

Qix- avatar Dec 13 '18 15:12 Qix-

holy 🐟 -- let's hope I don't have to do that again!

moos avatar Feb 03 '19 07:02 moos

Ping! Would hate to see this become stale again.

moos avatar Feb 27 '19 04:02 moos