Skip to content

fix(lambda): avoid catastrophic regex backtracking in Java/C# CodeLens - #8887

Open
ofri-peretz wants to merge 1 commit into
aws:masterfrom
ofri-peretz:fix/codelens-regex-backtracking
Open

ofri-peretz wants to merge 1 commit into
aws:masterfrom
ofri-peretz:fix/codelens-regex-backtracking

Conversation

@ofri-peretz

Copy link
Copy Markdown

Problem

stripGenericsFromParams in javaCodeLensProvider.ts and csharpCodeLensProvider.ts uses a regex where one identifier can be split across the repeated group in many ways ([a-zA-Z_$][a-zA-Z0-9_$]*?[\s,]*? with an optional separator). When a parameter list has a < followed by a long identifier and no closing >, V8 backtracks exponentially. It runs on Java/C# source in the workspace, so one such method signature can freeze the extension host. Measured on Node: 24 characters after < already takes 150 ms (Java) / 316 ms (C#), and every additional character roughly doubles it.

Separately, getRandomString() builds its output from Math.random(), and four webviews (CloudFormation stack resources, stack events, diff, and Threat Composer) use it as the CSP script nonce. A CSP nonce should come from a cryptographically secure source. No exploitable path was found; this is hardening.

Solution

  • Rewrite both regexes so each repetition must begin after a separator, which removes the ambiguity. They accept exactly the same strings as before:
    • Java: /<\s*[a-zA-Z_$][a-zA-Z0-9_$]*(?:[\s,]+[a-zA-Z_$][a-zA-Z0-9_$]*)*[\s,]*>/g
    • C#: /<(?:\s*[a-zA-Z_@][a-zA-Z0-9._@]*(?:(?:,\s*|\s+)[a-zA-Z_@][a-zA-Z0-9._@]*)*[\s,]?)?>/g
  • getRandomString() picks characters with crypto.randomInt() (same signature, same alphabet; crypto is already imported in that file).
  • Changelog entry for the toolkit.

Verification

  • Differential fuzz: 300,000 random strings per language (alphabet <>aB1_$@ ,.\t, length 1 to 14), input.replace(old, '') vs input.replace(new, ''): 0 mismatches.
  • The new regexes run in about 1 ms on a 100k-character unclosed input, versus exponential time for the old ones.
  • I did not run the full VS Code test suite locally (it needs a full install and Electron); I'm relying on CI for the existing javaCodeLensProvider.test.ts / csharpCodeLensProvider.test.ts cases.

The regex was flagged by the secure-coding/no-redos-vulnerable-regex ESLint rule.


  • Treat all work as PUBLIC. Private feature/x branches will not be squash-merged at release time.
  • Your code changes must meet the guidelines in CONTRIBUTING.md.
  • License: I confirm that my contribution is made under the terms of the Apache 2.0 license.

AI disclosure: drafted with AI assistance (Claude); I reviewed and tested the change.

Problem:
The regexes that strip generics from Java and C# handler parameters let an
identifier be split across repetitions in many ways, so an unclosed generic
such as `List<aaaa...` backtracks exponentially and blocks the extension host.
getRandomString(), used for webview CSP nonces, draws from Math.random().

Solution:
- Rewrite both regexes so each repetition starts after a separator; they
  match the same strings, in linear time.
- Draw getRandomString() characters from crypto.randomInt().
@ofri-peretz
ofri-peretz requested a review from a team November 30, 2026 23:12
@amazon-inspector-ohio

Copy link
Copy Markdown

⏳ I'm reviewing this pull request for security vulnerabilities and code quality issues. I'll provide an update when I'm done

@github-actions

Copy link
Copy Markdown
  • This pull request modifies code in src/* but no tests were added/updated.
    • Confirm whether tests should be added or ensure the PR description explains why tests are not required.

@amazon-inspector-ohio

Copy link
Copy Markdown

✅ I finished the code review, and didn't find any security or code quality issues.

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