Hushfang

Hushfang / Guide

Design principles for logging secrets

A system can prevent every disclosure through its logs by dropping every event. An operator then loses the evidence needed to explain a failure. A practical design has to account for three things together: what escapes, what remains useful to a reader and how much traffic the system can handle. Broader matching can hide useful identifiers and cost more CPU on every record.

Secret lifecycle from creation and deliberate disclosure to masking, transport, storage and access, with an independent error-reporting path.

Static diagram · Workflow model

The code that loads a credential knows exactly which value is sensitive. A client sees a string. A log collector might see only a fragment of an error message. Every step away from the source loses information that a downstream filter would have to guess.

Keep the value wrapped until a client needs it

Give credentials a secret type when they are created or first read. At this point the application knows their purpose, the clients that will use them and the forms those clients may emit. Choose the masking policy here and carry the wrapper through the rest of the application. Ordinary printing and serialization should produce a mask. Reveal the value at the call that needs it, such as constructing an authorization header. Keep those calls in a few named functions so a reviewer or a static check can account for every reveal. Inspect the failure path there as carefully as the request itself. A client can put the header back into an exception, its cause or an attached request.

Carry what the application knows to the log handler

An application can register a credential before handing it to a client. A formatter can then recognize that value even in a message like retry failed for %s, which carries no field name. Hushfang prepares common encodings at registration. When a client emits a Basic authorization header, the matcher can decode the header and check its password against the registry, even if the application registered only the password. The same relationship between registration and output is visible in the runnable SDK retry example.

A registry has a cost. It holds sensitive material in memory. Its owner should decide which requests share it, how rotations overlap with queued records and when to discard old values. A four-digit PIN can occur inside an ordinary job ID. Matching shorter fragments broadens that ambiguity.

What to record on a retry

A support engineer investigating a rejected request wants to find the other attempts and see how the upstream service responded. An application that controls the event can answer both questions with a few fields.

{"event": "upstream_retry", "request_id": "r-17", "attempt": 2, "http_status": 401}

Generate a request ID independent of the credential and carry it through the operation. Using the credential itself as a correlation ID would copy it into every related record. For a rejected queue message, its ID, kind and byte count often suffice. For an upstream connection, a configured service name is usually enough. A full URL can carry a token in the path even when it has no password or query string.

Field names help while a payload is still structured. If an SDK passes a dictionary with a password key as a logging argument, a Hushfang formatter configured with mask_extra={"password"} can mask that field even for a value the registry has never seen. If the SDK serializes the dictionary as JSON, its member names can still identify private fields. Hushfang can mask a configured JSON member in the rendered text. If the client logs only the value or flattens the payload into free text, a registry can still recognize a known value. A string of 64 hexadecimal characters alone gives little basis for deciding whether it is a public digest or a private key.

See all four combinations in Hushbench.

On the final failure, record the traceback where the application decides to give up. Logging the same exception at each intermediate layer creates more copies of anything the exception contains. If the error reporter captures frame locals, a discarded request body or raw credential can reappear in its attachment. Configure that capture separately and inspect the actual report alongside the text log.

Follow each route out of the process

Trace a failed request to its destinations. A text handler can clean a formatted traceback while an error reporter sends the original exception and its attributes independently. Check what each destination receives, including structured fields and chained exceptions. Before forwarding, a collector can still remove familiar sensitive fields or token patterns. At ingestion, shape what may be stored and how long it is kept. At read time, restrict access and apply the same redaction policy to searches, downloads and exports. A mask in a dashboard does nothing for an earlier copy already written to disk.

Hushfang works at two points in this path. The application registers known Pydantic secrets, and a configured Python logging handler uses that registry to redact its formatted output. The application can use the revealing context manager to sanitize an exception leaving the client call. Exporters, storage and readers each need their own controls.

Preserve the evidence an operator needs

For a failed request, keep the operation, public request ID, retry count and exception type wherever possible. Write down the expected output for the whole event. A blank line conceals a credential just as thoroughly as a useful redacted message, but it cannot explain a failed retry.

Exercise the actual output path with synthetic credentials and public lookalikes. Count exposed values, changed public records, dropped events and formatting failures separately. Measure formatting cost at realistic record sizes and registry sizes. A passing fixture establishes what happens to that input on that path. Each newly observed failure is a reason to add another fixture.

Hushfang's current coverage · How Hushbench tests these principles