open-telemetry / opentelemetry-js

OpenTelemetry JavaScript Client
https://opentelemetry.io
Apache License 2.0
2.74k stars 802 forks source link

use of eval is strongly discouraged #4987

Open lnmp4000 opened 1 month ago

lnmp4000 commented 1 month ago

Bundling with rollupjs generated the following error because of protobufjs usage.

(!) Use of eval is strongly discouraged
https://rollupjs.org/troubleshooting/#avoiding-eval
../node_modules/@protobufjs/inquire/index.js
10: function inquire(moduleName) {
11:     try {
12:         var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
                      ^
13:         if (mod && (mod.length || Object.keys(mod).length))
14:             return mod;

This is included via the following tree:

  └─┬ @opentelemetry/exporter-trace-otlp-http@0.53.0
    └─┬ @opentelemetry/otlp-transformer@0.53.0
      └── protobufjs@7.4.0

I don't understand why this is necessary, but it could potentially cause issues for code bundled via rollup.

https://rollupjs.org/troubleshooting/#avoiding-eval

Also see https://github.com/protobufjs/protobuf.js/issues/1754

pichlermarc commented 1 month ago

Hi @lnmp4000, thanks for reaching out. I was under the impression that this would actually get tree-shaken out for users of the @opentelemetry/exporter-trace-otlp-http package - but I'm not very familar with rollup so I'd be somewhat time consuming to come up with a repro for this. Maybe it's generating that warning before it's tree-shaken out?

Would you mind providing a small repro so I can investigate? :thinking: Is it an error or a warning that's generated by rollup?

Regardless of tree-shaking, we still use this in @opentelemetry/exporter-trace-otlp-proto and @opentelemetry/exporter-logs-otlp-proto and looking at the the issue you linked, my preferred solution would be to fix this upstream in the protobufjs repo as this seems to affect quite a few people...

pichlermarc commented 1 month ago

Would you mind providing a small repro so I can investigate? 🤔

Nevermind, I took some time to reproduce this :slightly_smiling_face: https://github.com/pichlermarc/repro-4987

Is it an error or a warning that's generated by rollup?

Looking at my reproducer it looks like the following is happening:

Therefore code using @opentelemetry/exporter-trace-otlp-http does not violate CSPs by using eval, but using @opentelemetry/exporter-trace-otlp-proto does, as the eval call will eventually end up in the final bundle.

There's a few ways we can partially/fully fix this:

lnmp4000 commented 1 month ago

@pichlermarc Thanks so much for looking at this. In my case, based on your research, It seems I can just ignore this warning for now as the eval code will not be in the final bundle.

pichlermarc commented 1 month ago

@pichlermarc Thanks so much for looking at this. In my case, based on your research, It seems I can just ignore this warning for now as the eval code will not be in the final bundle.

Yes, exactly. :slightly_smiling_face:

If you don't mind, I'll spin off two bugs from this issue to clarify the situation: