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 · 9 comments

Comments

Projects
None yet
4 participants
@diracdeltas

diracdeltas commented Dec 29, 2016

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

Update to protobufjs 6.2.1
fix #11

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

auditors: @ayumi
@dcodeIO

This comment has been minimized.

Owner

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

This comment has been minimized.

Owner

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 Jan 3, 2017

@kalitine

This comment has been minimized.

kalitine commented Jul 12, 2017

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

This comment has been minimized.

Owner

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

This comment has been minimized.

kalitine commented Jul 13, 2017

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

This comment has been minimized.

Owner

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

This comment has been minimized.

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

This comment has been minimized.

dignifiedquire commented Sep 5, 2017

@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

This comment has been minimized.

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;'
          }
        ]
      })
    ]
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment