Other: Fix IMAP/SMTP/Login leaks/race conditions

Depending on the timing of bridge closure, it was possible for the 
IMAP/SMTP servers to not have started serving yet. By grouping this in
a cancelable goroutine group (*xsync.Group), we mitigate this issue.

Further, depending on internet disconnection timing during user login,
it was possible for a user to be improperly logged in. This change 
fixes this and adds test coverage for it.

Lastly, depending on timing, certain background tasks (updates check,
connectivity ping) could be improperly started or closed. This change
groups them in the *xsync.Group as well to be closed properly.
This commit is contained in:
James Houlahan
2022-10-24 13:25:11 +02:00
parent 828385b049
commit 6fbf6d90dc
13 changed files with 518 additions and 461 deletions

View File

@ -20,6 +20,7 @@ package bridge
import (
"context"
"fmt"
"net"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/v2/internal/updater"
@ -131,7 +132,15 @@ func (bridge *Bridge) SetGluonDir(ctx context.Context, newGluonDir string) error
return fmt.Errorf("failed to set new gluon dir: %w", err)
}
imapServer, err := newIMAPServer(bridge.vault.GetGluonDir(), bridge.curVersion, bridge.tlsConfig, bridge.logIMAPClient, bridge.logIMAPServer)
imapServer, err := newIMAPServer(
bridge.vault.GetGluonDir(),
bridge.curVersion,
bridge.tlsConfig,
bridge.logIMAPClient,
bridge.logIMAPServer,
bridge.imapEventCh,
bridge.tasks,
)
if err != nil {
return fmt.Errorf("failed to create new IMAP server: %w", err)
}
@ -210,7 +219,7 @@ func (bridge *Bridge) SetAutoUpdate(autoUpdate bool) error {
return err
}
bridge.updateCheckCh <- struct{}{}
bridge.goUpdate()
return nil
}
@ -228,7 +237,7 @@ func (bridge *Bridge) SetUpdateChannel(channel updater.Channel) error {
return err
}
bridge.updateCheckCh <- struct{}{}
bridge.goUpdate()
return nil
}
@ -276,3 +285,16 @@ func (bridge *Bridge) FactoryReset(ctx context.Context) {
logrus.WithError(err).Error("Failed to clear data paths")
}
}
func getPort(addr net.Addr) int {
switch addr := addr.(type) {
case *net.TCPAddr:
return addr.Port
case *net.UDPAddr:
return addr.Port
default:
return 0
}
}