From 2d11a433c9e89130d37ebd90c9c8f395176a8a7a Mon Sep 17 00:00:00 2001 From: Ehren Kret Date: Wed, 15 Sep 2021 19:37:06 -0500 Subject: [PATCH] Wrap all calls to getAcceptableLanguages ContainerRequestContext#getAcceptableLanguages throws a ProcessingException if the header has invalid values in it. Rather than error out of the request entirely with the exception handler for that exception, we just treat it as though no Accept-Languages header was specified. --- .../controllers/ProfileController.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ProfileController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ProfileController.java index 21b22b158..f9657fb7b 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ProfileController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ProfileController.java @@ -10,7 +10,6 @@ import io.dropwizard.auth.Auth; import java.security.SecureRandom; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Optional; @@ -194,13 +193,8 @@ public class ProfileController { if (!isZkEnabled) { throw new WebApplicationException(Response.Status.NOT_FOUND); } - final List acceptableLanguages = new ArrayList<>(); - try { - acceptableLanguages.addAll(containerRequestContext.getAcceptableLanguages()); - } catch (final ProcessingException e) { - logger.warn("Could not get acceptable languages", e); - } - return getVersionedProfile(auth.map(AuthenticatedAccount::getAccount), accessKey, acceptableLanguages, uuid, + return getVersionedProfile(auth.map(AuthenticatedAccount::getAccount), accessKey, + getAcceptableLanguagesForRequest(containerRequestContext), uuid, version, Optional.empty()); } @@ -219,13 +213,8 @@ public class ProfileController { if (!isZkEnabled) { throw new WebApplicationException(Response.Status.NOT_FOUND); } - final List acceptableLanguages = new ArrayList<>(); - try { - acceptableLanguages.addAll(containerRequestContext.getAcceptableLanguages()); - } catch (final ProcessingException e) { - logger.warn("Could not get acceptable languages", e); - } - return getVersionedProfile(auth.map(AuthenticatedAccount::getAccount), accessKey, acceptableLanguages, uuid, + return getVersionedProfile(auth.map(AuthenticatedAccount::getAccount), accessKey, + getAcceptableLanguagesForRequest(containerRequestContext), uuid, version, Optional.of(credentialRequest)); } @@ -329,7 +318,7 @@ public class ProfileController { UserCapabilities.createForAccount(accountProfile.get()), username, accountProfile.get().getUuid(), - profileBadgeConverter.convert(containerRequestContext.getAcceptableLanguages(), accountProfile.get().getBadges()), + profileBadgeConverter.convert(getAcceptableLanguagesForRequest(containerRequestContext), accountProfile.get().getBadges()), null); } @@ -404,7 +393,7 @@ public class ProfileController { UserCapabilities.createForAccount(accountProfile.get()), username.orElse(null), null, - profileBadgeConverter.convert(containerRequestContext.getAcceptableLanguages(), accountProfile.get().getBadges()), + profileBadgeConverter.convert(getAcceptableLanguagesForRequest(containerRequestContext), accountProfile.get().getBadges()), null); } @@ -449,4 +438,13 @@ public class ProfileController { return "profiles/" + Base64.encodeBase64URLSafeString(object); } + + private List getAcceptableLanguagesForRequest(ContainerRequestContext containerRequestContext) { + try { + return containerRequestContext.getAcceptableLanguages(); + } catch (final ProcessingException e) { + logger.warn("Could not get acceptable languages", e); + return List.of(); + } + } }