Interface Masker

All Superinterfaces:
Function<String,String>, UnaryOperator<String>
All Known Implementing Classes:
PathRedactor
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 Masker extends UnaryOperator<String>
Masks sensitive parts of a text before it is written to a log, to evidence or to a prompt.

A masker is a plain UnaryOperator<String>, so it can be handed to any API that takes one. Implementations must accept null and return it unchanged, and must be safe to use from several threads at once.

Masking is off by default: callers that are not configured otherwise use noOp(). hidingOutside(Path) and PathRedactor are the opt-in maskers.

Since:
0.18.6
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static Masker
    Returns the opt-in masker that hides every absolute path outside root and every well-known credential shape.
    static Masker
    Returns the masker that changes nothing; the default wherever masking is configurable.

    Methods inherited from interface java.util.function.Function

    andThen, apply, compose
  • Method Details

    • noOp

      static Masker noOp()
      Returns the masker that changes nothing; the default wherever masking is configurable.
      Returns:
      the identity masker
    • hidingOutside

      static Masker hidingOutside(Path root)
      Returns the opt-in masker that hides every absolute path outside root and every well-known credential shape.

      Credentials matching CredentialPatterns are replaced by [REDACTED:KIND], with the kind names tnsai-quality's PatternRedactor uses: PEM and PGP private-key blocks (to the end of the text when the END line is missing), OpenAI and Anthropic API keys, GitHub tokens, AWS access key ids, JWTs (unsigned ones too), Bearer values, and Slack tokens and webhook URLs. A credential is found also when it is glued to a preceding word, as in API_KEY_sk-…. PII such as e-mail addresses or card numbers is not masked.

      Absolute POSIX paths outside root are replaced by [REDACTED:path]; paths under it are left as they are. A path is compared after lexical normalization, so ROOT/../etc/passwd counts as outside. root is matched in its absolute normalized form and, when it exists at construction time, its real form with links resolved (on macOS /tmp/x is also /private/tmp/x). Paths found in the text are never resolved against the file system. A path ends at whitespace, a quote, a bracket, ,, ;, : or =, and a // inside it starts another path, so PATH=ROOT/bin:/home/x and ROOT//home/x hide /home/x. A path glued to a command-line option, as in -I/home/x, is found too. A URL's path (https://host/x), relative paths, ~/…, Windows drive paths, percent-encoded paths and the part of a path after a space are not recognised. A root of / leaves every path visible.

      The masker is immutable and thread-safe.

      Parameters:
      root - the folder whose paths stay visible
      Returns:
      a masker for root
      Throws:
      NullPointerException - if root is null