Skip to content

Commit

Permalink
Fix phoneNumberSharing on profile if it’s wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
max-signal authored Jan 24, 2024
1 parent eea4fc5 commit 7a6abea
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PhoneNumberPrivacySettingsViewController: OWSTableViewController2 {
databaseStorage.read { tx in
let tsAccountManager = DependenciesBridge.shared.tsAccountManager
phoneNumberDiscoverability = tsAccountManager.phoneNumberDiscoverability(tx: tx.asV2Read).orDefault
phoneNumberSharingMode = udManager.phoneNumberSharingMode(tx: tx).orDefault
phoneNumberSharingMode = udManager.phoneNumberSharingMode(tx: tx.asV2Read).orDefault
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ class StorageServiceAccountRecordUpdater: StorageServiceRecordUpdater {
let linkPreviewsEnabled = SSKPreferences.areLinkPreviewsEnabled(transaction: transaction)
builder.setLinkPreviews(linkPreviewsEnabled)

let phoneNumberSharingMode = udManager.phoneNumberSharingMode(tx: transaction)
let phoneNumberSharingMode = udManager.phoneNumberSharingMode(tx: transaction.asV2Read)
builder.setPhoneNumberSharingMode(phoneNumberSharingMode.asProtoMode)

builder.setNotDiscoverableByPhoneNumber(
Expand Down Expand Up @@ -1365,7 +1365,7 @@ class StorageServiceAccountRecordUpdater: StorageServiceRecordUpdater {
SSKPreferences.setAreLegacyLinkPreviewsEnabled(record.proxiedLinkPreviews, transaction: transaction)
}

let localPhoneNumberSharingMode = udManager.phoneNumberSharingMode(tx: transaction)
let localPhoneNumberSharingMode = udManager.phoneNumberSharingMode(tx: transaction.asV2Read)
if record.phoneNumberSharingMode != localPhoneNumberSharingMode.asProtoMode {
if let localMode = record.phoneNumberSharingMode?.asLocalMode {
udManager.setPhoneNumberSharingMode(localMode, updateStorageServiceAndProfile: false, tx: transaction)
Expand Down
2 changes: 1 addition & 1 deletion SignalMessaging/profiles/VersionedProfilesImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public class VersionedProfilesImpl: NSObject, VersionedProfilesSwift, VersionedP
let bioEmojiValue = try encryptOptionalString(profileBioEmoji, paddedLengths: [32])
let paymentAddressValue = try encryptOptionalData(profilePaymentAddressData, paddedLengths: [554])
let phoneNumberSharingValue = try encryptBoolean(databaseStorage.read { tx in
udManager.phoneNumberSharingMode(tx: tx).orDefault == .everybody
udManager.phoneNumberSharingMode(tx: tx.asV2Read).orDefault == .everybody
})

let profileKeyVersion = try localProfileKey.getProfileKeyVersion(userId: localAci)
Expand Down
3 changes: 2 additions & 1 deletion SignalServiceKit/Dependencies/DependenciesBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,8 @@ public class DependenciesBridge {
messageProcessor: messageProcessor,
profileManager: profileManager,
storageServiceManager: storageServiceManager,
tsAccountManager: self.tsAccountManager
tsAccountManager: self.tsAccountManager,
udManager: udManager
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ extension MessageSender {
}

private static func senderCertificate(from senderCertificates: SenderCertificates, tx: SDSAnyReadTransaction) -> SenderCertificate {
switch udManager.phoneNumberSharingMode(tx: tx).orDefault {
switch udManager.phoneNumberSharingMode(tx: tx.asV2Read).orDefault {
case .everybody:
return senderCertificates.defaultCert
case .nobody:
Expand Down
2 changes: 1 addition & 1 deletion SignalServiceKit/src/Messages/MessageSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ public class MessageSender: Dependencies {
}

let senderCertificate: SenderCertificate = {
switch self.udManager.phoneNumberSharingMode(tx: tx).orDefault {
switch self.udManager.phoneNumberSharingMode(tx: tx.asV2Read).orDefault {
case .everybody:
return senderCertificates.defaultCert
case .nobody:
Expand Down
6 changes: 3 additions & 3 deletions SignalServiceKit/src/Messages/UD/OWSUDManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public protocol OWSUDManager {

func setShouldAllowUnrestrictedAccessLocal(_ value: Bool)

func phoneNumberSharingMode(tx: SDSAnyReadTransaction) -> PhoneNumberSharingMode?
func phoneNumberSharingMode(tx: DBReadTransaction) -> PhoneNumberSharingMode?

func setPhoneNumberSharingMode(
_ mode: PhoneNumberSharingMode,
Expand Down Expand Up @@ -517,8 +517,8 @@ public class OWSUDManagerImpl: NSObject, OWSUDManager {

private static var phoneNumberSharingModeKey: String { "phoneNumberSharingMode" }

public func phoneNumberSharingMode(tx: SDSAnyReadTransaction) -> PhoneNumberSharingMode? {
guard let rawMode = keyValueStore.getInt(Self.phoneNumberSharingModeKey, transaction: tx) else {
public func phoneNumberSharingMode(tx: DBReadTransaction) -> PhoneNumberSharingMode? {
guard let rawMode = keyValueStore.getInt(Self.phoneNumberSharingModeKey, transaction: SDSDB.shimOnlyBridge(tx)) else {
return nil
}
return PhoneNumberSharingMode(rawValue: rawMode)
Expand Down
3 changes: 3 additions & 0 deletions SignalServiceKit/src/Network/API/SignalServiceProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class SignalServiceProfile {
public let credential: Data?
public let badges: [(OWSUserProfileBadgeInfo, ProfileBadge)]
public let isPniCapable: Bool
public let phoneNumberSharingEncrypted: Data?

public init(serviceId: ServiceId, responseObject: Any?) throws {
guard let params = ParamParser(responseObject: responseObject) else {
Expand Down Expand Up @@ -53,6 +54,8 @@ public class SignalServiceProfile {

self.isPniCapable = Self.parseCapabilityFlag(capabilityKey: "pni", params: params, requireCapability: true)

self.phoneNumberSharingEncrypted = try params.optionalBase64EncodedData(key: "phoneNumberSharing")

if let badgeArray: [[String: Any]] = try params.optional(key: "badges") {
self.badges = badgeArray.compactMap {
do {
Expand Down
12 changes: 12 additions & 0 deletions SignalServiceKit/src/Util/OWSUserProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ public extension OWSUserProfile {
return string
}

@nonobjc
class func decrypt(profileBooleanData: Data, profileKey: OWSAES256Key) -> Bool? {
switch decrypt(profileData: profileBooleanData, profileKey: profileKey) {
case Data([1]):
return true
case Data([0]):
return false
default:
return nil
}
}

@nonobjc
class func encrypt(
givenName: OWSUserProfile.NameComponent,
Expand Down
9 changes: 8 additions & 1 deletion SignalServiceKit/src/Util/Profiles/LocalProfileChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ final class LocalProfileChecker {
private let profileManager: any ProfileManager
private let storageServiceManager: any StorageServiceManager
private let tsAccountManager: any TSAccountManager
private let udManager: any OWSUDManager

init(
db: any DB,
messageProcessor: MessageProcessor,
profileManager: any ProfileManager,
storageServiceManager: any StorageServiceManager,
tsAccountManager: any TSAccountManager
tsAccountManager: any TSAccountManager,
udManager: any OWSUDManager
) {
self.db = db
self.messageProcessor = messageProcessor
self.profileManager = profileManager
self.storageServiceManager = storageServiceManager
self.tsAccountManager = tsAccountManager
self.udManager = udManager
}

struct RemoteProfile {
Expand Down Expand Up @@ -110,6 +113,10 @@ final class LocalProfileChecker {
if localProfile.unfilteredFamilyName != mostRecentRemoteProfile.decryptedProfile.familyName {
mismatchedProperties.append("familyName")
}
let localPhoneNumberSharing = udManager.phoneNumberSharingMode(tx: tx) == .everybody
if localPhoneNumberSharing != mostRecentRemoteProfile.decryptedProfile.phoneNumberSharing {
mismatchedProperties.append("phoneNumberSharing")
}

if mismatchedProperties.isEmpty {
return false
Expand Down
10 changes: 9 additions & 1 deletion SignalServiceKit/src/Util/Profiles/ProfileFetcherJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public class ProfileFetcherJob: NSObject {
let bio = fetchedProfile.decryptedProfile?.bio
let bioEmoji = fetchedProfile.decryptedProfile?.bioEmoji
let paymentAddress = fetchedProfile.decryptedProfile?.paymentAddress(identityKey: fetchedProfile.identityKey)
let phoneNumberSharing = fetchedProfile.decryptedProfile?.phoneNumberSharing

if DebugFlags.internalLogging {
let profileKeyDescription = fetchedProfile.profileKey?.keyData.hexadecimalString ?? "None"
Expand All @@ -423,6 +424,7 @@ public class ProfileFetcherJob: NSObject {
let hasBio = bio != nil
let hasBioEmoji = bioEmoji != nil
let hasPaymentAddress = paymentAddress != nil
let hasPhoneNumberSharing = phoneNumberSharing != nil
let badges = fetchedProfile.profile.badges.map { "\"\($0.0.description)\"" }.joined(separator: "; ")

Logger.info(
Expand All @@ -434,6 +436,7 @@ public class ProfileFetcherJob: NSObject {
"hasBio: \(hasBio), " +
"hasBioEmoji: \(hasBioEmoji), " +
"hasPaymentAddress: \(hasPaymentAddress), " +
"hasPhoneNumberSharing: \(hasPhoneNumberSharing), " +
"profileKey: \(profileKeyDescription), " +
"badges: \(badges)"
)
Expand Down Expand Up @@ -581,6 +584,7 @@ public struct DecryptedProfile {
public let bio: String?
public let bioEmoji: String?
public let paymentAddressData: Data?
public let phoneNumberSharing: Bool?
}

// MARK: -
Expand Down Expand Up @@ -614,12 +618,16 @@ public struct FetchedProfile {
let paymentAddressData = profile.paymentAddressEncrypted.flatMap {
OWSUserProfile.decrypt(profileData: $0, profileKey: profileKey)
}
let phoneNumberSharing = profile.phoneNumberSharingEncrypted.flatMap {
OWSUserProfile.decrypt(profileBooleanData: $0, profileKey: profileKey)
}
return DecryptedProfile(
givenName: nameComponents?.givenName,
familyName: nameComponents?.familyName,
bio: bio,
bioEmoji: bioEmoji,
paymentAddressData: paymentAddressData
paymentAddressData: paymentAddressData,
phoneNumberSharing: phoneNumberSharing
)
}
}
Expand Down

0 comments on commit 7a6abea

Please sign in to comment.