Skip to content

Commit 2797204

Browse files
authored
LOG4J2-3211 - Remove Messge Lookups (#623)
* Remove Messge Lookups * Log a message that the option is no longer supported * LOG4J2-3211 - Log a message. Update doc * Add changes.xml entry. Don't limit visibility of LOGGER
1 parent a2028d6 commit 2797204

6 files changed

Lines changed: 21 additions & 61 deletions

File tree

log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Locale;
2222

23+
import org.apache.logging.log4j.Logger;
2324
import org.apache.logging.log4j.core.LogEvent;
2425
import org.apache.logging.log4j.core.config.Configuration;
2526
import org.apache.logging.log4j.core.config.plugins.Plugin;
@@ -38,25 +39,14 @@
3839
@ConverterKeys({ "m", "msg", "message" })
3940
@PerformanceSensitive("allocation")
4041
public class MessagePatternConverter extends LogEventPatternConverter {
41-
42+
4243
private static final String LOOKUPS = "lookups";
4344
private static final String NOLOOKUPS = "nolookups";
4445

4546
private MessagePatternConverter() {
4647
super("Message", "message");
4748
}
4849

49-
private static boolean loadLookups(final String[] options) {
50-
if (options != null) {
51-
for (final String option : options) {
52-
if (LOOKUPS.equalsIgnoreCase(option)) {
53-
return true;
54-
}
55-
}
56-
}
57-
return false;
58-
}
59-
6050
private static TextRenderer loadMessageRenderer(final String[] options) {
6151
if (options != null) {
6252
for (final String option : options) {
@@ -86,15 +76,11 @@ private static TextRenderer loadMessageRenderer(final String[] options) {
8676
* @return instance of pattern converter.
8777
*/
8878
public static MessagePatternConverter newInstance(final Configuration config, final String[] options) {
89-
boolean lookups = loadLookups(options);
9079
String[] formats = withoutLookupOptions(options);
9180
TextRenderer textRenderer = loadMessageRenderer(formats);
9281
MessagePatternConverter result = formats == null || formats.length == 0
9382
? SimpleMessagePatternConverter.INSTANCE
9483
: new FormattedMessagePatternConverter(formats);
95-
if (lookups && config != null) {
96-
result = new LookupMessagePatternConverter(result, config);
97-
}
9884
if (textRenderer != null) {
9985
result = new RenderingPatternConverter(result, textRenderer);
10086
}
@@ -107,7 +93,9 @@ private static String[] withoutLookupOptions(final String[] options) {
10793
}
10894
List<String> results = new ArrayList<>(options.length);
10995
for (String option : options) {
110-
if (!LOOKUPS.equalsIgnoreCase(option) && !NOLOOKUPS.equalsIgnoreCase(option)) {
96+
if (LOOKUPS.equalsIgnoreCase(option) || NOLOOKUPS.equalsIgnoreCase(option)) {
97+
LOGGER.info("The {} option will be ignored. Message Lookups are no longer supported.", option);
98+
} else {
11199
results.add(option);
112100
}
113101
}
@@ -164,30 +152,6 @@ public void format(final LogEvent event, final StringBuilder toAppendTo) {
164152
}
165153
}
166154

167-
private static final class LookupMessagePatternConverter extends MessagePatternConverter {
168-
private final MessagePatternConverter delegate;
169-
private final Configuration config;
170-
171-
LookupMessagePatternConverter(final MessagePatternConverter delegate, final Configuration config) {
172-
this.delegate = delegate;
173-
this.config = config;
174-
}
175-
176-
/**
177-
* {@inheritDoc}
178-
*/
179-
@Override
180-
public void format(final LogEvent event, final StringBuilder toAppendTo) {
181-
int start = toAppendTo.length();
182-
delegate.format(event, toAppendTo);
183-
int indexOfSubstitution = toAppendTo.indexOf("${", start);
184-
if (indexOfSubstitution >= 0) {
185-
config.getStrSubstitutor()
186-
.replaceIn(event, toAppendTo, indexOfSubstitution, toAppendTo.length() - indexOfSubstitution);
187-
}
188-
}
189-
}
190-
191155
private static final class RenderingPatternConverter extends MessagePatternConverter {
192156

193157
private final MessagePatternConverter delegate;

log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutLookupDateTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.apache.logging.log4j.test.appender.ListAppender;
2323
import org.junit.jupiter.api.Test;
2424

25-
import static org.junit.jupiter.api.Assertions.assertFalse;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
2626

2727
/**
2828
* See (LOG4J2-905) Ability to disable (date) lookup completely, compatibility issues with other libraries like camel.
@@ -38,7 +38,7 @@ public void testDateLookupInMessage(final LoggerContext context, @Named("List")
3838
final String template = "${date:YYYY-MM-dd}";
3939
context.getLogger(PatternLayoutLookupDateTest.class.getName()).info(template);
4040
final String string = listAppender.getMessages().get(0);
41-
assertFalse(string.contains(template), string);
41+
assertTrue(string.contains(template), string);
4242
}
4343

4444
}

log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void testLookup() {
121121
.setMessage(msg).build();
122122
final StringBuilder sb = new StringBuilder();
123123
converter.format(event, sb);
124-
assertEquals("bar", sb.toString(), "Unexpected result");
124+
assertEquals("${foo}", sb.toString(), "Unexpected result");
125125
}
126126

127127
@Test

log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/RegexReplacementTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testMessageReplacement() {
6767
List<String> msgs = app.getMessages();
6868
assertNotNull(msgs);
6969
assertEquals(1, msgs.size(), "Incorrect number of messages. Should be 1 is " + msgs.size());
70-
assertEquals("LoggerTest This is a test for Apache" + Strings.LINE_SEPARATOR, msgs.get(0));
70+
assertEquals("LoggerTest This is a test for ${ctx:MyKey}" + Strings.LINE_SEPARATOR, msgs.get(0));
7171
}
7272

7373
@Test

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
- "update" - Change
3030
- "remove" - Removed
3131
-->
32-
<release version="2.15.1" date="2021-12-11" description="GA Release 2.15.1">
32+
<release version="2.16.0" date="2021-12-13" description="GA Release 2.16.0">
3333
<action issue="LOG4J2-3208" dev="rgoers" type="fix">
3434
Disable JNDI by default. Require log4j2.enableJndi to be set to true to allow JNDI.
3535
</action>
36+
<action issue="LOG4J2-3211" dev="rgoers" type="fix">
37+
Completely remove support for Message Lookups.
38+
</action>
3639
</release>
3740
<release version="2.15.0" date="2021-12-06" description="GA Release 2.15.0">
3841
<!-- ADDS -->

src/site/xdoc/manual/layouts.xml.vm

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,14 +1460,19 @@ WARN [main]: Message 2</pre>
14601460
<tr>
14611461
<td align="center">
14621462
<a name="PatternMessage"/>
1463-
<b>m</b>{lookups}{ansi}<br />
1464-
<b>msg</b>{lookups}{ansi}<br />
1465-
<b>message</b>{lookups}{ansi}
1463+
<b>m</b>{ansi}<br />
1464+
<b>msg</b>{ansi}<br />
1465+
<b>message</b>{ansi}
14661466
</td>
14671467
<td>
14681468
<p>
14691469
Outputs the application supplied message associated with the logging event.
14701470
</p>
1471+
<p>
1472+
From Log4j 2.16.0, support for lookups in log messages has been removed for security reasons.
1473+
Both the<code>{lookups}</code> and the <code>{nolookups}</code> options on the %m, %msg and %message
1474+
pattern are now ignored. If either is specified a message will be logged.
1475+
</p>
14711476
<!-- Copied and tweaked from Javadoc for org.apache.logging.log4j.core.pattern.JAnsiMessageRenderer -->
14721477
<p>
14731478
Add <code>{ansi}</code> to render messages with ANSI escape codes (requires JAnsi,
@@ -1497,18 +1502,6 @@ WARN [main]: Message 2</pre>
14971502
The call site can look like this:
14981503
</p>
14991504
<pre class="prettyprint linenums">logger.info("@|KeyStyle {}|@ = @|ValueStyle {}|@", entry.getKey(), entry.getValue());</pre>
1500-
<p>
1501-
Use <code>{lookups}</code> to log messages like <code>logger.info("Try ${esc.d}{date:YYYY-MM-dd}")</code>
1502-
using lookups, this will replace the date template <code>${esc.d}{date:YYYY-MM-dd}</code>
1503-
with an actual date. This can be confusing in many cases, and it's often both easier and
1504-
more obvious to handle the lookup in code.
1505-
This feature is disabled by default and the message string is logged untouched.
1506-
</p>
1507-
<p>
1508-
<b>Note: </b>Users are <b>STRONGLY</b> discouraged from using the lookups option. Doing so may allow uncontrolled user input
1509-
containing lookups to take unintended actions. In almost all cases the software developer can accomplish the same tasks
1510-
lookups perform directly in the application code.
1511-
</p>
15121505
</td>
15131506
</tr>
15141507
<tr>

0 commit comments

Comments
 (0)