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

Bug Fix: Phishing Detection Dataset Discrepancies #1032

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
43 changes: 37 additions & 6 deletions Sources/PhishingDetection/PhishingDetectionDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,43 @@ public class PhishingDetectionDataStore: PhishingDetectionDataSaving {
}

private func loadHashPrefix() -> Set<String> {
guard let data = fileStorageManager.read(from: hashPrefixFilename) else { return dataProvider.loadEmbeddedHashPrefixes() }
guard let data = fileStorageManager.read(from: hashPrefixFilename) else {
return dataProvider.loadEmbeddedHashPrefixes()
}
let decoder = JSONDecoder()
do {
return Set(try decoder.decode(Set<String>.self, from: data))
if loadRevisionFromDisk() < dataProvider.embeddedRevision {
return dataProvider.loadEmbeddedHashPrefixes()
}
let onDiskHashPrefixes = Set(try decoder.decode(Set<String>.self, from: data))
return onDiskHashPrefixes
} catch {
Logger.phishingDetectionDataStore.error("Error decoding \(self.hashPrefixFilename): \(error.localizedDescription)")
return dataProvider.loadEmbeddedHashPrefixes()
}
}

private func loadFilterSet() -> Set<Filter> {
guard let data = fileStorageManager.read(from: filterSetFilename) else { return dataProvider.loadEmbeddedFilterSet() }
guard let data = fileStorageManager.read(from: filterSetFilename) else {
return dataProvider.loadEmbeddedFilterSet()
}
let decoder = JSONDecoder()
do {
return Set(try decoder.decode(Set<Filter>.self, from: data))
if loadRevisionFromDisk() < dataProvider.embeddedRevision {
return dataProvider.loadEmbeddedFilterSet()
}
let onDiskFilterSet = Set(try decoder.decode(Set<Filter>.self, from: data))
return onDiskFilterSet
} catch {
Logger.phishingDetectionDataStore.error("Error decoding \(self.filterSetFilename): \(error.localizedDescription)")
return dataProvider.loadEmbeddedFilterSet()
}
}

private func loadRevision() -> Int {
guard let data = fileStorageManager.read(from: revisionFilename) else { return dataProvider.embeddedRevision }
private func loadRevisionFromDisk() -> Int {
guard let data = fileStorageManager.read(from: revisionFilename) else {
return dataProvider.embeddedRevision
}
let decoder = JSONDecoder()
do {
return try decoder.decode(Int.self, from: data)
Expand All @@ -167,6 +181,23 @@ public class PhishingDetectionDataStore: PhishingDetectionDataSaving {
return dataProvider.embeddedRevision
}
}

private func loadRevision() -> Int {
guard let data = fileStorageManager.read(from: revisionFilename) else {
return dataProvider.embeddedRevision
}
let decoder = JSONDecoder()
do {
let loadedRevision = try decoder.decode(Int.self, from: data)
if loadedRevision < dataProvider.embeddedRevision {
return dataProvider.embeddedRevision
}
return loadedRevision
} catch {
Logger.phishingDetectionDataStore.error("Error decoding \(self.revisionFilename): \(error.localizedDescription)")
return dataProvider.embeddedRevision
}
}
}

extension PhishingDetectionDataStore {
Expand Down
16 changes: 16 additions & 0 deletions Tests/PhishingDetectionTests/PhishingDetectionDataStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ class PhishingDetectionDataStoreTests: XCTestCase {
XCTAssertEqual(actualHashPrefix, expectedHashPrefix)
}

func testWhenEmbeddedRevisionNewerThanOnDisk_ThenLoadEmbedded() async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should set the dataStore sets and revision to something else as well and then check which one of the two wins…
At the moment if loadRevisionFromDisk() < dataProvider.embeddedRevision { this line of code is not called at all from the test.

Also I would recommend to check both cases disk higher and embedded higher

mockDataProvider.embeddedRevision = 5
let expectedFilerSet = Set([Filter(hashValue: "some", regex: "some")])
let expectedHashPrefix = Set(["sassa"])
mockDataProvider.shouldReturnFilterSet(set: expectedFilerSet)
mockDataProvider.shouldReturnHashPrefixes(set: expectedHashPrefix)

let actualRevision = dataStore.currentRevision
let actualFilterSet = dataStore.filterSet
let actualHashPrefix = dataStore.hashPrefixes

XCTAssertEqual(actualFilterSet, expectedFilerSet)
XCTAssertEqual(actualHashPrefix, expectedHashPrefix)
XCTAssertEqual(actualRevision, 5)
}

func testWriteAndLoadData() async {
// Get and write data
let expectedHashPrefixes = Set(["aabb"])
Expand Down
Loading