π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
wall: bind link sessions to link identity
Neeraj Guptae6b6bda6fdec
diff --git a/server/migrations/121_wall.up.sql b/server/migrations/121_wall.up.sqlindex 1ef69d2ef2..91aa243e8b 100644--- a/server/migrations/121_wall.up.sql+++ b/server/migrations/121_wall.up.sql@@ -195,6 +195,8 @@ CREATE TABLE IF NOT EXISTS wall_link_sessions (token_hash BYTEA PRIMARY KEY,wall_id TEXT NOT NULL REFERENCES walls (wall_id) ON DELETE CASCADE,owner_id BIGINT NOT NULL REFERENCES users (user_id) ON DELETE CASCADE,+ auth_key_hash BYTEA NOT NULL,+ key_version INTEGER NOT NULL,expires_at BIGINT NOT NULL,created_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds());diff --git a/server/wall/controller/links.go b/server/wall/controller/links.goindex f265be2a75..c4759af3a6 100644--- a/server/wall/controller/links.go+++ b/server/wall/controller/links.go@@ -113,7 +113,7 @@ func (c *LinksController) Login(ctx *gin.Context, req models.WallLinkLoginRequesreturn nil, err}sessionHash := sha256.Sum256([]byte(sessionToken))- if err := c.LinksRepo.CreateSession(ctx.Request.Context(), sessionHash[:], link.WallID, link.OwnerID, timeutil.MicrosecondsAfterMinutes(wallLinkSessionDurationMinutes)); err != nil {+ if err := c.LinksRepo.CreateSession(ctx.Request.Context(), sessionHash[:], link.WallID, link.AuthKeyHash, link.KeyVersion, timeutil.MicrosecondsAfterMinutes(wallLinkSessionDurationMinutes)); err != nil {return nil, err}return &models.WallLinkLoginResponse{diff --git a/server/wall/repo/links.go b/server/wall/repo/links.goindex 140abbf3ec..cdf9d2a84a 100644--- a/server/wall/repo/links.go+++ b/server/wall/repo/links.go@@ -76,26 +76,44 @@ func (r *LinksRepository) GetLinkByAuthHash(ctx context.Context, wallID string,`, wallID, authHash))}-func (r *LinksRepository) CreateSession(ctx context.Context, tokenHash []byte, wallID string, ownerID int64, expiresAt int64) error {- _, err := r.DB.ExecContext(ctx, `- INSERT INTO wall_link_sessions (token_hash, wall_id, owner_id, expires_at)- VALUES ($1, $2, $3, $4)- `, tokenHash, wallID, ownerID, expiresAt)- return stacktrace.Propagate(err, "")+func (r *LinksRepository) CreateSession(ctx context.Context, tokenHash []byte, wallID string, authKeyHash []byte, keyVersion int, expiresAt int64) error {+ res, err := r.DB.ExecContext(ctx, `+ INSERT INTO wall_link_sessions (token_hash, wall_id, owner_id, auth_key_hash, key_version, expires_at)+ SELECT $1, l.wall_id, w.owner_id, l.auth_key_hash, l.key_version, $5+ FROM wall_links l+ JOIN walls w ON w.wall_id = l.wall_id+ WHERE l.wall_id = $2+ AND l.auth_key_hash = $3+ AND l.key_version = $4+ AND l.active = TRUE+ `, tokenHash, wallID, authKeyHash, keyVersion, expiresAt)+ if err != nil {+ return stacktrace.Propagate(err, "")+ }+ affected, err := res.RowsAffected()+ if err != nil {+ return stacktrace.Propagate(err, "")+ }+ if affected == 0 {+ return sql.ErrNoRows+ }+ return nil}func (r *LinksRepository) GetSession(ctx context.Context, tokenHash []byte) (*WallLinkSessionRecord, error) {row := r.DB.QueryRowContext(ctx, `- SELECT s.token_hash, s.wall_id, s.owner_id, s.expires_at, s.created_at,- w.wall_slug, owner_wall.wall_slug, l.key_version, l.encrypted_wall_key+ SELECT s.token_hash, s.wall_id, s.owner_id, s.auth_key_hash, s.key_version, s.expires_at, s.created_at,+ w.wall_slug, owner_wall.wall_slug, l.encrypted_wall_keyFROM wall_link_sessions sJOIN walls w ON w.wall_id = s.wall_idJOIN walls owner_wall ON owner_wall.owner_id = s.owner_idJOIN wall_links l ON l.wall_id = s.wall_id+ AND l.auth_key_hash = s.auth_key_hash+ AND l.key_version = s.key_versionWHERE s.token_hash = $1 AND l.active = TRUE`, tokenHash)var rec WallLinkSessionRecord- if err := row.Scan(&rec.TokenHash, &rec.WallID, &rec.OwnerID, &rec.ExpiresAt, &rec.CreatedAt, &rec.WallSlug, &rec.OwnerSlug, &rec.KeyVersion, &rec.EncryptedWallKey); err != nil {+ if err := row.Scan(&rec.TokenHash, &rec.WallID, &rec.OwnerID, &rec.AuthKeyHash, &rec.KeyVersion, &rec.ExpiresAt, &rec.CreatedAt, &rec.WallSlug, &rec.OwnerSlug, &rec.EncryptedWallKey); err != nil {return nil, stacktrace.Propagate(err, "")}return &rec, nildiff --git a/server/wall/repo/module.go b/server/wall/repo/module.goindex 9e1fc71d6d..9a35081343 100644--- a/server/wall/repo/module.go+++ b/server/wall/repo/module.go@@ -155,11 +155,12 @@ type WallLinkSessionRecord struct {TokenHash []byteWallID stringOwnerID int64+ AuthKeyHash []byte+ KeyVersion intExpiresAt int64CreatedAt int64WallSlug stringOwnerSlug string- KeyVersion intEncryptedWallKey string}diff --git a/server/wall/repo/module_test.go b/server/wall/repo/module_test.goindex c545582d0f..893b9d3a36 100644--- a/server/wall/repo/module_test.go+++ b/server/wall/repo/module_test.go@@ -226,19 +226,19 @@ func TestWallModuleLifecycle(t *testing.T) {require.NoError(t, err)require.Equal(t, "wall-link-key", link.EncryptedWallKey)- err = module.Links.CreateSession(ctx, []byte("token-hash"), aliceWall.WallID, aliceID, timeutil.NMinFromNow(30))+ err = module.Links.CreateSession(ctx, []byte("token-hash"), link.WallID, link.AuthKeyHash, link.KeyVersion, timeutil.NMinFromNow(30))require.NoError(t, err)session, err := module.Links.GetSession(ctx, []byte("token-hash"))require.NoError(t, err)require.Equal(t, aliceWall.WallID, session.WallID)- _, err = module.Links.UpsertLink(ctx, aliceWall.WallID, []byte("new-hash"), rotatedWall.CurrentVersion, "new-wall-link-key")+ newLink, err := module.Links.UpsertLink(ctx, aliceWall.WallID, []byte("new-hash"), rotatedWall.CurrentVersion, "new-wall-link-key")require.NoError(t, err)_, err = module.Links.GetSession(ctx, []byte("token-hash"))require.Error(t, err)- err = module.Links.CreateSession(ctx, []byte("token-hash"), aliceWall.WallID, aliceID, timeutil.NMinFromNow(30))+ err = module.Links.CreateSession(ctx, []byte("token-hash"), newLink.WallID, newLink.AuthKeyHash, newLink.KeyVersion, timeutil.NMinFromNow(30))require.NoError(t, err)err = module.Links.DeleteLink(ctx, aliceWall.WallID)require.NoError(t, err)@@ -262,7 +262,7 @@ func TestRotateKeyRevokesWallLinks(t *testing.T) {require.NoError(t, err)require.Equal(t, "wall-link-key", link.EncryptedWallKey)- err = module.Links.CreateSession(ctx, []byte("token-hash"), wall.WallID, aliceID, timeutil.NMinFromNow(30))+ err = module.Links.CreateSession(ctx, []byte("token-hash"), link.WallID, link.AuthKeyHash, link.KeyVersion, timeutil.NMinFromNow(30))require.NoError(t, err)_, err = module.Links.GetSession(ctx, []byte("token-hash"))require.NoError(t, err)@@ -281,6 +281,62 @@ func TestRotateKeyRevokesWallLinks(t *testing.T) {require.Len(t, versions, 2)}+func TestCreateSessionRejectsStaleLinkAuthHash(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)++ oldLink, err := module.Links.UpsertLink(ctx, wall.WallID, []byte("old-hash"), wall.CurrentVersion, "old-wall-link-key")+ require.NoError(t, err)+ newLink, err := module.Links.UpsertLink(ctx, wall.WallID, []byte("new-hash"), wall.CurrentVersion, "new-wall-link-key")+ require.NoError(t, err)++ err = module.Links.CreateSession(ctx, []byte("stale-token"), oldLink.WallID, oldLink.AuthKeyHash, oldLink.KeyVersion, timeutil.NMinFromNow(30))+ require.Error(t, err)++ err = module.Links.CreateSession(ctx, []byte("fresh-token"), newLink.WallID, newLink.AuthKeyHash, newLink.KeyVersion, timeutil.NMinFromNow(30))+ require.NoError(t, err)+ session, err := module.Links.GetSession(ctx, []byte("fresh-token"))+ require.NoError(t, err)+ require.Equal(t, newLink.KeyVersion, session.KeyVersion)+ require.Equal(t, newLink.AuthKeyHash, session.AuthKeyHash)+}++func TestGetSessionRejectsStaleLinkMetadata(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)++ oldLink, err := module.Links.UpsertLink(ctx, wall.WallID, []byte("old-hash"), wall.CurrentVersion, "old-wall-link-key")+ require.NoError(t, err)+ _, err = module.Links.UpsertLink(ctx, wall.WallID, []byte("new-hash"), wall.CurrentVersion, "new-wall-link-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)+ VALUES ($1, $2, $3, $4, $5, $6)+ `, []byte("stale-auth-token"), oldLink.WallID, oldLink.OwnerID, oldLink.AuthKeyHash, oldLink.KeyVersion, timeutil.NMinFromNow(30))+ require.NoError(t, err)+ _, 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")+ 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)+ VALUES ($1, $2, $3, $4, $5, $6)+ `, []byte("stale-version-token"), reusedHashLink.WallID, reusedHashLink.OwnerID, reusedHashLink.AuthKeyHash, wall.CurrentVersion, timeutil.NMinFromNow(30))+ require.NoError(t, err)+ _, err = module.Links.GetSession(ctx, []byte("stale-version-token"))+ require.Error(t, err)+}+func TestListTopLevelCommentsPaginates(t *testing.T) {ctx := context.Background()module := newWallTestModule(t)