Class SecretMasking

java.lang.Object
dev.langchain4j.internal.SecretMasking

public final class SecretMasking extends Object
Hides credentials in what LangChain4j writes to logs and exception messages.

When in doubt, it masks: a value that was masked without need costs one debugging round trip, an unmasked credential costs a key rotation.

  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Returns true if a header, query parameter or other value with this name probably carries a credential: its name contains auth, key, token, secret, password, credential, cookie, signature or session, ignoring case.
    static @Nullable String
    mask(@Nullable String value)
    Masks a credential: a value of 20 or more characters is shown as its first 5 and last 2 characters (sk-pr...Xy), a shorter value is replaced by ....
    static String
    Replaces the user info of a URL with ***: https://user:password@host/path becomes https://***@host/path.
    static @Nullable String
    withoutCredentials(@Nullable String url)
    Removes the user info, the query and the fragment of a URL, as all of them can carry credentials: https://user:password@host/path?key=secret becomes https://host/path.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isSecretName

      public static boolean isSecretName(String name)
      Returns true if a header, query parameter or other value with this name probably carries a credential: its name contains auth, key, token, secret, password, credential, cookie, signature or session, ignoring case.
    • mask

      public static @Nullable String mask(@Nullable String value)
      Masks a credential: a value of 20 or more characters is shown as its first 5 and last 2 characters (sk-pr...Xy), a shorter value is replaced by ....
      Returns:
      the masked value, or the value itself if it is null or blank
    • maskUserInfo

      public static String maskUserInfo(String url)
      Replaces the user info of a URL with ***: https://user:password@host/path becomes https://***@host/path. The rest of the URL is kept.
    • withoutCredentials

      public static @Nullable String withoutCredentials(@Nullable String url)
      Removes the user info, the query and the fragment of a URL, as all of them can carry credentials: https://user:password@host/path?key=secret becomes https://host/path. Also works for malformed URLs.
      Returns:
      the URL without credentials, or null if the URL is null