Skip to content

Commit 1db2df5

Browse files
committed
fix(common): simplify class name pattern to avoid backtracking
Replace the nested-quantifier regex with a single linear character class. SonarCloud flagged the previous pattern for possible catastrophic backtracking on large inputs. The character class rejects the same malformed entries (paths, URL-encoded spaces, synthetic lambda names) without the backtracking risk.
1 parent 6f64a17 commit 1db2df5

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

  • powertools-common/src/main/java/software/amazon/lambda/powertools/common/internal

powertools-common/src/main/java/software/amazon/lambda/powertools/common/internal/ClassPreLoader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public final class ClassPreLoader {
3434

3535
private static final Logger LOG = LoggerFactory.getLogger(ClassPreLoader.class);
3636

37-
// A binary class name is a series of dot-separated Java identifiers ($ is allowed for nested
38-
// classes). This filters out malformed entries such as runtime-synthetic lambda classes
39-
// (e.g. "com.example.Foo$$Lambda$1/0x0000...") and lines that contain a path or other junk,
40-
// none of which Class.forName can load.
41-
private static final Pattern BINARY_CLASS_NAME = Pattern.compile(
42-
"[\\p{L}_$][\\p{L}\\p{N}_$]*(\\.[\\p{L}_$][\\p{L}\\p{N}_$]*)*");
37+
// A binary class name contains only letters, digits, and the '.', '_' and '$' characters. This
38+
// filters out malformed entries such as runtime-synthetic lambda classes
39+
// (e.g. "com.example.Foo$$Lambda$1/0x0000...") and lines that contain a path or other junk
40+
// (e.g. a URL-encoded space "%20" followed by a file path), none of which Class.forName can
41+
// load. The character class is a single linear match, so it is not prone to backtracking.
42+
private static final Pattern BINARY_CLASS_NAME = Pattern.compile("[\\p{L}\\p{N}_$.]+");
4343

4444
private ClassPreLoader() {
4545
// Hide default constructor

0 commit comments

Comments
 (0)