Skip to content
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

Malicious site protection navigation detection integration #3730

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
21fac20
Malicious site protection - extract special error page navigation log…
alessandroboron Dec 2, 2024
b60a018
Remove duplicated mock after merging
alessandroboron Dec 2, 2024
b6373fc
Malicious site protection navigation detection (#3707)
alessandroboron Jan 14, 2025
cd64847
Malicious site protection address bar and privacy dashboard changes (…
alessandroboron Jan 14, 2025
3af5452
Malicious site protection feature flags (#3719)
alessandroboron Jan 14, 2025
dee1156
Add MaliciouSiteProtectionManager config and resources
alessandroboron Dec 11, 2024
a251015
Add manager initial logic
alessandroboron Dec 11, 2024
183ea12
Refactor logic and added unit tests
alessandroboron Dec 12, 2024
aa2cc79
Add MaliciousSiteProtection integration tests
alessandroboron Dec 13, 2024
5c045dd
Reset Error Data when new navigation happens
alessandroboron Dec 13, 2024
fbd0f90
Remove files added by mistake to ATBUITests target
alessandroboron Dec 13, 2024
1f521df
Fix closing tab for malicious site protection and open a new tab in s…
alessandroboron Dec 13, 2024
646dc75
Move MaliciousSiteProtectionPreferences to its own file
alessandroboron Dec 13, 2024
0381bc3
Fix tests
alessandroboron Dec 13, 2024
4e15686
Change file store directory to application support
alessandroboron Dec 16, 2024
c7a3099
Disable SwiftLint rule nesting
alessandroboron Dec 16, 2024
671d2fa
clean up code
alessandroboron Dec 16, 2024
d05d5e5
Fixed tests
alessandroboron Jan 9, 2025
91622ac
Use malicious site protection remote settings
alessandroboron Jan 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Core/FeatureFlag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public enum FeatureFlag: String {

/// https://app.asana.com/0/0/1208767141940869/f
case freeTrials

/// Feature flag to enable / disable phishing and malware protection
/// https://app.asana.com/0/1206329551987282/1207149365636877/f
case maliciousSiteProtection
}

extension FeatureFlag: FeatureFlagDescribing {
Expand Down Expand Up @@ -146,6 +150,8 @@ extension FeatureFlag: FeatureFlagDescribing {
return .remoteReleasable(.subfeature(PrivacyProSubfeature.isLaunchedROWOverride))
case .freeTrials:
return .remoteDevelopment(.subfeature(PrivacyProSubfeature.freeTrials))
case .maliciousSiteProtection:
return .remoteDevelopment(.subfeature(MaliciousSiteProtectionSubfeature.onByDefault))
}
}
}
Expand Down
229 changes: 222 additions & 7 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/DuckDuckGo/BrowserServicesKit",
"state" : {
"revision" : "14384f05a6e43842c4626e06736e2c4f5d758057",
"version" : "224.1.0"
"branch" : "alessandro/malicious-site-protection-ios",
"revision" : "797518c8dab1455af63f64844a202097c0ff8e40"
}
},
{
Expand Down Expand Up @@ -122,8 +122,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/privacy-dashboard",
"state" : {
"revision" : "2e2baf7d31c7d8e158a58bc1cb79498c1c727fd2",
"version" : "7.5.0"
"revision" : "bea4d750913ef82c10cd06e791686501c8e648e4",
"version" : "7.6.0"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/AppLifecycle/AppStateTransitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension Init {
switch event {
case .launching(let application, let isTesting):
if isTesting {
return Testing(application: application)
return AppTesting(application: application)
}
return Launched(stateContext: makeStateContext(application: application))
default:
Expand Down Expand Up @@ -116,7 +116,7 @@ extension Background {

}

extension Testing {
extension AppTesting {

func apply(event: AppEvent) -> any AppState { self }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Testing.swift
// AppTesting.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
Expand All @@ -21,7 +21,7 @@ import Core
import UIKit

@MainActor
struct Testing: AppState {
struct AppTesting: AppState {

init(application: UIApplication) {
Pixel.isDryRun = true
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "Alert-Recolorable-24.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
7 changes: 3 additions & 4 deletions DuckDuckGo/Base.lproj/OmniBar.xib
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
<constraint firstAttribute="width" constant="158" id="yFt-j7-mPi"/>
</constraints>
</view>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="LogoIcon" translatesAutoresizingMaskIntoConstraints="NO" id="eoZ-Ly-QHi">
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="eoZ-Ly-QHi">
<rect key="frame" x="0.0" y="2" width="43" height="38"/>
</imageView>
</subviews>
Expand All @@ -185,9 +185,9 @@
<constraint firstItem="eoZ-Ly-QHi" firstAttribute="leading" secondItem="99p-YW-bjm" secondAttribute="leading" id="VgX-60-gzX"/>
</constraints>
<connections>
<outlet property="daxLogoImageView" destination="eoZ-Ly-QHi" id="aTN-UG-1Em"/>
<outlet property="shieldAnimationView" destination="dqb-pm-Da8" id="m3S-lp-2hi"/>
<outlet property="shieldDotAnimationView" destination="D46-aN-352" id="yuP-Tc-fZl"/>
<outlet property="staticImageView" destination="eoZ-Ly-QHi" id="DD9-w9-GZy"/>
<outlet property="staticShieldAnimationView" destination="jQy-me-Afa" id="pez-0Q-5Gq"/>
<outlet property="staticShieldDotAnimationView" destination="KE6-Wh-7Yp" id="92x-it-Oyn"/>
<outletCollection property="gestureRecognizers" destination="qXd-RO-1cS" appends="YES" id="3xf-ou-ORG"/>
Expand Down Expand Up @@ -458,7 +458,6 @@
<image name="Clear-24" width="24" height="24"/>
<image name="Close-24" width="24" height="24"/>
<image name="Find-Search-20" width="20" height="20"/>
<image name="LogoIcon" width="24" height="24"/>
<image name="Microphone-24" width="24" height="24"/>
<image name="Reload-24" width="24" height="24"/>
<image name="Settings-24" width="24" height="24"/>
Expand All @@ -467,7 +466,7 @@
<color red="0.96078431372549022" green="0.96078431372549022" blue="0.96078431372549022" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>
<systemColor name="systemGreenColor">
<color red="0.20392156862745098" green="0.7803921568627451" blue="0.34901960784313724" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color red="0.20392156859999999" green="0.78039215689999997" blue="0.34901960780000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemYellowColor">
<color red="1" green="0.80000000000000004" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand Down
18 changes: 13 additions & 5 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2320,8 +2320,8 @@ extension MainViewController: TabDelegate {
return newTab.webView
}

func tabDidRequestClose(_ tab: TabViewController) {
closeTab(tab.tabModel)
func tabDidRequestClose(_ tab: TabViewController, shouldCreateEmptyTabAtSamePosition: Bool) {
closeTab(tab.tabModel, andOpenEmptyOneAtSamePosition: shouldCreateEmptyTabAtSamePosition)
}

func tabLoadingStateDidChange(tab: TabViewController) {
Expand Down Expand Up @@ -2571,12 +2571,20 @@ extension MainViewController: TabSwitcherDelegate {
showFireButtonPulse()
}
}
func closeTab(_ tab: Tab) {

func closeTab(_ tab: Tab, andOpenEmptyOneAtSamePosition shouldOpen: Bool = false) {
guard let index = tabManager.model.indexOf(tab: tab) else { return }
hideSuggestionTray()
hideNotificationBarIfBrokenSitePromptShown()
tabManager.remove(at: index)

if shouldOpen {
let newTab = Tab()
tabManager.replaceTab(at: index, withNewTab: newTab)
tabManager.selectTab(newTab)
} else {
tabManager.remove(at: index)
}

updateCurrentTab()
tabsBarController?.refresh(tabsModel: tabManager.model)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// MaliciousSiteProtectionFeatureFlags.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// 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 BrowserServicesKit
import Core

protocol MaliciousSiteProtectionFeatureFlagger {
/// A Boolean value indicating whether malicious site protection is enabled.
/// - Returns: `true` if malicious site protection is enabled; otherwise, `false`.
var isMaliciousSiteProtectionEnabled: Bool { get }

/// Checks if should detect malicious threats for a specific domain.
/// - Parameter domain: The domain to check for malicious threat.
/// - Returns: `true` if should check for malicious threats for the specified domain; otherwise, `false`.
func shouldDetectMaliciousThreat(forDomain domain: String?) -> Bool
}

protocol MaliciousSiteProtectionFeatureFlagsSettingsProvider {
/// The frequency, in minutes, at which the hash prefix should be updated.
var hashPrefixUpdateFrequency: Int { get }
/// The frequency, in minutes, at which the filter set should be updated.
var filterSetUpdateFrequency: Int { get }
}

/// An enum representing the different settings for malicious site protection feature flags.
enum MaliciousSiteProtectionFeatureSettings: String {
/// The setting for hash prefix update frequency.
case hashPrefixUpdateFrequency
/// The setting for filter set update frequency.
case filterSetUpdateFrequency

var defaultValue: Int {
switch self {
case .hashPrefixUpdateFrequency: return 20 // Default frequency for hash prefix updates is 20 minutes.
case .filterSetUpdateFrequency: return 720 // Default frequency for filter set updates is 720 minutes (12 hours).
}
}
}

final class MaliciousSiteProtectionFeatureFlags {
private let featureFlagger: FeatureFlagger
private let privacyConfigManager: PrivacyConfigurationManaging

private var remoteSettings: PrivacyConfigurationData.PrivacyFeature.FeatureSettings {
privacyConfigManager.privacyConfig.settings(for: .maliciousSiteProtection)
}

init(
featureFlagger: FeatureFlagger = AppDependencyProvider.shared.featureFlagger,
privacyConfigManager: PrivacyConfigurationManaging = ContentBlocking.shared.privacyConfigurationManager
) {
self.featureFlagger = featureFlagger
self.privacyConfigManager = privacyConfigManager
}
}

// MARK: - MaliciousSiteProtectionFeatureFlagger

extension MaliciousSiteProtectionFeatureFlags: MaliciousSiteProtectionFeatureFlagger {

var isMaliciousSiteProtectionEnabled: Bool {
featureFlagger.isFeatureOn(.maliciousSiteProtection)
}

func shouldDetectMaliciousThreat(forDomain domain: String?) -> Bool {
isMaliciousSiteProtectionEnabled && privacyConfigManager.privacyConfig.isFeature(.maliciousSiteProtection, enabledForDomain: domain)
}

}

// MARK: - MaliciousSiteProtectionFeatureFlagsSettingsProvider

extension MaliciousSiteProtectionFeatureFlags: MaliciousSiteProtectionFeatureFlagsSettingsProvider {

var hashPrefixUpdateFrequency: Int {
getSettings(MaliciousSiteProtectionFeatureSettings.hashPrefixUpdateFrequency)
}

var filterSetUpdateFrequency: Int {
getSettings(MaliciousSiteProtectionFeatureSettings.filterSetUpdateFrequency)
}

private func getSettings(_ value: MaliciousSiteProtectionFeatureSettings) -> Int {
remoteSettings[value.rawValue] as? Int ?? value.defaultValue
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// MaliciousSiteProtectionManager+Config.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// 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 MaliciousSiteProtection

extension MaliciousSiteProtectionManager {

static func fileName(for dataType: MaliciousSiteProtection.DataManager.StoredDataType) -> String {
switch (dataType, dataType.threatKind) {
case (.hashPrefixSet, .phishing): "phishingHashPrefixes.json"
case (.filterSet, .phishing): "phishingFilterSet.json"
case (.hashPrefixSet, .malware): "malwareHashPrefixes.json"
case (.filterSet, .malware): "malwareFilterSet.json"
}
}

struct EmbeddedDataProvider: MaliciousSiteProtection.EmbeddedDataProviding {

// swiftlint:disable:next nesting
private enum Constants {
static let embeddedDataRevision = 1696473
static let phishingEmbeddedHashPrefixDataSHA = "cdb609c37e950b7d0dcdaa80ae4071cf2c87223cfdd189caafae723722bd3158"
static let phishingEmbeddedFilterSetDataSHA = "4e52518aba04b0fd360fada76c9899001d3137d4a745cc13c484a54115a0fcd8"
static let malwareEmbeddedHashPrefixDataSHA = "6b5eb296e9e10ae9ea41c5b5356f532226d647e4f3b832c30ac670102446ea7a"
static let malwareEmbeddedFilterSetDataSHA = "4dc971fffaf244ee99267f28222a2c116743e35ef837dcbc0199693ed6a691cd"
}

func revision(for dataType: MaliciousSiteProtection.DataManager.StoredDataType) -> Int {
Constants.embeddedDataRevision
}

func url(for dataType: MaliciousSiteProtection.DataManager.StoredDataType) -> URL {
let fileName = fileName(for: dataType)
guard let url = Bundle.main.url(forResource: fileName, withExtension: nil) else {
fatalError("Could not find embedded data file \"\(fileName)\"")
}
return url
}

func hash(for dataType: MaliciousSiteProtection.DataManager.StoredDataType) -> String {
switch (dataType, dataType.threatKind) {
case (.hashPrefixSet, .phishing): Constants.phishingEmbeddedHashPrefixDataSHA
case (.filterSet, .phishing): Constants.phishingEmbeddedFilterSetDataSHA
case (.hashPrefixSet, .malware): Constants.malwareEmbeddedHashPrefixDataSHA
case (.filterSet, .malware): Constants.malwareEmbeddedFilterSetDataSHA
}
}

}
}
Loading
Loading