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

Added usersyncers cookie-family-name property validation #2790

Merged
merged 4 commits into from
Dec 21, 2023
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
33 changes: 31 additions & 2 deletions src/main/java/org/prebid/server/handler/SetuidHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.RoutingContext;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.activity.Activity;
Expand Down Expand Up @@ -52,10 +53,13 @@
import org.prebid.server.util.HttpUtil;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -103,13 +107,22 @@ public SetuidHandler(long defaultTimeout,
this.analyticsDelegator = Objects.requireNonNull(analyticsDelegator);
this.metrics = Objects.requireNonNull(metrics);
this.timeoutFactory = Objects.requireNonNull(timeoutFactory);
this.cookieNameToSyncType = collectMap(bidderCatalog);
}

private static Map<String, UsersyncMethodType> collectMap(BidderCatalog bidderCatalog) {

cookieNameToSyncType = bidderCatalog.names().stream()
final Supplier<Stream<Usersyncer>> usersyncers = () -> bidderCatalog.names()
.stream()
.filter(bidderCatalog::isActive)
.map(bidderCatalog::usersyncerByName)
.filter(Optional::isPresent)
.map(Optional::get)
.distinct() // built-in aliases looks like bidders with the same usersyncers
.distinct();

validateUsersyncers(usersyncers.get());

return usersyncers.get()
VeryExtraordinaryUsername marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toMap(Usersyncer::getCookieFamilyName, SetuidHandler::preferredUserSyncType));
}

Expand All @@ -121,6 +134,22 @@ private static UsersyncMethodType preferredUserSyncType(Usersyncer usersyncer) {
.get(); // when usersyncer is present, it will contain at least one method
}

private static void validateUsersyncers(Stream<Usersyncer> usersyncers) {
final List<String> cookieFamilyNameDuplicates = usersyncers.map(Usersyncer::getCookieFamilyName)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.filter(name -> name.getValue() > 1)
.map(Map.Entry::getKey)
.distinct()
.toList();
if (CollectionUtils.isNotEmpty(cookieFamilyNameDuplicates)) {
throw new IllegalArgumentException(
"Duplicated \"cookie-family-name\" found, values: "
+ String.join(", ", cookieFamilyNameDuplicates));
}
}

@Override
public void handle(RoutingContext routingContext) {
toSetuidContext(routingContext)
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/prebid/server/handler/SetuidHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.function.Executable;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
Expand Down Expand Up @@ -771,6 +773,39 @@ public void shouldPassSuccessfulEventToAnalyticsReporter() {
.build());
}

@Test
public void shouldThrowExceptionInCaseOfCookieFamilyNameDuplicates() {
// given
final Clock clock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
final String firstDuplicateName = "firstBidderWithDuplicate";
final String secondDuplicateName = "secondBidderWithDuplicate";
given(bidderCatalog.names())
.willReturn(new HashSet<>(asList(RUBICON, FACEBOOK, firstDuplicateName, secondDuplicateName)));
given(bidderCatalog.usersyncerByName(eq(firstDuplicateName))).willReturn(
Optional.of(Usersyncer.of(RUBICON, iframeMethod(), redirectMethod())));
given(bidderCatalog.usersyncerByName(eq(secondDuplicateName))).willReturn(
Optional.of(Usersyncer.of(FACEBOOK, iframeMethod(), redirectMethod())));
final Executable exceptionSource = () -> new SetuidHandler(
2000,
uidsCookieService,
applicationSettings,
bidderCatalog,
privacyEnforcementService,
gppService,
activityInfrastructureCreator,
tcfDefinerService,
analyticsReporterDelegator,
metrics,
new TimeoutFactory(clock));

//when
final IllegalArgumentException exception =
Assertions.assertThrows(IllegalArgumentException.class, exceptionSource);

//then
assertThat(exception).hasMessage("Duplicated \"cookie-family-name\" found, values: audienceNetwork, rubicon");
}

private String getUidsCookie() {
return httpResponse.headers().get("Set-Cookie");
}
Expand Down
Loading