Skip to content

Commit 346fffa

Browse files
authored
fix(logging): Avoid formatting preformatted log messages (#6737)
* fix(logging): Avoid formatting preformatted log messages Let logging integrations mark message bodies as already formatted while preserving their original arguments as structured log attributes. This prevents percent sequences from user input from being interpreted during a second formatting pass. Fixes #6715 * changelog
1 parent e13909a commit 346fffa

11 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Fixes
1111

12+
- Prevent Logback, Log4j2, and JUL log arguments containing percent format sequences from being formatted a second time, which could corrupt messages or exhaust memory ([#6737](https://github.com/getsentry/sentry-java/pull/6737))
1213
- Nav3: Drop an entry’s arguments with `duplicate_key` when distinct map keys stringify to the same key, instead of silently overwriting a value ([#6714](https://github.com/getsentry/sentry-java/pull/6714))
1314
- Nav3: Serialize `NaN` and infinite navigation arguments as strings so they cannot prevent the containing error report or transaction from being serialized ([#6713](https://github.com/getsentry/sentry-java/pull/6713))
1415
- Kotlin now sees `Attachment.getBytes()`, `Device.getArchs()`, `Spring.getActiveProfiles()` and `JsonSerializationUtils.bytesFrom(...)` as nullable, because `@Nullable` now annotates the returned array instead of its elements ([#6558](https://github.com/getsentry/sentry-java/pull/6558))

‎sentry-jul/src/main/java/io/sentry/jul/SentryHandler.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ protected void captureLog(@NotNull LogRecord loggingEvent) {
172172

173173
final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
174174
params.setOrigin("auto.log.jul");
175+
params.setMessageAlreadyFormatted(true);
175176

176177
Sentry.logger().log(sentryLevel, params, formattedMessage, arguments);
177178
}

‎sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,24 @@ class SentryHandlerTest {
524524
)
525525
}
526526

527+
@Test
528+
fun `does not format an already formatted log message again`() {
529+
fixture = Fixture(minimumLevel = Level.SEVERE)
530+
fixture.logger.log(Level.SEVERE, "q={0}", arrayOf("PO%2010B"))
531+
532+
Sentry.flush(1000)
533+
534+
verify(fixture.transport)
535+
.send(
536+
checkLogs { logs ->
537+
val log = logs.items.first()
538+
assertEquals("q=PO%2010B", log.body)
539+
assertEquals("q={0}", log.attributes?.get("sentry.message.template")?.value)
540+
assertEquals("PO%2010B", log.attributes?.get("sentry.message.parameter.0")?.value)
541+
},
542+
)
543+
}
544+
527545
@Test
528546
fun `sets template on log when logging message with parameters and using printfStyle`() {
529547
fixture = Fixture(minimumLevel = Level.SEVERE, printfStyle = true)

‎sentry-log4j2/src/main/java/io/sentry/log4j2/SentryAppender.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {
265265

266266
final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
267267
params.setOrigin("auto.log.log4j2");
268+
params.setMessageAlreadyFormatted(true);
268269

269270
scopes.logger().log(sentryLevel, params, formattedMessage, arguments);
270271
}

‎sentry-log4j2/src/test/kotlin/io/sentry/log4j2/SentryAppenderTest.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,24 @@ class SentryAppenderTest {
579579
)
580580
}
581581

582+
@Test
583+
fun `does not format an already formatted log message again`() {
584+
val logger = fixture.getSut(minimumLevel = Level.ERROR)
585+
logger.error("q={}", "PO%2010B")
586+
587+
Sentry.flush(1000)
588+
589+
verify(fixture.transport)
590+
.send(
591+
checkLogs { logs ->
592+
val log = logs.items.first()
593+
assertEquals("q=PO%2010B", log.body)
594+
assertEquals("q={}", log.attributes?.get("sentry.message.template")?.value)
595+
assertEquals("PO%2010B", log.attributes?.get("sentry.message.parameter.0")?.value)
596+
},
597+
)
598+
}
599+
582600
@Test
583601
fun `sets template on log when logging message with parameters and number of parameters is wrong`() {
584602
val logger = fixture.getSut(minimumLevel = Level.ERROR)

‎sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ protected void captureLog(@NotNull ILoggingEvent loggingEvent) {
196196

197197
final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
198198
params.setOrigin("auto.log.logback");
199+
params.setMessageAlreadyFormatted(true);
199200

200201
Sentry.logger()
201202
.log(sentryLevel, params, formattedMessage, arguments != null ? arguments : new Object[0]);

‎sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,24 @@ class SentryAppenderTest {
845845
)
846846
}
847847

848+
@Test
849+
fun `does not format an already formatted log message again`() {
850+
fixture = Fixture(minimumLevel = Level.ERROR, enableLogs = true)
851+
fixture.logger.error("q={}", "PO%2010B")
852+
853+
Sentry.flush(1000)
854+
855+
verify(fixture.transport)
856+
.send(
857+
checkLogs { logs ->
858+
val log = logs.items.first()
859+
assertEquals("q=PO%2010B", log.body)
860+
assertEquals("q={}", log.attributes?.get("sentry.message.template")?.value)
861+
assertEquals("PO%2010B", log.attributes?.get("sentry.message.parameter.0")?.value)
862+
},
863+
)
864+
}
865+
848866
@Test
849867
fun `sets template on log when logging message with parameters and number of parameters is wrong`() {
850868
fixture = Fixture(minimumLevel = Level.ERROR, enableLogs = true)

‎sentry/api/sentry.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5620,7 +5620,9 @@ public final class io/sentry/logger/SentryLogParameters {
56205620
public fun getAttributes ()Lio/sentry/SentryAttributes;
56215621
public fun getOrigin ()Ljava/lang/String;
56225622
public fun getTimestamp ()Lio/sentry/SentryDate;
5623+
public fun isMessageAlreadyFormatted ()Z
56235624
public fun setAttributes (Lio/sentry/SentryAttributes;)V
5625+
public fun setMessageAlreadyFormatted (Z)V
56245626
public fun setOrigin (Ljava/lang/String;)V
56255627
public fun setTimestamp (Lio/sentry/SentryDate;)V
56265628
}

‎sentry/src/main/java/io/sentry/logger/LoggerApi.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ private void captureLog(
110110
final @Nullable SentryDate timestamp = params.getTimestamp();
111111
final @NotNull SentryDate timestampToUse =
112112
timestamp == null ? options.getDateProvider().now() : timestamp;
113-
final @NotNull String messageToUse = maybeFormatMessage(message, args);
113+
final @NotNull String messageToUse =
114+
params.isMessageAlreadyFormatted() ? message : maybeFormatMessage(message, args);
114115

115116
final @NotNull IScope combinedScope = scopes.getCombinedScopeView();
116117
final @NotNull PropagationContext propagationContext = combinedScope.getPropagationContext();

‎sentry/src/main/java/io/sentry/logger/SentryLogParameters.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public final class SentryLogParameters {
1010
private @Nullable SentryDate timestamp;
1111
private @Nullable SentryAttributes attributes;
1212
private @NotNull String origin = "manual";
13+
private boolean messageAlreadyFormatted = false;
1314

1415
public @Nullable SentryDate getTimestamp() {
1516
return timestamp;
@@ -35,6 +36,14 @@ public void setOrigin(final @NotNull String origin) {
3536
this.origin = origin;
3637
}
3738

39+
public boolean isMessageAlreadyFormatted() {
40+
return messageAlreadyFormatted;
41+
}
42+
43+
public void setMessageAlreadyFormatted(final boolean messageAlreadyFormatted) {
44+
this.messageAlreadyFormatted = messageAlreadyFormatted;
45+
}
46+
3847
public static @NotNull SentryLogParameters create(
3948
final @Nullable SentryDate timestamp, final @Nullable SentryAttributes attributes) {
4049
final @NotNull SentryLogParameters params = new SentryLogParameters();

0 commit comments

Comments
 (0)