Move validation logic to a new class and add tests

This commit is contained in:
ismailgulek
2022-05-30 16:05:20 +03:00
parent e252b481e7
commit ce5ec19e7b
3 changed files with 172 additions and 20 deletions

View File

@@ -4277,26 +4277,11 @@ static CGSize kThreadListBarButtonItemImageSize;
default:
{
MXEvent *tappedEvent = userInfo[kMXKRoomBubbleCellEventKey];
NSString *format = tappedEvent.content[@"format"];
NSString *formattedBody = tappedEvent.content[@"formatted_body"];
NSString *body = tappedEvent.content[kMXMessageBodyKey];
// if an html formatted body exists
if ([format isEqualToString:kMXRoomMessageFormatHTML] && formattedBody)
URLValidationResult *result = [URLValidator validateTappedURL:url in:tappedEvent];
if (result.shouldShowConfirmationAlert)
{
NSURL *visibleURL = [formattedBodyParser getVisibleURLForURL:url inFormattedBody:formattedBody];
if (visibleURL && ![url isEqual:visibleURL])
{
// urls are different, show confirmation alert
[self showDifferentURLsAlertFor:url visibleString:visibleURL.absoluteString];
return NO;
}
}
else if ([body mxk_containsRTLOverride] && ![body isEqualToString:url.absoluteString])
{
// we don't know where the url in the body, assuming visibleString is just a reverse of the url
[self showDifferentURLsAlertFor:url
visibleString:[url.absoluteString mxk_reversed]];
visibleURLString:result.visibleURLString];
return NO;
}
// Try to open the link
@@ -4423,10 +4408,10 @@ static CGSize kThreadListBarButtonItemImageSize;
return roomInputToolbarView;
}
- (void)showDifferentURLsAlertFor:(NSURL *)url visibleString:(NSString *)visibleString
- (void)showDifferentURLsAlertFor:(NSURL *)url visibleURLString:(NSString *)visibleURLString
{
// urls are different, show confirmation alert
UIAlertController *alert = [UIAlertController alertControllerWithTitle:[VectorL10n externalLinkConfirmationTitle] message:[VectorL10n externalLinkConfirmationMessage:visibleString :url.absoluteString] preferredStyle:UIAlertControllerStyleAlert];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:[VectorL10n externalLinkConfirmationTitle] message:[VectorL10n externalLinkConfirmationMessage:visibleURLString :url.absoluteString] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *continueAction = [UIAlertAction actionWithTitle:[VectorL10n continue] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// Try to open the link

View File

@@ -0,0 +1,70 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import MatrixSDK
@objcMembers
/// URL validation result object
class URLValidationResult: NSObject {
/// Should confirm the tapped url
let shouldShowConfirmationAlert: Bool
/// User visible string the user tapped
let visibleURLString: String?
init(shouldShowConfirmationAlert: Bool,
visibleURLString: String?) {
self.shouldShowConfirmationAlert = shouldShowConfirmationAlert
self.visibleURLString = visibleURLString
super.init()
}
static let passed = URLValidationResult(shouldShowConfirmationAlert: false,
visibleURLString: nil)
}
@objcMembers
class URLValidator: NSObject {
/// Validated tapped url in the given event
/// - Parameters:
/// - url: User tapped URL
/// - event: Event containing the link
/// - Returns: Validation result
static func validateTappedURL(_ url: URL, in event: MXEvent) -> URLValidationResult {
if let format = event.content["format"] as? String,
let formattedBody = event.content["formatted_body"] as? String {
if format == kMXRoomMessageFormatHTML {
let visibleURL = FormattedBodyParser().getVisibleURL(forURL: url, inFormattedBody: formattedBody)
if url != visibleURL {
// urls are different, show confirmation alert
return .init(shouldShowConfirmationAlert: true,
visibleURLString: visibleURL?.absoluteString)
}
}
}
if let body = event.content[kMXMessageBodyKey] as? String,
body.vc_containsRTLOverride(),
body != url.absoluteString {
// we don't know where the url is in the body, assuming visibleString is just a reverse of the url
return .init(shouldShowConfirmationAlert: true,
visibleURLString: url.absoluteString.vc_reversed())
}
return .passed
}
}

View File

@@ -0,0 +1,97 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
@testable import Riot
class URLValidatorTests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testWithOnlyURLEvent() {
guard let event = MXEvent(fromJSON: [
"content": [
kMXMessageBodyKey: "https://www.example.com"
]
]) else {
XCTFail("Failed to setup test conditions")
return
}
guard let url = URL(string: "https://www.example.com") else {
XCTFail("Failed to setup test conditions")
return
}
let result = URLValidator.validateTappedURL(url, in: event)
XCTAssertFalse(result.shouldShowConfirmationAlert, "Should not show a confirmation alert for given event and url")
XCTAssertNil(result.visibleURLString)
}
func testWithHTMLEvent() {
guard let event = MXEvent(fromJSON: [
"content": [
kMXMessageBodyKey: "[link](https://www.example.com)",
"format": kMXRoomMessageFormatHTML,
"formatted_body": "<a href=\"https://www.example.com\">link</a>"
]
]) else {
XCTFail("Failed to setup test conditions")
return
}
guard let url = URL(string: "https://www.example.com") else {
XCTFail("Failed to setup test conditions")
return
}
let result = URLValidator.validateTappedURL(url, in: event)
XCTAssertTrue(result.shouldShowConfirmationAlert, "Should show a confirmation alert for given event and url")
XCTAssertEqual(result.visibleURLString, "link")
}
func testWithRTLOverriddenEvent() {
let realLink = "https://www.dangerous.com/=qhcraes#/moc.elgoog.www//:sptth"
let visibleLink = realLink.vc_reversed()
guard let event = MXEvent(fromJSON: [
"content": [
kMXMessageBodyKey: "\u{202E}" + visibleLink
]
]) else {
XCTFail("Failed to setup test conditions")
return
}
guard let url = URL(string: realLink) else {
XCTFail("Failed to setup test conditions")
return
}
let result = URLValidator.validateTappedURL(url, in: event)
XCTAssertTrue(result.shouldShowConfirmationAlert, "Should show a confirmation alert for given event and url")
XCTAssertEqual(result.visibleURLString, visibleLink)
}
}