Class StreamReadConstraints

java.lang.Object
com.fasterxml.jackson.core.StreamReadConstraints
All Implemented Interfaces:
Serializable

public class StreamReadConstraints extends Object implements Serializable
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 TokenStreamFactory (such as JsonFactory); if nothing explicitly specified, default constraints are used.

Currently constrained aspects, with default settings, are:

Since:
2.15
See Also:
  • Field Details

  • Constructor Details

    • StreamReadConstraints

      @Deprecated protected StreamReadConstraints(int maxNestingDepth, long maxDocLen, int maxNumLen, int maxStringLen)
      Deprecated.
    • StreamReadConstraints

      @Deprecated protected StreamReadConstraints(int maxNestingDepth, long maxDocLen, int maxNumLen, int maxStringLen, int maxNameLen)
      Deprecated.
      Parameters:
      maxNestingDepth - Maximum input document nesting to allow
      maxDocLen - Maximum input document length to allow
      maxNumLen - Maximum number representation length to allow
      maxStringLen - Maximum String value length to allow
      maxNameLen - Maximum Object property name length to allow
      Since:
      2.16
    • StreamReadConstraints

      protected StreamReadConstraints(int maxNestingDepth, long maxDocLen, int maxNumLen, int maxStringLen, int maxNameLen, long maxTokenCount)
      Parameters:
      maxNestingDepth - Maximum input document nesting to allow
      maxDocLen - Maximum input document length to allow
      maxNumLen - Maximum number representation length to allow
      maxStringLen - Maximum String value length to allow
      maxNameLen - Maximum Object property name length to allow
      maxTokenCount - Maximum number of tokens to allow
      Since:
      2.18
  • Method Details

    • overrideDefaultStreamReadConstraints

      public static void overrideDefaultStreamReadConstraints(StreamReadConstraints streamReadConstraints)
      Override the default StreamReadConstraints. These defaults are only used when JsonFactory instances are not configured with their own StreamReadConstraints.

      Library maintainers should not set this as it will affect other code that uses Jackson. Library maintainers who want to configure StreamReadConstraints for the Jackson usage within their lib should create ObjectMapper instances that have a JsonFactory instance with the required StreamReadConstraints.

      This method is meant for users delivering applications. If they use this, they set it when they start their application to avoid having other code initialize their mappers before the defaults are overridden.

      Parameters:
      streamReadConstraints - new default for StreamReadConstraints (a null value will reset to built-in default)
      Since:
      v2.15.2
      See Also:
    • builder

      public static StreamReadConstraints.Builder builder()
    • defaults

      public static StreamReadConstraints defaults()
      Returns:
      the default StreamReadConstraints (when none is set on the JsonFactory explicitly)
      See Also:
    • rebuild

      Returns:
      New StreamReadConstraints.Builder initialized with settings of this constraints instance
    • getMaxNestingDepth

      public int getMaxNestingDepth()
      Accessor for maximum depth. see StreamReadConstraints.Builder.maxNestingDepth(int) for details.
      Returns:
      Maximum allowed depth
    • getMaxDocumentLength

      public long getMaxDocumentLength()
      Accessor for maximum document length. see StreamReadConstraints.Builder.maxDocumentLength(long) for details.
      Returns:
      Maximum allowed depth
    • hasMaxDocumentLength

      public boolean hasMaxDocumentLength()
      Convenience method, basically same as:
        getMaxDocumentLength() > 0L
      
      Returns:
      True if this constraints instance has a limit for maximum document length to enforce; false otherwise.
    • getMaxTokenCount

      public long getMaxTokenCount()
      Accessor for maximum token count. see StreamReadConstraints.Builder.maxTokenCount(long) for details.
      Returns:
      Maximum allowed token count
      Since:
      2.18
    • hasMaxTokenCount

      public boolean hasMaxTokenCount()
      Convenience method, basically same as:
        getMaxTokenCount() > 0L
      
      Returns:
      True if this constraints instance has a limit for maximum token count to enforce; false otherwise.
      Since:
      2.18
    • getMaxNumberLength

      public int getMaxNumberLength()
      Accessor for maximum length of numbers to decode. see StreamReadConstraints.Builder.maxNumberLength(int) for details.
      Returns:
      Maximum allowed number length
    • getMaxStringLength

      public int getMaxStringLength()
      Accessor for maximum length of strings to decode. see StreamReadConstraints.Builder.maxStringLength(int) for details.
      Returns:
      Maximum allowed string length
    • getMaxNameLength

      public int getMaxNameLength()
      Accessor for maximum length of names to decode. see StreamReadConstraints.Builder.maxNameLength(int) for details.
      Returns:
      Maximum allowed name length
    • validateNestingDepth

      public void validateNestingDepth(int depth) throws StreamConstraintsException
      Convenience method that can be used to verify that the nesting depth does not exceed the maximum specified by this constraints object: if it does, a StreamConstraintsException is thrown.
      Parameters:
      depth - count of unclosed objects and arrays
      Throws:
      StreamConstraintsException - If depth exceeds maximum
    • validateDocumentLength

      public void validateDocumentLength(long len) throws StreamConstraintsException
      Convenience method that can be used to verify that the document length does not exceed the maximum specified by this constraints object (if any): if it does, a StreamConstraintsException is thrown.
      Parameters:
      len - Current length of processed document content
      Throws:
      StreamConstraintsException - If length exceeds maximum
      Since:
      2.16
    • validateTokenCount

      public void validateTokenCount(long count) throws StreamConstraintsException
      Convenience method that can be used to verify that the token count does not exceed the maximum specified by this constraints object (if any): if it does, a StreamConstraintsException is thrown.
      Parameters:
      count - Current token count for processed document content
      Throws:
      StreamConstraintsException - If length exceeds maximum
      Since:
      2.18
    • validateFPLength

      public void validateFPLength(int length) throws StreamConstraintsException
      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 StreamConstraintsException is thrown.
      Parameters:
      length - Length of number in input units
      Throws:
      StreamConstraintsException - If length exceeds maximum
    • validateIntegerLength

      public void validateIntegerLength(int length) throws StreamConstraintsException
      Convenience method that can be used to verify that an integer number of specified length does not exceed maximum specific by this constraints object: if it does, a StreamConstraintsException is thrown.
      Parameters:
      length - Length of number in input units
      Throws:
      StreamConstraintsException - If length exceeds maximum
    • validateStringLength

      public void validateStringLength(int length) throws StreamConstraintsException
      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 StreamConstraintsException is thrown.
      Parameters:
      length - Length of string in input units
      Throws:
      StreamConstraintsException - If length exceeds maximum
    • validateNameLength

      public void validateNameLength(int length) throws StreamConstraintsException
      Convenience method that can be used to verify that a name of specified length does not exceed maximum specific by this constraints object: if it does, a StreamConstraintsException is thrown.
      Parameters:
      length - Length of name in input units
      Throws:
      StreamConstraintsException - If length exceeds maximum
    • validateBigIntegerScale

      public void validateBigIntegerScale(int scale) throws StreamConstraintsException
      Convenience method that can be used to verify that a conversion to BigInteger StreamConstraintsException is thrown.
      Parameters:
      scale - Scale (possibly negative) of BigDecimal to convert
      Throws:
      StreamConstraintsException - If magnitude (absolute value) of scale exceeds maximum allowed
    • _constructException

      protected StreamConstraintsException _constructException(String msgTemplate, Object... args) throws StreamConstraintsException
      Throws:
      StreamConstraintsException
    • _constrainRef

      protected String _constrainRef(String method)