Skip to content
Merged
Show file tree
Hide file tree
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
34 changes: 32 additions & 2 deletions src/main/java/com/uid2/shared/attest/UidOptOutClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class UidOptOutClient extends UidCoreClient {
Expand Down Expand Up @@ -45,8 +47,7 @@ public InputStream download(String path) throws CloudStorageException {

if (this.attestationResponseHandler.getOptOutUrl() != null) {
try {
URL baseUrl = new URL(this.attestationResponseHandler.getOptOutUrl());
URL fullUrl = new URL(baseUrl, path);
URL fullUrl = buildOptOutUrl(this.attestationResponseHandler.getOptOutUrl(), path);
return super.download(fullUrl.toExternalForm());
} catch (MalformedURLException e) {
LOGGER.error("Unable to parse OptOut URL", e);
Expand All @@ -60,4 +61,33 @@ public InputStream download(String path) throws CloudStorageException {

return InputStream.nullInputStream();
}

/**
* Builds an opt-out URL from the scheme, host and port of {@code optOutBaseUrl} and only the path and query of
* {@code configuredLocation}, so the host always comes from the base URL.
*/
public static URL buildOptOutUrl(String optOutBaseUrl, String configuredLocation) throws MalformedURLException {
URL base = new URL(optOutBaseUrl);
String pathAndQuery = "";
if (configuredLocation != null && !configuredLocation.isEmpty()) {
URI location;
try {
location = new URI(configuredLocation.trim());
} catch (URISyntaxException e) {
throw new MalformedURLException("Invalid opt-out path: " + e.getReason() + " at index " + e.getIndex());
}
String scheme = location.getScheme();
if (location.isOpaque() || (scheme != null && !"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme))) {
throw new MalformedURLException("Invalid opt-out path");
}
String rawPath = location.getRawPath();
if (rawPath != null && !rawPath.isEmpty()) {
pathAndQuery = rawPath.startsWith("/") ? rawPath : "/" + rawPath;
}
if (location.getRawQuery() != null) {
pathAndQuery = (pathAndQuery.isEmpty() ? "/" : pathAndQuery) + "?" + location.getRawQuery();
}
}
return new URL(base.getProtocol(), base.getHost(), base.getPort(), pathAndQuery);
}
}
34 changes: 25 additions & 9 deletions src/test/java/com/uid2/shared/attest/UidOptOutClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void close() {

@BeforeEach
void setUp() {
reset(mockedLogger);
mockedLoggerFactory = mockStatic(LoggerFactory.class);
mockedLoggerFactory.when(() -> LoggerFactory.getLogger(any(Class.class))).thenReturn(mockedLogger);

Expand All @@ -54,11 +55,20 @@ void getJwtReturnsOptOutToken() {

@ParameterizedTest
@CsvSource(value = {
"/path1/path2?var=3;https://core.example.com/path1/path2?var=3",
";https://core.example.com",
"/;https://core.example.com/",
"https://core.example.com;/path1/path2?var=3;https://core.example.com/path1/path2?var=3",
"https://core.example.com;;https://core.example.com",
"https://core.example.com;/;https://core.example.com/",
"https://core.example.com;https://other.example/optout/refresh?x=1;https://core.example.com/optout/refresh?x=1",
"https://core.example.com;http://other.example:8081/optout/replicate;https://core.example.com/optout/replicate",
"https://core.example.com;//other.example/x;https://core.example.com/x",
"https://core.example.com;https://a//other.example/x;https://core.example.com//other.example/x",
"https://core.example.com;optout/refresh;https://core.example.com/optout/refresh",
"https://core.example.com;other.example/x;https://core.example.com/other.example/x",
"https://core.example.com;?x=1;https://core.example.com/?x=1",
"http://localhost:8081;https://other.example/optout/refresh;http://localhost:8081/optout/refresh",
"https://core.example.com;' /optout/refresh ';https://core.example.com/optout/refresh",
}, delimiter = ';')
void usesOptOutUrlForDownloadWithPath(String path, String expectedFullPath) throws CloudStorageException, IOException {
void usesOptOutUrlForDownloadWithPath(String optOutUrl, String path, String expectedFullPath) throws CloudStorageException, IOException {
HttpResponse<String> mockHttpResponse = mock(HttpResponse.class);
String expectedResponseBody = "Hello, world!";
when(mockHttpResponse.body()).thenReturn(expectedResponseBody);
Expand All @@ -67,7 +77,7 @@ void usesOptOutUrlForDownloadWithPath(String path, String expectedFullPath) thro

when(mockAttestationResponseHandler.attested()).thenReturn(true);
when(mockAttestationResponseHandler.getAttestationToken()).thenReturn("testAttToken");
when(mockAttestationResponseHandler.getOptOutUrl()).thenReturn("https://core.example.com");
when(mockAttestationResponseHandler.getOptOutUrl()).thenReturn(optOutUrl);

InputStream is = this.optOutClient.download(path);
verify(mockAttestationResponseHandler, times(2)).getOptOutUrl();
Expand All @@ -84,12 +94,18 @@ void returnsEmptyStreamWhenNoOptOutUrl() throws CloudStorageException, IOExcepti
verify(mockedLogger).warn("UidOptOutClient attempting to download but OptOutUrl not available");
}

@Test
void returnsEmptyStreamWhenMalformedUrl() throws CloudStorageException, IOException {
@ParameterizedTest
@CsvSource(value = {
"nowhere;/path",
"https://core.example.com;other.example:8081/path",
"https://core.example.com;/path with space",
"https://core.example.com;file:/tmp/optout/refresh",
}, delimiter = ';')
void returnsEmptyStreamWhenMalformedUrl(String optOutUrl, String path) throws CloudStorageException, IOException {
when(mockAttestationResponseHandler.attested()).thenReturn(true);
when(mockAttestationResponseHandler.getOptOutUrl()).thenReturn("nowhere");
when(mockAttestationResponseHandler.getOptOutUrl()).thenReturn(optOutUrl);

InputStream is = this.optOutClient.download("/path");
InputStream is = this.optOutClient.download(path);
assertEquals(0, is.available());
verify(mockedLogger).error(eq("Unable to parse OptOut URL"), any(MalformedURLException.class));
}
Expand Down
Loading