Skip to content

Commit 1c71f27

Browse files
committed
Only accept delete requests for contacts we have in the DB
1 parent f8e4e48 commit 1c71f27

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

handlers/meta/facebook_test.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,14 @@ func TestFacebookDescribeURN(t *testing.T) {
323323
}
324324

325325
func TestDeleteRequest(t *testing.T) {
326+
urn, _ := urns.New(urns.Facebook, "218471")
326327
RunIncomingTestCases(t, facebookTestChannels, newHandler("FBA", "Facebook"), []IncomingTestCase{
327328
{
328-
Label: "Receive Delete request FBA",
329-
URL: "/c/fba/delete",
330-
Data: `{"algorithm":"HMAC-SHA256","expires":1291840400,"issued_at":1291836800,"user_id":"218471"}`,
331-
PrepRequest: addValidSignature,
329+
Label: "Receive Delete request FBA",
330+
URL: "/c/fba/delete",
331+
Data: `{"algorithm":"HMAC-SHA256","expires":1291840400,"issued_at":1291836800,"user_id":"218471"}`,
332+
PrepRequest: addValidSignature,
333+
ExistingDBURNs: []urns.URN{urn},
332334

333335
ExpectedRespStatus: 200,
334336
ExpectedBodyContains: "Deletion Request Received",
@@ -340,10 +342,24 @@ func TestDeleteRequest(t *testing.T) {
340342
},
341343
},
342344
{
343-
Label: "Receive Delete request FBA",
344-
URL: "/c/fba/delete",
345-
Data: `{"algorithm":"HMAC-SHA256","expires":1291840400,"issued_at":1291836800,"user_id":"abc1234"}`,
346-
PrepRequest: addValidSignature,
345+
Label: "Receive Delete request FBA, contact not existing",
346+
URL: "/c/fba/delete",
347+
Data: `{"algorithm":"HMAC-SHA256","expires":1291840400,"issued_at":1291836800,"user_id":"123456"}`,
348+
PrepRequest: addValidSignature,
349+
ExistingDBURNs: []urns.URN{urn},
350+
351+
ExpectedRespStatus: 200,
352+
ExpectedBodyContains: "ignoring request, no existing contact matched",
353+
NoQueueErrorCheck: true,
354+
NoInvalidChannelCheck: true,
355+
NoLogsExpected: true,
356+
},
357+
{
358+
Label: "Receive Delete request FBA, invalid facebook ID",
359+
URL: "/c/fba/delete",
360+
Data: `{"algorithm":"HMAC-SHA256","expires":1291840400,"issued_at":1291836800,"user_id":"abc1234"}`,
361+
PrepRequest: addValidSignature,
362+
ExistingDBURNs: []urns.URN{urn},
347363

348364
ExpectedRespStatus: 200,
349365
ExpectedBodyContains: "invalid facebook id",

handlers/meta/handlers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ func (h *handler) deleteContactEvents(ctx context.Context, channel courier.Chann
240240
if err != nil {
241241
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, errors.New("invalid facebook id"))
242242
}
243+
244+
contact, err := h.Server().Backend().GetContact(ctx, channel, urn, nil, "", false, clog)
245+
if contact == nil {
246+
return nil, handlers.WriteAndLogRequestIgnored(ctx, h, channel, w, r, "ignoring request, no existing contact matched")
247+
}
248+
243249
date := parseTimestamp(payload.IssuedAt)
244250

245251
events := make([]courier.Event, 0, 2)

handlers/test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type IncomingTestCase struct {
5252
NoInvalidChannelCheck bool
5353
PrepRequest RequestPrepFunc
5454

55+
ExistingDBURNs []urns.URN
56+
5557
URL string
5658
Data string
5759
Headers map[string]string
@@ -159,6 +161,11 @@ func RunIncomingTestCases(t *testing.T, channels []courier.Channel, handler cour
159161
handler.Initialize(s)
160162

161163
for _, tc := range testCases {
164+
for _, urn := range tc.ExistingDBURNs {
165+
ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
166+
s.Backend().GetContact(ctx, channels[0], urn, nil, "", true, nil)
167+
}
168+
162169
t.Run(tc.Label, func(t *testing.T) {
163170
require := require.New(t)
164171

0 commit comments

Comments
 (0)