Skip to content

fix: prevent electrum connect ANR in url regex#1077

Merged
jvsena42 merged 3 commits into
masterfrom
fix/regex-anr
Jul 15, 2026
Merged

fix: prevent electrum connect ANR in url regex#1077
jvsena42 merged 3 commits into
masterfrom
fix/regex-anr

Conversation

@jvsena42

@jvsena42 jvsena42 commented Jul 15, 2026

Copy link
Copy Markdown
Member

Fixes #1068

This PR fixes an ANR that could freeze the app when tapping Connect on the Electrum server settings screen.

Description

Play Vitals reported an ANR where URL validation ran a hostname regex on the main thread and blocked input dispatch. Two things caused it:

  • The hostname pattern used a nested quantifier that is prone to catastrophic backtracking (ReDoS). A long host without a dot made the match time grow exponentially — verified locally at roughly 0.4s for 24 characters, 7.6s for 28, and 120s for 32 — long enough to hang the UI indefinitely.
  • Validation ran on the main thread, since Connect and scan handlers launched without a background dispatcher.

The fix rewrites the hostname pattern to an equivalent form without nested quantifiers (linear matching, verified to accept and reject the same inputs), hoists it into a compiled constant instead of recompiling it on every call, and moves the Connect and scan validation onto the background dispatcher.

The same regex shape also existed in the Rapid Gossip Sync server validation. It already ran off the main thread so it could not ANR, but it was the same class of issue and got the same linear rewrite.

Preview

electrum-valid.webm
electrum-invalid.webm
rgs.webm

QA Notes

OBS: run in mainnetDebug for using main net electrum addresses without errors

Manual Tests

  • 1. Settings → Advanced → Electrum server → type a long host with no dot (40+ chars) → tap Connect: a validation toast appears immediately with no UI freeze.
  • 2. Electrum server → enter a valid host and port (e.g. electrum.blockstream.info / 50002) → tap Connect: connects successfully.
  • 3. regression: Settings → Advanced → Rapid Gossip Sync server → paste a long URL with a dotless host: no freeze and Connect stays disabled.

Automated Checks

  • Unit tests added: cover valid/IP/.local host acceptance, dotless-host rejection, and the ANR regression (Connect with a long dotless host resolves within a timeout without calling the node restart) in ElectrumConfigViewModelTest.kt.
  • Unit tests modified: add a long dotless-host no-hang case in RgsServerViewModelTest.kt.
  • CI: standard compile, unit test, and detekt checks run by the PR bot.

jvsena42 and others added 2 commits July 15, 2026 07:58
@jvsena42 jvsena42 self-assigned this Jul 15, 2026
@jvsena42 jvsena42 added this to the 2.4.0 milestone Jul 15, 2026
@greptile-apps

greptile-apps Bot commented Jul 15, 2026

Copy link
Copy Markdown

Greptile Summary

This PR prevents malformed Electrum server input from blocking the app. The main changes are:

  • Replaces vulnerable hostname regexes with precompiled linear forms.
  • Runs Electrum Connect and scan validation on the background dispatcher.
  • Adds timeout-based tests for long dotless hostnames.
  • Documents the user-facing fix in the changelog.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
app/src/main/java/to/bitkit/ui/settings/advanced/ElectrumConfigViewModel.kt Precompiles a linear hostname regex and moves Connect and scan validation to the background dispatcher.
app/src/main/java/to/bitkit/ui/settings/advanced/RgsServerViewModel.kt Replaces the matching hostname regex shape with a linear precompiled pattern.
app/src/test/java/to/bitkit/ui/settings/advanced/ElectrumConfigViewModelTest.kt Adds hostname validation and non-blocking Connect tests.
app/src/test/java/to/bitkit/ui/settings/advanced/RgsServerViewModelTest.kt Adds a timeout test for long dotless RGS hostnames.

Reviews (1): Last reviewed commit: "chore: rename changelog fragment" | Re-trigger Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 27a021e9e7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@jvsena42

Copy link
Copy Markdown
Member Author

Tested 1 only with Unit tests

@jvsena42
jvsena42 enabled auto-merge July 15, 2026 11:47
@jvsena42
jvsena42 requested a review from piotr-iohk July 15, 2026 11:47

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

tACK

@jvsena42
jvsena42 merged commit c0a7451 into master Jul 15, 2026
31 of 33 checks passed
@jvsena42
jvsena42 deleted the fix/regex-anr branch July 15, 2026 13:02
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.

[Bug]: Electrum config connect ANR in isValidURL regex on main thread

2 participants