Skip to content

Logback integrations run already-formatted log messages through String.format again: a % in a log argument can cause OutOfMemoryError #6715

Open

Description

@kdwittwer

Integration

sentry-logback

Java Version

17

Other Error Monitoring Solution

No

Other Error Monitoring Solution Name

No response

Version

8.22.0

Steps to Reproduce

Create a new Maven project with the following files
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>repro</groupId>
  <artifactId>sentry-logback-double-format</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>io.sentry</groupId>
      <artifactId>sentry-logback</artifactId>
      <version>8.22.0</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.5.18</version>
    </dependency>
  </dependencies>
</project>

src/main/resources/logback.xml

<configuration>
  <appender name="SENTRY" class="io.sentry.logback.SentryAppender"/>
  <root level="INFO">
    <appender-ref ref="SENTRY"/>
  </root>
</configuration>

src/main/java/Repro.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Repro {
  public static void main(String[] args) {
    Sentry.init(options -> {
      options.setDsn("https://key@localhost/1");
      options.getLogs().setEnabled(true);
      options.getLogs().setBeforeSend(log -> {
        System.out.println("Sent to Sentry (" + log.getBody().length() + " chars): " + log.getBody());
        return null;
      });
    });

    Logger log = LoggerFactory.getLogger(Repro.class);
    log.info("q={}", "PO%2010B");        // message corrupted
    log.info("q={}", "PO%2006007404B");  // OutOfMemoryError
  }
}

Run:
MAVEN_OPTS="-Xmx1g -XX:+ExitOnOutOfMemoryError" mvn -q compile exec:java -Dexec.mainClass=Repro

Expected Result

Both messages reach Sentry exactly as logback formatted them, and the program exits normally:

Sent to Sentry (11 chars): q=PO%2010B
Sent to Sentry (17 chars): q=PO%2006007404B

Actual Result

The first message reaches Sentry corrupted:
%2010B becomes TRUE padded to 2,010 characters.
The second kills the JVM with exit code 3:

Sent to Sentry (2014 chars): q=PO                    …about 2,000 spaces…                    TRUE

Terminating due to java.lang.OutOfMemoryError: Java heap space

In our case the input was "PO 06007404B". The %20 came from URL-encoding the space when the request URI was built. Rejecting or stripping % in user input would not have prevented it.

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

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions