-
-
Notifications
You must be signed in to change notification settings - Fork 887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to use RE2 (defaults on native regex engine) #1684
Conversation
Signed-off-by: Efe Barlas <ebarlas@purdue.edu>
Signed-off-by: Efe Barlas <ebarlas@purdue.edu>
Signed-off-by: Efe Barlas <ebarlas@purdue.edu>
Questions and notes:
|
Signed-off-by: Efe Barlas <ebarlas@purdue.edu>
I am not terribly excited about the idea of making node-re2 the dependency... I understand the problem though. A better approach could be to make an option Some other comments in code |
lib/vocabularies/code.ts
Outdated
const {useRE2} = opts | ||
if (u === "u" && useRE2) { | ||
try { | ||
const engine = new RE2(pattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's probably incorrect to call it "engine", RE2 is an engine, this one is probably just "regExp" or something...
lib/vocabularies/code.ts
Outdated
}) | ||
} catch (e) { | ||
self.logger.log( | ||
"Warning: One of the regexes in the schema is not supported by RE2. Falling back to the native regex engine" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this fallback would be just handled in the passed function
@efebarlas thanks - getting there! Possibly, having re2.ts file might create the expectation that re2 doesn't have to be imported separately... I guess it can be all covered in the docs. |
Signed-off-by: efebarlas <ebarlas@purdue.edu>
Tests added for code.regExp option
Signed-off-by: efebarlas <ebarlas@purdue.edu>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool - looks good - I can finalise
commit 1835f3517ffb750ea4c75ce3ee8d9c262374e8f4 Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat Nov 13 18:04:08 2021 +0000 simplify regExp option commit e7f1eb9 Merge: 98f04d3 f68ef8f Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat Nov 13 17:20:15 2021 +0000 Merge branch 'master' into master commit 98f04d3 Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat Nov 13 17:20:04 2021 +0000 Update docs/options.md commit 0ff99ed Merge: d9ea90c 8fccddb Author: Efe Barlas <43009963+efebarlas@users.noreply.github.com> Date: Wed Nov 10 00:15:33 2021 -0500 Merge branch 'master' into master commit d9ea90c Author: efebarlas <ebarlas@purdue.edu> Date: Wed Nov 10 00:09:17 2021 -0500 prettier:write to pass CI Signed-off-by: efebarlas <ebarlas@purdue.edu> commit b29cd91 Merge: f50eb43 20089ed Author: efebarlas <ebarlas@purdue.edu> Date: Tue Nov 9 21:54:45 2021 -0500 Merge branch 'master' of github.com:efebarlas/ajv Tests added for code.regExp option commit f50eb43 Author: efebarlas <ebarlas@purdue.edu> Date: Tue Nov 9 21:54:28 2021 -0500 Tests added Signed-off-by: efebarlas <ebarlas@purdue.edu> commit 20089ed Author: Efe Barlas <43009963+efebarlas@users.noreply.github.com> Date: Tue Nov 9 21:53:34 2021 -0500 Update options.md commit fd3e290 Merge: 41dd4bc 6ef0c66 Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun Sep 12 19:07:28 2021 +0100 Merge branch 'master' into master commit 41dd4bc Merge: 698f411 a9f38cd Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun Sep 12 11:35:20 2021 +0100 Merge branch 'master' into master commit 698f411 Author: Efe Barlas <ebarlas@purdue.edu> Date: Thu Aug 12 14:55:17 2021 -0400 dev-dependency to node-re2 added commit a0720f8 Author: Efe Barlas <ebarlas@purdue.edu> Date: Thu Aug 12 14:43:39 2021 -0400 re2 runtime lib + regExp code option added commit 1470c23 Author: Efe Barlas <ebarlas@purdue.edu> Date: Fri Jul 9 14:14:45 2021 -0400 variable name changes Signed-off-by: Efe Barlas <ebarlas@purdue.edu> commit 8f7ca34 Author: Efe Barlas <ebarlas@purdue.edu> Date: Fri Jul 9 13:22:38 2021 -0400 minor changes Signed-off-by: Efe Barlas <ebarlas@purdue.edu> commit 9791cce Author: Efe Barlas <ebarlas@purdue.edu> Date: Fri Jul 9 13:20:47 2021 -0400 remove comments Signed-off-by: Efe Barlas <ebarlas@purdue.edu> commit b07542d Author: Efe Barlas <ebarlas@purdue.edu> Date: Fri Jul 9 11:28:29 2021 -0400 added: RE2 Option with fallback Signed-off-by: Efe Barlas <ebarlas@purdue.edu>
commit 1835f3517ffb750ea4c75ce3ee8d9c262374e8f4 Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat Nov 13 18:04:08 2021 +0000 simplify regExp option commit e7f1eb9 Merge: 98f04d3 f68ef8f Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat Nov 13 17:20:15 2021 +0000 Merge branch 'master' into master commit 98f04d3 Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat Nov 13 17:20:04 2021 +0000 Update docs/options.md commit 0ff99ed Merge: d9ea90c 8fccddb Author: Efe Barlas <43009963+efebarlas@users.noreply.github.com> Date: Wed Nov 10 00:15:33 2021 -0500 Merge branch 'master' into master commit d9ea90c Author: efebarlas <ebarlas@purdue.edu> Date: Wed Nov 10 00:09:17 2021 -0500 prettier:write to pass CI Signed-off-by: efebarlas <ebarlas@purdue.edu> commit b29cd91 Merge: f50eb43 20089ed Author: efebarlas <ebarlas@purdue.edu> Date: Tue Nov 9 21:54:45 2021 -0500 Merge branch 'master' of github.com:efebarlas/ajv Tests added for code.regExp option commit f50eb43 Author: efebarlas <ebarlas@purdue.edu> Date: Tue Nov 9 21:54:28 2021 -0500 Tests added Signed-off-by: efebarlas <ebarlas@purdue.edu> commit 20089ed Author: Efe Barlas <43009963+efebarlas@users.noreply.github.com> Date: Tue Nov 9 21:53:34 2021 -0500 Update options.md commit fd3e290 Merge: 41dd4bc 6ef0c66 Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun Sep 12 19:07:28 2021 +0100 Merge branch 'master' into master commit 41dd4bc Merge: 698f411 a9f38cd Author: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun Sep 12 11:35:20 2021 +0100 Merge branch 'master' into master commit 698f411 Author: Efe Barlas <ebarlas@purdue.edu> Date: Thu Aug 12 14:55:17 2021 -0400 dev-dependency to node-re2 added commit a0720f8 Author: Efe Barlas <ebarlas@purdue.edu> Date: Thu Aug 12 14:43:39 2021 -0400 re2 runtime lib + regExp code option added commit 1470c23 Author: Efe Barlas <ebarlas@purdue.edu> Date: Fri Jul 9 14:14:45 2021 -0400 variable name changes Signed-off-by: Efe Barlas <ebarlas@purdue.edu> commit 8f7ca34 Author: Efe Barlas <ebarlas@purdue.edu> Date: Fri Jul 9 13:22:38 2021 -0400 minor changes Signed-off-by: Efe Barlas <ebarlas@purdue.edu> commit 9791cce Author: Efe Barlas <ebarlas@purdue.edu> Date: Fri Jul 9 13:20:47 2021 -0400 remove comments Signed-off-by: Efe Barlas <ebarlas@purdue.edu> commit b07542d Author: Efe Barlas <ebarlas@purdue.edu> Date: Fri Jul 9 11:28:29 2021 -0400 added: RE2 Option with fallback Signed-off-by: Efe Barlas <ebarlas@purdue.edu> Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
merged |
@epoberezkin and @efebarlas, passing a custom engine will also affect the validation of |
After some experimenting, I don't think the validation of format: 'regex' is affected by changing the engine, because it seems that regexes with features RE2 doesn't support, such as (?=.@.), pass validation when RE2 is used. I think with formats, we just use the default regex engine. I might work on a patch for this at some point, but I'm unfortunately busy at the moment. |
Thank you! |
What is the import Ajv from 'ajv';
import RE2 from 're2';
const ajv = new Ajv({ code: { regExp: RE2 } });
|
@reed-lawrence , can you clarify whether you are asking "What is the purpose of this feature?" or something else? |
@davisjam Thanks for the reply. I guess I am wondering if the declared interface is correct or how to reconcile that the code sample in the documentation for ReDoS attack mitigation does not build. What would be the proper integration of RE2 given the error posted in my prior comment? Is the |
Signed-off-by: Efe Barlas ebarlas@purdue.edu
What issue does this pull request resolve?
#1683
What changes did you make?
-I added an option on using RE2 or the native regex engine. This option defaults on the native regex engine.
-I added a dependency to node-re2. The unpacked size of node-re2 is 1.26 MB, but I do not know the minified size.
-I changed the 'usePattern' function to read the option, and to try and compile the regex in RE2. If RE2 throws an error, the system produces a log message, and falls back to the native regex engine.
Is there anything that requires more attention while reviewing?
-I haven't added tests which demonstrates the correctness of regex validation when RE2 is used. I'm waiting for a green light from the maintainers of ajv to do so.
Answer to this comment:
-When RE2 is enabled, some test cases in json-schema-test fail, because RE2 and ECMA 262, section 15.10.1 differ in their definition of whitespace (JS includes \v, the other does not).