Skip to content

"Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src URL1 URL2 URL3 ... 'self' blob: 'nonce-xxxxxx-xxxxx-xxxxxx'"Β #955

Closed

Description

@maurice

Report a bug

πŸ”Ž Search Terms

"unsafe-eval is not an allowed source of script"

🧩 Context

  • ArkType version: 2.0.0-dev.12
  • TypeScript version (5.1+): 5.4.5
  • Other context you think may be relevant (JS flavor, OS, etc.):

πŸ§‘β€πŸ’» Repro

I'll explain the issue but I don't think I'll be able to provide a reproduction sorry...

I started using ArkType at work and it's working great in a local Vite app.

However once I deployed it to our UAT server it kills the app with the error above. So it's a total blocker for me and I'm going to have to undo a bunch of work 😒.

The stack trace is roughly (sorry I am forbidden from copy+pasting anything from my work machine to the public internet):

throwError (errors.js:5)
throwInternalError (errors.js:3)
DynamicFunction (functions.js:23)
compile (compilation.js:76)
compileScope (scope.js:348)
bindCompiledScope (scope.js:318)
export (scope.js:245)

So to me it looks creating functions from strings is simply not going to work in my case (my employer is rather security conscious and may have restricted our browsers with additional security controls?).

TBH I would expect security scanners in general to flag this as a potential problem, for obvious reasons.

Perhaps you'd consider providing a second (slower?) validator implementation? Don't worry if not of course!

That's probably what I'll end up doing myself TBH, since I'm already generating TypeScript types for the API I was hoping to cover with ArkType, so I might as well extend it to cover validation too 🀷

Activity

  1. added
    bug
    on May 17, 2024
  2. ssalbdivad commented on May 17, 2024

    @ssalbdivad
    Member

    This actually exists already! Try a structure like this:

    arkConfig.ts

    import { configure } from "arktype/config"
    
    configure({
    	jitless: true
    })

    entrypoint.ts

    // this must come first
    import "./arkConfig.js"
    
    import { type } from "arktype"
    
    // create and validate types with no jit!
    
    const user = type({
    	name: "string",
    	age: "number"
    })
    
    const out = user({
    	name: "David",
    	age: 30
    })

    This will be the first time this is tried in the wild so let me know how it goes πŸ™

  3. ssalbdivad commented on May 17, 2024

    @ssalbdivad
    Member

    I basically had to implement all validation logic in parallel between standard and precompiled TS for exactly this reason so I really hope it works 🀣

  4. maurice commented on May 17, 2024

    @maurice
    Author

    Awesome, that's amazing! This will save me a ton of extra work πŸ™

    I tried to do what you said but

    1. apparently there are no types for that import from arktype/config so I had to slap a // @ts-ignore on it πŸ˜•. I noticed there is also a config option for scope() so that is probably what I would use because I am describing an API which is factored into many smaller utility types.
    2. when I step through in the debugger I am still seeing calls to DynamicFunction's constructor, eg, you are using it from the library itself in a file called internal.ts in schemaScope({ ... }).export() which executes as the file is loaded. I assume this would also crash my app when deployed to our UAT servers. Perhaps that could be lazily evaluated after the user has had a chance to globally configure jitless?
  5. maurice commented on May 17, 2024

    @maurice
    Author

    In fact now I go back to the original bug in our UAT env, it's the same call (internals.ts) that is crashing the app

  6. maurice commented on May 17, 2024

    @maurice
    Author

    Since the jitless option already exists, I wonder if it could be auto-set following a sniff-test at load-time?

    I don't think it will work for me to try and set it in my entry point because my entry point is actually the last chunk of code in my compiled app bundle, below everything it depends on, like arktype.

    I'd have to modify my app's compiled bundle code and somehow inject the configure() when enough of ArkType has loaded but not the bits that call schemaScope(...).export()!

    Or maybe I'm overcomplicating it?

  7. ssalbdivad commented on May 17, 2024

    @ssalbdivad
    Member

    @maurice I'm not really sure the details of how your bundling process works nor do I have great intuitions about that in general since I've just been working on library code for a few years πŸ˜†

    I do recall bundled PWAs I worked on years ago leveraging this pattern. It should be similar to how .env files could be loaded by importing "dotenv/config" (see https://github.com/motdotla/dotenv).

    This worked the same way where your env vars would be loaded after the import resolves so you had to put it above your other imports. I'd expect it should be possible for your bundler to resolve that. Just make the first line of your primary entrypoint that arkConfig.ts import if possible.

    If there's a cheap check I can do that's not going to crash anywhere to see if a CSP will prevent me from using JIT, I'd definitely love to be able to use that to set the default value. Let me know if you find something!

  8. maurice commented on May 17, 2024

    @maurice
    Author

    Ok I found a place to put this configure code that does the trick, so I think I am unblocked for now....

    I added the line to one of the packages that doesn't directly depend on arktype itself, but has dependencies that depend on arktype. Which TBH doesn't make sense given what I said earlier about the compiled bundle having lower-level dependencies come first in the bundle. (I would expect that if package B depends on A which depends on arktype, then the order of code in the bundle would be arktype, A, B, but apparently not.) I reckon Vite/Rollup is making some judgement calls about what can go where and not necessarily obeying the same order as the code is defined in.

    But it feels really fragile because my app is a monorepo built from ~50 packages, and likely to grow, and as I said this problem is only reproducible in our UAT env, so it's possible that I or someone else could make a change that causes the configure to appear after the arktype internals.ts is loaded 🀷

    In terms of cheap tricks I was thinking simply

    try {
      new Function('test', 'console.log("it works")');
    } catch (e) {
      configure({ jitless: true });
    }

    Funnily enough I am now on my home/personal machine and I tried that in the browser debugger and see the same thing!

    image


    ADDED IN EDIT: it's also not clear to me that if I call configure({ jitless: true }) say in a unit test setup function that it'll actually use my setting globally, or whether the arktype internal types have already loaded and used the non-Function approach. Basically if I'm going to go jitless, then I'd better do that everywhere to make sure I'm unit testing the same code I'm running in prod!

  9. maurice commented on May 17, 2024

    @maurice
    Author

    And...

    image

    Edit: since I apparently can't actually create a function at all like this, I suggest you don't use my example code because I actually don't know what the function body should look like: is that ^^ even a valid function body when done this way?

  10. ssalbdivad commented on May 17, 2024

    @ssalbdivad
    Member

    @maurice Yeah the idea would be the same as dotenv where both in dev and in prod you'd want to load that config before anything runs.

    That said, I've tested the behavior fairly extensively and it should be very close to identical between the two versions.

    Ideally I'd rather avoid try/catch as its fairly expensive but I suppose as a one-time cost on initialization it could be doable.

    I'll add something like this to the next release to ensure that people don't have to go through this process whenever they want to run in cloudflare or whatever.

  11. maurice commented on May 17, 2024

    @maurice
    Author

    Ok that would be amazing and save me and probably others the pain of having to manually configure jitless πŸ™

  12. ssalbdivad commented on May 17, 2024

    @ssalbdivad
    Member

    @maurice Will ship with 2.0.0-dev.14!

  13. moved this from To do to Implemented in arktypeioon May 17, 2024
  14. maurice commented on May 22, 2024

    @maurice
    Author

    Thanks @ssalbdivad πŸ™ I'll give it a try when it's published πŸ‘

  15. ssalbdivad commented on May 22, 2024

    @ssalbdivad
    Member

    @maurice You can try now with 2.0.0-dev.14! Hopefully it will default to JIT-less in environments that don't support new Function.

  16. moved this from Implemented to Done (merged or closed) in arktypeioon May 22, 2024
  17. maurice commented on Jun 5, 2024

    @maurice
    Author

    @ssalbdivad sorry for the late feedback but wanted to say your change to auto-detect CSP is working perfectly in our UAT environment πŸ™

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions