Skip to content

Commit

Permalink
Fix NPE when there is no subscriber for user events
Browse files Browse the repository at this point in the history
(cherry picked from commit 4f83a4c)
  • Loading branch information
manuel-alvarez-alvarez committed Jan 21, 2025
1 parent ec1744f commit d78a868
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ class AppSecEventTrackerSpecification extends DDSpecification {
thrown(BlockingException)
}
void 'should not fail on null callback'() {
when:
tracker.onUserEvent(UserIdCollectionMode.IDENTIFICATION, 'test-user')
then:
noExceptionThrown()
provider.getCallback(EVENTS.user()) >> null
}
private static class ActionFlow<T> implements Flow<T> {
private Action action
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package datadog.trace.instrumentation.springsecurity5;

import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContext;

public class AppSecDeferredContext implements Supplier<SecurityContext> {

private static final Logger LOGGER = LoggerFactory.getLogger(AppSecDeferredContext.class);

private final Supplier<SecurityContext> delegate;

public AppSecDeferredContext(final Supplier<SecurityContext> delegate) {
Expand All @@ -15,7 +19,11 @@ public AppSecDeferredContext(final Supplier<SecurityContext> delegate) {
public SecurityContext get() {
SecurityContext context = delegate.get();
if (context != null) {
SpringSecurityUserEventDecorator.DECORATE.onUser(context.getAuthentication());
try {
SpringSecurityUserEventDecorator.DECORATE.onUser(context.getAuthentication());
} catch (Throwable e) {
LOGGER.debug("Error handling user event", e);
}
}
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ private <T> void dispatch(
return;
}
final T callback = cbp.getCallback(event);
if (callback == null) {
return;
}
final Flow<Void> flow = consumer.apply(ctx, callback);
if (flow == null) {
return;
Expand Down

0 comments on commit d78a868

Please sign in to comment.