Skip to content

Commit

Permalink
Malware protection 1: rename PhishingDetection to MaliciousSiteProtec…
Browse files Browse the repository at this point in the history
…tion (#1091)

Task/Issue URL: https://app.asana.com/0/1202406491309510/1208033567421351/f
iOS PR: duckduckgo/iOS#3642
macOS PR: duckduckgo/macos-browser#3588

**Description**:
- Renamed `PhishingDetection` to `MaliciousSiteProtection`
- Adjusted and cleaned up some code parts
  • Loading branch information
mallexxx authored Dec 3, 2024
1 parent 59f8fb2 commit befc1f1
Show file tree
Hide file tree
Showing 84 changed files with 2,826 additions and 2,052 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "PhishingDetection"
BuildableName = "PhishingDetection"
BlueprintName = "PhishingDetection"
BlueprintIdentifier = "MaliciousSiteProtection"
BuildableName = "MaliciousSiteProtection"
BlueprintName = "MaliciousSiteProtection"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand Down Expand Up @@ -796,9 +796,9 @@
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "PhishingDetectionTests"
BuildableName = "PhishingDetectionTests"
BlueprintName = "PhishingDetectionTests"
BlueprintIdentifier = "MaliciousSiteProtectionTests"
BuildableName = "MaliciousSiteProtectionTests"
BlueprintName = "MaliciousSiteProtectionTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
Expand Down
27 changes: 27 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@
"version" : "3.0.0"
}
},
{
"identity" : "swift-clocks",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-clocks.git",
"state" : {
"revision" : "b9b24b69e2adda099a1fa381cda1eeec272d5b53",
"version" : "1.0.5"
}
},
{
"identity" : "swift-concurrency-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-concurrency-extras",
"state" : {
"revision" : "163409ef7dae9d960b87f34b51587b6609a76c1f",
"version" : "1.3.0"
}
},
{
"identity" : "swifter",
"kind" : "remoteSourceControl",
Expand All @@ -89,6 +107,15 @@
"revision" : "5de0a610a7927b638a5fd463a53032c9934a2c3b",
"version" : "3.0.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "a3f634d1a409c7979cabc0a71b3f26ffa9fc8af1",
"version" : "1.4.3"
}
}
],
"version" : 2
Expand Down
29 changes: 18 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let package = Package(
.library(name: "PixelKitTestingUtilities", targets: ["PixelKitTestingUtilities"]),
.library(name: "SpecialErrorPages", targets: ["SpecialErrorPages"]),
.library(name: "DuckPlayer", targets: ["DuckPlayer"]),
.library(name: "PhishingDetection", targets: ["PhishingDetection"]),
.library(name: "MaliciousSiteProtection", targets: ["MaliciousSiteProtection"]),
.library(name: "Onboarding", targets: ["Onboarding"]),
.library(name: "BrokenSitePrompt", targets: ["BrokenSitePrompt"]),
.library(name: "PageRefreshMonitor", targets: ["PageRefreshMonitor"]),
Expand All @@ -58,7 +58,8 @@ let package = Package(
.package(url: "https://github.com/duckduckgo/privacy-dashboard", exact: "7.2.1"),
.package(url: "https://github.com/httpswift/swifter.git", exact: "1.5.0"),
.package(url: "https://github.com/duckduckgo/bloom_cpp.git", exact: "3.0.0"),
.package(url: "https://github.com/1024jp/GzipSwift.git", exact: "6.0.1")
.package(url: "https://github.com/1024jp/GzipSwift.git", exact: "6.0.1"),
.package(url: "https://github.com/pointfreeco/swift-clocks.git", exact: "1.0.5"),
],
targets: [
.target(
Expand Down Expand Up @@ -250,6 +251,7 @@ let package = Package(
"ContentBlocking",
"Persistence",
"BrowserServicesKit",
"MaliciousSiteProtection",
.product(name: "PrivacyDashboardResources", package: "privacy-dashboard")
],
path: "Sources/PrivacyDashboard",
Expand Down Expand Up @@ -391,7 +393,8 @@ let package = Package(
dependencies: [
"Common",
"UserScript",
"BrowserServicesKit"
"BrowserServicesKit",
"MaliciousSiteProtection",
],
swiftSettings: [
.define("DEBUG", .when(configuration: .debug))
Expand All @@ -408,9 +411,11 @@ let package = Package(
]
),
.target(
name: "PhishingDetection",
name: "MaliciousSiteProtection",
dependencies: [
"Common"
"Common",
"Networking",
"PixelKit",
],
swiftSettings: [
.define("DEBUG", .when(configuration: .debug))
Expand Down Expand Up @@ -655,19 +660,21 @@ let package = Package(
.testTarget(
name: "DuckPlayerTests",
dependencies: [
"DuckPlayer"
"DuckPlayer",
"BrowserServicesKitTestsUtils",
]
),

.testTarget(
name: "PhishingDetectionTests",
name: "MaliciousSiteProtectionTests",
dependencies: [
"PhishingDetection",
"PixelKit"
"TestUtils",
"MaliciousSiteProtection",
.product(name: "Clocks", package: "swift-clocks"),
],
resources: [
.copy("Resources/hashPrefixes.json"),
.copy("Resources/filterSet.json")
.copy("Resources/phishingHashPrefixes.json"),
.copy("Resources/phishingFilterSet.json"),
]
),
.testTarget(
Expand Down
4 changes: 2 additions & 2 deletions Sources/BrowserServicesKit/Autofill/AutofillUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
//

import Common
import WebKit
import UserScript
import os.log
import UserScript
@preconcurrency import WebKit

var previousIncontextSignupPermanentlyDismissedAt: Double?
var previousEmailSignedIn: Bool?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
// limitations under the License.
//

import WebKit
import Common
import ContentBlocking
import TrackerRadarKit
import UserScript
import ContentBlocking
import Common
@preconcurrency import WebKit

public protocol SurrogatesUserScriptDelegate: NSObjectProtocol {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public final class SpecialPagesUserScript: NSObject, UserScript, UserScriptMessa
@available(macOS 11.0, iOS 14.0, *)
extension SpecialPagesUserScript: WKScriptMessageHandlerWithReply {
@MainActor
public func userContentController(_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage) async -> (Any?, String?) {
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) async -> (Any?, String?) {
let action = broker.messageHandlerFor(message)
do {
let json = try await broker.execute(action: action, original: message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import Combine
import Common
import UserScript
import WebKit
import QuartzCore
import os.log
import QuartzCore
import UserScript
@preconcurrency import WebKit

public protocol UserContentControllerDelegate: AnyObject {
@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum PrivacyFeature: String {
case sslCertificates
case brokenSiteReportExperiment
case toggleReports
case phishingDetection
case maliciousSiteProtection
case brokenSitePrompt
case remoteMessaging
case additionalCampaignPixelParams
Expand Down Expand Up @@ -182,10 +182,9 @@ public enum DuckPlayerSubfeature: String, PrivacySubfeature {
case enableDuckPlayer // iOS DuckPlayer rollout feature
}

public enum PhishingDetectionSubfeature: String, PrivacySubfeature {
public var parent: PrivacyFeature { .phishingDetection }
public enum MaliciousSiteProtectionSubfeature: String, PrivacySubfeature {
public var parent: PrivacyFeature { .maliciousSiteProtection }
case allowErrorPage
case allowPreferencesToggle
}

public enum SyncPromotionSubfeature: String, PrivacySubfeature {
Expand Down
81 changes: 51 additions & 30 deletions Sources/Common/Concurrency/TaskExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,72 @@

import Foundation

public struct Sleeper {

public static let `default` = Sleeper(sleep: {
try await Task<Never, Never>.sleep(interval: $0)
})

private let sleep: (TimeInterval) async throws -> Void

public init(sleep: @escaping (TimeInterval) async throws -> Void) {
self.sleep = sleep
}

@available(macOS 13.0, iOS 16.0, *)
public init(clock: any Clock<Duration>) {
self.sleep = { interval in
try await clock.sleep(for: .nanoseconds(UInt64(interval * Double(NSEC_PER_SEC))))
}
}

public func sleep(for interval: TimeInterval) async throws {
try await sleep(interval)
}

}

public func performPeriodicJob(withDelay delay: TimeInterval? = nil,
interval: TimeInterval,
sleeper: Sleeper = .default,
operation: @escaping @Sendable () async throws -> Void,
cancellationHandler: (@Sendable () async -> Void)? = nil) async throws -> Never {

do {
if let delay {
try await sleeper.sleep(for: delay)
}

repeat {
try await operation()

try await sleeper.sleep(for: interval)
} while true
} catch let error as CancellationError {
await cancellationHandler?()
throw error
}
}

public extension Task where Success == Never, Failure == Error {

static func periodic(delay: TimeInterval? = nil,
interval: TimeInterval,
sleeper: Sleeper = .default,
operation: @escaping @Sendable () async -> Void,
cancellationHandler: (@Sendable () async -> Void)? = nil) -> Task {

Task {
do {
if let delay {
try await Task<Never, Never>.sleep(interval: delay)
}

repeat {
await operation()

try await Task<Never, Never>.sleep(interval: interval)
} while true
} catch {
await cancellationHandler?()
throw error
}
}
return periodic(delay: delay, interval: interval, sleeper: sleeper, operation: { await operation() } as @Sendable () async throws -> Void, cancellationHandler: cancellationHandler)
}

static func periodic(delay: TimeInterval? = nil,
interval: TimeInterval,
sleeper: Sleeper = .default,
operation: @escaping @Sendable () async throws -> Void,
cancellationHandler: (@Sendable () async -> Void)? = nil) -> Task {

Task {
do {
if let delay {
try await Task<Never, Never>.sleep(interval: delay)
}

repeat {
try await operation()

try await Task<Never, Never>.sleep(interval: interval)
} while true
} catch {
await cancellationHandler?()
throw error
}
try await performPeriodicJob(withDelay: delay, interval: interval, sleeper: sleeper, operation: operation, cancellationHandler: cancellationHandler)
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions Sources/Common/Extensions/HashExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ extension Data {
extension String {

public var sha1: String {
let dataBytes = data(using: .utf8)!
return dataBytes.sha1
let result = utf8data.sha1
return result
}

public var sha256: String {
let result = utf8data.sha256
return result
}

}
8 changes: 4 additions & 4 deletions Sources/Common/Extensions/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ public extension String {

// MARK: Regex

func matches(_ regex: NSRegularExpression) -> Bool {
let matches = regex.matches(in: self, options: .anchored, range: self.fullRange)
return matches.count == 1
func matches(_ regex: RegEx) -> Bool {
let firstMatch = firstMatch(of: regex, options: .anchored)
return firstMatch != nil
}

func matches(pattern: String, options: NSRegularExpression.Options = [.caseInsensitive]) -> Bool {
Expand All @@ -406,7 +406,7 @@ public extension String {
return matches(regex)
}

func replacing(_ regex: NSRegularExpression, with replacement: String) -> String {
func replacing(_ regex: RegEx, with replacement: String) -> String {
regex.stringByReplacingMatches(in: self, range: self.fullRange, withTemplate: replacement)
}

Expand Down
Loading

0 comments on commit befc1f1

Please sign in to comment.