GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix NPE in AppSecConfigServiceImpl by manuel-alvarez-alvarez · Pull Request #9165 · DataDog/dd-trace-java

Fix NPE in AppSecConfigServiceImpl #9165

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.

Sign up for GitHub

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

Merged
manuel-alvarez-alvarez merged 1 commit into master from malvarez/fix-appsec-config-service-npe
Jul 15, 2025

Uh oh!

There was an error while loading. Please reload this page.

Merged

Fix NPE in AppSecConfigServiceImpl #9165

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
9a188f8
Fix NPE in AppSecConfigServiceImpl
manuel-alvarez-alvarez Jul 14, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

33 changes: 14 additions & 19 deletions 33 dd-java-agent/appsec/src/main/java/com/datadog/appsec/config/AppSecConfigServiceImpl.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public class AppSecConfigServiceImpl implements AppSecConfigService {
private final ConfigurationPoller configurationPoller;
private WafBuilder wafBuilder;

private MergedAsmFeatures mergedAsmFeatures;
private volatile boolean initialized;
private final MergedAsmFeatures mergedAsmFeatures = new MergedAsmFeatures();

private final ConcurrentHashMap<String, SubconfigListener> subconfigListeners =
new ConcurrentHashMap<>();
Expand Down Expand Up @@ -173,9 +172,7 @@ private class AppSecConfigChangesListener implements ProductListener {
@Override
public void accept(ConfigKey configKey, byte[] content, PollingRateHinter pollingRateHinter)
throws IOException {
if (!initialized) {
throw new IllegalStateException();
}
maybeInitializeDefaultConfig();

if (content == null) {
try {
Expand Down Expand Up @@ -219,8 +216,8 @@ public void accept(ConfigKey configKey, byte[] content, PollingRateHinter pollin
}
defaultConfigActivated = false;
}
super.accept(configKey, content, pollingRateHinter);
usedDDWafConfigKeys.add(configKey.toString());
super.accept(configKey, content, pollingRateHinter);
}

@Override
Expand Down Expand Up @@ -282,13 +279,7 @@ private void subscribeAsmFeatures() {
Product.ASM_FEATURES,
AppSecFeaturesDeserializer.INSTANCE,
(configKey, newConfig, hinter) -> {
if (!hasUserWafConfig && !defaultConfigActivated) {
// features activated in runtime
init();
}
if (!initialized) {
throw new IllegalStateException();
}
maybeInitializeDefaultConfig();
if (newConfig == null) {
mergedAsmFeatures.removeConfig(configKey);
} else {
Expand All @@ -305,10 +296,7 @@ private void subscribeAsmFeatures() {

private void distributeSubConfigurations(
String key, AppSecModuleConfigurer.Reconfiguration reconfiguration) {
if (usedDDWafConfigKeys.isEmpty() && !defaultConfigActivated && !hasUserWafConfig) {
// no config left in the WAF builder, add the default config
init();
}
maybeInitializeDefaultConfig();
for (Map.Entry<String, SubconfigListener> entry : subconfigListeners.entrySet()) {
SubconfigListener listener = entry.getValue();
try {
Expand All @@ -320,6 +308,13 @@ private void distributeSubConfigurations(
}
}

private void maybeInitializeDefaultConfig() {
Comment thread Outdated

@AlexeyKuznetsov-DD AlexeyKuznetsov-DD Jul 14, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggestion: Maybe initializeDefaultConfigIfNeeded() would be a better name here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@PerfectSlayer PerfectSlayer Jul 15, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The maybe pattern looks to be used here and there among the code base.
Not sure where it comes from, but that seems to spread to both common parts (agent) and products.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@manuel-alvarez-alvarez manuel-alvarez-alvarez Jul 15, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yep, there are other examples even in the same class (e.g. maybeSubscribeConfigPolling), I think it's better to leave it as it is for consistency.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
if (usedDDWafConfigKeys.isEmpty() && !hasUserWafConfig && !defaultConfigActivated) {
// no config left in the WAF builder, add the default config
init();
}
}

@Override
public void init() {
Map<String, Object> wafConfig;
Expand All @@ -341,8 +336,8 @@ public void init() {
} else {
hasUserWafConfig = true;
}
this.mergedAsmFeatures = new MergedAsmFeatures();
this.initialized = true;
this.mergedAsmFeatures.clear();
this.usedDDWafConfigKeys.clear();

if (wafConfig.isEmpty()) {
throw new IllegalStateException("Expected default waf config to be available");
Expand Down
5 changes: 5 additions & 0 deletions 5 dd-java-agent/appsec/src/main/java/com/datadog/appsec/config/MergedAsmFeatures.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ private void mergeAutoUserInstrum(
}
target.autoUserInstrum = newValue;
}

public void clear() {
mergedData = null;
configs.clear();
}
}
61 changes: 61 additions & 0 deletions 61 ...sec/src/test/groovy/com/datadog/appsec/config/AppSecConfigServiceImplSpecification.groovy
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,67 @@ class AppSecConfigServiceImplSpecification extends DDSpecification {
p.toFile().delete()
}

// https://github.com/DataDog/dd-trace-java/issues/9159
void 'test initialization issues while applying remote config'() {
setup:
final key = new ParsedConfigKey('Test', '1234', 1, 'ASM_DD', 'ID')
final service = new AppSecConfigServiceImpl(config, poller, reconf)
config.getAppSecActivation() >> ProductActivation.ENABLED_INACTIVE

when:
service.maybeSubscribeConfigPolling()

then:
1 * poller.addListener(Product.ASM_DD, _) >> {
listeners.savedWafDataChangesListener = it[1]
}

when:
listeners.savedWafDataChangesListener.accept(key, '''{"rules_override": [{"rules_target": [{"rule_id": "foo"}], "enabled": false}]}'''.getBytes(), NOOP)

then:
noExceptionThrown()
}

void 'config keys are added and removed to the set when receiving ASM_DD payloads'() {
setup:
final key = new ParsedConfigKey('Test', '1234', 1, 'ASM_DD', 'ID')
final service = new AppSecConfigServiceImpl(config, poller, reconf)
config.getAppSecActivation() >> ProductActivation.ENABLED_INACTIVE

when:
service.maybeSubscribeConfigPolling()

then:
1 * poller.addListener(Product.ASM_DD, _) >> {
listeners.savedWafDataChangesListener = it[1]
}
1 * poller.addListener(Product.ASM_FEATURES, _, _) >> {
listeners.savedFeaturesDeserializer = it[1]
listeners.savedFeaturesListener = it[2]
}

when:
listeners.savedFeaturesListener.accept('asm_features conf',
listeners.savedFeaturesDeserializer.deserialize('{"asm":{"enabled": true}}'.bytes),
NOOP)

then:
service.usedDDWafConfigKeys.empty

when:
listeners.savedWafDataChangesListener.accept(key, '''{"rules_override": [{"rules_target": [{"rule_id": "foo"}], "enabled": false}]}'''.getBytes(), NOOP)

then:
service.usedDDWafConfigKeys.toList() == [key.toString()]

when:
listeners.savedWafDataChangesListener.remove(key, NOOP)

then:
service.usedDDWafConfigKeys.empty
}

private static AppSecFeatures autoUserInstrum(String mode) {
return new AppSecFeatures().tap { features ->
features.autoUserInstrum = new AppSecFeatures.AutoUserInstrum().tap { instrum ->
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.