Skip to content

Commit 400277e

Browse files
Prevent NPE when there is no subscriber for user events (#8260)
(cherry picked from commit 4f83a4c)
1 parent ec1744f commit 400277e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/user/AppSecEventTrackerSpecification.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,15 @@ class AppSecEventTrackerSpecification extends DDSpecification {
268268
thrown(BlockingException)
269269
}
270270
271+
void 'should not fail on null callback'() {
272+
when:
273+
tracker.onUserEvent(UserIdCollectionMode.IDENTIFICATION, 'test-user')
274+
275+
then:
276+
noExceptionThrown()
277+
provider.getCallback(EVENTS.user()) >> null
278+
}
279+
271280
private static class ActionFlow<T> implements Flow<T> {
272281
273282
private Action action

dd-java-agent/instrumentation/spring-security-5/src/main/java/datadog/trace/instrumentation/springsecurity5/AppSecDeferredContext.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package datadog.trace.instrumentation.springsecurity5;
22

33
import java.util.function.Supplier;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46
import org.springframework.security.core.context.SecurityContext;
57

68
public class AppSecDeferredContext implements Supplier<SecurityContext> {
79

10+
private static final Logger LOGGER = LoggerFactory.getLogger(AppSecDeferredContext.class);
11+
812
private final Supplier<SecurityContext> delegate;
913

1014
public AppSecDeferredContext(final Supplier<SecurityContext> delegate) {
@@ -15,7 +19,11 @@ public AppSecDeferredContext(final Supplier<SecurityContext> delegate) {
1519
public SecurityContext get() {
1620
SecurityContext context = delegate.get();
1721
if (context != null) {
18-
SpringSecurityUserEventDecorator.DECORATE.onUser(context.getAuthentication());
22+
try {
23+
SpringSecurityUserEventDecorator.DECORATE.onUser(context.getAuthentication());
24+
} catch (Throwable e) {
25+
LOGGER.debug("Error handling user event", e);
26+
}
1927
}
2028
return context;
2129
}

internal-api/src/main/java/datadog/trace/api/appsec/AppSecEventTracker.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ private <T> void dispatch(
142142
return;
143143
}
144144
final T callback = cbp.getCallback(event);
145+
if (callback == null) {
146+
return;
147+
}
145148
final Flow<Void> flow = consumer.apply(ctx, callback);
146149
if (flow == null) {
147150
return;

0 commit comments

Comments
 (0)