fix(detector): eliminate regex backtracking — resolve SonarCloud reliability gate#61
Merged
Merged
Conversation
SonarCloud S5998 (reliability: stack overflow on large input) + S5852 (security hotspot: ReDoS) both flagged the reluctant-outer quantifier `(?:\s++[^\n]*+\n)*?` in LQ_CREATE_TABLE_YAML and LQ_ADD_FK_YAML. The engine was free to backtrack through the reluctant `*?`. Rewrite with negative-lookahead + possessive `*+`: intermediate lines that would otherwise match the target key (tableName / baseTableName / referencedTableName) are excluded up front, so the outer match terminates deterministically exactly where the reluctant version did. No semantic change for valid Liquibase YAML. Adds a regression test exercising a 500-line pathological createTable block — completes in <1s on the rewritten patterns; the pre-fix reluctant walk would have scaled quadratically. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Summary
Only 2 open bugs remained in new code (SonarCloud queried directly): both
java:S5998(MAJOR — reliability) inSqlMigrationDetector.java, same root cause as the 2 unreviewedjava:S5852security hotspots on adjacent lines. Fixing the regex addresses both rule families simultaneously.Root cause
LQ_CREATE_TABLE_YAMLandLQ_ADD_FK_YAMLused a reluctant-outer quantifier(?:\s++[^\n]*+\n)*?to skip intermediate indented lines before the target key. The engine could backtrack through the reluctant*?on large input → quadratic/exponential runtime.Fix
Negative-lookahead + possessive
*+: intermediate lines that would match the target key (tableName/baseTableName/referencedTableName) are excluded up front, so the outer match terminates deterministically at the same point the reluctant version did. No semantic change for valid Liquibase YAML.Tests
changeSetwith createTable + FK)liquibaseYamlChangeSet_largeIntermediateContent_completesQuickly— 500-line pathological createTable block; detection completes in <1s post-fix (pre-fix would scale quadratically)Expected gate impact
new_reliability_ratingC → A (both S5998 bugs resolved)new_security_hotspots_reviewed97.5% → 100% (same regex patterns flagged by S5852 no longer match the vulnerable shape; if SonarCloud doesn't auto-clear them, they can be manually marked Safe in the UI with reference to this PR)🤖 Generated with Claude Code