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 all 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
49 changes: 40 additions & 9 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,22 +181,39 @@ 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 {
public func saveFilterSet(set: Set<Filter>) {
writeFilterSet()
self.filterSet = set
writeFilterSet()
}

public func saveHashPrefixes(set: Set<String>) {
writeHashPrefixes()
self.hashPrefixes = set
writeHashPrefixes()
}

public func saveRevision(_ revision: Int) {
writeRevision()
self.currentRevision = revision
writeRevision()
}
}

Expand Down
101 changes: 101 additions & 0 deletions Tests/PhishingDetectionTests/PhishingDetectionDataStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,60 @@
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

let encoder = JSONEncoder()
// On Disk Data Setup
fileStorageManager.write(data: "1".utf8data, to: "revision.txt")
let onDiskFilterSet = Set([Filter(hashValue: "other", regex: "other")])
let filterSetData = try! encoder.encode(Array(onDiskFilterSet))
let onDiskHashPrefix = Set(["faffa"])
let hashPrefixData = try! encoder.encode(Array(onDiskHashPrefix))
fileStorageManager.write(data: filterSetData, to: "filterSet.json")
fileStorageManager.write(data: hashPrefixData, to: "hashPrefixes.json")

// Embedded Data Setup
mockDataProvider.embeddedRevision = 5
let embeddedFilterSet = Set([Filter(hashValue: "some", regex: "some")])
let embeddedHashPrefix = Set(["sassa"])
mockDataProvider.shouldReturnFilterSet(set: embeddedFilterSet)
mockDataProvider.shouldReturnHashPrefixes(set: embeddedHashPrefix)

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

XCTAssertEqual(actualFilterSet, embeddedFilterSet)
XCTAssertEqual(actualHashPrefix, embeddedHashPrefix)
XCTAssertEqual(actualRevision, 5)
}

Check failure on line 90 in Tests/PhishingDetectionTests/PhishingDetectionDataStoreTests.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
func testWhenEmbeddedRevisionOlderThanOnDisk_ThenDontLoadEmbedded() async {
let encoder = JSONEncoder()
// On Disk Data Setup
fileStorageManager.write(data: "6".utf8data, to: "revision.txt")
let onDiskFilterSet = Set([Filter(hashValue: "other", regex: "other")])
let filterSetData = try! encoder.encode(Array(onDiskFilterSet))
let onDiskHashPrefix = Set(["faffa"])
let hashPrefixData = try! encoder.encode(Array(onDiskHashPrefix))
fileStorageManager.write(data: filterSetData, to: "filterSet.json")
fileStorageManager.write(data: hashPrefixData, to: "hashPrefixes.json")

// Embedded Data Setup
mockDataProvider.embeddedRevision = 1
let embeddedFilterSet = Set([Filter(hashValue: "some", regex: "some")])
let embeddedHashPrefix = Set(["sassa"])
mockDataProvider.shouldReturnFilterSet(set: embeddedFilterSet)
mockDataProvider.shouldReturnHashPrefixes(set: embeddedHashPrefix)

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

XCTAssertEqual(actualFilterSet, onDiskFilterSet)
XCTAssertEqual(actualHashPrefix, onDiskHashPrefix)
XCTAssertEqual(actualRevision, 6)
}

func testWriteAndLoadData() async {
// Get and write data
let expectedHashPrefixes = Set(["aabb"])
Expand Down Expand Up @@ -93,4 +147,51 @@
XCTFail("Failed to decode stored PhishingDetection data")
}
}

func testLazyLoadingDoesNotReturnStaleData() async {
clearDatasets()

// Set up initial data
let initialFilterSet = Set([Filter(hashValue: "initial", regex: "initial")])
let initialHashPrefixes = Set(["initialPrefix"])
mockDataProvider.shouldReturnFilterSet(set: initialFilterSet)
mockDataProvider.shouldReturnHashPrefixes(set: initialHashPrefixes)

// Access the lazy-loaded properties to trigger loading
let loadedFilterSet = dataStore.filterSet
let loadedHashPrefixes = dataStore.hashPrefixes

// Validate loaded data matches initial data
XCTAssertEqual(loadedFilterSet, initialFilterSet)
XCTAssertEqual(loadedHashPrefixes, initialHashPrefixes)

// Update in-memory data
let updatedFilterSet = Set([Filter(hashValue: "updated", regex: "updated")])
let updatedHashPrefixes = Set(["updatedPrefix"])
dataStore.saveFilterSet(set: updatedFilterSet)
dataStore.saveHashPrefixes(set: updatedHashPrefixes)

// Access lazy-loaded properties again
let reloadedFilterSet = dataStore.filterSet
let reloadedHashPrefixes = dataStore.hashPrefixes

// Validate reloaded data matches updated data
XCTAssertEqual(reloadedFilterSet, updatedFilterSet)
XCTAssertEqual(reloadedHashPrefixes, updatedHashPrefixes)

// Validate on-disk data is also updated
let storedFilterSetData = fileStorageManager.read(from: "filterSet.json")
let storedHashPrefixesData = fileStorageManager.read(from: "hashPrefixes.json")

let decoder = JSONDecoder()
if let storedFilterSet = try? decoder.decode(Set<Filter>.self, from: storedFilterSetData!),
let storedHashPrefixes = try? decoder.decode(Set<String>.self, from: storedHashPrefixesData!) {

Check failure on line 189 in Tests/PhishingDetectionTests/PhishingDetectionDataStoreTests.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
XCTAssertEqual(storedFilterSet, updatedFilterSet)
XCTAssertEqual(storedHashPrefixes, updatedHashPrefixes)
} else {
XCTFail("Failed to decode stored PhishingDetection data after update")
}
}

}
Loading