Skip to content
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

Update ReDoS section of security.md to accommodate #1683 #1828

Merged
merged 4 commits into from Dec 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/security.md
Expand Up @@ -64,6 +64,33 @@ Following all these recommendation is not a guarantee that validation using of u
Certain regular expressions can lead to the exponential evaluation time even with relatively short strings.

Please assess the regular expressions you use in the schemas on their vulnerability to this attack - see [safe-regex](https://github.com/substack/safe-regex), for example.
By default, Ajv uses the regex engine built into Node.js. This engine has exponential worst-case performance. This performance (and ReDoS attacks) can be mitigated by using a linear-time regex engine. Ajv supports the use of a third-party regex engine for this purpose.
epoberezkin marked this conversation as resolved.
Show resolved Hide resolved

To use a third-party regex engine in Ajv, set the ajv.opts.code.regExp property to that regex engine during instantiation. Here we use Google’s RE2 engine as an example.

```

const Ajv = require(“ajv”)

var RE2 = require(“re2”)

const ajv = new Ajv({regExp:RE2})

epoberezkin marked this conversation as resolved.
Show resolved Hide resolved
```

For details about the interface of the `regexp` option, see options.md under the docs folder.

Although linear-time regex engines eliminate ReDoS vulnerabilities, changing a regex engine carries some risk, including:

- Minor changes in regex syntax.

- Minor changes in regex semantics. For example, RE2 always interprets regexes in Unicode, and disagrees with JavaScript in its definition of whitespace. To avoid regressions, develop and test your regexes in the same regex engine that you use in production.

- May not support some advanced features, such as lookaheads or backreferences.

- May be (minor) common-case performance degradation.

- May inflate size of distributable (e.g. RE2 includes a non-trivial C component).
epoberezkin marked this conversation as resolved.
Show resolved Hide resolved

::: warning ReDoS attack
Some formats that [ajv-formats](https://github.com/ajv-validator/ajv-formats) package implements use [regular expressions](https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts) that can be vulnerable to ReDoS attack.
Expand Down