Skip to content

Bind rmqID instead of using stringWithFormat. #14856

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 2 commits into from
May 15, 2025
Merged
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
3 changes: 3 additions & 0 deletions FirebaseMessaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- [fixed] Fix a potential SQL injection issue. (#14846).

# 11.9.0
- [fixed] Migrate FCM codebase to new NSKeyedUnarchiver APIs. (#14424).

Expand Down
13 changes: 10 additions & 3 deletions FirebaseMessaging/Sources/FIRMessagingRmqManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ - (int64_t)queryLastRmqId {
- (FIRMessagingPersistentSyncMessage *)querySyncMessageWithRmqID:(NSString *)rmqID {
__block FIRMessagingPersistentSyncMessage *persistentMessage;
dispatch_sync(_databaseOperationQueue, ^{
NSString *queryFormat = @"SELECT %@ FROM %@ WHERE %@ = '%@'";
NSString *queryFormat = @"SELECT %@ FROM %@ WHERE %@ = ?";
NSString *query =
[NSString stringWithFormat:queryFormat,
kSyncMessagesColumns, // SELECT (rmq_id, expiration_ts,
// apns_recv, mcs_recv)
kTableSyncMessages, // FROM sync_rmq
kRmqIdColumn, // WHERE rmq_id
rmqID];
kRmqIdColumn // WHERE rmq_id
];

sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(self->_database, [query UTF8String], -1, &stmt, NULL) != SQLITE_OK) {
Expand All @@ -293,6 +293,13 @@ - (FIRMessagingPersistentSyncMessage *)querySyncMessageWithRmqID:(NSString *)rmq
return;
}

if (sqlite3_bind_text(stmt, 1, [rmqID UTF8String], (int)[rmqID length], SQLITE_STATIC) !=
SQLITE_OK) {
[self logError];
sqlite3_finalize(stmt);
return;
}

const int rmqIDColumn = 0;
const int expirationTimestampColumn = 1;
const int apnsReceivedColumn = 2;
Expand Down
15 changes: 15 additions & 0 deletions FirebaseMessaging/Tests/UnitTests/FIRMessagingRmqManagerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ - (void)testSavingSyncMessage {
XCTAssertFalse(persistentMessage.mcsReceived);
}

- (void)testQuerySyncMessageWithRmqID {
// This is to make sure there is no sql injection vulnerability.
// Otherwise, this would generate an SQL like this:
// SELECT ... FROM ... WHERE rmq_id = '' --';
// Which is a valid SQL and matches empty rmq_id.
NSString *rmqID = @"' --";
int64_t expirationTime = FIRMessagingCurrentTimestampInSeconds() + 1;
[self.rmqManager saveSyncMessageWithRmqID:rmqID expirationTime:expirationTime];

FIRMessagingPersistentSyncMessage *persistentMessage =
[self.rmqManager querySyncMessageWithRmqID:rmqID];
XCTAssertEqual(persistentMessage.expirationTime, expirationTime);
XCTAssertEqualObjects(persistentMessage.rmqID, rmqID);
}

/**
* Test updating a sync message initially received via MCS, now being received via APNS.
*/
Expand Down
Loading