Commit 3296e9bb authored by Livvie Lin's avatar Livvie Lin Committed by Commit Bot

Reland "[iOS] Add lookalike_url_egtest and update comments"

This is a reland of 90cc207c

Original change's description:
> [iOS] Add lookalike_url_egtest and update comments
>
> Bug: 1058898
> Change-Id: I80883ee1258493eb0ac56950d654606c393ee65e
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2284144
> Commit-Queue: Livvie Lin <livvielin@chromium.org>
> Reviewed-by: Eugene But <eugenebut@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#788917}

Bug: 1058898
Change-Id: Id68637bccd15735173bfea3ae41f91ad0ed39f58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2307611Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Livvie Lin <livvielin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790027}
parent 980fb0fc
...@@ -347,6 +347,14 @@ source_set("unit_tests_internal") { ...@@ -347,6 +347,14 @@ source_set("unit_tests_internal") {
] ]
} }
source_set("constants") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"lookalike_url_constants.h",
"lookalike_url_constants.mm",
]
}
source_set("eg_test_support+eg2") { source_set("eg_test_support+eg2") {
defines = [ "CHROME_EARL_GREY_2" ] defines = [ "CHROME_EARL_GREY_2" ]
configs += [ configs += [
...@@ -355,7 +363,10 @@ source_set("eg_test_support+eg2") { ...@@ -355,7 +363,10 @@ source_set("eg_test_support+eg2") {
] ]
testonly = true testonly = true
sources = [ "progress_indicator_app_interface.h" ] sources = [
"lookalike_url_app_interface.h",
"progress_indicator_app_interface.h",
]
deps = [ deps = [
"//ios/chrome/test/earl_grey:eg_test_support+eg2", "//ios/chrome/test/earl_grey:eg_test_support+eg2",
...@@ -372,15 +383,23 @@ source_set("eg_app_support+eg2") { ...@@ -372,15 +383,23 @@ source_set("eg_app_support+eg2") {
defines = [ "CHROME_EARL_GREY_2" ] defines = [ "CHROME_EARL_GREY_2" ]
sources = [ sources = [
"lookalike_url_app_interface.h",
"lookalike_url_app_interface.mm",
"progress_indicator_app_interface.h", "progress_indicator_app_interface.h",
"progress_indicator_app_interface.mm", "progress_indicator_app_interface.mm",
] ]
deps = [ deps = [
":constants",
"//base", "//base",
"//components/lookalikes/core",
"//ios/chrome/test/app:test_support",
"//ios/components/security_interstitials/lookalikes",
"//ios/testing/earl_grey:eg_app_support+eg2", "//ios/testing/earl_grey:eg_app_support+eg2",
"//ios/third_party/earl_grey2:app_framework+link", "//ios/third_party/earl_grey2:app_framework+link",
"//ios/third_party/material_components_ios", "//ios/third_party/material_components_ios",
"//ios/web/public",
"//net",
] ]
} }
...@@ -402,6 +421,7 @@ source_set("eg2_tests") { ...@@ -402,6 +421,7 @@ source_set("eg2_tests") {
"forms_egtest.mm", "forms_egtest.mm",
"http_auth_egtest.mm", "http_auth_egtest.mm",
"js_print_egtest.mm", "js_print_egtest.mm",
"lookalike_url_egtest.mm",
"navigation_egtest.mm", "navigation_egtest.mm",
"progress_indicator_egtest.mm", "progress_indicator_egtest.mm",
"push_and_replace_state_navigation_egtest.mm", "push_and_replace_state_navigation_egtest.mm",
...@@ -414,6 +434,7 @@ source_set("eg2_tests") { ...@@ -414,6 +434,7 @@ source_set("eg2_tests") {
] ]
deps = [ deps = [
":constants",
":eg_test_support+eg2", ":eg_test_support+eg2",
"//components/content_settings/core/common", "//components/content_settings/core/common",
"//components/strings", "//components/strings",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_WEB_LOOKALIKE_URL_APP_INTERFACE_H_
#define IOS_CHROME_BROWSER_WEB_LOOKALIKE_URL_APP_INTERFACE_H_
#import <Foundation/Foundation.h>
// The app interface for lookalike URL blocking page tests. It sets
// up LookalikeUrlDecider, which does the following:
// - Lets a navigation proceed if the domain is explicitly allowed
// - Cancels the navigation and shows error page with a suggested URL
// for the /lookalike.html path
// - Cancels the navigation and shows error page with no suggested URL
// for the /lookalike-empty.html path
// - Allows other navigations to proceed
@interface LookalikeUrlAppInterface : NSObject
// Sets up lookalike policy decider. Used for testing.
+ (void)setUpLookalikeUrlDeciderForWebState;
// Tear down lookalike policy decider. Used for testing.
+ (void)tearDownLookalikeUrlDeciderForWebState;
@end
#endif // IOS_CHROME_BROWSER_WEB_LOOKALIKE_URL_APP_INTERFACE_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/web/lookalike_url_app_interface.h"
#include "components/lookalikes/core/lookalike_url_util.h"
#import "ios/chrome/browser/web/lookalike_url_constants.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#import "ios/chrome/test/app/tab_test_util.h"
#include "ios/components/security_interstitials/lookalikes/lookalike_url_container.h"
#include "ios/components/security_interstitials/lookalikes/lookalike_url_error.h"
#include "ios/components/security_interstitials/lookalikes/lookalike_url_tab_allow_list.h"
#import "ios/web/public/navigation/web_state_policy_decider.h"
#import "ios/web/public/web_state_user_data.h"
#import "net/base/mac/url_conversions.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// This decider determines whether a URL is a lookalike. If so, it cancels
// navigation and shows an error.
class LookalikeUrlDecider : public web::WebStatePolicyDecider,
public web::WebStateUserData<LookalikeUrlDecider> {
public:
LookalikeUrlDecider(web::WebState* web_state)
: web::WebStatePolicyDecider(web_state), web_state_(web_state) {}
void ShouldAllowResponse(
NSURLResponse* response,
bool for_main_frame,
web::WebStatePolicyDecider::PolicyDecisionCallback callback) override {
LookalikeUrlContainer* lookalike_container =
LookalikeUrlContainer::FromWebState(web_state_);
LookalikeUrlTabAllowList* allow_list =
LookalikeUrlTabAllowList::FromWebState(web_state_);
GURL response_url = net::GURLWithNSURL(response.URL);
if (allow_list->IsDomainAllowed(response_url.host())) {
return std::move(callback).Run(
web::WebStatePolicyDecider::PolicyDecision::Allow());
}
if (response_url.path() == kLookalikePagePathForTesting) {
GURL::Replacements safeReplacements;
safeReplacements.SetPathStr("echo");
lookalike_container->SetLookalikeUrlInfo(
response_url.ReplaceComponents(safeReplacements), response_url,
LookalikeUrlMatchType::kSkeletonMatchTop5k);
std::move(callback).Run(CreateLookalikeErrorDecision());
return;
}
if (response_url.path() == kLookalikePageEmptyUrlPathForTesting) {
lookalike_container->SetLookalikeUrlInfo(
GURL::EmptyGURL(), response_url,
LookalikeUrlMatchType::kSkeletonMatchTop5k);
std::move(callback).Run(CreateLookalikeErrorDecision());
return;
}
return std::move(callback).Run(
web::WebStatePolicyDecider::PolicyDecision::Allow());
}
WEB_STATE_USER_DATA_KEY_DECL();
private:
web::WebState* web_state_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(LookalikeUrlDecider);
};
WEB_STATE_USER_DATA_KEY_IMPL(LookalikeUrlDecider)
} // namespace
@implementation LookalikeUrlAppInterface
+ (void)setUpLookalikeUrlDeciderForWebState {
LookalikeUrlDecider::CreateForWebState(
chrome_test_util::GetCurrentWebState());
}
+ (void)tearDownLookalikeUrlDeciderForWebState {
LookalikeUrlDecider::RemoveFromWebState(
chrome_test_util::GetCurrentWebState());
}
@end
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_WEB_LOOKALIKE_URL_CONSTANTS_H_
#define IOS_CHROME_BROWSER_WEB_LOOKALIKE_URL_CONSTANTS_H_
// Constant used for testing a lookalike URL interstitial.
extern const char kLookalikePagePathForTesting[];
// Constant used for testing a lookalike URL interstitial with no suggested URL.
extern const char kLookalikePageEmptyUrlPathForTesting[];
#endif // IOS_CHROME_BROWSER_WEB_LOOKALIKE_URL_CONSTANTS_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/web/lookalike_url_constants.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
const char kLookalikePagePathForTesting[] = "/lookalike.html";
const char kLookalikePageEmptyUrlPathForTesting[] = "/lookalike-empty.html";
This diff is collapsed.
...@@ -46,9 +46,10 @@ void LookalikeUrlControllerClient::GoBack() { ...@@ -46,9 +46,10 @@ void LookalikeUrlControllerClient::GoBack() {
if (!safe_url_.is_valid()) { if (!safe_url_.is_valid()) {
IOSBlockingPageControllerClient::GoBack(); IOSBlockingPageControllerClient::GoBack();
} else { } else {
// TODO(crbug.com/1058898): Replace the last committed navigation // For simplicity and because replacement doesn't always work, the
// (the interstitial) with the safe URL navigation to prevent the // navigation to the safe URL does not replace the navigation to
// back button from returning to the bad site. // the interstitial. However, this is acceptable since if a user
// navigates back to the lookalike, the interstitial will be shown.
OpenUrlInCurrentTab(safe_url_); OpenUrlInCurrentTab(safe_url_);
} }
} }
......
...@@ -7,10 +7,16 @@ ...@@ -7,10 +7,16 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "ios/web/public/navigation/web_state_policy_decider.h"
// The error domain for lookalike URL errors. // The error domain for lookalike URL errors.
extern const NSErrorDomain kLookalikeUrlErrorDomain; extern const NSErrorDomain kLookalikeUrlErrorDomain;
// Error code for navigations to lookalike URLs. // Error code for navigations to lookalike URLs.
extern const NSInteger kLookalikeUrlErrorCode; extern const NSInteger kLookalikeUrlErrorCode;
// Creates a PolicyDecision that cancels a navigation to show a lookalike
// error.
web::WebStatePolicyDecider::PolicyDecision CreateLookalikeErrorDecision();
#endif // IOS_COMPONENTS_SECURITY_INTERSTITIALS_LOOKALIKES_LOOKALIKE_URL_ERROR_H_ #endif // IOS_COMPONENTS_SECURITY_INTERSTITIALS_LOOKALIKES_LOOKALIKE_URL_ERROR_H_
...@@ -11,3 +11,10 @@ ...@@ -11,3 +11,10 @@
const NSErrorDomain kLookalikeUrlErrorDomain = const NSErrorDomain kLookalikeUrlErrorDomain =
@"com.google.chrome.lookalike_url"; @"com.google.chrome.lookalike_url";
const NSInteger kLookalikeUrlErrorCode = -1003; const NSInteger kLookalikeUrlErrorCode = -1003;
web::WebStatePolicyDecider::PolicyDecision CreateLookalikeErrorDecision() {
return web::WebStatePolicyDecider::PolicyDecision::CancelAndDisplayError(
[NSError errorWithDomain:kLookalikeUrlErrorDomain
code:kLookalikeUrlErrorCode
userInfo:nil]);
}
...@@ -19,15 +19,6 @@ ...@@ -19,15 +19,6 @@
#endif #endif
namespace { namespace {
// Creates a PolicyDecision that cancels a navigation to show a lookalike
// error.
web::WebStatePolicyDecider::PolicyDecision CreateLookalikeErrorDecision() {
return web::WebStatePolicyDecider::PolicyDecision::CancelAndDisplayError(
[NSError errorWithDomain:kLookalikeUrlErrorDomain
code:kLookalikeUrlErrorCode
userInfo:nil]);
}
// Creates a PolicyDecision that allows the navigation. // Creates a PolicyDecision that allows the navigation.
web::WebStatePolicyDecider::PolicyDecision CreateAllowDecision() { web::WebStatePolicyDecider::PolicyDecision CreateAllowDecision() {
return web::WebStatePolicyDecider::PolicyDecision::Allow(); return web::WebStatePolicyDecider::PolicyDecision::Allow();
...@@ -50,7 +41,7 @@ void LookalikeUrlTabHelper::ShouldAllowResponse( ...@@ -50,7 +41,7 @@ void LookalikeUrlTabHelper::ShouldAllowResponse(
return; return;
} }
// TODO(crbug.com/1058898): Create container and ReleaseInterstitialParams. // TODO(crbug.com/1104386): Create container and ReleaseInterstitialParams.
// Get stored interstitial parameters early. Doing so ensures that a // Get stored interstitial parameters early. Doing so ensures that a
// navigation to an irrelevant (for this interstitial's purposes) URL such as // navigation to an irrelevant (for this interstitial's purposes) URL such as
// chrome://settings while the lookalike interstitial is being shown clears // chrome://settings while the lookalike interstitial is being shown clears
...@@ -78,7 +69,7 @@ void LookalikeUrlTabHelper::ShouldAllowResponse( ...@@ -78,7 +69,7 @@ void LookalikeUrlTabHelper::ShouldAllowResponse(
return; return;
} }
// TODO(crbug.com/1058898): If this is a reload and if the current // TODO(crbug.com/1104386): If this is a reload and if the current
// URL is the last URL of the stored redirect chain, the interstitial // URL is the last URL of the stored redirect chain, the interstitial
// was probably reloaded. Stop the reload and navigate back to the // was probably reloaded. Stop the reload and navigate back to the
// original lookalike URL so that the full checks are exercised again. // original lookalike URL so that the full checks are exercised again.
...@@ -91,7 +82,7 @@ void LookalikeUrlTabHelper::ShouldAllowResponse( ...@@ -91,7 +82,7 @@ void LookalikeUrlTabHelper::ShouldAllowResponse(
return; return;
} }
// TODO(crbug.com/1058898): After site engagement has been componentized, // TODO(crbug.com/1104384): After site engagement has been componentized,
// fetch and set |engaged_sites| here so that an interstitial won't be // fetch and set |engaged_sites| here so that an interstitial won't be
// shown on engaged sites, and so that the interstitial will be shown on // shown on engaged sites, and so that the interstitial will be shown on
// lookalikes of engaged sites. // lookalikes of engaged sites.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment