Skip to content
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

Communication: Fix encryption issue in push notifications #10060

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@ public abstract class PushNotificationService implements InstantNotificationServ

protected final ObjectMapper mapper = new ObjectMapper();

private static final Cipher cipher;

static {
try {
cipher = Cipher.getInstance(Constants.PUSH_NOTIFICATION_ENCRYPTION_ALGORITHM);
}
catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException(e);
}
}

private static final Logger log = LoggerFactory.getLogger(PushNotificationService.class);

private final RestTemplate restTemplate;
Expand Down Expand Up @@ -204,11 +193,14 @@ record PushNotificationData(String[] notificationPlaceholders, String target, St
*/
private static Optional<String> encrypt(@NotNull String payload, SecretKey key, byte[] initializationVector) {
try {
// We need to get a fresh instance here for every notification to avoid a race condition between tasks
var cipher = Cipher.getInstance(Constants.PUSH_NOTIFICATION_ENCRYPTION_ALGORITHM);
Dismissed Show dismissed Hide dismissed

PaRangger marked this conversation as resolved.
Show resolved Hide resolved
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(initializationVector));

return Optional.of(Base64.getEncoder().encodeToString(cipher.doFinal(payload.getBytes(StandardCharsets.UTF_8))));
}
catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException e) {
log.error("Error encrypting push notification payload!", e);
return Optional.empty();
}
Expand Down
Loading