Skip to content

chore(coderd): add tests for big oidc tokens #12424

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
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore(coderd): add tests for big oidc tokens
  • Loading branch information
johnstcn committed Mar 5, 2024
commit ebd95f456ff50b36af705f2713a20dbad6de6603
39 changes: 39 additions & 0 deletions coderd/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/mail"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/coreos/go-oidc/v3/oidc"
Expand Down Expand Up @@ -1189,6 +1191,26 @@ func blankFields(claims map[string]interface{}) []string {
return fields
}

// fieldSizes returns the sorted sizes of fields in the claims map
func fieldSizes(claims map[string]interface{}) []int {
fs := claimFields(claims)
lens := make([]int, len(fs))
for i, f := range fs {
v, ok := claims[f]
if !ok {
lens[i] = -1
continue
}
vs, ok := v.(string)
if !ok {
lens[i] = -1
continue
}
lens[i] = len(vs)
}
return lens
}

// mergeClaims merges the claims from a and b and returns the merged set.
// claims from b take precedence over claims from a.
func mergeClaims(a, b map[string]interface{}) map[string]interface{} {
Expand Down Expand Up @@ -1872,3 +1894,20 @@ func parseStringSliceClaim(claim interface{}) ([]string, error) {
// Not sure what the user gave us.
return nil, xerrors.Errorf("invalid claim type. Expected an array of strings, got: %T", claim)
}

type countWriter struct {
w io.Writer
bytesWritten atomic.Int64
}

func (cw *countWriter) Write(p []byte) (int, error) {
n, err := cw.w.Write(p)
if n > 0 {
cw.bytesWritten.Add(int64(n))
}
return n, err
}

func (cw *countWriter) Length() int {
return int(cw.bytesWritten.Load())
}
37 changes: 35 additions & 2 deletions coderd/userauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/coderd"
"github.com/coder/coder/v2/coderd/audit"
Expand All @@ -30,6 +31,7 @@ import (
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/promoauth"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/cryptorand"
"github.com/coder/coder/v2/testutil"
)

Expand Down Expand Up @@ -937,7 +939,28 @@ func TestUserOIDC(t *testing.T) {
IgnoreUserInfo: true,
AllowSignups: true,
StatusCode: http.StatusOK,
}} {
},
{
Name: "HugeIDToken",
IDTokenClaims: inflateClaims(t, jwt.MapClaims{
"email": "user@domain.tld",
"email_verified": true,
}, 65536),
Username: "user",
AllowSignups: true,
StatusCode: http.StatusOK,
},
{
Name: "HugeClaims",
IDTokenClaims: jwt.MapClaims{
"email": "user@domain.tld",
"email_verified": true,
},
UserInfoClaims: inflateClaims(t, jwt.MapClaims{}, 65536),
Username: "user",
AllowSignups: true,
StatusCode: http.StatusOK,
}} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
Expand All @@ -956,7 +979,7 @@ func TestUserOIDC(t *testing.T) {
})

auditor := audit.NewMock()
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
owner := coderdtest.New(t, &coderdtest.Options{
Auditor: auditor,
OIDCConfig: cfg,
Expand Down Expand Up @@ -1287,3 +1310,13 @@ func authCookieValue(cookies []*http.Cookie) string {
}
return ""
}

// inflateClaims 'inflates' a jwt.MapClaims from a seed by
// adding a ridiculously large key-value pair of length size.
func inflateClaims(t testing.TB, seed jwt.MapClaims, size int) jwt.MapClaims {
t.Helper()
junk, err := cryptorand.String(size)
require.NoError(t, err)
seed["random_data"] = junk
return seed
}