Interface SanitizingFunction

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface SanitizingFunction
Function that takes a SanitizableData and applies sanitization to the value, if necessary. Can be used by a Sanitizer to determine the sanitized value.

This interface also provides convenience methods that can help build a SanitizingFunction instances, for example to return from a @Bean method. See sanitizeValue() for an example.

Since:
2.6.0
Author:
Madhura Bhave, Phillip Webb
See Also:
  • Method Details

    • apply

      Apply the sanitizing function to the given data.
      Parameters:
      data - the data to sanitize
      Returns:
      the sanitized data or the original instance is no sanitization is applied
    • filter

      default Predicate<SanitizableData> filter()
      Return an optional filter that determines if the sanitizing function applies.
      Returns:
      a predicate used to filter functions or null if no filter is declared
      Since:
      3.5.0
      See Also:
    • applyUnlessFiltered

      default SanitizableData applyUnlessFiltered(SanitizableData data)
      Apply the sanitizing function as long as the filter passes or there is no filter.
      Parameters:
      data - the data to sanitize
      Returns:
      the sanitized data or the original instance is no sanitization is applied
      Since:
      3.5.0
    • ifLikelySensitive

      default SanitizingFunction ifLikelySensitive()
      Return a new function with a filter that also applies if the data is likely to contain a sensitive value. This method can help construct a useful sanitizing function, but may not catch all sensitive data so care should be taken to test the results for your specific environment.
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifLikelyCredential

      default SanitizingFunction ifLikelyCredential()
      Return a new function with a filter that also applies if the data is likely to contain a credential. This method can help construct a useful sanitizing function, but may not catch all sensitive data so care should be taken to test the results for your specific environment.
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifLikelyUri

      default SanitizingFunction ifLikelyUri()
      Return a new function with a filter that also applies if the data is likely to contain a URI. This method can help construct a useful sanitizing function, but may not catch all sensitive data so care should be taken to test the results for your specific environment.
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifLikelySensitiveProperty

      default SanitizingFunction ifLikelySensitiveProperty()
      Return a new function with a filter that also applies if the data is likely to contain a sensitive property value. This method can help construct a useful sanitizing function, but may not catch all sensitive data so care should be taken to test the results for your specific environment.
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifVcapServices

      default SanitizingFunction ifVcapServices()
      Return a new function with a filter that also applies if the data is for VCAP services.
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifKeyEquals

      default SanitizingFunction ifKeyEquals(String... values)
      Return a new function with a filter that also applies if the data key is equal to any of the given values (ignoring case).
      Parameters:
      values - the case insensitive values that the key can equal
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifKeyEndsWith

      default SanitizingFunction ifKeyEndsWith(String... suffixes)
      Return a new function with a filter that also applies if the data key ends with any of the given values (ignoring case).
      Parameters:
      suffixes - the case insensitive suffixes that they key can end with
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifKeyContains

      default SanitizingFunction ifKeyContains(String... values)
      Return a new function with a filter that also applies if the data key contains any of the given values (ignoring case).
      Parameters:
      values - the case insensitive values that the key can contain
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifKeyMatchesIgnoringCase

      default SanitizingFunction ifKeyMatchesIgnoringCase(BiPredicate<String,String> predicate, String... values)
      Return a new function with a filter that also applies if the data key and any of the values match the given predicate. The predicate is only called with lower case values.
      Parameters:
      predicate - the predicate used to check the key against a value. The key is the first argument and the value is the second. Both are converted to lower case
      values - the case insensitive values that the key can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifKeyMatches

      default SanitizingFunction ifKeyMatches(String... regexes)
      Return a new function with a filter that also applies if the data key matches any of the given regex patterns (ignoring case).
      Parameters:
      regexes - the case insensitive regexes that the key can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifKeyMatches

      default SanitizingFunction ifKeyMatches(Pattern... patterns)
      Return a new function with a filter that also applies if the data key matches any of the given patterns.
      Parameters:
      patterns - the patterns that the key can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifKeyMatches

      default SanitizingFunction ifKeyMatches(List<Predicate<String>> predicates)
      Return a new function with a filter that also applies if the data key matches any of the given predicates.
      Parameters:
      predicates - the predicates that the key can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifKeyMatches

      default SanitizingFunction ifKeyMatches(Predicate<String> predicate)
      Return a new function with a filter that also applies if the data key matches any of the given predicate.
      Parameters:
      predicate - the predicate that the key can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifValueStringMatches

      default SanitizingFunction ifValueStringMatches(String... regexes)
      Return a new function with a filter that also applies if the data string value matches any of the given regex patterns (ignoring case).
      Parameters:
      regexes - the case insensitive regexes that the values string can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifValueStringMatches

      default SanitizingFunction ifValueStringMatches(Pattern... patterns)
      Return a new function with a filter that also applies if the data string value matches any of the given patterns.
      Parameters:
      patterns - the patterns that the value string can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifValueStringMatches

      default SanitizingFunction ifValueStringMatches(List<Predicate<String>> predicates)
      Return a new function with a filter that also applies if the data string value matches any of the given predicates.
      Parameters:
      predicates - the predicates that the value string can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifValueMatches

      default SanitizingFunction ifValueMatches(List<Predicate<Object>> predicates)
      Return a new function with a filter that also applies if the data value matches any of the given predicates.
      Parameters:
      predicates - the predicates that the value can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifValueStringMatches

      default SanitizingFunction ifValueStringMatches(Predicate<String> predicate)
      Return a new function with a filter that also applies if the data string value matches the given predicate.
      Parameters:
      predicate - the predicate that the value string can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifValueMatches

      default SanitizingFunction ifValueMatches(Predicate<Object> predicate)
      Return a new function with a filter that also applies if the data value matches the given predicate.
      Parameters:
      predicate - the predicate that the value can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifMatches

      default SanitizingFunction ifMatches(List<Predicate<SanitizableData>> predicates)
      Return a new function with a filter that also applies if the data matches any of the given predicates.
      Parameters:
      predicates - the predicates that the data can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • ifMatches

      default SanitizingFunction ifMatches(Predicate<SanitizableData> predicate)
      Return a new function with a filter that also applies if the data matches the given predicate.
      Parameters:
      predicate - the predicate that the data can match
      Returns:
      a new sanitizing function with an updated filter()
      Since:
      3.5.0
      See Also:
    • sanitizeValue

      static SanitizingFunction sanitizeValue()
      Factory method to return a SanitizingFunction that sanitizes the value. This method is often chained with one or more if... methods. For example:
       return SanitizingFunction.sanitizeValue()
              .ifKeyContains("password", "secret")
              .ifValueStringMatches("^gh._[a-zA-Z0-9]{36}$");
       
      Returns:
      a SanitizingFunction that sanitizes values.
    • of

      static SanitizingFunction of(SanitizingFunction sanitizingFunction)
      Helper method that can be used working with a sanitizingFunction as a lambda. For example:
       SanitizingFunction.of((data) -> data.withValue("----")).ifKeyContains("password");
       
      Parameters:
      sanitizingFunction - the sanitizing function lambda
      Returns:
      a SanitizingFunction for further method calls
      Since:
      3.5.0