mirror of
https://github.com/vector-im/element-ios.git
synced 2024-09-28 15:22:39 +00:00
Trim whitespace and trailing slashes from a user entered homeserver address.
This commit is contained in:
parent
98c3fb7b82
commit
3e6925b980
7 changed files with 46 additions and 4 deletions
|
@ -30,6 +30,8 @@
|
|||
|
||||
#import "MXKSwiftHeader.h"
|
||||
|
||||
#import "GeneratedInterface-Swift.h"
|
||||
|
||||
@interface MXKAuthenticationViewController ()
|
||||
{
|
||||
/**
|
||||
|
@ -1626,7 +1628,7 @@
|
|||
|
||||
- (void)updateRESTClient
|
||||
{
|
||||
NSString *homeserverURL = _homeServerTextField.text;
|
||||
NSString *homeserverURL = [HomeserverAddress sanitized:_homeServerTextField.text];
|
||||
|
||||
if (homeserverURL.length)
|
||||
{
|
||||
|
|
|
@ -64,6 +64,7 @@ targets:
|
|||
- path: ../Riot/Modules/MatrixKit
|
||||
excludes:
|
||||
- "**/MXKAuthenticationRecaptchaWebView.*"
|
||||
- "**/MXKAuthenticationViewController.*"
|
||||
- path: ../Riot/Modules/Analytics
|
||||
- path: ../Riot/Managers/UserSessions
|
||||
- path: ../Riot/Managers/AppInfo/
|
||||
|
|
|
@ -72,6 +72,7 @@ targets:
|
|||
- path: ../Riot/Modules/MatrixKit
|
||||
excludes:
|
||||
- "**/MXKAuthenticationRecaptchaWebView.*"
|
||||
- "**/MXKAuthenticationViewController.*"
|
||||
- path: ../Riot/Modules/Analytics
|
||||
- path: ../Riot/Managers/UserSessions
|
||||
excludes:
|
||||
|
|
|
@ -78,10 +78,23 @@ enum LoginError: String, Error {
|
|||
case resetPasswordNotStarted
|
||||
}
|
||||
|
||||
struct HomeserverAddress {
|
||||
/// Ensures the address contains a scheme, otherwise makes it `https`.
|
||||
@objcMembers
|
||||
class HomeserverAddress: NSObject {
|
||||
/// Sanitizes a user entered homeserver address with the following rules
|
||||
/// - Trim any whitespace.
|
||||
/// - Lowercase the address.
|
||||
/// - Ensure the address contains a scheme, otherwise make it `https`.
|
||||
/// - Remove any trailing slashes.
|
||||
static func sanitized(_ address: String) -> String {
|
||||
!address.contains("://") ? "https://\(address.lowercased())" : address.lowercased()
|
||||
var address = address.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
|
||||
|
||||
if !address.contains("://") {
|
||||
address = "https://\(address)"
|
||||
}
|
||||
|
||||
address = address.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
|
||||
|
||||
return address
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -349,4 +349,27 @@ import XCTest
|
|||
XCTAssertFalse(forgotPasswordString.contains(password), "The password must not be included in any strings.")
|
||||
XCTAssertFalse(changePasswordString.contains(password), "The password must not be included in any strings.")
|
||||
}
|
||||
|
||||
func testHomeserverAddressSanitization() {
|
||||
let basicAddress = "matrix.org"
|
||||
let httpAddress = "http://localhost"
|
||||
let trailingSlashAddress = "https://matrix.example.com/"
|
||||
let whitespaceAddress = " https://matrix.example.com/ "
|
||||
let validAddress = "https://matrix.example.com"
|
||||
let validAddressWithPort = "https://matrix.example.com:8484"
|
||||
|
||||
let sanitizedBasicAddress = HomeserverAddress.sanitized(basicAddress)
|
||||
let sanitizedHTTPAddress = HomeserverAddress.sanitized(httpAddress)
|
||||
let sanitizedTrailingSlashAddress = HomeserverAddress.sanitized(trailingSlashAddress)
|
||||
let sanitizedWhitespaceAddress = HomeserverAddress.sanitized(whitespaceAddress)
|
||||
let sanitizedValidAddress = HomeserverAddress.sanitized(validAddress)
|
||||
let sanitizedValidAddressWithPort = HomeserverAddress.sanitized(validAddressWithPort)
|
||||
|
||||
XCTAssertEqual(sanitizedBasicAddress, "https://matrix.org")
|
||||
XCTAssertEqual(sanitizedHTTPAddress, "http://localhost")
|
||||
XCTAssertEqual(sanitizedTrailingSlashAddress, "https://matrix.example.com")
|
||||
XCTAssertEqual(sanitizedWhitespaceAddress, "https://matrix.example.com")
|
||||
XCTAssertEqual(sanitizedValidAddress, validAddress)
|
||||
XCTAssertEqual(sanitizedValidAddressWithPort, validAddressWithPort)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@ targets:
|
|||
- path: ../Riot/Modules/MatrixKit
|
||||
excludes:
|
||||
- "**/MXKAuthenticationRecaptchaWebView.*"
|
||||
- "**/MXKAuthenticationViewController.*"
|
||||
- path: ../Riot/Modules/Analytics
|
||||
- path: ../Riot/Managers/UserSessions
|
||||
- path: ../Riot/Managers/AppInfo/
|
||||
|
|
1
changelog.d/995.bugfix
Normal file
1
changelog.d/995.bugfix
Normal file
|
@ -0,0 +1 @@
|
|||
Authentication: Trim whitespace and trailing slashes from the entered homeserver address.
|
Loading…
Reference in a new issue