Skip to content

Commit 9f81dc5

Browse files
authored
force lifetime on messages in ephemeral policy conversations CORE-10097 (keybase#15838)
1 parent 618ce46 commit 9f81dc5

29 files changed

Lines changed: 756 additions & 113 deletions

‎go/.go_package_deps_darwin‎

Lines changed: 128 additions & 0 deletions
Large diffs are not rendered by default.

‎go/.go_package_deps_linux‎

Lines changed: 129 additions & 0 deletions
Large diffs are not rendered by default.

‎go/.go_package_deps_windows‎

Lines changed: 130 additions & 0 deletions
Large diffs are not rendered by default.

‎go/chat/commands_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestChatCommands(t *testing.T) {
2929
users := ctc.users()
3030
useRemoteMock = false
3131
defer func() { useRemoteMock = true }()
32-
timeout := 2 * time.Second
32+
timeout := 20 * time.Second
3333
checkMsgText := func(list *serverChatListener, text string) {
3434
select {
3535
case msg := <-list.newMessageRemote:

‎go/chat/sender.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,23 @@ func (s *BlockingSender) Prepare(ctx context.Context, plaintext chat1.MessagePla
527527
return res, err
528528
}
529529

530+
// If no ephemeral data set, then let's double check to make sure no exploding policy
531+
// or Gregor state should set it
532+
if msg.EphemeralMetadata() == nil {
533+
s.Debug(ctx, "Prepare: attempting to set ephemeral policy from conversation")
534+
elf, err := utils.EphemeralLifetimeFromConv(ctx, s.G(), *conv)
535+
if err != nil {
536+
s.Debug(ctx, "Prepare: failed to get ephemeral lifetime from conv: %s", err)
537+
elf = nil
538+
}
539+
if elf != nil {
540+
s.Debug(ctx, "Prepare: setting ephemeral lifetime from conv: %v", *elf)
541+
msg.ClientHeader.EphemeralMetadata = &chat1.MsgEphemeralMetadata{
542+
Lifetime: *elf,
543+
}
544+
}
545+
}
546+
530547
metadata, err := s.getSupersederEphemeralMetadata(ctx, uid, convID, msg)
531548
if err != nil {
532549
s.Debug(ctx, "Prepare: error getting superdeder ephemeral metadata: %s", err)

‎go/chat/server_test.go‎

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"github.com/keybase/client/go/chat/types"
3030
"github.com/keybase/client/go/chat/utils"
3131
"github.com/keybase/client/go/chat/wallet"
32+
"github.com/keybase/client/go/gregor"
33+
grutils "github.com/keybase/client/go/gregor/utils"
3234
"github.com/keybase/client/go/kbtest"
3335
"github.com/keybase/client/go/libkb"
3436
"github.com/keybase/client/go/logger"
@@ -129,6 +131,34 @@ func (g *gregorTestConnection) BroadcastMessage(ctx context.Context, m gregor1.M
129131
return nil
130132
}
131133

134+
func (g *gregorTestConnection) State(ctx context.Context) (gregor.State, error) {
135+
return gregor1.IncomingClient{Cli: g.cli}.State(ctx, gregor1.StateArg{
136+
Uid: g.uid,
137+
})
138+
}
139+
140+
func (g *gregorTestConnection) DismissItem(ctx context.Context, cli gregor1.IncomingInterface, id gregor.MsgID) error {
141+
msg, err := grutils.FormMessageForDismissItem(ctx, g.uid, id)
142+
if err != nil {
143+
return err
144+
}
145+
return gregor1.IncomingClient{Cli: g.cli}.ConsumeMessage(ctx, msg.(gregor1.Message))
146+
}
147+
148+
func (g *gregorTestConnection) InjectItem(ctx context.Context, cat string, body []byte,
149+
dtime gregor1.TimeOrOffset) (gregor1.MsgID, error) {
150+
msg, err := grutils.FormMessageForInjectItem(ctx, g.uid, cat, body, dtime)
151+
if err != nil {
152+
return nil, err
153+
}
154+
retMsgID := gregor1.MsgID(msg.ToInBandMessage().Metadata().MsgID().Bytes())
155+
return retMsgID, gregor1.IncomingClient{Cli: g.cli}.ConsumeMessage(ctx, msg.(gregor1.Message))
156+
}
157+
158+
func (g *gregorTestConnection) LocalDismissItem(ctx context.Context, id gregor.MsgID) error {
159+
return nil
160+
}
161+
132162
func (g *gregorTestConnection) OnConnectError(err error, reconnectThrottleDuration time.Duration) {
133163
}
134164

@@ -311,6 +341,7 @@ func (c *chatTestContext) as(t *testing.T, user *kbtest.FakeUser) *chatTestUserC
311341
require.NoError(t, err)
312342
sessionToken := nist.Token().String()
313343
gh := newGregorTestConnection(tc.Context(), uid, sessionToken)
344+
g.GregorState = gh
314345
require.NoError(t, gh.Connect(ctx))
315346
ri = gh.GetClient()
316347
}
@@ -4362,14 +4393,11 @@ func TestChatSrvRetentionSweepConv(t *testing.T) {
43624393
require.NoError(t, err)
43634394
require.Len(t, tvres.Thread.Messages, 1, "the TEXTs should be deleted")
43644395

4365-
// If we are using an ephemeral policy make sure non-ephemeral
4366-
// messages and message with a lifetime exceeding the policy age
4367-
// are blocked.
4396+
// If we are using an ephemeral policy make sure messages with a lifetime exceeding
4397+
// the policy age are blocked.
43684398
if ephemeralLifetime != nil {
4369-
_, err := postLocalForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{Body: "hello!"}))
4370-
require.Error(t, err)
43714399
badLifetime := *ephemeralLifetime + 1
4372-
_, err = postLocalEphemeralForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{Body: "hello!"}), &badLifetime)
4400+
_, err := postLocalEphemeralForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{Body: "hello!"}), &badLifetime)
43734401
require.Error(t, err)
43744402

