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

Prevents a NPE when there is no subscriber for user events #8258

Merged
merged 1 commit into from
Jan 21, 2025
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
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
Loading