Skip to content

DefinePlugin inserts entire object when desired object property is missing #15559

Closed

Description

@knightcode

Bug report

What is the current behavior?

We collect a bunch of secrets into an object and pass them into Typescript with the DefinePlugin:

const secrets = Object.assign({}, locals, defaults);

const config = {
   ...
    devtool: "source-map",
    resolve: {
      extensions: [".ts", ".tsx", ".js", ".jsx"],
      fallback: {
        crypto: false
      }
    },
    module: { ... },
    plugins: [
      new CKEditorWebpackPlugin({language: "en"}),
      new webpack.DefinePlugin({
        secrets: secrets
      })
    ],
   entry: {
      app: "./src/frontend/ts/main.tsx"
    },
    output: {
      path: path.join(__dirname, "public/js"),
      publicPath: "js/",
      filename: "[name].js",
      chunkFilename: "[name].js"
    },
    externals: {
      "react": "React",
      "react-dom": "ReactDOM"
    }
  };

And then refer to them individually like:

pushManager.subscribe({
      userVisibleOnly: true,
      applicationServerKey: secrets.VAPID_PUB_KEY
});

But, if a given property like VAPID_PUB_KEY, is missing, the entire secrets object is substituted in place of
just the secrets part of the expression, so that it ends up producing:

pushManager.subscribe({
      userVisibleOnly: true,
      applicationServerKey:  {  ...  }.VAPID_PUB_KEY
});

What is the expected behavior?

I would expect either an error to be thrown or for nothing to be substituted in the place.

I hope I don't have to express the potential for this to expose data that was never intended to be included in public javascript.

Other relevant information:
webpack version: 5.70.0
Node.js version: v16.13.0
Operating System: MacOS 12.1, Ubuntu 20.04

Activity

  1. sokra commented on Mar 28, 2022

    @sokra
    Member

    We can fix that case, but with DefinePlugin({ secrets }) you are saying replace the "secrets" variable with my secrets, do webpack is doing that. What you want to do instead is to define it more grandular via:

    DefinePlugin({
      "secrets.VAPID_PUB_KEY": JSON.stringify("something"),
      "secrets.OTHER": JSON.stringify("other")
    })

    so it only replaces the direct reference to a secret.

    @vankop will fix the case with secrets.VAPID_PUB_KEY nonetheless, but how should we behave in case of someCall(secrets)... (we will inline the whole object)

  2. webpack-bot commented on Jun 27, 2022

    @webpack-bot
    Contributor

    This issue had no activity for at least three months.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  3. added
    status: staleNo author response after a needs: request; close candidate
    on Jun 27, 2022
  4. vankop commented on Jun 30, 2022

    @vankop
    Contributor

    @sokra please take a look #15562

  5. removed
    status: staleNo author response after a needs: request; close candidate
    on Jun 30, 2022
  6. added
    status: staleNo author response after a needs: request; close candidate
    on Sep 30, 2022
  7. webpack-bot commented on Oct 15, 2022

    @webpack-bot
    Contributor

    Issue was closed because of inactivity.

    If you think this is still a valid issue, please file a new issue with additional information.

  8. removed
    status: staleNo author response after a needs: request; close candidate
    on Oct 17, 2022
  9. added
    status: staleNo author response after a needs: request; close candidate
    on Jan 17, 2023
  10. removed
    status: staleNo author response after a needs: request; close candidate
    on Feb 10, 2023
  11. added
    status: staleNo author response after a needs: request; close candidate
    on May 13, 2023
  12. moved this to Priority - Low in webpack 5/6on May 28, 2023
  13. knightcode commented on Apr 4, 2025

    @knightcode
    Author

    noticed this happening again

  14. alexander-akait commented on Apr 4, 2025

    @alexander-akait
    Member

    @knightcode Please provide reproducible test repo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: staleNo author response after a needs: request; close candidatewebpack-5Concerns the webpack 5 line

    Type

    No type

    Fields

    Priority

    None yet

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions