Separate eval
(new Function
) from sourceCode
generation (CodeGen
)
#2527
Labels
eval
(new Function
) from sourceCode
generation (CodeGen
)
#2527
What version of Ajv are you using?
8.17.1
What problem do you want to solve?
Currently, using AJV on sites with Content Security Policy (CSP) rules that disallow
unsafe-eval
is not feasible due to its reliance onnew Function
. This limitation is documented here: https://ajv.js.org/security.html#content-security-policyAnd the suggested approach around this limitation is a two-step process:
sourceCode
for the schema validation.sourceCode
as a standard.js
file, which can then be included alongside other JavaScript assets on the site.However, unfortunately, purely doing the first step—JUST generating the
sourceCode
—is currently impossible too. BecausecompileSchema()
generates thesourceCode
but immediately attempts to create thevalidate
function usingnew Function()
, which violates CSP rules:ajv/lib/compile/index.ts
Lines 167 to 172 in 82735a1
While in this two-step process, the
validate
function isn't needed at all, during the first step. (Additionally, this is an extra work).And the problem is in our case, we do the job in a service-worker which is subject to CSP regulations itself and so it can't execute
eval()
(ornew Function()
).What do you think is the correct solution to the problem?
Introduce a dedicated, pure function that generates only the
sourceCode
. This function should work independently ofcompileSchema
and should not attempt to create avalidate
function. Such a function would be compatible with CSP-bound environments and could be used by bothcompileSchema
and developers needingsourceCode
generation.Will you be able to implement it?
Probably yes.
The text was updated successfully, but these errors were encountered: