Skip to content

fix: guard settings screens against long-hostname freeze#629

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

fix: guard settings screens against long-hostname freeze#629
jvsena42 merged 3 commits into
masterfrom
fix/regex-anr

Conversation

@jvsena42

@jvsena42 jvsena42 commented Jul 15, 2026

Copy link
Copy Markdown
Member

Description

This PR:

  1. Guards the Electrum and RGS server settings screens against a freeze when a long hostname is entered.
  2. Re-validates the RGS URL at connect time so a stale, debounced validity result can no longer enable Connect for an invalid URL.
  3. Verifies the RGS endpoint is actually reachable before reporting a successful connection, instead of accepting any well-formed URL.

Freeze fix. Both screens validated hostnames with a regex whose nested quantifiers could backtrack catastrophically (ReDoS): a long hostname without dots scaled exponentially — roughly 0.1s at 24 characters, 5s at 28, and over 30s at 30 — and, because validation ran on the main thread, it froze the UI. The Electrum screen froze on tapping Connect, and the RGS screen froze while typing, since it re-validated (and rebuilt its regex) on every keystroke. The fix replaces the pattern with a linear, length-bounded one pulled into reusable named constants, rejects over-long hosts before matching, precompiles and debounces the RGS check, and moves all hostname validation off the main thread.

RGS connect verification. RGS connect previously just saved the URL and restarted the node, always reporting success — so a well-formed but wrong URL (e.g. a typo in the snapshot path) looked like it connected. Connect now re-validates the exact current URL and then confirms the endpoint actually serves a snapshot with a lightweight reachability check before restarting, matching the behavior already shipped in bitkit-android.

This mirrors the Android fix for the same issue.

Linked Issues/Tasks

Screenshot / Video

electrum.mov
rgs-server.mov

QA Notes

Manual Tests

  • 1. Settings → Advanced → RGS Server → type a 40+ character dotless string (e.g. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa): typing stays responsive and Connect stays disabled.
  • 2a. RGS Server → enter a valid reachable https:// URL → tap Connect: success toast.
    • 2b. Enter a well-formed but wrong URL (e.g. the default with a typo in the path like .../rgs/snapshotttt) → tap Connect: error toast, not success.
    • 2c. Replace a valid URL with an invalid one and tap Connect within ~300ms: error toast, node is not restarted with the bad URL.
  • 3a. Settings → Advanced → Electrum Server → enter a 40+ character dotless host with a valid port → tap Connect: no freeze; invalid host error shows.
    • 3b. Enter a valid host:port → tap Connect: connects as before.
  • 4. regression: RGS/Electrum → enter a normal domain, an IPv4 address, and a .local host: each is accepted as before.
  • 5. regression: RGS Server → clear the field → tap Connect: RGS is disabled (empty URL) without a reachability error.

Automated Checks

  • Unit tests added: BitkitTests/SettingsUrlValidationTests.swift covers the long-dotless-host timeout regression (asserts validation completes in under 100ms) plus domain, IPv4, .local, and empty/non-https correctness for both validators.
  • The RGS reachability check hits the network and has no unit coverage yet (no HTTP-mock harness in the test target); it is exercised via the manual tests above.
  • CI: standard build and test checks run by the PR bot.

@jvsena42 jvsena42 self-assigned this Jul 15, 2026
@greptile-apps

greptile-apps Bot commented Jul 15, 2026

Copy link
Copy Markdown

Greptile Summary

This PR prevents long hostnames from freezing the Electrum and RGS settings screens. The main changes are:

  • Adds bounded, reusable hostname validation patterns.
  • Moves hostname validation off the main thread.
  • Debounces RGS validation and revalidates the current URL before connecting.
  • Adds validation performance and compatibility tests.

Confidence Score: 5/5

This looks safe to merge.

  • The current URL is checked again before the RGS configuration is saved.
  • Bounded patterns remove the long-input freeze path.
  • No additional blocking issues were found in the updated code.

Important Files Changed

Filename Overview
Bitkit/Utilities/URLValidationPattern.swift Adds bounded hostname and URL patterns to avoid catastrophic regex backtracking.
Bitkit/ViewModels/Extensions/SettingsViewModel+Electrum.swift Moves Electrum validation off the main thread and applies the bounded host pattern.
Bitkit/ViewModels/Extensions/SettingsViewModel+Rgs.swift Revalidates the current RGS URL before applying it and adds an endpoint check.
Bitkit/ViewModels/SettingsViewModel.swift Adds debounced background RGS validation and stores its result for UI state.
BitkitTests/SettingsUrlValidationTests.swift Adds coverage for long inputs and representative valid and invalid server addresses.

Reviews (2): Last reviewed commit: "fix: cehck if endpoint is reachable" | Re-trigger Greptile

Comment thread Bitkit/ViewModels/SettingsViewModel.swift
@jvsena42
jvsena42 marked this pull request as draft July 15, 2026 14:01
@jvsena42
jvsena42 marked this pull request as ready for review July 15, 2026 14:09
@jvsena42
jvsena42 enabled auto-merge July 15, 2026 14:17
@jvsena42
jvsena42 requested a review from piotr-iohk July 15, 2026 14:34

@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 401995e into master Jul 15, 2026
27 of 31 checks passed
@jvsena42
jvsena42 deleted the fix/regex-anr branch July 15, 2026 21:22
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.

2 participants