refactor(kobo): safer URI building in kobo server proxy (#2759)
This commit is contained in:
@@ -31,7 +31,9 @@ import java.util.regex.Pattern;
|
||||
public class KoboServerProxy {
|
||||
|
||||
private static final Pattern KOBO_API_PREFIX_PATTERN = Pattern.compile("^/api/kobo/[^/]+");
|
||||
private final String KOBO_BASE_URI = "https://storeapi.kobo.com";
|
||||
private final String KOBO_BOOK_IMAGE_CDN_URL = "https://cdn.kobo.com/book-images/{ImageId}/{Width}/{Height}/{IsGreyscale}/image.jpg";
|
||||
private final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
|
||||
private final HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofMinutes(1)).build();
|
||||
private final ObjectMapper objectMapper;
|
||||
private final BookloreSyncTokenGenerator bookloreSyncTokenGenerator;
|
||||
@@ -57,7 +59,6 @@ public class KoboServerProxy {
|
||||
|
||||
public ResponseEntity<JsonNode> proxyCurrentRequest(Object body, boolean includeSyncToken) {
|
||||
HttpServletRequest request = RequestUtils.getCurrentRequest();
|
||||
String path = KOBO_API_PREFIX_PATTERN.matcher(request.getRequestURI()).replaceFirst("");
|
||||
|
||||
BookloreSyncToken syncToken = null;
|
||||
if (includeSyncToken) {
|
||||
@@ -67,7 +68,7 @@ public class KoboServerProxy {
|
||||
}
|
||||
}
|
||||
|
||||
return executeProxyRequest(request, body, path, includeSyncToken, syncToken);
|
||||
return executeProxyRequest(request, body, includeSyncToken, syncToken);
|
||||
}
|
||||
|
||||
public URI getKoboCDNCoverUri(String imageId, int width, int height, boolean isGreyscale) {
|
||||
@@ -77,17 +78,26 @@ public class KoboServerProxy {
|
||||
.toUri();
|
||||
}
|
||||
|
||||
private ResponseEntity<JsonNode> executeProxyRequest(HttpServletRequest request, Object body, String path, boolean includeSyncToken, BookloreSyncToken syncToken) {
|
||||
private URI buildKoboURI(HttpServletRequest request) {
|
||||
String path = KOBO_API_PREFIX_PATTERN.matcher(request.getRequestURI()).replaceFirst("");
|
||||
String queryString = request.getQueryString();
|
||||
|
||||
URI uri = UriComponentsBuilder.fromUriString(KOBO_BASE_URI)
|
||||
.path(path)
|
||||
.build(true)
|
||||
.toUri();
|
||||
|
||||
if (queryString == null || queryString.isEmpty()) {
|
||||
return uri;
|
||||
}
|
||||
|
||||
return URI.create(uri + "?" + queryString);
|
||||
}
|
||||
|
||||
private ResponseEntity<JsonNode> executeProxyRequest(HttpServletRequest request, Object body, boolean includeSyncToken, BookloreSyncToken syncToken) {
|
||||
try {
|
||||
String koboBaseUrl = "https://storeapi.kobo.com";
|
||||
URI uri = buildKoboURI(request);
|
||||
|
||||
String queryString = request.getQueryString();
|
||||
String uriString = koboBaseUrl + path;
|
||||
if (queryString != null && !queryString.isBlank()) {
|
||||
uriString += "?" + queryString;
|
||||
}
|
||||
|
||||
URI uri = URI.create(uriString);
|
||||
log.debug("Kobo proxy URL: {}", uri);
|
||||
|
||||
String bodyString = body != null ? objectMapper.writeValueAsString(body) : "{}";
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
@@ -57,6 +58,9 @@ class KoboServerProxyTest {
|
||||
@InjectMocks
|
||||
private KoboServerProxy koboServerProxy;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor;
|
||||
|
||||
private MockHttpServletRequest mockRequest;
|
||||
|
||||
@BeforeEach
|
||||
@@ -89,6 +93,37 @@ class KoboServerProxyTest {
|
||||
.thenReturn(httpResponse);
|
||||
}
|
||||
|
||||
@Test
|
||||
void proxyCurrentRequest_shouldPreserveEncodedQueryParameters() throws Exception {
|
||||
String queryString = "Test&Filters=[%7BKey:TestKey%26ETag:W/TestEtagValue123%7D]";
|
||||
mockRequest.setQueryString(queryString);
|
||||
setupSuccessfulProxyResponse();
|
||||
|
||||
ResponseEntity<JsonNode> response = koboServerProxy.proxyCurrentRequest(null, false);
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
verify(httpClient).<String>send(httpRequestArgumentCaptor.capture(), any());
|
||||
|
||||
var requestUri = httpRequestArgumentCaptor.getValue().uri();
|
||||
assertThat(requestUri.getRawQuery()).isEqualTo(queryString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void proxyCurrentRequest_shouldPreserveEncodedPath() throws Exception {
|
||||
mockRequest.setRequestURI("/api/kobo/v1/library/test%20spaces");
|
||||
setupSuccessfulProxyResponse();
|
||||
|
||||
ResponseEntity<JsonNode> response = koboServerProxy.proxyCurrentRequest(null, false);
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
verify(httpClient).<String>send(httpRequestArgumentCaptor.capture(), any());
|
||||
|
||||
var requestUri = httpRequestArgumentCaptor.getValue().uri();
|
||||
assertThat(requestUri.getRawPath()).isEqualTo("/library/test%20spaces");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "Authorization", "authorization" })
|
||||
void proxyCurrentRequest_withSquareBracketsInQueryParams_shouldIncludeAuthHeaders(String authHeader) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user