cbor-x icon indicating copy to clipboard operation
cbor-x copied to clipboard

Add an option to disable the compiled reader

Open huw opened this issue 3 years ago • 3 comments

Hey there, I’ve been using cbor-x in Cloudflare Workers quite successfully for a while now, but ran into a niche error when trying to do some bulk work:

EvalError: Code generation from strings disallowed for this context
    at new Function (<anonymous>)

The Workers runtime spits this out because it disables the use of eval() and new Function(source) to avoid security issues.

I’ve patched the package locally (just removing the whole if (this.slowReads++ >= 3) block here, but it would be nice to have an option in the package that disables that code path (I also noticed another potential user was concerned about the security of it while researching this issue). I’m more than happy to eat the reduced performance.

huw avatar Mar 01 '23 06:03 huw

@huw That actually already exists, its available in cbor-x/dist/index-no-eval.cjs (and decode-only variant as well).

I suppose you might run into problems importing, I should probably add those to the exports of the cbor-x package as well.

kriszyp avatar Mar 04 '23 17:03 kriszyp

Ah--that's embarrassing! I should've read through the docs a bit more thoroughly.

Thanks for adding the variants though, that should make things a bit easier :) (no compilation issues on my side though)

huw avatar Mar 05 '23 00:03 huw

Hi, sorry to revive this but I had trouble using cbor-x in Cloudflare Workers, as well, and stumbled upon this issue. After importing cbor-x/dist/index-no-eval all that really came from that is that I would get a:

(error) ReferenceError: BlockedFunction is not defined

I'm not really sure what I'm doing wrong here. Additionally, the no-eval variants seem to not work at all work with TypeScript.

@huw's workaround worked fine for me. however, I'd rather not be in the habit of modifying node_modules if I can help it.

Aenigma avatar May 08 '23 01:05 Aenigma