| 1 |
package op_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"io" |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"net/url" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 16 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 17 |
) |
| 18 |
|
| 19 |
func TestLegacyServer_CodeExchange_ClientOwnership(t *testing.T) { |
| 20 |
server := op.RegisterLegacyServer( |
| 21 |
op.NewLegacyServer(testProvider, *op.DefaultEndpoints), |
| 22 |
op.AuthorizeCallbackHandler(testProvider), |
| 23 |
) |
| 24 |
|
| 25 |
storage := testProvider.Storage().(routesTestStorage) |
| 26 |
ctx := op.ContextWithIssuer(context.Background(), testIssuer) |
| 27 |
|
| 28 |
// Client A ("web") — the legitimate owner of the authorization code. |
| 29 |
clientA, err := storage.GetClientByClientID(ctx, "web") |
| 30 |
require.NoError(t, err) |
| 31 |
|
| 32 |
authReq, err := storage.CreateAuthRequest(ctx, &oidc.AuthRequest{ |
| 33 |
ClientID: clientA.GetID(), |
| 34 |
RedirectURI: "https://example.com", |
| 35 |
Scopes: oidc.SpaceDelimitedArray{oidc.ScopeOpenID}, |
| 36 |
ResponseType: oidc.ResponseTypeCode, |
| 37 |
}, "user-1") |
| 38 |
require.NoError(t, err) |
| 39 |
storage.AuthRequestDone(authReq.GetID()) |
| 40 |
storage.SaveAuthCode(ctx, authReq.GetID(), "test-code-x") |
| 41 |
|
| 42 |
// Client B ("api") — attempts to exchange a code it does not own. |
| 43 |
clientB, err := storage.GetClientByClientID(ctx, "api") |
| 44 |
require.NoError(t, err) |
| 45 |
|
| 46 |
form := url.Values{ |
| 47 |
"grant_type": {string(oidc.GrantTypeCode)}, |
| 48 |
"code": {"test-code-x"}, |
| 49 |
"redirect_uri": {"https://example.com"}, |
| 50 |
} |
| 51 |
|
| 52 |
u, err := url.Parse(testProvider.TokenEndpoint().Absolute(testIssuer)) |
| 53 |
require.NoError(t, err) |
| 54 |
|
| 55 |
req := httptest.NewRequest(http.MethodPost, u.String(), strings.NewReader(form.Encode())) |
| 56 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 57 |
req.SetBasicAuth(clientB.GetID(), "secret") |
| 58 |
|
| 59 |
rec := httptest.NewRecorder() |
| 60 |
server.ServeHTTP(rec, req) |
| 61 |
|
| 62 |
resp := rec.Result() |
| 63 |
require.Equal(t, http.StatusBadRequest, resp.StatusCode) |
| 64 |
|
| 65 |
body, err := io.ReadAll(resp.Body) |
| 66 |
require.NoError(t, err) |
| 67 |
|
| 68 |
var errResp oidc.Error |
| 69 |
err = json.Unmarshal(body, &errResp) |
| 70 |
require.NoError(t, err) |
| 71 |
|
| 72 |
require.Equal(t, oidc.ErrInvalidGrant().Error(), errResp.Error()) |
| 73 |
require.Empty(t, errResp.Description, |
| 74 |
"client mismatch must not include a description — it would leak information") |
| 75 |
} |
| 76 |
|
| 77 |
// The callback reads the request id back with Form.Get, which decodes it, so an |
| 78 |
// id containing reserved characters must survive the round trip unchanged. |
| 79 |
func TestAuthCallbackURL_EscapesRequestID(t *testing.T) { |
| 80 |
ctx := op.ContextWithIssuer(context.Background(), testIssuer) |
| 81 |
const requestID = "a+b/c=d&e#f %" |
| 82 |
|
| 83 |
builders := map[string]func(context.Context, string) string{ |
| 84 |
"provider": op.AuthCallbackURL(testProvider), |
| 85 |
"legacy server": op.NewLegacyServer(testProvider, *op.DefaultEndpoints).AuthCallbackURL(), |
| 86 |
} |
| 87 |
for name, build := range builders { |
| 88 |
t.Run(name, func(t *testing.T) { |
| 89 |
callback, err := url.Parse(build(ctx, requestID)) |
| 90 |
require.NoError(t, err) |
| 91 |
require.Equal(t, requestID, callback.Query().Get("id")) |
| 92 |
require.Empty(t, callback.Fragment) |
| 93 |
}) |
| 94 |
} |
| 95 |
} |