Skip to content

Commit

Permalink
rename PhishingDetection to MaliciousSiteProtection
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx committed Nov 22, 2024
1 parent deacf61 commit 4b8a1f0
Show file tree
Hide file tree
Showing 47 changed files with 1,254 additions and 1,047 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 @@ -782,9 +782,9 @@
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "PhishingDetectionTests"
BuildableName = "PhishingDetectionTests"
BlueprintName = "PhishingDetectionTests"
BlueprintIdentifier = "MaliciousSiteProtectionTests"
BuildableName = "MaliciousSiteProtectionTests"
BlueprintName = "MaliciousSiteProtectionTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
Expand Down
18 changes: 10 additions & 8 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 Down Expand Up @@ -407,9 +407,12 @@ let package = Package(
]
),
.target(
name: "PhishingDetection",
name: "MaliciousSiteProtection",
dependencies: [
"Common"
"Common",
"Networking",
"SpecialErrorPages",
"PixelKit",
],
swiftSettings: [
.define("DEBUG", .when(configuration: .debug))
Expand Down Expand Up @@ -645,14 +648,13 @@ let package = Package(
),

.testTarget(
name: "PhishingDetectionTests",
name: "MaliciousSiteProtectionTests",
dependencies: [
"PhishingDetection",
"PixelKit"
"MaliciousSiteProtection",
],
resources: [
.copy("Resources/hashPrefixes.json"),
.copy("Resources/filterSet.json")
.copy("Resources/phishingHashPrefixes.json"),
.copy("Resources/phishingFilterSet.json"),
]
),
.testTarget(
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 @@ -173,8 +173,8 @@ 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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// PhishingDetectionClient.swift
// APIClient.swift
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
// 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.
Expand All @@ -16,45 +16,14 @@
// limitations under the License.
//

import Foundation
import Common
import Foundation
import os
import Networking

public struct HashPrefixResponse: Codable, Equatable {
public var insert: [String]
public var delete: [String]
public var revision: Int
public var replace: Bool

public init(insert: [String], delete: [String], revision: Int, replace: Bool) {
self.insert = insert
self.delete = delete
self.revision = revision
self.replace = replace
}
}

public struct FilterSetResponse: Codable, Equatable {
public var insert: [Filter]
public var delete: [Filter]
public var revision: Int
public var replace: Bool

public init(insert: [Filter], delete: [Filter], revision: Int, replace: Bool) {
self.insert = insert
self.delete = delete
self.revision = revision
self.replace = replace
}
}

public struct MatchResponse: Codable, Equatable {
public var matches: [Match]
}

public protocol PhishingDetectionClientProtocol {
func getFilterSet(revision: Int) async -> FilterSetResponse
func getHashPrefixes(revision: Int) async -> HashPrefixResponse
public protocol APIClientProtocol {
func getFilterSet(revision: Int) async -> APIClient.FiltersChangeSetResponse
func getHashPrefixes(revision: Int) async -> APIClient.HashPrefixesChangeSetResponse
func getMatches(hashPrefix: String) async -> [Match]
}

Expand All @@ -70,7 +39,7 @@ extension URLSessionProtocol {
}
}

public class PhishingDetectionAPIClient: PhishingDetectionClientProtocol {
public struct APIClient: APIClientProtocol {

public enum Environment {
case production
Expand Down Expand Up @@ -113,20 +82,20 @@ public class PhishingDetectionAPIClient: PhishingDetectionClientProtocol {
self.session = session
}

public func getFilterSet(revision: Int) async -> FilterSetResponse {
public func getFilterSet(revision: Int) async -> FiltersChangeSetResponse {
guard let url = createURL(for: .filterSet, revision: revision) else {
logDebug("🔸 Invalid filterSet revision URL: \(revision)")
return FilterSetResponse(insert: [], delete: [], revision: revision, replace: false)
return FiltersChangeSetResponse(insert: [], delete: [], revision: revision, replace: false)
}
return await fetch(url: url, responseType: FilterSetResponse.self) ?? FilterSetResponse(insert: [], delete: [], revision: revision, replace: false)
return await fetch(url: url, responseType: FiltersChangeSetResponse.self) ?? FiltersChangeSetResponse(insert: [], delete: [], revision: revision, replace: false)
}

public func getHashPrefixes(revision: Int) async -> HashPrefixResponse {
public func getHashPrefixes(revision: Int) async -> HashPrefixesChangeSetResponse {
guard let url = createURL(for: .hashPrefix, revision: revision) else {
logDebug("🔸 Invalid hashPrefix revision URL: \(revision)")
return HashPrefixResponse(insert: [], delete: [], revision: revision, replace: false)
return HashPrefixesChangeSetResponse(insert: [], delete: [], revision: revision, replace: false)
}
return await fetch(url: url, responseType: HashPrefixResponse.self) ?? HashPrefixResponse(insert: [], delete: [], revision: revision, replace: false)
return await fetch(url: url, responseType: HashPrefixesChangeSetResponse.self) ?? HashPrefixesChangeSetResponse(insert: [], delete: [], revision: revision, replace: false)
}

public func getMatches(hashPrefix: String) async -> [Match] {
Expand All @@ -140,10 +109,10 @@ public class PhishingDetectionAPIClient: PhishingDetectionClientProtocol {
}

// MARK: Private Methods
extension PhishingDetectionAPIClient {
extension APIClient {

private func logDebug(_ message: String) {
Logger.phishingDetectionClient.debug("\(message)")
Logger.api.debug("\(message)")
}

private func createURL(for path: Constants.APIPath, revision: Int? = nil, queryItems: [URLQueryItem]? = nil) -> URL? {
Expand Down
40 changes: 40 additions & 0 deletions Sources/MaliciousSiteProtection/API/ChangeSetResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ChangeSetResponse.swift
//
// 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

extension APIClient {

public struct ChangeSetResponse<T: Codable & Hashable>: Codable, Equatable {
let insert: [T]
let delete: [T]
let revision: Int
let replace: Bool

public init(insert: [T], delete: [T], revision: Int, replace: Bool) {
self.insert = insert
self.delete = delete
self.revision = revision
self.replace = replace
}
}

public typealias FiltersChangeSetResponse = ChangeSetResponse<Filter>
public typealias HashPrefixesChangeSetResponse = ChangeSetResponse<String>

}
25 changes: 25 additions & 0 deletions Sources/MaliciousSiteProtection/API/MatchResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// MatchResponse.swift
//
// 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.
//

extension APIClient {

public struct MatchResponse: Codable, Equatable {
public var matches: [Match]
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Logger+MaliciousSiteProtection.swift
//
// 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 os

public extension os.Logger {
struct MaliciousSiteProtection {
public static var general = os.Logger(subsystem: "MSP", category: "General")
public static var api = os.Logger(subsystem: "MSP", category: "API")
public static var dataManager = os.Logger(subsystem: "MSP", category: "DataManager")
public static var updateManager = os.Logger(subsystem: "MSP", category: "UpdateManager")
// TODO: to be dropped

Check failure on line 28 in Sources/MaliciousSiteProtection/Logger+MaliciousSiteProtection.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

TODOs should be resolved (to be dropped) (todo)
static var phishingDetectionTasks = os.Logger(subsystem: "MSP", category: "BackgroundActivities")
}
}

internal typealias Logger = os.Logger.MaliciousSiteProtection
Loading

0 comments on commit 4b8a1f0

Please sign in to comment.