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

uses unsafe-eval #593

Closed
diracdeltas opened this issue Dec 29, 2016 · 14 comments
Closed

uses unsafe-eval #593

diracdeltas opened this issue Dec 29, 2016 · 14 comments

Comments

@diracdeltas
Copy link

protobuf.js version: 6.3.x

it appears that recent versions of protobufjs are using eval, which prevents the browserified library from working in web applications that have a Content Security Policy without unsafe-eval, such as is the case for Chrome extensions.

EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: default-src 'self'
diracdeltas added a commit to brave/sync that referenced this issue Dec 29, 2016
fix #11

i can't upgrade to the latest easily because of protobufjs/protobuf.js#593

auditors: @ayumi
@dcodeIO
Copy link
Member

dcodeIO commented Dec 29, 2016

Fallback encoders and decoders were rather slow and large, so I decided to remove them entirely. The alternative here is generating static code instead.

@dcodeIO
Copy link
Member

dcodeIO commented Jan 3, 2017

Closing this issue for now as it hasn't received any replies recently. Feel free to reopen it if necessary!

@dcodeIO dcodeIO closed this as completed Jan 3, 2017
@kalitine
Copy link

I'm generating static code, but still got this warning with RollupJS:

@protobufjs/inquire/index.js (12:18) Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification

Does it exist some compile option to prevent using eval?

@dcodeIO
Copy link
Member

dcodeIO commented Jul 12, 2017

See

What's happening there is that the long module is require-d only if available in the environment.

@kalitine
Copy link

I see. I'll take a closer look this weekend and see how to get rid of eval without breaking this functionality, if it's ok for you.

@dcodeIO
Copy link
Member

dcodeIO commented Jul 13, 2017

Sure. Not a big fan of the eval solution either. Just seemed to be the only workaround reliably preventing warnings and stuff in webpack.

@kalitine
Copy link

kalitine commented Jul 16, 2017

I wasn't able to find a common solution which muches all scenarios. This problem is about different bundle tools: Webpack, Rollup, Browserify etc.
For example in Rollup if the inquire function would like this:

function inquire(moduleName) {
     try {
         var mod = require(moduleName)
         if (mod && (mod.length || Object.keys(mod).length))
             return mod;
     } catch (e) {
       return null;
     } // eslint-disable-line no-empty
     return null;
 }

It's good enough to not have any errors or warnings on build. But Webpack understands this code and still will try to extract some information (https://webpack.js.org/guides/dependency-management/). I do not know much about Browserify, but I suppose to have this kind of problem too.

What is common among theses bundlers is that it is possible to specify externals.
Webpack: https://webpack.js.org/configuration/externals/
Browserify: https://benclinkinbeard.com/posts/external-bundles-for-faster-browserify-builds/#marking-dependencies-external
Rollup: just does not care about require, because is cares only about ES2015 modules.

Thus I think one of the solution would be to use require function as it without inquire and specify in documentation the externals dependencies of protobufjs. API consumers will be aware of those dependencies and would be able to specify it in their configuration specific to their bundle tool.

Of course this solution requires an additional step for API consumers if they do not want to see errors/warnings, but eliminates the eval problem and also makes clients to be aware of the externals depending on the environment.

Let me know how you see this problem. I wish to help to fix it.

@dignifiedquire
Copy link

@dcodeIO any thoughts on this? I am trying to switch to this library to get rid of unsafe-evals and this is an issue for me, maybe using long at all could be compile flag?

@kalitine
Copy link

kalitine commented Sep 5, 2017

Temporary solution could be to use a preprocessor. For example for Rollup is rollup-plugin-re:

import replace from 'rollup-plugin-re'
// ...
plugins: [
      replace({
        patterns: [
          {
            test: /eval.*\(moduleName\);/g,
            replace: 'undefined;'
          }
        ]
      })
    ]
// ...

@BichengWang
Copy link

met same problem. Is there any long term solution?

@alejandroclaro
Copy link

Any news about this issue?

I have the same problem.

My two cents here. The proposed solution can be improved by using require.resolve instead of expeting an exception if the module is not present.

@yvele
Copy link

yvele commented May 20, 2022

There is an easy workaround for eval like this one aws/amazon-chime-sdk-js#800 (comment) when using Webpack (or this when using Rollup)

But now I'm struggling with codegen that has the same problem: #1483 😕
Why is codegen used for reflection? 🤔 is it possible to replace it?

@paralin
Copy link

paralin commented Oct 4, 2022

@dcodeIO using eval throws a lot of errors in esbuild as well.

@MentalGear
Copy link

Is this eval trick still needed for require ? Maybe it was an issue in 2016 when this issue was introduced, but with modern build tools, it should be safe to replace. Using eval is the bigger hindrance since modern browsers and extensions do not allow unsafe-eval in their CSP (Content Security Policy).

Alternatively, I'd be great if at least the eval usage could be made option-able via a flag.

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

No branches or pull requests

9 participants