Skip to content

Commit

Permalink
Add check for binary data in FCM data fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Jun 27, 2020
1 parent 8b86de4 commit d21925a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CHANGELOG

## [Unreleased]
### Changed
* Message data added to a with `CloudMessage::withData()` now rejects binary data to avoid broken
messages being sent to the Firebase API.
([#441](https://github.com/kreait/firebase-php/issues/441))

## [5.5.0] - 2020-06-19
### Added
Expand Down
12 changes: 12 additions & 0 deletions src/Firebase/Messaging/MessageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public static function fromArray(array $data): self
throw new InvalidArgumentException('Message data must be a one-dimensional array of string(able) keys and values.');
}

if (self::isBinary((string) $value)) {
throw new InvalidArgumentException(
"The message data field '{$key}' seems to contain binary data. As this can lead to broken messages, "
.'please convert it to a string representation first, e.g. with bin2hex() or base64encode().'
);
}

$messageData->data[(string) $key] = (string) $value;
}

Expand All @@ -48,4 +55,9 @@ private static function isStringable($value): bool
{
return \is_null($value) || \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString'));
}

private static function isBinary(string $value): bool
{
return \mb_detect_encoding($value) === false;
}
}
7 changes: 7 additions & 0 deletions tests/Unit/Messaging/MessageDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function __toString()
}
}],
],
'UTF-8 string' => [
['key' => 'Jérôme'],
],
];
}

Expand All @@ -68,6 +71,10 @@ public function invalidData()
'nested array' => [
['key' => ['sub_key' => 'sub_value']],
],
// @see https://github.com/kreait/firebase-php/issues/441
'binary data' => [
['key' => hex2bin('81612bcffb')], // generated with \openssl_random_pseudo_bytes(5)
],
];
}
}

0 comments on commit d21925a

Please sign in to comment.