π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
wall: reject stale link creation during rotation
Neeraj Gupta9bf1a854dab6
diff --git a/server/wall/controller/links.go b/server/wall/controller/links.goindex c4759af3a6..5c762f404c 100644--- a/server/wall/controller/links.go+++ b/server/wall/controller/links.go@@ -2,7 +2,9 @@ package controllerimport ("crypto/sha256"+ "database/sql""encoding/base64"+ "errors""strings""github.com/ente-io/museum/ente"@@ -71,6 +73,9 @@ func (c *LinksController) Create(ctx *gin.Context, req models.WallLinkCreateRequsum := sha256.Sum256(authKeyBytes)link, err := c.LinksRepo.UpsertLink(ctx.Request.Context(), wall.WallID, sum[:], req.KeyVersion, req.EncryptedWallKey)if err != nil {+ if errors.Is(err, sql.ErrNoRows) {+ return nil, ente.NewBadRequestWithMessage("keyVersion does not match current wall version")+ }return nil, err}return &models.WallLinkStatusResponse{diff --git a/server/wall/repo/links.go b/server/wall/repo/links.goindex cdf9d2a84a..d2ffadea7e 100644--- a/server/wall/repo/links.go+++ b/server/wall/repo/links.go@@ -13,6 +13,20 @@ func (r *LinksRepository) UpsertLink(ctx context.Context, wallID string, authKeyreturn nil, stacktrace.Propagate(err, "")}defer tx.Rollback()+ var ownerID int64+ var wallSlug string+ var currentVersion int+ if err := tx.QueryRowContext(ctx, `+ SELECT owner_id, wall_slug, current_version+ FROM walls+ WHERE wall_id = $1+ FOR UPDATE+ `, wallID).Scan(&ownerID, &wallSlug, ¤tVersion); err != nil {+ return nil, stacktrace.Propagate(err, "")+ }+ if currentVersion != keyVersion {+ return nil, sql.ErrNoRows+ }if _, err := tx.ExecContext(ctx, `DELETE FROM wall_link_sessions WHERE wall_id = $1`, wallID); err != nil {return nil, stacktrace.Propagate(err, "")}@@ -24,9 +38,8 @@ func (r *LinksRepository) UpsertLink(ctx context.Context, wallID string, authKeykey_version = EXCLUDED.key_version,encrypted_wall_key = EXCLUDED.encrypted_wall_key,active = TRUE- RETURNING wall_id, (SELECT wall_slug FROM walls WHERE wall_id = $1), (SELECT owner_id FROM walls WHERE wall_id = $1),- (SELECT wall_slug FROM walls WHERE wall_id = $1), auth_key_hash, key_version, encrypted_wall_key, active, created_at, updated_at- `, wallID, authKeyHash, keyVersion, encryptedWallKey))+ RETURNING wall_id, $5::text, $6::bigint, $5::text, auth_key_hash, key_version, encrypted_wall_key, active, created_at, updated_at+ `, wallID, authKeyHash, keyVersion, encryptedWallKey, wallSlug, ownerID))if err != nil {return nil, err}diff --git a/server/wall/repo/module_test.go b/server/wall/repo/module_test.goindex 893b9d3a36..34a160c5e8 100644--- a/server/wall/repo/module_test.go+++ b/server/wall/repo/module_test.go@@ -326,7 +326,9 @@ func TestGetSessionRejectsStaleLinkMetadata(t *testing.T) {_, err = module.Links.GetSession(ctx, []byte("stale-auth-token"))require.Error(t, err)- reusedHashLink, err := module.Links.UpsertLink(ctx, wall.WallID, oldLink.AuthKeyHash, wall.CurrentVersion+1, "reused-hash-new-version-key")+ rotatedWall, err := module.Walls.RotateKey(ctx, aliceID, wall.WallID, "alice-wall-key-v2", "wrapped-prev-key", nil)+ require.NoError(t, err)+ reusedHashLink, err := module.Links.UpsertLink(ctx, wall.WallID, oldLink.AuthKeyHash, rotatedWall.CurrentVersion, "reused-hash-new-version-key")require.NoError(t, err)_, err = module.Links.DB.Exec(`INSERT INTO wall_link_sessions (token_hash, wall_id, owner_id, auth_key_hash, key_version, expires_at)@@ -337,6 +339,25 @@ func TestGetSessionRejectsStaleLinkMetadata(t *testing.T) {require.Error(t, err)}+func TestUpsertLinkRejectsStaleKeyVersion(t *testing.T) {+ ctx := context.Background()+ module := newWallTestModule(t)++ aliceID := insertWallUser(t, module, "[email protected]", "alice-public")+ wall, err := module.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")+ require.NoError(t, err)++ rotatedWall, err := module.Walls.RotateKey(ctx, aliceID, wall.WallID, "alice-wall-key-v2", "wrapped-prev-key", nil)+ require.NoError(t, err)+ require.Equal(t, wall.CurrentVersion+1, rotatedWall.CurrentVersion)++ _, err = module.Links.UpsertLink(ctx, wall.WallID, []byte("stale-hash"), wall.CurrentVersion, "stale-wall-link-key")+ require.ErrorIs(t, err, sql.ErrNoRows)++ _, err = module.Links.GetLink(ctx, wall.WallID)+ require.Error(t, err)+}+func TestListTopLevelCommentsPaginates(t *testing.T) {ctx := context.Background()module := newWallTestModule(t)diff --git a/server/wall/repo/walls.go b/server/wall/repo/walls.goindex 9133a1aec3..30e6645d18 100644--- a/server/wall/repo/walls.go+++ b/server/wall/repo/walls.go@@ -163,6 +163,7 @@ func (r *WallsRepository) RotateKey(ctx context.Context, ownerID int64, wallID,avatar_object_key, avatar_bucket_id, avatar_size, created_at, updated_atFROM wallsWHERE owner_id = $1 AND wall_id = $2+ FOR UPDATE`, ownerID, wallID))if err != nil {return nil, err