Skip to content

Fixed NPE in user events tracking#5732

Merged
ValentinZakharov merged 2 commits into
masterfrom
vzakharov/npe_fix
Aug 17, 2023
Merged

Fixed NPE in user events tracking#5732
ValentinZakharov merged 2 commits into
masterfrom
vzakharov/npe_fix

Conversation

@ValentinZakharov

Copy link
Copy Markdown
Contributor

What Does This Do

Fixed SpringSecurity NullPointerException when user creation happens outside request context.

Motivation

Reported exception message in debug log:

[dd.trace 2023-08-16 12:27:42:204 +0000] [main] DEBUG datadog.trace.bootstrap.ExceptionLogger - Failed to handle exception in instrumentation for org.springframework.security.provisioning.InMemoryUserDetailsManager
java.lang.NullPointerException
        at datadog.trace.agent.core.CoreTracer.getTraceSegment(CoreTracer.java:1232)
        at datadog.trace.bootstrap.instrumentation.decorator.AppSecUserEventDecorator.getSegment(AppSecUserEventDecorator.java:68)
        at datadog.trace.bootstrap.instrumentation.decorator.AppSecUserEventDecorator.onSignup(AppSecUserEventDecorator.java:41)
        at datadog.trace.instrumentation.springsecurity5.SpringSecurityUserEventDecorator.onSignup(SpringSecurityUserEventDecorator.java:38)
        at org.springframework.security.provisioning.InMemoryUserDetailsManager.createUser(InMemoryUserDetailsManager.java:96)
        at org.springframework.security.provisioning.InMemoryUserDetailsManager.<init>(InMemoryUserDetailsManager.java:71)
        at org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.inMemoryUserDetailsManager(UserDetailsServiceAutoConfiguration.java:81)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)

@ValentinZakharov ValentinZakharov added type: bug fix Bug fix comp: asm waf Application Security Management (WAF) labels Aug 16, 2023
@ValentinZakharov
ValentinZakharov requested a review from a team as a code owner August 16, 2023 18:02
@ValentinZakharov ValentinZakharov self-assigned this Aug 16, 2023

@devinsba devinsba left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the JdbcUserDetailsManager has the same failure before this fix

