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'
Fallback encoders and decoders were rather slow and large, so I decided to remove them entirely. The alternative here is generating static code instead.
Closing this issue for now as it hasn't received any replies recently. Feel free to reopen it if necessary!
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?
What's happening there is that the long module is require-d only if available in the environment.
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.
Sure. Not a big fan of the eval solution either. Just seemed to be the only workaround reliably preventing warnings and stuff in webpack.
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.
@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?
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;'
}
]
})
]
// ...
met same problem. Is there any long term solution?
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.
Most helpful comment
Any news about this issue?
I have the same problem.
My two cents here. The proposed solution can be improved by using
require.resolveinstead of expeting an exception if the module is not present.