qBitControl icon indicating copy to clipboard operation
qBitControl copied to clipboard

[Fix] sanitize the server url for trailing slashes while adding a server

Open danpiths opened this issue 2 months ago • 1 comments

Summary

Normalize user-entered server URLs by removing any trailing slash characters before saving or testing the connection. This ensures consistent URL formatting across the app and avoids duplicate entries or subtle connection issues caused by extra slashes.

What changed

  • Added sanitizeInputs() to ServerAddViewModel that strips trailing slashes using a regex:
    • url = url.replacingOccurrences(of: "/+$", with: "", options: .regularExpression)
  • Invoked sanitizeInputs() at the start of addServer(dismiss:), after input validation and before:
    • building the Server model
    • attempting a connection check
    • persisting the server

Why this is needed

  • Users may paste URLs ending with / or multiple ///.
  • Trailing slashes can lead to:
    • Mismatched URLs when comparing or deduplicating servers
    • Inconsistent request paths when constructing endpoints
    • Confusing UI where the same server appears as different entries

Normalizing the URL at the point of creation/editing prevents these issues and keeps storage/logic consistent.

How it works

  • The regex "/+$" matches one or more slashes at the end of the string and removes them.
  • Examples:
    • https://example.com/https://example.com
    • http://example.com///http://example.com
  • Note: This field represents the base server URL; normalizing to the host/root avoids accidental path suffixes.

Risk assessment

  • Low risk. Only trailing slashes are removed; scheme, host, ports, and credentials remain unchanged.
  • No behavioral change when the URL is already normalized.

Testing

  • Manually verified adding servers with:
    • https://example.com/ → saved as https://example.com
    • http://example.com/// → saved as http://example.com
  • Confirmed connectivity check runs against the sanitized URL.
  • Edited an existing server with trailing slashes and confirmed it is normalized upon save.

danpiths avatar Oct 12 '25 11:10 danpiths

Hi, thanks for the PR. I'll check it out and merge when I can.

Michael-128 avatar Nov 20 '25 20:11 Michael-128