-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(messaging): enforce NSSecureCoding for keychain registration tokens #16511
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e97b193
refactor(messaging): enforce NSSecureCoding for keychain registration…
ncooke3 ac695c4
Merge branch 'main' into nc.fcm.secure
ncooke3 0243697
consistent comment formatting
ncooke3 6b9f0e8
Apply suggestions from code review
ncooke3 7405be3
Apply suggestion from @ncooke3
ncooke3 04037c9
feedback
ncooke3 3546d52
tokeninfo changes
ncooke3 38b4b10
fix(messaging): adhere to Cocoa conventions for unarchiver and archiv…
ncooke3 1840ddb
fix(messaging): log decode errors in fallback and store unarchivers
ncooke3 ff87d00
Merge branch 'main' into nc.fcm.secure
ncooke3 8726b15
Update CHANGELOG.md
ncooke3 c9d61b5
Merge branch 'main' into nc.fcm.secure
ncooke3 f64484c
Merge branch 'main' into nc.fcm.secure
ncooke3 e60cee4
refactor(messaging): retire legacy 10.18 APNSInfo fallback for Fireba…
ncooke3 8a517e3
fix(messaging): decode legacy APNS info under secure coding
ncooke3 b63ef28
fix(messaging): report keychain archive failures consistently
ncooke3 94640fe
test(messaging): pin secure coding and close keychain compat gaps
ncooke3 23ec153
test(messaging): cover corrupt legacy APNS info blobs
ncooke3 7bb6d78
Merge branch 'main' into nc.fcm.secure
ncooke3 58329f1
Merge branch 'main' into nc.fcm.secure
ncooke3 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
127 changes: 127 additions & 0 deletions
127
FirebaseMessaging/Tests/UnitTests/FIRMessagingLegacyArchiveFixtures.h
This file contains hidden or 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,127 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
|
|
||
| #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.h" | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /// Flavours of a corrupt nested `apns_info` blob, ordered by how far into `NSKeyedUnarchiver` | ||
| /// they get before failing. | ||
| typedef NS_ENUM(NSInteger, FIRMessagingCorruptPayloadKind) { | ||
| /// Not a property list at all. | ||
| FIRMessagingCorruptPayloadKindNotAPropertyList, | ||
| /// A valid property list that is not a keyed archive. | ||
| FIRMessagingCorruptPayloadKindPropertyListButNotAnArchive, | ||
| /// A real keyed archive, truncated partway through. | ||
| FIRMessagingCorruptPayloadKindTruncatedArchive, | ||
| }; | ||
|
|
||
| /// Reproduces the `<= 10.18.0` *encoding* format, in which `apns_info` was written as a nested | ||
| /// `NSKeyedArchiver` blob rather than as a directly encoded object. | ||
| /// | ||
| /// Verified line-by-line against `-[FIRMessagingTokenInfo encodeWithCoder:]` at tag `10.18.0`: | ||
| /// | ||
| /// ``` | ||
| /// git show 10.18.0:FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.m | ||
| /// ``` | ||
| /// | ||
| /// Note that `token_type` is deliberately absent: that key was introduced after 10.18.0. | ||
| /// | ||
| /// > IMMUTABLE FIXTURE: do not "modernize" this encoder. It exists to emit bytes byte-for-byte | ||
| /// > equivalent to what 10.18.0 wrote. If a test using it fails, fix the production decoder | ||
| /// > rather than this mock. | ||
| @interface FIRMessagingTokenInfo_Legacy10_18 : FIRMessagingTokenInfo | ||
| @end | ||
|
|
||
| /// Reproduces the `12.x` *decoding* logic, used to prove that an older SDK can still read | ||
| /// archives written by the current one. | ||
| /// | ||
| /// Verified line-by-line against `-[FIRMessagingTokenInfo initWithCoder:]` at tag `12.19.0`: | ||
| /// | ||
| /// ``` | ||
| /// git show 12.19.0:FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.m | ||
| /// ``` | ||
| /// | ||
| /// The only intentional deviations, neither of which affects decoded output: | ||
| /// 1. Values are assigned with KVC because a subclass cannot reach the parent's ivars. | ||
| /// 2. The `FIRMessagingLoggerInfo` call in the `@catch` is omitted. | ||
| /// | ||
| /// > IMMUTABLE FIXTURE: do not "modernize" this decoder, and in particular do not delete the | ||
| /// > nested-blob branch. It is a record of what 12.x shipped. | ||
| @interface FIRMessagingTokenInfo_Legacy12 : FIRMessagingTokenInfo | ||
| @end | ||
|
|
||
| /// A stand-in for the "gadget class" in an Objective-C deserialization attack: it conforms to | ||
| /// `NSCoding` but deliberately not to `NSSecureCoding`, and it records whether the runtime ever | ||
| /// handed it a decoder. | ||
| /// | ||
| /// A `requiresSecureCoding = YES` unarchiver must refuse to construct it. A | ||
| /// `requiresSecureCoding = NO` unarchiver will happily run its `-initWithCoder:`, which is the | ||
| /// primitive the real vulnerability was built on. | ||
| @interface FIRMessagingArchiveGadget : NSObject <NSCoding> | ||
|
|
||
| /// YES once `-initWithCoder:` has run on any instance. Reset this in `-setUp`; it is global | ||
| /// because the point is to observe construction from code that never returns the object. | ||
| @property(class, nonatomic, assign) BOOL wasDecoded; | ||
|
|
||
| @end | ||
|
|
||
| /// Read and write paths lifted from released SDKs, so compatibility tests exercise the genuine | ||
| /// legacy logic instead of an approximation of it. | ||
| @interface FIRMessagingLegacyArchiveFixtures : NSObject | ||
|
|
||
| /// Archives `tokenInfo` the way `-[FIRMessagingTokenStore saveTokenInfo:handler:]` did at tag | ||
| /// `12.19.0`: the deprecated insecure API, with the root object renamed to | ||
| /// `FIRInstanceIDTokenInfo`. | ||
| + (NSData *)archiveWrittenBy12:(FIRMessagingTokenInfo *)tokenInfo; | ||
|
|
||
| /// Archives `tokenInfo` in the `<= 10.18.0` on-disk format. `tokenInfo` must be a | ||
| /// `FIRMessagingTokenInfo_Legacy10_18` so the legacy `encodeWithCoder:` runs. | ||
| + (NSData *)archiveWrittenBy10_18:(FIRMessagingTokenInfo_Legacy10_18 *)tokenInfo; | ||
|
|
||
| /// Decodes `item` exactly as `+[FIRMessagingTokenStore tokenInfoFromKeychainItem:]` did at tag | ||
| /// `12.19.0`, routing the root object to `FIRMessagingTokenInfo_Legacy12` so that 12.x's | ||
| /// `initWithCoder:` is what actually runs. | ||
| + (nullable FIRMessagingTokenInfo *)tokenInfoReadBy12:(NSData *)item; | ||
|
|
||
| /// A keychain item whose root object is a `FIRMessagingArchiveGadget` rather than a token info: | ||
| /// what a hostile process sharing the keychain access group would plant to attack the *outer* | ||
| /// unarchiver in `+[FIRMessagingTokenStore tokenInfoFromKeychainItem:]`. | ||
| + (NSData *)archiveOfGadget; | ||
|
|
||
| /// A keychain item in the `<= 10.18.0` shape whose nested `apns_info` blob carries a | ||
| /// `FIRMessagingArchiveGadget` instead of an APNSInfo: the same attack aimed at the *nested* | ||
| /// unarchiver in `-[FIRMessagingTokenInfo initWithCoder:]`, which is the one the legacy fallback | ||
| /// keeps reachable. | ||
| + (NSData *)archiveWithGadgetInNestedAPNSInfoForAuthorizedEntity:(NSString *)authorizedEntity | ||
| scope:(NSString *)scope | ||
| token:(NSString *)token; | ||
|
|
||
| /// A keychain item in the `<= 10.18.0` shape whose nested `apns_info` blob is corrupt rather than | ||
| /// hostile -- bit rot, a partial write, or a bug in some other writer. Distinct from the gadget | ||
| /// case because these fail at different depths of `NSKeyedUnarchiver`, and the shallow ones are | ||
| /// the ones that can raise rather than return an error. | ||
| + (NSData *)archiveWithCorruptNestedAPNSInfoForAuthorizedEntity:(NSString *)authorizedEntity | ||
| scope:(NSString *)scope | ||
| token:(NSString *)token | ||
| payloadKind: | ||
| (FIRMessagingCorruptPayloadKind)kind; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
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.
Uh oh!
There was an error while loading. Please reload this page.