You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24-6Lines changed: 24 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -173,15 +173,31 @@ $res = array(
173
173
);
174
174
```
175
175
176
-
### Payload length and security
177
-
Payload will be encrypted by the library. The maximum payload length is 4078 bytes (or ASCII characters).
176
+
### Payload length, security, and performance
177
+
Payloads are encrypted by the library. The maximum payload length is theoretically 4078 bytes (or ASCII characters).
178
+
For [compatibility reasons](mozilla-services/autopush/issues/748) though, your payload should be less than 3052 bytes long.
178
179
179
-
However, when you encrypt a string of a certain length, the resulting string will always have the same length,
180
+
The library pads the payload by default. This is more secure but it decreases performance for both your server and your user's device.
181
+
182
+
#### Why is it more secure?
183
+
When you encrypt a string of a certain length, the resulting string will always have the same length,
180
184
no matter how many times you encrypt the initial string. This can make attackers guess the content of the payload.
181
-
In order to circumvent this, this library can add some null padding to the initial payload, so that all the input of the encryption process
185
+
In order to circumvent this, this library adds some null padding to the initial payload, so that all the input of the encryption process
182
186
will have the same length. This way, all the output of the encryption process will also have the same length and attackers won't be able to
183
-
guess the content of your payload. The downside of this approach is that you will use more bandwidth than if you didn't pad the string.
184
-
That's why the library provides the option to disable this security measure:
187
+
guess the content of your payload.
188
+
189
+
#### Why does it decrease performance?
190
+
Encrypting more bytes takes more runtime on your server, and also slows down the user's device with decryption. Moreover, sending and receiving the packet will take more time.
191
+
It's also not very friendly with users who have limited data plans.
192
+
193
+
#### How can I disable or customize automatic padding?
194
+
You can customize automatic padding in order to better fit your needs.
195
+
196
+
Here are some ideas of settings:
197
+
* (default) `Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH` (3052 bytes) for compatibility purposes with Firefox for Android
198
+
*`Encryption::MAX_PAYLOAD_LENGTH` (4078 bytes) for maximum security
199
+
*`false` for maximum performance
200
+
* If you know your payloads will not exceed `X` bytes, then set it to `X` for the best balance between security and performance.
185
201
186
202
```php
187
203
<?php
@@ -190,6 +206,8 @@ use Minishlink\WebPush\WebPush;
$webPush->setAutomaticPadding(512); // enable automatic padding to 512 bytes (you should make sure that your payload is less than 512 bytes, or else an attacker could guess the content)
210
+
$webPush->setAutomaticPadding(true); // enable automatic padding to default maximum compatibility length
if ($automaticPadding > Encryption::MAX_PAYLOAD_LENGTH) {
318
+
thrownew \Exception('Automatic padding is too large. Max is '.Encryption::MAX_PAYLOAD_LENGTH.'. Recommended max is '.Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH.' for compatibility reasons (see README).');
319
+
} elseif ($automaticPadding < 0) {
320
+
thrownew \Exception('Padding length should be positive or zero.');
0 commit comments