Skip to content

fix(oidc): omit unset optional token exchange parameters - #1029

Open
shlee1223 wants to merge 2 commits into
zitadel:mainfrom
shlee1223:tokenexchange-omit-unset-params
Open

shlee1223 wants to merge 2 commits into
zitadel:mainfrom
shlee1223:tokenexchange-omit-unset-params

Conversation

@shlee1223

Copy link
Copy Markdown

Which Problems Are Solved

  • The token exchange client sends unset optional parameters as empty values (actor_token=&actor_token_type=&requested_token_type=&scope=). RFC 8693 §2.1 says actor_token_type "MUST NOT be included" when actor_token is absent, and servers that check for presence
    may reject or misroute the request.

How the Problems Are Solved

  • Adds omitempty to the optional parameters of TokenExchangeRequest.

Additional Changes

  • Adds a test that checks unset parameters are not sent.

Additional Context

@wim07101993 wim07101993 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thank you for the contribution. Looks good but I have a few small comments on the tests. Can you have a quick look?


func TestExchangeToken_OmitsUnsetParameters(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, r.ParseForm())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require.NoError calls t.FailNow on the handler goroutine, which testing doesn't allow (testifylint's go-require flags this line). If ParseForm ever failed, the test would also report a second, misleading Post …: EOF from the client. Could you capture r.PostForm in the handler and assert after ExchangeToken returns, like pkg/client/client_test.go does?

func TestExchangeToken_OmitsUnsetParameters(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, r.ParseForm())
for _, p := range []string{"actor_token", "actor_token_type", "resource", "audience", "scope", "requested_token_type"} {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks that values are absent, so it would also pass if nothing were sent. I tagged every TokenExchangeRequest field schema:"-" and the test still passed. The resource and audience checks passed before this change too, because nil slices were never encoded.

Could you assert that grant_type, subject_token and subject_token_type are still sent, and add a case with every optional value set, so the test shows omitempty doesn't drop them?

assert.NotContains(t, r.PostForm, p)
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"access_token":"a","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer"}`))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errcheck flags this; the other test handlers ignore the result explicitly.

Suggested change
w.Write([]byte(`{"access_token":"a","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer"}`))
_, _ = w.Write([]byte(`{"access_token":"a","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer"}`))

@wim07101993 wim07101993 self-assigned this Dec 2, 2026
@wim07101993 wim07101993 added the os-contribution This is a contribution from our open-source community label Dec 2, 2026
@wim07101993 wim07101993 added the waiting-for-author AI triage asked the author questions; their reply re-runs triage label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

os-contribution This is a contribution from our open-source community waiting-for-author AI triage asked the author questions; their reply re-runs triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants