Skip to content

Add a built-in RE2 implementation based on re2js#290

Open
jonbodner-buf wants to merge 11 commits intomainfrom
jonbodner/add_re2
Open

Add a built-in RE2 implementation based on re2js#290
jonbodner-buf wants to merge 11 commits intomainfrom
jonbodner/add_re2

Conversation

@jonbodner-buf
Copy link
Copy Markdown

The CEL spec says that its regular expressions meet the RE2 spec, but the existing ES implementation defaults to the regex implementation that's built into ES runtimes, which uses a backtracking implementation that has pathological cases susceptible to ReDOS attacks.

This PR adds a stripped-down version of RE2JS (https://github.com/le0pard/re2js) as a package, and integrates it into CEL as the default regex engine.

Copy link
Copy Markdown

@srikrsna srikrsna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments regarding the integration. I'll review the RE2 implementation in a second pass.

Comment thread packages/cel/src/checker.test.ts Outdated
Comment thread packages/cel/src/std/logic.ts Outdated
Comment thread packages/cel/src/std/logic.ts Outdated
/\\c[A-Z]/, // control character eg: /\cM\cJ/
/\\u[0-9a-fA-F]{4}/, // UTF-16 code-unit
/\\0(?!\d)/, // NUL
/\[\\b.*\]/, // Backspace eg: [\b]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this if it is re2?

Comment thread packages/cel/src/std/logic.ts Outdated
Comment on lines +60 to +66
// can probably delete this since the RE2 engine will already reject them, but keep for now
for (const invalidPattern of invalidPatterns) {
if (invalidPattern.test(pattern)) {
throw new Error(
`Error evaluating pattern ${pattern}, invalid RE2 syntax`,
);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should delete them

Comment thread packages/cel/src/std/logic.ts Outdated
}
const re = new RegExp(pattern, flags);
return re.test(this);
const re: RE2JS = RE2JS.compile(pattern, flagVal);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that flags are part of the syntax in RE2? Can we add support for them in the library instead of trying to identify them here? Or am I missing something?

cel-go also passes them directly to regex engine without any preprocessing: https://github.com/google/cel-go/blob/646cdc1728643aec9499e3a00236ef1007a5d3fa/common/types/string.go#L156

Comment thread packages/cel/package.json
"@bufbuild/re2": "0.4.0"
},
"devDependencies": {
"@unicode/unicode-16.0.0": "^1.6.16",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a peer dependency that is required?

Comment thread packages/cel/package.json
"peggy": "^5.0.6",
"peggy-ts": "github:hudlow/peggy-ts#v0.0.9",
"expect-type": "^1.3.0"
"unicode-property-value-aliases": "^3.9.0"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a peer dependency that is required?

Comment thread packages/re2/package.json
Comment on lines +46 to +47
"@unicode/unicode-16.0.0": "^1.6.16",
"unicode-property-value-aliases": "^3.9.0"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that these are added as dev dependencies in the cel package as well, will users end up needing them?

Comment thread package.json
Comment on lines +36 to +38
},
"dependencies": {
"@bufbuild/re2": "^0.1.0"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants