Repository navigation
feat: add Tesla.SecretString to keep secrets out of inspect output #939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yordis
merged 1 commit into
elixir-tesla:master
from
BlueCollarChris:feat/redacting-client-inspect
Sep 25, 2026
+156
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| defmodule Tesla.SecretString do | ||
| @moduledoc ~S""" | ||
| A string that is redacted when inspected. | ||
|
|
||
| Middleware options live in the client for as long as it exists, and every | ||
| `%Tesla.Env{}` embeds its client, so a token given to a middleware would | ||
| otherwise print wherever a client or env is inspected, including a | ||
| `Logger.error("request failed: #{inspect(env)}")`. Wrapping the value keeps | ||
| the redaction with the value itself, however the surrounding structure is | ||
| inspected: | ||
|
|
||
| iex> secret = Tesla.SecretString.new("Bearer s3cret") | ||
| iex> inspect(secret) | ||
| "#Tesla.SecretString<redacted>" | ||
| iex> inspect([{"authorization", secret}]) | ||
| ~s([{"authorization", #Tesla.SecretString<redacted>}]) | ||
| iex> to_string(secret) | ||
| "Bearer s3cret" | ||
|
|
||
| `String.Chars` returns the value, so middleware that builds a header by | ||
| interpolation works with a wrapped value unchanged. The flip side is that | ||
| `"#{secret}"` prints it: interpolate a secret where it goes on the request, | ||
| never into a log message. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```elixir | ||
| Tesla.client([ | ||
| {Tesla.Middleware.Headers, [{"authorization", Tesla.SecretString.new("Bearer #{token}")}]} | ||
| ]) | ||
|
|
||
| Tesla.client([ | ||
| {Tesla.Middleware.BearerAuth, token: Tesla.SecretString.new(token)} | ||
| ]) | ||
|
|
||
| Tesla.client([ | ||
| {Tesla.Middleware.BasicAuth, %{username: username, password: Tesla.SecretString.new(password)}} | ||
| ]) | ||
| ``` | ||
| """ | ||
|
|
||
| @opaque t :: %__MODULE__{value: String.t()} | ||
|
|
||
| defstruct [:value] | ||
|
|
||
| @doc "Wraps `value`." | ||
| @spec new(String.t()) :: t() | ||
| def new(value) when is_binary(value), do: %__MODULE__{value: value} | ||
|
|
||
| defimpl Inspect do | ||
| def inspect(_secret, _opts), do: "#Tesla.SecretString<redacted>" | ||
| end | ||
|
|
||
| defimpl String.Chars do | ||
| def to_string(secret), do: secret.value | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| defmodule Tesla.SecretStringTest do | ||
| use ExUnit.Case, async: true | ||
| doctest Tesla.SecretString | ||
|
|
||
| alias Tesla.SecretString | ||
|
|
||
| test "inspect never prints the value, at any nesting" do | ||
| secret = SecretString.new("Bearer s3cret") | ||
|
|
||
| assert inspect(secret) == "#Tesla.SecretString<redacted>" | ||
| refute inspect([{"authorization", secret}]) =~ "s3cret" | ||
| refute inspect(%{opts: [token: secret]}, pretty: true) =~ "s3cret" | ||
| end | ||
|
|
||
| test "to_string and interpolation return the value" do | ||
| secret = SecretString.new("s3cret") | ||
|
|
||
| assert to_string(secret) == "s3cret" | ||
| assert "Bearer #{secret}" == "Bearer s3cret" | ||
| end | ||
|
|
||
| test "a client holding secrets in middleware options does not print them" do | ||
| client = | ||
| Tesla.client([ | ||
| {Tesla.Middleware.Headers, [{"authorization", SecretString.new("Bearer s3cret-1")}]}, | ||
| {Tesla.Middleware.BearerAuth, token: SecretString.new("s3cret-2")}, | ||
| {Tesla.Middleware.BasicAuth, %{username: "u", password: SecretString.new("s3cret-3")}} | ||
| ]) | ||
|
|
||
| refute inspect(client) =~ "s3cret" | ||
| refute inspect(%Tesla.Env{__client__: client}) =~ "s3cret" | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.