43754403
_, err = postLocalEphemeralForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{Body: "hello!"}), ephemeralLifetime)
@@ -4481,14 +4509,10 @@ func TestChatSrvRetentionSweepTeam(t *testing.T) {
44814509
checkThread(convC.Id, false)
44824510
if ephemeralLifetime != nil {
44834511
for _, conv := range []chat1.ConversationInfoLocal{convA, convB} {
4484-
// If we are using an ephemeral policy make sure non-ephemeral
4485-
// messages and message with a lifetime exceeding the policy age
4486-
// are blocked.
4487-
_, err := postLocalForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{Body: "hello!"}))
4488-
require.Error(t, err)
4489-
4512+
// If we are using an ephemeral policy make sure messages with a lifetime exceeding
4513+
// the policy age are blocked.
44904514
badLifetime := *ephemeralLifetime + 1
4491-
_, err = postLocalEphemeralForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{Body: "hello!"}), &badLifetime)
4515+
_, err := postLocalEphemeralForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{Body: "hello!"}), &badLifetime)
44924516
require.Error(t, err)
44934517

44944518
_, err = postLocalEphemeralForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{Body: "hello!"}), ephemeralLifetime)
@@ -5865,6 +5889,73 @@ func TestChatSrvStellarUI(t *testing.T) {
58655889
consumeNewPendingMsg(t, listener)
58665890
}
58675891

5892+
func TestChatSrvEphemeralPolicy(t *testing.T) {
5893+
ctc := makeChatTestContext(t, "TestChatSrvEphemeralPolicy", 1)
5894+
defer ctc.cleanup()
5895+
users := ctc.users()
5896+
useRemoteMock = false
5897+
defer func() { useRemoteMock = true }()
5898+
5899+
timeout := 20 * time.Second
5900+
ctx := ctc.as(t, users[0]).startCtx
5901+
tc := ctc.world.Tcs[users[0].Username]
5902+
listener0 := newServerChatListener()
5903+
ctc.as(t, users[0]).h.G().NotifyRouter.SetListener(listener0)
5904+
getMsg := func(ctx context.Context, convID chat1.ConversationID, msgID chat1.MessageID) chat1.MessageUnboxed {
5905+
res, err := ctc.as(t, users[0]).chatLocalHandler().GetMessagesLocal(ctx, chat1.GetMessagesLocalArg{
5906+
ConversationID: convID,
5907+
MessageIDs: []chat1.MessageID{msgID},
5908+
})
5909+
require.NoError(t, err)
5910+
require.Equal(t, 1, len(res.Messages))
5911+
return res.Messages[0]
5912+
}
5913+
checkEph := func(convID chat1.ConversationID, exp int) {
5914+
select {
5915+
case rmsg := <-listener0.newMessageRemote:
5916+
msg := getMsg(ctx, convID, rmsg.Message.GetMessageID())
5917+
require.True(t, msg.IsValid())
5918+
require.NotNil(t, msg.Valid().ClientHeader.EphemeralMetadata)
5919+
require.Equal(t, gregor1.DurationSec(exp), msg.Valid().ClientHeader.EphemeralMetadata.Lifetime)
5920+
case <-time.After(timeout):
5921+
require.Fail(t, "no message")
5922+
}
5923+
}
5924+
5925+
impconv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT,
5926+
chat1.ConversationMembersType_IMPTEAMNATIVE)
5927+
mustSetConvRetentionPolicy(t, ctc, users[0], impconv.Id, chat1.NewRetentionPolicyWithEphemeral(chat1.RpEphemeral{
5928+
Age: gregor1.DurationSec(86400),
5929+
}), 0)
5930+
consumeSetConvRetention(t, listener0)
5931+
mustPostLocalForTest(t, ctc, users[0], impconv,
5932+
chat1.NewMessageBodyWithText(chat1.MessageText{
5933+
Body: "HI",
5934+
}))
5935+
checkEph(impconv.Id, 86400)
5936+
_, err := tc.Context().GregorState.InjectItem(ctx, fmt.Sprintf("exploding:%s", impconv.Id),
5937+
[]byte("3600"), gregor1.TimeOrOffset{})
5938+
require.NoError(t, err)
5939+
mustPostLocalForTest(t, ctc, users[0], impconv,
5940+
chat1.NewMessageBodyWithText(chat1.MessageText{
5941+
Body: "HI",
5942+
}))
5943+
checkEph(impconv.Id, 3600)
5944+
5945+
teamconv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT,
5946+
chat1.ConversationMembersType_TEAM)
5947+
mustSetTeamRetentionPolicy(t, ctc, users[0], keybase1.TeamID(teamconv.Triple.Tlfid.String()),
5948+
chat1.NewRetentionPolicyWithEphemeral(chat1.RpEphemeral{
5949+
Age: gregor1.DurationSec(86400),
5950+
}), 0)
5951+
consumeSetTeamRetention(t, listener0)
5952+
mustPostLocalForTest(t, ctc, users[0], teamconv,
5953+
chat1.NewMessageBodyWithText(chat1.MessageText{
5954+
Body: "HI",
5955+
}))
5956+
checkEph(teamconv.Id, 86400)
5957+
}
5958+
58685959
func TestChatSrvStellarMessages(t *testing.T) {
58695960
runWithMemberTypes(t, func(mt chat1.ConversationMembersType) {
58705961
runWithEphemeral(t, mt, func(ephemeralLifetime *gregor1.DurationSec) {

‎go/chat/utils/utils.go‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,69 @@ func IsPermanentErr(err error) bool {
17441744
return err != nil
17451745
}
17461746

1747+
func EphemeralLifetimeFromConv(ctx context.Context, g *globals.Context, conv chat1.Conversation) (res *gregor1.DurationSec, err error) {
1748+
// Check to see if the conversation has an exploding policy
1749+
var retentionRes *gregor1.DurationSec
1750+
var gregorRes *gregor1.DurationSec
1751+
var rentTyp chat1.RetentionPolicyType
1752+
if conv.ConvRetention != nil {
1753+
if rentTyp, err = conv.ConvRetention.Typ(); err != nil {
1754+
return res, err
1755+
}
1756+
if rentTyp == chat1.RetentionPolicyType_EPHEMERAL {
1757+
e := conv.ConvRetention.Ephemeral()
1758+
retentionRes = &e.Age
1759+
}
1760+
}
1761+
if retentionRes == nil && conv.TeamRetention != nil {
1762+
if rentTyp, err = conv.TeamRetention.Typ(); err != nil {
1763+
return res, err
1764+
}
1765+
if rentTyp == chat1.RetentionPolicyType_EPHEMERAL {
1766+
e := conv.TeamRetention.Ephemeral()
1767+
retentionRes = &e.Age
1768+
}
1769+
}
1770+
1771+
// See if there is anything in Gregor
1772+
st, err := g.GregorState.State(ctx)
1773+
if err != nil {
1774+
return res, err
1775+
}
1776+
// Note: this value is present on the JS frontend as well
1777+
key := fmt.Sprintf("exploding:%s", conv.GetConvID())
1778+
cat, err := gregor1.ObjFactory{}.MakeCategory(key)
1779+
if err != nil {
1780+
return res, err
1781+
}
1782+
items, err := st.ItemsWithCategoryPrefix(cat)
1783+
if err != nil {
1784+
return res, err
1785+
}
1786+
if len(items) > 0 {
1787+
it := items[0]
1788+
body := string(it.Body().Bytes())
1789+
sec, err := strconv.ParseInt(body, 0, 0)
1790+
if err != nil {
1791+
return res, nil
1792+
}
1793+
gsec := gregor1.DurationSec(sec)
1794+
gregorRes = &gsec
1795+
}
1796+
if retentionRes != nil && gregorRes != nil {
1797+
if *gregorRes < *retentionRes {
1798+
return gregorRes, nil
1799+
}
1800+
return retentionRes, nil
1801+
} else if retentionRes != nil {
1802+
return retentionRes, nil
1803+
} else if gregorRes != nil {
1804+
return gregorRes, nil
1805+
} else {
1806+
return nil, nil
1807+
}
1808+
}
1809+
17471810
var decorateBegin = "$>kb$"
17481811
var decorateEnd = "$<kb$"
17491812
var decorateEscapeRe = regexp.MustCompile(`\\*\$\>kb\$`)

‎go/engine/track_token.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ func (e *TrackToken) Run(m libkb.MetaContext) (err error) {
147147

148148
// Dismiss any associated gregor item.
149149
if outcome.ResponsibleGregorItem != nil {
150-
err = m.G().GregorDismisser.DismissItem(m.Ctx(), nil, outcome.ResponsibleGregorItem.Metadata().MsgID())
150+
err = m.G().GregorState.DismissItem(m.Ctx(), nil,
151+
outcome.ResponsibleGregorItem.Metadata().MsgID())
151152
}
152153
}
153154

‎go/engine/track_token_test.go‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,26 @@ func TestTrackFailTempRecover(t *testing.T) {
439439
assertTracking(tc, username)
440440
}
441441

442-
type FakeGregorDismisser struct {
442+
type FakeGregorState struct {
443443
dismissedMsgID gregor.MsgID
444444
}
445445

446-
var _ libkb.GregorDismisser = (*FakeGregorDismisser)(nil)
446+
var _ libkb.GregorState = (*FakeGregorState)(nil)
447447

448-
func (d *FakeGregorDismisser) DismissItem(ctx context.Context, cli gregor1.IncomingInterface, id gregor.MsgID) error {
448+
func (d *FakeGregorState) State(ctx context.Context) (gregor.State, error) {
449+
return nil, nil
450+
}
451+
452+
func (d *FakeGregorState) InjectItem(ctx context.Context, cat string, body []byte, dtime gregor1.TimeOrOffset) (gregor1.MsgID, error) {
453+
return nil, nil
454+
}
455+
456+
func (d *FakeGregorState) DismissItem(ctx context.Context, cli gregor1.IncomingInterface, id gregor.MsgID) error {
449457
d.dismissedMsgID = id
450458
return nil
451459
}
452460

453-
func (d *FakeGregorDismisser) LocalDismissItem(ctx context.Context, id gregor.MsgID) error {
461+
func (d *FakeGregorState) LocalDismissItem(ctx context.Context, id gregor.MsgID) error {
454462
return nil
455463
}
456464

@@ -460,8 +468,8 @@ func TestTrackWithTokenDismissesGregor(t *testing.T) {
460468
sigVersion := libkb.GetDefaultSigVersion(tc.G)
461469
fu := CreateAndSignupFakeUser(tc, "track")
462470

463-
dismisser := &FakeGregorDismisser{}
464-
tc.G.GregorDismisser = dismisser
471+
dismisser := &FakeGregorState{}
472+
tc.G.GregorState = dismisser
465473

466474
msgID := gregor1.MsgID("my_random_id")
467475
responsibleGregorItem := gregor1.ItemAndMetadata{

‎go/gregor/utils/utils.go‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package utils
2+
3+
import (
4+
"github.com/keybase/client/go/gregor"
5+
"github.com/keybase/client/go/libkb"
6+
"github.com/keybase/client/go/protocol/gregor1"
7+
"golang.org/x/net/context"
8+
)
9+
10+
func NewGregorMsgID() (gregor1.MsgID, error) {
11+
r, err := libkb.RandBytes(16)
12+
if err != nil {
13+
return nil, err
14+
}
15+
return gregor1.MsgID(r), nil
16+
}
17+
18+
func TemplateMessage(uid gregor1.UID) (gregor1.Message, error) {
19+
newMsgID, err := NewGregorMsgID()
20+
if err != nil {
21+
return gregor1.Message{}, err
22+
}
23+
return gregor1.Message{
24+
Ibm_: &gregor1.InBandMessage{
25+
StateUpdate_: &gregor1.StateUpdateMessage{
26+
Md_: gregor1.Metadata{
27+
Uid_: uid,
28+
MsgID_: newMsgID,
29+
},
30+
},
31+
},
32+
}, nil
33+
}
34+
35+
func FormMessageForInjectItem(ctx context.Context, uid gregor1.UID, cat string, body []byte,
36+
dtime gregor1.TimeOrOffset) (gregor.Message, error) {
37+
creation, err := TemplateMessage(uid)
38+
if err != nil {
39+
return nil, err
40+
}
41+
creation.Ibm_.StateUpdate_.Creation_ = &gregor1.Item{
42+
Category_: gregor1.Category(cat),
43+
Body_: gregor1.Body(body),
44+
Dtime_: dtime,
45+
}
46+
return creation, nil
47+
}
48+
49+
func FormMessageForDismissItem(ctx context.Context, uid gregor1.UID, id gregor.MsgID) (gregor.Message, error) {
50+
dismissal, err := TemplateMessage(uid)
51+
if err != nil {
52+
return nil, err
53+
}
54+
dismissal.Ibm_.StateUpdate_.Dismissal_ = &gregor1.Dismissal{
55+
MsgIDs_: []gregor1.MsgID{gregor1.MsgID(id.Bytes())},
56+
}
57+
return dismissal, nil
58+
}

0 commit comments

Comments
 (0)