-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix MM 56723 #7883
Merged
Merged
Fix MM 56723 #7883
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2927795
Fix MM 56723 (iOS)
larkox 811f579
Add android
larkox 57b6ee5
Android fixes and version checking
larkox 707c1ce
Add version check to ios
larkox be8a2ea
Merge branch 'main' into fixMM56723
larkox 402a592
Address feedback
larkox 1c9603a
Add all versions to android
larkox 63ef845
Check all versions on iOS
larkox ec2642e
Fix unhandled version case
larkox 94d2754
Add comments
larkox 6fca57b
Add final version numbers
larkox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
ios/Gekidou/Sources/Gekidou/PushNotification/PushNotification+Signature.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import Foundation | ||
import UserNotifications | ||
import os.log | ||
import SwiftJWT | ||
|
||
struct NotificationClaims : Claims { | ||
var ack_id: String; | ||
var device_id: String; | ||
} | ||
|
||
extension PushNotification { | ||
public func verifySignatureFromNotification(_ notification: UNMutableNotificationContent) -> Bool { | ||
return self.verifySignature(notification.userInfo) | ||
} | ||
public func verifySignature(_ userInfo: [AnyHashable : Any]) -> Bool { | ||
guard let signature = userInfo["signature"] as? String | ||
else { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: No signature in the notification" | ||
) | ||
return true | ||
enahum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
guard let serverId = userInfo["server_id"] as? String | ||
else { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: No server_id in the notification" | ||
) | ||
return false | ||
} | ||
|
||
guard let serverUrl = try? Database.default.getServerUrlForServer(serverId) | ||
else { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: No server_url for server_id" | ||
) | ||
return false | ||
} | ||
|
||
if signature == "NO_SIGNATURE" { | ||
guard let version = Database.default.getConfig(serverUrl, "Version") | ||
else { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: No server version" | ||
) | ||
return false | ||
} | ||
|
||
// Verify server version | ||
} | ||
|
||
guard let signingKey = Database.default.getConfig(serverUrl, "AsymmetricSigningPublicKey") | ||
else { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: No signing key" | ||
) | ||
return false | ||
} | ||
|
||
let keyPEM = """ | ||
-----BEGIN PUBLIC KEY----- | ||
\(signingKey) | ||
-----END PUBLIC KEY----- | ||
""" | ||
let jwtVerifier = JWTVerifier.es256(publicKey: keyPEM.data(using: .utf8)!) | ||
guard let newJWT = try? JWT<NotificationClaims>(jwtString: signature, verifier: jwtVerifier) | ||
else { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: Cannot verify the signature" | ||
) | ||
return false | ||
} | ||
|
||
guard let ackId = userInfo["ack_id"] as? String | ||
else { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: No ack_id in the notification" | ||
) | ||
return false | ||
} | ||
|
||
if (ackId != newJWT.claims.ack_id) { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: ackId is different" | ||
) | ||
return false | ||
} | ||
|
||
guard let storedDeviceToken = Database.default.getDeviceToken() | ||
enahum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: No device token" | ||
) | ||
return false | ||
} | ||
|
||
let tokenParts = storedDeviceToken.components(separatedBy: ":") | ||
if (tokenParts.count != 2) { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: Wrong stored device token format" | ||
) | ||
return false | ||
} | ||
let deviceToken = tokenParts[1].dropLast(1) | ||
if (deviceToken.isEmpty) { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: Empty stored device token" | ||
) | ||
return false | ||
} | ||
|
||
if (deviceToken != newJWT.claims.device_id) { | ||
os_log( | ||
OSLogType.default, | ||
"Mattermost Notifications: Signature verification: Device token is different" | ||
) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this for backward compatibility with older servers? If so, can we add a comment and ticket to make signature verification mandatory after, say, next two ESRs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for compatibility with old push proxies. I can add a comment about that.