Fix issue where device keys are removed if a device ID is reused (#2982)

Fixes https://github.com/matrix-org/dendrite/issues/2980
This commit is contained in:
Till 2023-02-20 15:26:09 +01:00 committed by GitHub
parent 4594233f89
commit 7f114cc538
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 16 deletions

View file

@ -254,6 +254,17 @@ func (a *UserInternalAPI) PerformDeviceCreation(ctx context.Context, req *api.Pe
if !a.Config.Matrix.IsLocalServerName(serverName) {
return fmt.Errorf("server name %s is not local", serverName)
}
// If a device ID was specified, check if it already exists and
// avoid sending an empty device list update which would remove
// existing device keys.
isExisting := false
if req.DeviceID != nil && *req.DeviceID != "" {
existingDev, err := a.DB.GetDeviceByID(ctx, req.Localpart, req.ServerName, *req.DeviceID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
isExisting = existingDev.ID == *req.DeviceID
}
util.GetLogger(ctx).WithFields(logrus.Fields{
"localpart": req.Localpart,
"device_id": req.DeviceID,
@ -265,7 +276,7 @@ func (a *UserInternalAPI) PerformDeviceCreation(ctx context.Context, req *api.Pe
}
res.DeviceCreated = true
res.Device = dev
if req.NoDeviceListUpdate {
if req.NoDeviceListUpdate || isExisting {
return nil
}
// create empty device keys and upload them to trigger device list changes