test(14.1-02): prove oauth-identities migration and secret-leak list
- Migration up/down covers jsonb profile_data, unique indexes, and cascade FK - GET list with Encrypted tokens fails if plaintext, key names, or [redacted] leak Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"git.golem15.com/golem15/sm-user-plugin/models"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/lagoon"
|
||||
"git.golem15.com/golem15/summercms/modules/phrasebook"
|
||||
"git.golem15.com/golem15/summercms/modules/towel"
|
||||
"gorm.io/gorm"
|
||||
@@ -182,6 +183,66 @@ func TestOAuthIdentitiesDestroyWinter404BodiesAreByteIdentical(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOAuthIdentitiesIndexDoesNotLeakEncryptedTokensOrProfileData(t *testing.T) {
|
||||
app, db := sessionApp(t, "0s")
|
||||
if err := lagoon.PublishEncryptionKeys(nil, bytes.Repeat([]byte("K"), 32), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := insertUser(t, db, "oauth-secret-leak@example.test", "correct-horse")
|
||||
const (
|
||||
plaintext = "secret-access-token-oauth-leak-probe"
|
||||
refresh = "secret-refresh-token-oauth-leak-probe"
|
||||
marker = "profile-distinctive-xyzzy-oauth"
|
||||
)
|
||||
now := time.Now().UTC()
|
||||
row := models.OAuthIdentity{
|
||||
UserID: user.ID,
|
||||
Provider: "google",
|
||||
ProviderID: "g-secret-leak",
|
||||
AccessToken: lagoon.NewEncrypted(plaintext),
|
||||
RefreshToken: lagoon.NewEncrypted(refresh),
|
||||
ProfileData: lagoon.Jsonable[map[string]any]{
|
||||
Data: map[string]any{"email": "leak@example.test", "marker": marker},
|
||||
Valid: true,
|
||||
},
|
||||
LinkedAt: &now,
|
||||
}
|
||||
if err := db.Create(&row).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodGet, "/_fonoteka/api/v1/oauth-identities", nil)
|
||||
req = req.WithContext(bouncer.WithUser(req.Context(), &bouncer.Principal{ID: user.ID}))
|
||||
rec := httptest.NewRecorder()
|
||||
controllers.OAuthIdentitiesIndex(app).ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
body := rec.Body.String()
|
||||
for _, needle := range []string{plaintext, refresh, marker, "access_token", "refresh_token", "profile_data", "[redacted]"} {
|
||||
if strings.Contains(body, needle) {
|
||||
t.Fatalf("GET list leaked %q: %s", needle, body)
|
||||
}
|
||||
}
|
||||
var parsed struct {
|
||||
Data []map[string]any `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &parsed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(parsed.Data) != 1 {
|
||||
t.Fatalf("data = %#v", parsed.Data)
|
||||
}
|
||||
if len(parsed.Data[0]) != 2 {
|
||||
t.Fatalf("row keys = %#v, want provider and linked_at only", parsed.Data[0])
|
||||
}
|
||||
if parsed.Data[0]["provider"] != "google" {
|
||||
t.Fatalf("provider = %#v", parsed.Data[0]["provider"])
|
||||
}
|
||||
if _, ok := parsed.Data[0]["linked_at"]; !ok {
|
||||
t.Fatalf("linked_at missing: %#v", parsed.Data[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestOAuthIdentitiesUnauthorizedOnGoogle(t *testing.T) {
|
||||
app, _ := sessionApp(t, "0s")
|
||||
getReq := httptest.NewRequest(http.MethodGet, "/_fonoteka/api/v1/oauth-identities", nil)
|
||||
|
||||
Reference in New Issue
Block a user