Skip to content

Commit 3ecc063

Browse files
nan-licursoragent
andcommitted
fix: [PR5] drop stale properties/subscription deltas instead of mis-stamping
Refuse to stamp the current user when the changed model is no longer theirs so a concurrent login cannot attach the wrong external_id for PR6 auth. Properties require the current properties model; email/SMS add requires the model still be in the store. Remove and push updates keep stamping the current user. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 31b27e1 commit 3ecc063

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSPropertiesModelStoreListener.swift‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ class OSPropertiesModelStoreListener: OSModelStoreListener {
4747
}
4848

4949
func getUpdateModelDelta(_ args: OSModelChangedArgs) -> OSDelta? {
50+
// Drop if this isn't the current user's properties model, the user has switched since
5051
guard let _ = OSPropertiesSupportedProperty(rawValue: args.property),
51-
let userInstance = OneSignalUserManagerImpl.sharedInstance._user
52+
let userInstance = OneSignalUserManagerImpl.sharedInstance._user,
53+
userInstance.propertiesModel.modelId == args.model.modelId
5254
else {
53-
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSPropertiesModelStoreListener.getUpdateModelDelta encountered unsupported property: \(args.property) or no user instance")
55+
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSPropertiesModelStoreListener.getUpdateModelDelta encountered unsupported property: \(args.property), no user instance, or a properties model that is not the current user's")
5456
return nil
5557
}
5658
let identityModel = userInstance.identityModel

‎iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSSubscriptionModelStoreListener.swift‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ class OSSubscriptionModelStoreListener: OSModelStoreListener {
3939
}
4040

4141
func getAddModelDelta(_ model: OSSubscriptionModel) -> OSDelta? {
42-
guard let userInstance = OneSignalUserManagerImpl.sharedInstance._user else {
43-
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSSubscriptionModelStoreListener.getAddModelDelta has no user instance")
42+
// Stale - drop if the model is no longer in this store, the user has switched since
43+
guard let userInstance = OneSignalUserManagerImpl.sharedInstance._user,
44+
store.getModel(modelId: model.modelId) != nil
45+
else {
46+
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSSubscriptionModelStoreListener.getAddModelDelta has no user instance or the model is not in the current store")
4447
return nil
4548
}
4649
let identityModel = userInstance.identityModel
@@ -56,6 +59,7 @@ class OSSubscriptionModelStoreListener: OSModelStoreListener {
5659

5760
/**
5861
The `property` and `value` is not needed for a remove operation, so just pass in some model data as placeholders.
62+
Stamps the current user: `onRemoved` runs after the model has left the store, so membership can't be checked.
5963
*/
6064
func getRemoveModelDelta(_ model: OSSubscriptionModel) -> OSDelta? {
6165
guard let userInstance = OneSignalUserManagerImpl.sharedInstance._user else {

‎iOS_SDK/OneSignalSDK/OneSignalUserTests/DeltaOwnershipTests.swift‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,24 @@ final class DeltaOwnershipTests: XCTestCase {
110110
XCTAssertEqual(delta.identityModelId, first.identityModel.modelId)
111111
}
112112

113+
/// Stale properties model after a switch: drop rather than stamp the new current user.
114+
func testPropertiesUpdateDeltaIsDroppedWhenTheChangedModelIsNotTheCurrentUser() throws {
115+
let first = newUser(externalId: userA)
116+
newUser(externalId: userB)
117+
118+
let delta = manager.propertiesModelStoreListener.getUpdateModelDelta(
119+
OSModelChangedArgs(model: first.propertiesModel, property: "tags", newValue: ["tag": "value"])
120+
)
121+
122+
XCTAssertNil(delta)
123+
}
124+
113125
func testSubscriptionAddDeltaCarriesTheOwningUsersExternalId() throws {
114126
let user = newUser(externalId: userA)
127+
let model = emailSubscriptionModel()
128+
manager.subscriptionModelStore.add(id: emailAddress, model: model, hydrating: true)
115129

116-
let delta = try XCTUnwrap(manager.subscriptionModelStoreListener.getAddModelDelta(emailSubscriptionModel()))
130+
let delta = try XCTUnwrap(manager.subscriptionModelStoreListener.getAddModelDelta(model))
117131

118132
XCTAssertEqual(delta.externalId, userA)
119133
XCTAssertEqual(delta.identityModelId, user.identityModel.modelId)
@@ -139,6 +153,18 @@ final class DeltaOwnershipTests: XCTestCase {
139153
XCTAssertEqual(delta.identityModelId, user.identityModel.modelId)
140154
}
141155

156+
/// Email/SMS models are cleared on switch but stay subscribed; refuse to stamp the new user.
157+
func testSubscriptionAddDeltaIsDroppedWhenTheChangedModelIsNotInTheCurrentStore() throws {
158+
newUser(externalId: userA)
159+
let model = emailSubscriptionModel()
160+
manager.subscriptionModelStore.add(id: emailAddress, model: model, hydrating: true)
161+
// What makes the model stale is the store clear, which setNewInternalUser does not do.
162+
manager.clearAllModelsFromStores()
163+
newUser(externalId: userB)
164+
165+
XCTAssertNil(manager.subscriptionModelStoreListener.getAddModelDelta(model))
166+
}
167+
142168
// MARK: - Deltas the User Manager enqueues itself
143169

144170
/// Session time and custom events skip the model stores, so they are stamped at enqueue.

0 commit comments

Comments
 (0)