Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
CVE-2021-41115: Use re2 for user-supplied linkifier patterns.
Zulip attempts to validate that the regular expressions that admins enter for linkifiers are well-formatted, and only contain a specific subset of regex grammar. The process of checking these properties (via a regex!) can cause denial-of-service via backtracking. Furthermore, this validation itself does not prevent the creation of linkifiers which themselves cause denial-of-service when they are executed. As the validator accepts literally anything inside of a `(?P<word>...)` block, any quadratic backtracking expression can be hidden therein. Switch user-provided linkifier patterns to be matched in the Markdown processor by the `re2` library, which is guaranteed constant-time. This somewhat limits the possible features of the regular expression (notably, look-head and -behind, and back-references); however, these features had never been advertised as working in the context of linkifiers. A migration removes any existing linkifiers which would not function under re2, after printing them for posterity during the upgrade; they are unlikely to be common, and are impossible to fix automatically. The denial-of-service in the linkifier validator was discovered by @erik-krogh and @yoff, as GHSL-2021-118.
- Loading branch information
Showing
6 changed files
with
101 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import re2 | ||
| from django.db import migrations | ||
| from django.db.backends.postgresql.schema import DatabaseSchemaEditor | ||
| from django.db.migrations.state import StateApps | ||
|
|
||
|
|
||
| def delete_re2_invalid(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: | ||
| options = re2.Options() | ||
| options.log_errors = False | ||
|
|
||
| RealmFilter = apps.get_model("zerver", "RealmFilter") | ||
| found_errors = False | ||
| for linkifier in RealmFilter.objects.all(): | ||
| try: | ||
| re2.compile(linkifier.pattern, options=options) | ||
| except re2.error: | ||
| if not found_errors: | ||
| print() | ||
| found_errors = True | ||
| print( | ||
| f"Deleting linkifier {linkifier.id} in realm {linkifier.realm.string_id} which is not compatible with new re2 engine:" | ||
| ) | ||
| print(f" {linkifier.pattern} -> {linkifier.url_format_string}") | ||
| linkifier.delete() | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("zerver", "0325_alter_realmplayground_unique_together"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| delete_re2_invalid, | ||
| reverse_code=migrations.RunPython.noop, | ||
| elidable=True, | ||
| ) | ||
| ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters