Skip to content

Commit

Permalink
Closes #820 Improve handling of failed steam redirect validations #820
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-Shaml committed Jan 13, 2024
1 parent c293989 commit f9c1eeb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/faforever/api/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public enum ErrorCode {
LESS_PERMISSIVE_LICENSE(207, "Less permissive license", "New license is less permissive than current license."),
MALFORMED_URL(208, "Malformed URL", "Provided url ''{0}'' is malformed."),
NOT_ALLOWED_URL_HOST(209, "URL host not allowed", "Provided URL's host is not allowed. URL: ''{0}'', allowed hosts: ''{1}''."),
STEAM_LOGIN_VALIDATION_FAILED(210, "Login via Steam failed", "Invalid OpenID redirect code"),
;


Expand Down
34 changes: 32 additions & 2 deletions src/main/java/com/faforever/api/user/SteamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import com.faforever.api.config.FafApiProperties;
import com.faforever.api.config.FafApiProperties.Steam;
import com.faforever.api.data.domain.AccountLink;
import com.faforever.api.data.domain.LinkedServiceType;
import com.faforever.api.error.ApiException;
import com.faforever.api.error.ErrorCode;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Expand All @@ -22,6 +28,7 @@
@RequiredArgsConstructor
public class SteamService {
private final FafApiProperties properties;
private final AccountLinkRepository accountLinkRepository;

String buildLoginUrl(String redirectUrl) {
log.debug("Building steam login url for redirect url: {}", redirectUrl);
Expand Down Expand Up @@ -73,15 +80,38 @@ void validateSteamRedirect(HttpServletRequest request) {
String recodedUri = builder.toUriString().replace("+", "%2B");
log.debug("Verification uri: {}", recodedUri);

// the Spring RestTemplate still struggles with the + character so we use the default Java http client
// the Spring RestTemplate still struggles with the + character, so we use the default Java http client
String result = HttpClient.newHttpClient()
.send(HttpRequest.newBuilder(new URI(recodedUri)).build(), BodyHandlers.ofString())
.body();

if (result == null || !result.contains("is_valid:true")) {
throw new IllegalArgumentException("Steam redirect could not be validated! Original response:\n" + result);
handleInvalidOpenIdRedirect(request, result);

Check warning on line 89 in src/main/java/com/faforever/api/user/SteamService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/user/SteamService.java#L89

Added line #L89 was not covered by tests
} else {
log.debug("Steam response successfully validated.");
}
}

void handleInvalidOpenIdRedirect(final HttpServletRequest request, final String openIdResponseBody) {
final String steamId = parseSteamIdFromLoginRedirect(request);

Check warning on line 96 in src/main/java/com/faforever/api/user/SteamService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/user/SteamService.java#L96

Added line #L96 was not covered by tests

if (StringUtils.isNotBlank(steamId)) {
accountLinkRepository.findOneByServiceIdAndServiceType(steamId,
LinkedServiceType.STEAM).map(AccountLink::getUser).ifPresentOrElse(u ->
log.warn(

Check warning on line 101 in src/main/java/com/faforever/api/user/SteamService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/user/SteamService.java#L99-L101

Added lines #L99 - L101 were not covered by tests
"Steam redirect could not be validated for user with id: ''{}'' and login: ''{}''. Original OpenId response:\n{}",
u.getId(), u.getLogin(), openIdResponseBody),

Check warning on line 103 in src/main/java/com/faforever/api/user/SteamService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/user/SteamService.java#L103

Added line #L103 was not covered by tests
() ->
log.warn(

Check warning on line 105 in src/main/java/com/faforever/api/user/SteamService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/user/SteamService.java#L105

Added line #L105 was not covered by tests
"Steam redirect could not be validated! The steam id ''{}'' does not match any account. Original OpenId response:\n{}",
StringUtils.deleteWhitespace(steamId).replace("'", ""), // prevent potential log poisoning attack

Check warning on line 107 in src/main/java/com/faforever/api/user/SteamService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/user/SteamService.java#L107

Added line #L107 was not covered by tests
openIdResponseBody)
);
}
else {
log.warn("Steam redirect could not be validated! The steamId from the OpenId redirect is blank. Original OpenId response:\n{}", openIdResponseBody);

Check warning on line 112 in src/main/java/com/faforever/api/user/SteamService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/user/SteamService.java#L112

Added line #L112 was not covered by tests
}

throw ApiException.of(ErrorCode.STEAM_LOGIN_VALIDATION_FAILED);

Check warning on line 115 in src/main/java/com/faforever/api/user/SteamService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/api/user/SteamService.java#L115

Added line #L115 was not covered by tests
}
}

0 comments on commit f9c1eeb

Please sign in to comment.