Skip to content

fix(google-cloud-bigquery): avoid regex ReDoS and fix lexer escapes - #19300

Draft
julianxu-goog wants to merge 1 commit into
mainfrom
fix/bigquery-regex-redos-lexer-escape
Draft

julianxu-goog wants to merge 1 commit into
mainfrom
fix/bigquery-regex-redos-lexer-escape

Conversation

@julianxu-goog

Copy link
Copy Markdown
Contributor

Fixes three regex issues in google-cloud-bigquery:

  1. _PROJECT_PREFIX_PATTERN (google/cloud/bigquery/_helpers.py): Replace (?P<project_id>\S+\:[^.]+) with ^(?P<project_id>[^:\s]+:[^.\s]+) so inputs with many colons do not trigger O(N^2) backtracking in _split_id.
  2. _extract_types (google/cloud/bigquery/dbapi/cursor.py): Add (?<!%) before (%*)% so long runs of % characters do not trigger O(N^2) backtracking while preserving identical matching behavior.
  3. PY_STRING (google/cloud/bigquery/magics/line_arg_parser/lexer.py): Fix raw-string escape sequence \. to \\. so escaped quotes and backslashes in --params string literals tokenize properly.

Testing

  • nox -r -s lint
  • nox -r -s "unit-3.14(test_type='unit')" (2967 passed, 2 skipped)

Fixes internal issue b/581933482

- Anchor `_PROJECT_PREFIX_PATTERN` and exclude colons/whitespace from
  its domain prefix to avoid polynomial backtracking in `_split_id`.
- Add a negative lookbehind `(?<!%)` before `(%*)%` in `_extract_types`
  to avoid polynomial backtracking on long runs of `%` characters.
- Fix raw-string escape matching (`\.` -> `\\.`) in `PY_STRING` inside
  `magics/line_arg_parser/lexer.py` so escaped quotes and backslashes
  tokenize properly.

Fixes internal issue b/581933482

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses potential Regular Expression Denial of Service (ReDoS) vulnerabilities and backtracking issues across several regex patterns. Specifically, it updates the project prefix pattern in _helpers.py to prevent polynomial backtracking on malformed IDs, refactors the percent-sign matching in cursor.py to avoid backtracking, and corrects the string escaping pattern in the lexer. Additionally, comprehensive unit tests are added to verify these fixes and ensure performance remains stable under pathological inputs. No review comments were provided, so there is no feedback to address.

This branch has not been deployed

No deployments
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.

1 participant