Skip to content

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

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

add keyExcludes to MDC resolvers to prevent overwriting of structural…#4186
ramanathan1504 wants to merge 4 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

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