@DependsOn("dataSourceInitializer")
public UserDetailsManager userDetailsService() {
return new JdbcUserDetailsManager(dataSource);
UserDetailsManager userDetailsManager = new JdbcUserDetailsManager(dataSource);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to make sure because this is a different impl than the exception in the issue, the JdbcUserDetailsManager also fails the tests without the fix?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm, the issue impacts all implementations, and fix will work for all of them.

@pr-commenter

pr-commenter Bot commented Aug 16, 2023

Copy link
Copy Markdown

Benchmarks

Startup

Parameters

Baseline Candidate
commit 1.20.0-SNAPSHOT~88fdf40003 1.20.0-SNAPSHOT~e74c44c1e7
config baseline candidate
See matching parameters
Baseline Candidate
application insecure-bank insecure-bank
module Agent Agent
parent None None
variant appsec appsec

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 62 cases.

Startup time reports for insecure-bank
gantt
    title insecure-bank - global startup overhead: candidate=1.20.0-SNAPSHOT~e74c44c1e7, baseline=1.20.0-SNAPSHOT~88fdf40003

    dateFormat X
    axisFormat %s
section tracing
Agent [baseline] (985.475 ms) : 0, 985475
Total [baseline] (8.657 s) : 0, 8657314
Agent [candidate] (995.43 ms) : 0, 995430
Total [candidate] (8.677 s) : 0, 8677433
section appsec
Agent [baseline] (1.07 s) : 0, 1070157
Total [baseline] (8.699 s) : 0, 8698629
Agent [candidate] (1.073 s) : 0, 1072993
Total [candidate] (8.743 s) : 0, 8743423
section iast
Agent [baseline] (1.088 s) : 0, 1088398
Total [baseline] (9.175 s) : 0, 9175361
Agent [candidate] (1.095 s) : 0, 1094757
Total [candidate] (9.21 s) : 0, 9209645
section profiling
Agent [baseline] (1.167 s) : 0, 1166759
Total [baseline] (8.889 s) : 0, 8889167
Agent [candidate] (1.157 s) : 0, 1157286
Total [candidate] (8.898 s) : 0, 8897693
Loading
  • baseline results
Module Variant Duration Δ tracing
Agent tracing 985.475 ms -
Agent appsec 1.07 s 84.682 ms (8.6%)
Agent iast 1.088 s 102.923 ms (10.4%)
Agent profiling 1.167 s 181.284 ms (18.4%)
Total tracing 8.657 s -
Total appsec 8.699 s 41.315 ms (0.5%)
Total iast 9.175 s 518.047 ms (6.0%)
Total profiling 8.889 s 231.853 ms (2.7%)
  • candidate results
Module Variant Duration Δ tracing
Agent tracing 995.43 ms -
Agent appsec 1.073 s 77.563 ms (7.8%)
Agent iast 1.095 s 99.327 ms (10.0%)
Agent profiling 1.157 s 161.857 ms (16.3%)
Total tracing 8.677 s -
Total appsec 8.743 s 65.99 ms (0.8%)
Total iast 9.21 s 532.213 ms (6.1%)
Total profiling 8.898 s 220.26 ms (2.5%)
gantt
    title insecure-bank - break down per module: candidate=1.20.0-SNAPSHOT~e74c44c1e7, baseline=1.20.0-SNAPSHOT~88fdf40003

    dateFormat X
    axisFormat %s
section tracing
BytebuddyAgent [baseline] (611.289 ms) : 0, 611289
BytebuddyAgent [candidate] (616.57 ms) : 0, 616570
GlobalTracer [baseline] (283.856 ms) : 0, 283856
GlobalTracer [candidate] (287.616 ms) : 0, 287616
AppSec [baseline] (50.0 ms) : 0, 50000
AppSec [candidate] (50.471 ms) : 0, 50471
Remote Config [baseline] (662.058 µs) : 0, 662
Remote Config [candidate] (675.69 µs) : 0, 676
Telemetry [baseline] (5.379 ms) : 0, 5379
Telemetry [candidate] (5.431 ms) : 0, 5431
section appsec
BytebuddyAgent [baseline] (610.261 ms) : 0, 610261
BytebuddyAgent [candidate] (612.019 ms) : 0, 612019
GlobalTracer [baseline] (284.067 ms) : 0, 284067
GlobalTracer [candidate] (285.381 ms) : 0, 285381
AppSec [baseline] (135.65 ms) : 0, 135650
AppSec [candidate] (135.4 ms) : 0, 135400
Remote Config [baseline] (732.303 µs) : 0, 732
Remote Config [candidate] (720.97 µs) : 0, 721
Telemetry [baseline] (5.16 ms) : 0, 5160
Telemetry [candidate] (5.142 ms) : 0, 5142
section iast
BytebuddyAgent [baseline] (716.153 ms) : 0, 716153
BytebuddyAgent [candidate] (719.312 ms) : 0, 719312
GlobalTracer [baseline] (271.184 ms) : 0, 271184
GlobalTracer [candidate] (273.694 ms) : 0, 273694
AppSec [baseline] (46.531 ms) : 0, 46531
AppSec [candidate] (46.933 ms) : 0, 46933
Remote Config [baseline] (625.487 µs) : 0, 625
Remote Config [candidate] (612.075 µs) : 0, 612
Telemetry [baseline] (5.254 ms) : 0, 5254
Telemetry [candidate] (5.198 ms) : 0, 5198
IAST [baseline] (14.225 ms) : 0, 14225
IAST [candidate] (14.292 ms) : 0, 14292
section profiling
BytebuddyAgent [baseline] (629.179 ms) : 0, 629179
BytebuddyAgent [candidate] (623.22 ms) : 0, 623220
GlobalTracer [baseline] (351.953 ms) : 0, 351953
GlobalTracer [candidate] (349.58 ms) : 0, 349580
AppSec [baseline] (49.522 ms) : 0, 49522
AppSec [candidate] (49.222 ms) : 0, 49222
Remote Config [baseline] (666.714 µs) : 0, 667
Remote Config [candidate] (652.748 µs) : 0, 653
Telemetry [baseline] (5.432 ms) : 0, 5432
Telemetry [candidate] (5.36 ms) : 0, 5360
ProfilingAgent [baseline] (76.625 ms) : 0, 76625
ProfilingAgent [candidate] (76.271 ms) : 0, 76271
Profiling [baseline] (76.649 ms) : 0, 76649
Profiling [candidate] (76.295 ms) : 0, 76295
Loading
Startup time reports for petclinic
gantt
    title petclinic - global startup overhead: candidate=1.20.0-SNAPSHOT~e74c44c1e7, baseline=1.20.0-SNAPSHOT~88fdf40003

    dateFormat X
    axisFormat %s
section tracing
Agent [baseline] (987.355 ms) : 0, 987355
Total [baseline] (9.182 s) : 0, 9182097
Agent [candidate] (989.567 ms) : 0, 989567
Total [candidate] (9.201 s) : 0, 9200797
section appsec
Agent [baseline] (1.072 s) : 0, 1072392
Total [baseline] (9.246 s) : 0, 9246146
Agent [candidate] (1.071 s) : 0, 1071500
Total [candidate] (9.286 s) : 0, 9286119
section iast
Agent [baseline] (1.087 s) : 0, 1087247
Total [baseline] (9.422 s) : 0, 9422402
Agent [candidate] (1.089 s) : 0, 1089205
Total [candidate] (9.472 s) : 0, 9471947
section profiling
Agent [baseline] (1.166 s) : 0, 1165913
Total [baseline] (9.52 s) : 0, 9519558
Agent [candidate] (1.16 s) : 0, 1160289
Total [candidate] (9.482 s) : 0, 9482069
Loading
  • baseline results
Module Variant Duration Δ tracing
Agent tracing 987.355 ms -
Agent appsec 1.072 s 85.037 ms (8.6%)
Agent iast 1.087 s 99.893 ms (10.1%)
Agent profiling 1.166 s 178.559 ms (18.1%)
Total tracing 9.182 s -
Total appsec 9.246 s 64.048 ms (0.7%)
Total iast 9.422 s 240.304 ms (2.6%)
Total profiling 9.52 s 337.461 ms (3.7%)
  • candidate results
Module Variant Duration Δ tracing
Agent tracing 989.567 ms -
Agent appsec 1.071 s 81.933 ms (8.3%)
Agent iast 1.089 s 99.638 ms (10.1%)
Agent profiling 1.16 s 170.722 ms (17.3%)
Total tracing 9.201 s -
Total appsec 9.286 s 85.323 ms (0.9%)
Total iast 9.472 s 271.15 ms (2.9%)
Total profiling 9.482 s 281.272 ms (3.1%)
gantt
    title petclinic - break down per module: candidate=1.20.0-SNAPSHOT~e74c44c1e7, baseline=1.20.0-SNAPSHOT~88fdf40003

    dateFormat X
    axisFormat %s
section tracing
BytebuddyAgent [baseline] (612.352 ms) : 0, 612352
BytebuddyAgent [candidate] (613.176 ms) : 0, 613176
GlobalTracer [baseline] (284.489 ms) : 0, 284489
GlobalTracer [candidate] (286.257 ms) : 0, 286257
AppSec [baseline] (50.075 ms) : 0, 50075
AppSec [candidate] (49.552 ms) : 0, 49552
Remote Config [baseline] (661.485 µs) : 0, 661
Remote Config [candidate] (664.848 µs) : 0, 665
Telemetry [baseline] (5.393 ms) : 0, 5393
Telemetry [candidate] (5.418 ms) : 0, 5418
section appsec
BytebuddyAgent [baseline] (612.459 ms) : 0, 612459
BytebuddyAgent [candidate] (611.982 ms) : 0, 611982
GlobalTracer [baseline] (283.959 ms) : 0, 283959
GlobalTracer [candidate] (284.445 ms) : 0, 284445
AppSec [baseline] (135.788 ms) : 0, 135788
AppSec [candidate] (135.011 ms) : 0, 135011
Remote Config [baseline] (724.521 µs) : 0, 725
Remote Config [candidate] (727.597 µs) : 0, 728
Telemetry [baseline] (5.172 ms) : 0, 5172
Telemetry [candidate] (5.124 ms) : 0, 5124
section iast
BytebuddyAgent [baseline] (714.848 ms) : 0, 714848
BytebuddyAgent [candidate] (715.247 ms) : 0, 715247
GlobalTracer [baseline] (271.37 ms) : 0, 271370
GlobalTracer [candidate] (272.571 ms) : 0, 272571
AppSec [baseline] (46.59 ms) : 0, 46590
AppSec [candidate] (46.83 ms) : 0, 46830
Remote Config [baseline] (618.526 µs) : 0, 619
Remote Config [candidate] (607.022 µs) : 0, 607
Telemetry [baseline] (5.288 ms) : 0, 5288
Telemetry [candidate] (5.197 ms) : 0, 5197
IAST [baseline] (14.33 ms) : 0, 14330
IAST [candidate] (14.285 ms) : 0, 14285
section profiling
BytebuddyAgent [baseline] (628.438 ms) : 0, 628438
BytebuddyAgent [candidate] (624.306 ms) : 0, 624306
GlobalTracer [baseline] (351.268 ms) : 0, 351268
GlobalTracer [candidate] (350.548 ms) : 0, 350548
AppSec [baseline] (49.548 ms) : 0, 49548
AppSec [candidate] (49.549 ms) : 0, 49549
Remote Config [baseline] (671.383 µs) : 0, 671
Remote Config [candidate] (655.586 µs) : 0, 656
Telemetry [baseline] (5.437 ms) : 0, 5437
Telemetry [candidate] (5.361 ms) : 0, 5361
ProfilingAgent [baseline] (77.35 ms) : 0, 77350
ProfilingAgent [candidate] (76.935 ms) : 0, 76935
Profiling [baseline] (77.375 ms) : 0, 77375
Profiling [candidate] (76.958 ms) : 0, 76958
Loading

Load

Parameters

Baseline Candidate
commit 1.20.0-SNAPSHOT~88fdf40003 1.20.0-SNAPSHOT~e74c44c1e7
config baseline candidate
end_time 2023-08-16T18:42:10 2023-08-16T19:00:19
start_time 2023-08-16T18:41:53 2023-08-16T19:00:01
See matching parameters
Baseline Candidate
application insecure-bank insecure-bank
variant appsec appsec

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 cases.

Request duration reports for insecure-bank
gantt
    title insecure-bank - request duration [CI 0.99] : candidate=1.20.0-SNAPSHOT~e74c44c1e7, baseline=1.20.0-SNAPSHOT~88fdf40003
    dateFormat X
    axisFormat %s
section baseline
no_agent (362.941 µs) : 342, 384
.   : milestone, 363,
appsec (673.167 µs) : 652, 694
.   : milestone, 673,
iast (468.493 µs) : 448, 489
.   : milestone, 468,
iast_FULL (530.378 µs) : 510, 551
.   : milestone, 530,
iast_INACTIVE (441.726 µs) : 421, 463
.   : milestone, 442,
profiling (448.922 µs) : 428, 470
.   : milestone, 449,
tracing (436.379 µs) : 416, 457
.   : milestone, 436,
section candidate
no_agent (364.646 µs) : 343, 386
.   : milestone, 365,
appsec (663.083 µs) : 642, 684
.   : milestone, 663,
iast (474.876 µs) : 454, 496
.   : milestone, 475,
iast_FULL (528.867 µs) : 508, 550
.   : milestone, 529,
iast_INACTIVE (437.262 µs) : 417, 458
.   : milestone, 437,
profiling (440.011 µs) : 419, 461
.   : milestone, 440,
tracing (431.568 µs) : 411, 452
.   : milestone, 432,
Loading
  • baseline results
Variant Request duration [CI 0.99] Δ no_agent
no_agent 362.941 µs [342.226 µs, 383.657 µs] -
appsec 673.167 µs [652.193 µs, 694.141 µs] 310.226 µs (85.5%)
iast 468.493 µs [447.552 µs, 489.435 µs] 105.552 µs (29.1%)
iast_FULL 530.378 µs [509.785 µs, 550.971 µs] 167.437 µs (46.1%)
iast_INACTIVE 441.726 µs [420.702 µs, 462.749 µs] 78.784 µs (21.7%)
profiling 448.922 µs [427.923 µs, 469.92 µs] 85.98 µs (23.7%)
tracing 436.379 µs [415.731 µs, 457.027 µs] 73.438 µs (20.2%)
  • candidate results
Variant Request duration [CI 0.99] Δ no_agent
no_agent 364.646 µs [343.299 µs, 385.993 µs] -
appsec 663.083 µs [642.019 µs, 684.146 µs] 298.437 µs (81.8%)
iast 474.876 µs [453.769 µs, 495.983 µs] 110.23 µs (30.2%)
iast_FULL 528.867 µs [508.022 µs, 549.712 µs] 164.222 µs (45.0%)
iast_INACTIVE 437.262 µs [416.618 µs, 457.907 µs] 72.617 µs (19.9%)
profiling 440.011 µs [419.073 µs, 460.949 µs] 75.366 µs (20.7%)
tracing 431.568 µs [411.349 µs, 451.788 µs] 66.923 µs (18.4%)
Request duration reports for petclinic
gantt
    title petclinic - request duration [CI 0.99] : candidate=1.20.0-SNAPSHOT~e74c44c1e7, baseline=1.20.0-SNAPSHOT~88fdf40003
    dateFormat X
    axisFormat %s
section baseline
no_agent (1.336 ms) : 1318, 1355
.   : milestone, 1336,
appsec (1.661 ms) : 1637, 1686
.   : milestone, 1661,
iast (1.473 ms) : 1449, 1497
.   : milestone, 1473,
profiling (1.469 ms) : 1444, 1493
.   : milestone, 1469,
tracing (1.454 ms) : 1430, 1478
.   : milestone, 1454,
section candidate
no_agent (1.338 ms) : 1319, 1357
.   : milestone, 1338,
appsec (1.686 ms) : 1662, 1710
.   : milestone, 1686,
iast (1.472 ms) : 1448, 1496
.   : milestone, 1472,
profiling (1.465 ms) : 1440, 1490
.   : milestone, 1465,
tracing (1.489 ms) : 1465, 1514
.   : milestone, 1489,
Loading
  • baseline results
Variant Request duration [CI 0.99] Δ no_agent
no_agent 1.336 ms [1.318 ms, 1.355 ms] -
appsec 1.661 ms [1.637 ms, 1.686 ms] 324.999 µs (24.3%)
iast 1.473 ms [1.449 ms, 1.497 ms] 136.44 µs (10.2%)
profiling 1.469 ms [1.444 ms, 1.493 ms] 132.094 µs (9.9%)
tracing 1.454 ms [1.43 ms, 1.478 ms] 117.707 µs (8.8%)
  • candidate results
Variant Request duration [CI 0.99] Δ no_agent
no_agent 1.338 ms [1.319 ms, 1.357 ms] -
appsec 1.686 ms [1.662 ms, 1.71 ms] 348.434 µs (26.0%)
iast 1.472 ms [1.448 ms, 1.496 ms] 133.855 µs (10.0%)
profiling 1.465 ms [1.44 ms, 1.49 ms] 127.423 µs (9.5%)
tracing 1.489 ms [1.465 ms, 1.514 ms] 151.728 µs (11.3%)

@ValentinZakharov
ValentinZakharov merged commit 5bb31ca into master Aug 17, 2023
@ValentinZakharov
ValentinZakharov deleted the vzakharov/npe_fix branch August 17, 2023 09:02
@github-actions github-actions Bot added this to the 1.20.0 milestone Aug 17, 2023
smola pushed a commit that referenced this pull request Aug 23, 2023
* Fixed SpringSecurity NullPointerException when user creation happens outside request.

* Spotless
@smola smola changed the title Fixed NPE in User Events Tracking Fixed NPE in user events tracking Aug 23, 2023
smola added a commit that referenced this pull request Aug 23, 2023
* Fixed Spring Security NullPointerException when user creation happens outside request.

Co-authored-by: Valentin Zakharov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: asm waf Application Security Management (WAF) type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants