-
Notifications
You must be signed in to change notification settings - Fork 3
fix: guard settings screens against long-hostname freeze #629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import Foundation | ||
|
|
||
| /// Linear (non-backtracking) regex fragments for hostname / URL validation. | ||
| /// | ||
| /// Labels are length-bounded to the DNS maximum, so the composed patterns cannot | ||
| /// backtrack catastrophically on long dotless input (ReDoS). Fragments are lowercase; | ||
| /// each call site applies its own case sensitivity (e.g. `.caseInsensitive`). | ||
| enum URLValidationPattern { | ||
| /// Maximum length of a DNS name; hosts longer than this are rejected before running the regex. | ||
| static let maxHostLength = 253 | ||
|
|
||
| /// A single DNS label: starts and ends alphanumeric, hyphens allowed inside, up to 63 chars. | ||
| static let hostLabel = #"[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?"# | ||
|
|
||
| /// One or more dot-terminated labels, e.g. "sub.example." — combine with a trailing TLD. | ||
| static let dotSeparatedLabels = #"(?:\#(hostLabel)\.)+"# | ||
|
|
||
| /// A dotted-decimal IPv4 address. | ||
| static let ipv4 = #"(?:\d{1,3}\.){3}\d{1,3}"# | ||
|
|
||
| /// A hostname requiring a public-style alphabetic TLD of >= 2 chars, e.g. "example.com". | ||
| static let domainWithPublicTld = #"\#(dotSeparatedLabels)[a-z]{2,}"# | ||
|
|
||
| /// A hostname allowing any final label, e.g. custom TLDs like ".local". | ||
| static let domainWithAnyTld = #"\#(dotSeparatedLabels)[a-z\d-]+"# | ||
|
|
||
| /// Full RGS server URL: optional scheme, a domain-or-IPv4 host, optional port and path. | ||
| static let rgsServerUrl = #"^(https?://)?(\#(domainWithPublicTld)|\#(ipv4))(:\d+)?(/[-a-z\d%_.~+]*)*"# | ||
|
|
||
| /// Electrum host: a domain with any TLD, or an IPv4 address. | ||
| static let electrumHost = #"^\#(domainWithAnyTld)|\#(ipv4)$"# | ||
| } |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| @testable import Bitkit | ||
| import XCTest | ||
|
|
||
| /// Regression + correctness tests for RGS and Electrum server URL validation. | ||
| /// The long-dotless-host cases guard against catastrophic regex backtracking (ReDoS) | ||
| /// that previously froze the settings screens. | ||
| final class SettingsUrlValidationTests: XCTestCase { | ||
| @MainActor | ||
| func testRgsUrlValidationRejectsLongDotlessHostQuickly() { | ||
| let settings = SettingsViewModel.shared | ||
| let longHost = "https://" + String(repeating: "a", count: 40) | ||
|
|
||
| let start = Date() | ||
| let isValid = settings.isValidRgsUrl(longHost) | ||
| let elapsed = Date().timeIntervalSince(start) | ||
|
|
||
| XCTAssertFalse(isValid) | ||
| XCTAssertLessThan(elapsed, 0.1, "RGS validation must not catastrophically backtrack") | ||
| } | ||
|
|
||
| @MainActor | ||
| func testElectrumUrlValidationRejectsLongDotlessHostQuickly() { | ||
| let settings = SettingsViewModel.shared | ||
| let longHost = String(repeating: "a", count: 40) | ||
|
|
||
| let start = Date() | ||
| let isValid = settings.isValidElectrumURL(longHost) | ||
| let elapsed = Date().timeIntervalSince(start) | ||
|
|
||
| XCTAssertFalse(isValid) | ||
| XCTAssertLessThan(elapsed, 0.1, "Electrum validation must not catastrophically backtrack") | ||
| } | ||
|
|
||
| @MainActor | ||
| func testRgsUrlValidationAcceptsValidUrls() { | ||
| let settings = SettingsViewModel.shared | ||
|
|
||
| XCTAssertTrue(settings.isValidRgsUrl(""), "empty URL disables RGS and is valid") | ||
| XCTAssertTrue(settings.isValidRgsUrl("https://rapidsync.lightningdevkit.org/snapshot")) | ||
| XCTAssertTrue(settings.isValidRgsUrl("https://1.2.3.4:443")) | ||
| } | ||
|
|
||
| @MainActor | ||
| func testRgsUrlValidationRejectsInvalidUrls() { | ||
| let settings = SettingsViewModel.shared | ||
|
|
||
| XCTAssertFalse(settings.isValidRgsUrl("http://example.com"), "non-https is rejected") | ||
| XCTAssertFalse(settings.isValidRgsUrl("https://"), "missing host is rejected") | ||
| } | ||
|
|
||
| @MainActor | ||
| func testElectrumUrlValidationAcceptsValidHosts() { | ||
| let settings = SettingsViewModel.shared | ||
|
|
||
| XCTAssertTrue(settings.isValidElectrumURL("electrum.blockstream.info")) | ||
| XCTAssertTrue(settings.isValidElectrumURL("1.2.3.4")) | ||
| XCTAssertTrue(settings.isValidElectrumURL("myhost.local")) | ||
| } | ||
|
|
||
| @MainActor | ||
| func testElectrumUrlValidationRejectsBareDotlessHost() { | ||
| let settings = SettingsViewModel.shared | ||
|
|
||
| XCTAssertFalse(settings.isValidElectrumURL("foobar")) | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed a freeze on the Electrum and RGS server settings screens when entering a long hostname. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.