Skip to content

fix: with_token sends the original token through memoized API objects - #429

Open
bruno-costanzo wants to merge 1 commit into
hashicorp:masterfrom
bruno-costanzo:fix/with-token-memoized-apis
Open

bruno-costanzo wants to merge 1 commit into
hashicorp:masterfrom
bruno-costanzo:fix/with-token-memoized-apis

Conversation

@bruno-costanzo

Copy link
Copy Markdown

Client#with_token copies the client with dup and sets the new token on the copy. The copy also keeps the memoized API objects (@logical, @auth, @sys, ...), and those still point at the original client. So when the original client has already used #logical, a request through the copy goes out with the original token:

client = Vault::Client.new(address: "...", token: "app-token")
client.logical.read("secret/a")                       # memoizes @logical

client.with_token("user-token") { |c| c.logical.read("secret/b") }
# => sent with X-Vault-Token: app-token

The call does not fail, so this is easy to miss, and the original token is often the more privileged one.

This adds Client#initialize_copy, which clears the memoized API objects so the copy builds its own. The connection pool stays shared, as before: the token is a per-request header.

The new spec in spec/unit/client_spec.rb fails on master (both requests carry abcd-1234) and passes with the change. bundle exec rspec spec/unit: 200 examples, 0 failures.

Client#with_token copies the client with dup. The copy kept the memoized
API objects (@logical, @auth, ...), which still point at the original
client, so a request through them went out with the original token.
@bruno-costanzo
bruno-costanzo requested a review from a team as a code owner November 3, 2026 14:18
@hashicorp-cla-app

hashicorp-cla-app Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@hashicorp-cla-app

Copy link
Copy Markdown

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes

Have you signed the CLA already but the status is still pending? Recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant