Skip to content

add keyExcludes to MDC resolvers to prevent overwriting of structural… - #4186

Open
ramanathan1504 wants to merge 7 commits into
2.xfrom
fix/issue-4166
Open

add keyExcludes to MDC resolvers to prevent overwriting of structural…#4186
ramanathan1504 wants to merge 7 commits into
2.xfrom
fix/issue-4166

Conversation

@ramanathan1504

Copy link
Copy Markdown
Contributor

Fixes #4166

Add keyExcludes to MDC resolvers to prevent user-controlled variables from overwriting structural log fields.


@Override
public void accept(final String key, final Object value, final LoopContext loopContext) {
if (loopContext.keyExcludes != null && loopContext.keyExcludes.contains(key)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about requiring loopContext.keyExcludes to be not null, so we don't need a null-check? The performance should be similar.

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.

are you describing using Collections.emptySet() to avoid the != null check? I can make that change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, this should not impact performance and would be easier to understand by developers. You can even add a JSpecify @NonNull annotation on the field, so it is clear it can not be null.

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.

@ppkarwasz ppkarwasz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @ramanathan1504,

Looks good to me, with two small comments:

@github-project-automation github-project-automation Bot moved this to Changes requested in Log4j pull request tracker Jul 22, 2026
@Measurement(iterations = 5, time = 1)
@Fork(1)
@State(Scope.Benchmark)
public class MdcKeyFilterBenchmark {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We probably should compare the performance of two ReadOnlyStringMapResolver instances, instead of comparing Pattern vs Map. This might give us an idea if the changes are worth it.

Map is clearly faster than Pattern, but the relative performance gain can not be determined, since it lacks the time needed to format the map as JSON.

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.

Benchmark                                       Mode  Cnt  Score   Error   Units
MdcKeyFilterBenchmark.testKeyExcludesResolver  thrpt    5  5.049 ± 0.175  ops/us
MdcKeyFilterBenchmark.testPatternResolver      thrpt    5  1.846 ± 0.023  ops/us

@ppkarwasz 5582862

@ramanathan1504

Copy link
Copy Markdown
Contributor Author

@vy done — keyExcludes is gone, it's literal with allowed and disallowed now, and the grammar block in the ReadOnlyStringMapResolver javadoc is updated to match the one u pasted.

Two things your example didn't pin down: disallowed currently wins over allowed, and when pattern is also set a key has to satisfy both — is that what u meant, or should allowed widen the pattern instead?

@ramanathan1504

Copy link
Copy Markdown
Contributor Author

@ppkarwasz reworked to the literal grammar @vy proposed on the issue, both your comments are still in — the sets are Collections.emptySet() when absent with @NonNull on the fields, and the benchmark compares two full layouts: 4.562 vs 1.833 ops/us.

A mis-shaped literal now throws the way flatten does, but a typo'd sub-key (disalowed) still parses and silently filters nothing — worth rejecting unknown keys under literal, or leave it as is?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Changes requested

Development

Successfully merging this pull request may close these issues.

JsonTemplateLayout: add MDC key filtering to ThreadContextMapResolver and harden EcsLayout.json

2 participants