Exploit database

GH-343c8816550f9b1931de47afeb46a592

sassoftware/jackson

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2025-52999

1 source

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars.

Repository files44
sassoftware/jacksonRevision 010aa1901029

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

src/main/java/com/fasterxml/jackson/core/StreamReadConstraints.java

Java · 16,008 bytes
package com.fasterxml.jackson.core;

import com.fasterxml.jackson.core.exc.StreamConstraintsException;

/**
 * The constraints to use for streaming reads: used to guard against malicious
 * input by preventing processing of "too big" input constructs (values,
 * structures).
 * Constraints are registered with {@code TokenStreamFactory} (such as
 * {@code JsonFactory}); if nothing explicitly specified, default
 * constraints are used.
 *<p>
 * Currently constrained aspects, with default settings, are:
 * <ul>
 *  <li>Maximum Number value length: default 1000 (see {@link #DEFAULT_MAX_NUM_LEN})
 *   </li>
 *  <li>Maximum String value length: default 5_000_000 (see {@link #DEFAULT_MAX_STRING_LEN})
 *   </li>
 *  <li>Maximum Nesting depth: default 1000 (see {@link #DEFAULT_MAX_DEPTH})
 *   </li>
 * </ul>
 *
 * @since 2.15
 */
public class StreamReadConstraints
    implements java.io.Serializable
{
    private static final long serialVersionUID = 1L;

    /**
     * Default maximum nesting depth. Chosen to balance legitimate document
     * complexity and protection against deeply-nested attacker-controlled
     * payloads that could lead to stack or memory exhaustion.
     *
     * This limit helps mitigate vulnerabilities that rely on unbounded
     * nesting (for example, CVE-2025-52999) by aborting parsing when
     * nesting exceeds a safe threshold, preventing stack or memory exhaustion.
     *
     * See {@link Builder#maxNestingDepth(int)} for configuration details.
     */
    public static final int DEFAULT_MAX_DEPTH = 1000;

    /**
     * Default maximum numeric token length. This limit reduces the risk of
     * pathological numeric tokens that force excessive parsing work or buffer
     * growth when decoding values from untrusted sources. Extremely long
     * numeric tokens can be used to consume CPU and memory (for example,
     * by triggering repeated buffer expansion or expensive numeric conversions);
     * enforcing a maximum numeric token length bounds work and memory use,
     * reducing the risk of parser denial-of-service.
     *
     * See {@link Builder#maxNumberLength(int)} for configuration details.
     */

    public static final int DEFAULT_MAX_NUM_LEN = 1000;

    /**
     * Default maximum string length. Protects against very large string values
     * that would allocate large buffers or otherwise consume excessive memory.
     *
     * See {@link Builder#maxStringLength(int)} for configuration details.
     */
    public static final int DEFAULT_MAX_STRING_LEN = 1000000;

    /**
     * Maximum allowed magnitude (absolute value) of {@link java.math.BigDecimal} scale
     * during a conversion to {@link java.math.BigInteger}. Scales beyond this magnitude
     * can force allocation of very large arrays.
     */
    public static final int MAX_BIGINT_SCALE_MAGNITUDE = 100000;

    /**
     * Default setting for maximum document length:
     * see {@link Builder#maxDocumentLength(long)} for details.
     *
     * @since 2.15
     */
    public static final long DEFAULT_MAX_DOC_LENGTH = Long.MAX_VALUE;

    protected final int _maxNestingDepth;
    protected final int _maxNumLen;
    protected final int _maxStringLen;
    protected final long _maxDocLen;

    private static final StreamReadConstraints DEFAULT =
        new StreamReadConstraints(DEFAULT_MAX_DEPTH, DEFAULT_MAX_NUM_LEN, DEFAULT_MAX_STRING_LEN,
                DEFAULT_MAX_DOC_LENGTH);

    public static final class Builder {
        private int maxNestingDepth;
        private int maxNumLen;
        private int maxStringLen;
        private long maxDocLen;

        /**
         * Configure the maximum nesting depth to accept. Limiting nesting depth
         * prevents malicious or accidental inputs from creating extremely deep
         * object/array structures that can exhaust parser resources.
         *
         * This helps mitigate vulnerabilities that rely on unbounded nesting
         * (e.g. CVE-2025-52999) by aborting parsing when nesting exceeds a
         * configured safe threshold.
         *
         * @param maxNestingDepth the maximum allowed nesting depth (must be >= 0)
         *
         * @return this builder
         * @throws IllegalArgumentException if {@code maxNestingDepth} is negative
         *
         * @since 2.15
         */
        public Builder maxNestingDepth(final int maxNestingDepth) {
            if (maxNestingDepth < 0) {
                throw new IllegalArgumentException("Cannot set maxNestingDepth to a negative value");
            }
            this.maxNestingDepth = maxNestingDepth;
            return this;
        }

        /**
         * Configure the maximum length for numeric tokens (characters or bytes,
         * depending on input). Restricting numeric token length reduces the
         * likelihood of excessive CPU or buffer growth caused by pathological
         * numeric values in untrusted input.
         *
         * @param maxNumLen maximum allowed numeric token length (must be >= 0)
         *
         * @return this builder
         * @throws IllegalArgumentException if {@code maxNumLen} is negative
         *
         * @since 2.15
         */
        public Builder maxNumberLength(final int maxNumLen) {
            if (maxNumLen < 0) {
                throw new IllegalArgumentException("Cannot set maxNumberLen to a negative value");
            }
            this.maxNumLen = maxNumLen;
            return this;
        }

        /**
         * Configure the maximum length for string values (characters or bytes,
         * depending on input). This limit helps avoid unbounded memory
         * allocation when buffering or constructing string contents from input.
         * Note that enforcement is applied at buffer growth points, so the
         * observed failure may occur when internal buffers expand beyond this
         * threshold.
         *
         * @param maxStringLen maximum allowed string length (must be >= 0)
         *
         * @return this builder
         * @throws IllegalArgumentException if {@code maxStringLen} is negative
         *
         * @since 2.15
         */
        public Builder maxStringLength(final int maxStringLen) {
            if (maxStringLen < 0) {
                throw new IllegalArgumentException("Cannot set maxStringLen to a negative value");
            }
            this.maxStringLen = maxStringLen;
            return this;
        }

        /**
         * Configure the maximum document length (in input units, bytes or chars).
         * Document-length checking is disabled when set to {@code Long.MAX_VALUE}
         * (the default).
         *
         * @param maxDocLen maximum allowed document length (must be positive)
         *
         * @return this builder
         * @throws IllegalArgumentException if {@code maxDocLen} is not positive
         *
         * @since 2.15
         */
        public Builder maxDocumentLength(final long maxDocLen) {
            if (maxDocLen <= 0) {
                throw new IllegalArgumentException("Cannot set maxDocumentLength to a non-positive value");
            }
            this.maxDocLen = maxDocLen;
            return this;
        }

        Builder() {
            this(DEFAULT_MAX_DEPTH, DEFAULT_MAX_NUM_LEN, DEFAULT_MAX_STRING_LEN,
                    DEFAULT_MAX_DOC_LENGTH);
        }

        Builder(final int maxNestingDepth, final int maxNumLen, final int maxStringLen,
                final long maxDocLen) {
            this.maxNestingDepth = maxNestingDepth;
            this.maxNumLen = maxNumLen;
            this.maxStringLen = maxStringLen;
            this.maxDocLen = maxDocLen;
        }

        Builder(StreamReadConstraints src) {
            maxNestingDepth = src._maxNestingDepth;
            maxNumLen = src._maxNumLen;
            maxStringLen = src._maxStringLen;
            maxDocLen = src._maxDocLen;
        }

        public StreamReadConstraints build() {
            return new StreamReadConstraints(maxNestingDepth, maxNumLen, maxStringLen,
                    maxDocLen);
        }
    }

    /**
     * Constructor used by {@link Builder}.
     *
     * @param maxNestingDepth configured maximum nesting depth
     * @param maxNumLen configured maximum numeric token length
     * @param maxStringLen configured maximum string length
     */
    protected StreamReadConstraints(final int maxNestingDepth, final int maxNumLen,
            final int maxStringLen, final long maxDocLen) {
        _maxNestingDepth = maxNestingDepth;
        _maxNumLen = maxNumLen;
        _maxStringLen = maxStringLen;
        _maxDocLen = maxDocLen;
    }

    /**
     * Create a fresh {@link Builder} with default settings.
     *
     * @return a new builder initialized to project defaults
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Returns a shared, immutable default {@link StreamReadConstraints} instance.
     * Use this when no custom limits are required; callers that need to change
     * limits should obtain a {@link Builder} instead.
     *
     * @return default constraints instance
     */
    public static StreamReadConstraints defaults() {
        return DEFAULT;
    }

    /**
     * Create a {@link Builder} pre-populated with values from this instance.
     * This is useful to tweak one or two limits while keeping others unchanged.
     *
     * @return a builder initialized from this constraints instance
     */
    public Builder rebuild() {
        return new Builder(this);
    }

    /*
     * Accessor methods return the effective constraint values. These are
     * intended for callers that need to inspect, report, or log the
     * currently configured limits.
     */

    /**
     * Returns the configured maximum nesting depth used to limit parser
     * recursion and resource usage for deeply-nested inputs.
     *
     * @return maximum allowed depth
     */
    public int getMaxNestingDepth() {
        return _maxNestingDepth;
    }

    /**
     * Returns the configured maximum numeric token length used to avoid
     * excessive parsing cost or buffer growth for unusually long numeric
     * values.
     *
     * @return maximum allowed number length
     */
    public int getMaxNumberLength() {
        return _maxNumLen;
    }

    /**
     * Returns the configured maximum string length used to limit memory
     * allocation for large string values coming from untrusted input.
     *
     * @return maximum allowed string length
     */
    public int getMaxStringLength() {
        return _maxStringLen;
    }

    /**
     * Returns the configured maximum document length, or {@code Long.MAX_VALUE}
     * if no limit has been configured.
     *
     * @return maximum allowed document length
     *
     * @since 2.15
     */
    public long getMaxDocumentLength() {
        return _maxDocLen;
    }

    /**
     * Returns {@code true} if a document-length limit has been explicitly
     * configured (i.e., is not the default {@code Long.MAX_VALUE}).
     *
     * @return whether a document length limit is active
     *
     * @since 2.15
     */
    public boolean hasMaxDocumentLength() {
        return _maxDocLen != DEFAULT_MAX_DOC_LENGTH;
    }

    /**
     * Validate that the provided nesting depth does not exceed the
     * configured maximum. This check exists to proactively detect and
     * abort parsing of inputs that would otherwise require excessive
     * resources to process. This validation helps mitigate vulnerabilities
     * that exploit unbounded nesting (for example, CVE-2025-52999) by
     * aborting parsing when nesting would otherwise lead to stack or
     * memory exhaustion.
     *
     * @param depth count of unclosed objects and arrays
     *
     * @throws StreamConstraintsException if {@code depth} exceeds the
     *   configured maximum
     */
    public void validateNestingDepth(int depth) throws StreamConstraintsException
    {
        if (depth > _maxNestingDepth) {
            throw new StreamConstraintsException(String.format("Depth (%d) exceeds the maximum allowed nesting depth (%d)",
                    depth, _maxNestingDepth), null);
        }
    }


   /**
     * Convenience method that can be used to verify that a floating-point
     * number of specified length does not exceed maximum specified by this
     * constraints object: if it does, a
     * {@link StreamConstraintsException}
     * is thrown.
     *
     * @param length Length of number in input units
     *
     * @throws StreamConstraintsException If length exceeds maximum
     */
    public void validateFPLength(int length) throws StreamConstraintsException
    {
        if (length > _maxNumLen) {
            throw new StreamConstraintsException(String.format("Number length (%d) exceeds the maximum length (%d)",
                    length, _maxNumLen));
        }
    }
/**
     * Convenience method that can be used to verify that a String
     * of specified length does not exceed maximum specific by this
     * constraints object: if it does, a
     * {@link StreamConstraintsException}
     * is thrown.
     *
     * @param length Length of string in input units
     *
     * @throws StreamConstraintsException If length exceeds maximum
     */
    public void validateStringLength(int length) throws StreamConstraintsException
    {
        if (length > _maxStringLen) {
            throw new StreamConstraintsException(String.format("String length (%d) exceeds the maximum length (%d)",
                    length, _maxStringLen));
        }
    }

    /**
     * Convenience method that can be used to verify that a conversion to
     * {@link java.math.BigInteger}
     * {@link StreamConstraintsException}
     * is thrown.
     *
     * @param scale Scale (possibly negative) of {@link java.math.BigDecimal} to convert
     *
     * @throws StreamConstraintsException If magnitude (absolute value) of scale exceeds maximum
     *    allowed
     */
    public void validateBigIntegerScale(int scale) throws StreamConstraintsException
    {
        final int absScale = Math.abs(scale);
        final int limit = MAX_BIGINT_SCALE_MAGNITUDE;

        if (absScale > limit) {
            throw new StreamConstraintsException(String.format(
                    "BigDecimal scale (%d) magnitude exceeds maximum allowed (%d)",
                    scale, limit));
        }
    }    

    /**
     * Validate that the provided numeric token length does not exceed the
     * configured maximum. Enforcing this limit prevents pathological numeric
     * tokens (e.g. a 200,000-digit BigInteger) from causing excessive CPU or
     * memory consumption during parsing or subsequent type conversion.
     * This is the primary mitigation for Sonatype-2022-6438.
     *
     * @param numLen total character length of the numeric token
     *
     * @throws StreamConstraintsException if {@code numLen} exceeds the
     *   configured maximum
     */
    public void validateIntegerLength(int numLen) throws StreamConstraintsException
    {
        if (numLen > _maxNumLen) {
            throw new StreamConstraintsException(String.format(
                    "Number length (%d) exceeds the maximum length (%d)",
                    numLen, _maxNumLen));
        }
    }

    /**
     * Validate that the total number of bytes or characters processed so far
     * does not exceed the configured maximum document length.
     *
     * @param len total bytes or characters processed
     *
     * @throws StreamConstraintsException if {@code len} exceeds the
     *   configured maximum document length
     *
     * @since 2.15
     */
    public void validateDocumentLength(long len) throws StreamConstraintsException
    {
        if (len > _maxDocLen) {
            throw new StreamConstraintsException(String.format(
                    "Document length (%d) exceeds the maximum allowed (%d)",
                    len, _maxDocLen));
        }
    }
}