Skip to content

Commit e5bae1e

Browse files
authored
fix(login): serialize login attempts and safely handle late 2FA replies (#212)
1 parent d4aee3a commit e5bae1e

2 files changed

Lines changed: 128 additions & 11 deletions

File tree

‎internal/manager/login_manager.go‎

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"io"
87
"os"
98
"strings"
9+
"sync"
1010
"time"
1111

1212
"github.com/bitxeno/atvloadly/internal/app"
@@ -16,9 +16,14 @@ import (
1616
)
1717

1818
type LoginManager struct {
19+
// Serializes attempts on this WebSocket to protect the shared output buffer.
20+
runMu sync.Mutex
21+
// Protects the currently active login subprocess and its input pipe.
22+
stateMu sync.Mutex
23+
1924
outputStdout *outputWriter
2025

21-
stdin io.WriteCloser
26+
stdin *os.File
2227

2328
cancel context.CancelFunc
2429
em *event.Manager
@@ -34,26 +39,43 @@ func NewLoginManager() *LoginManager {
3439
}
3540

3641
func (t *LoginManager) Start(ctx context.Context, account, password string) error {
42+
if !t.runMu.TryLock() {
43+
return errors.New("login already in progress")
44+
}
45+
defer t.runMu.Unlock()
3746
t.outputStdout.Reset()
3847

3948
// set execute timeout 10 minutes
4049
timeout := 10 * time.Minute
4150
ctx, cancel := context.WithTimeout(ctx, timeout)
42-
if t.cancel != nil {
43-
t.cancel()
44-
}
51+
t.stateMu.Lock()
4552
t.cancel = cancel
53+
t.stateMu.Unlock()
54+
defer func() {
55+
t.stateMu.Lock()
56+
t.cancel = nil
57+
t.stateMu.Unlock()
58+
cancel()
59+
}()
4660

4761
stdinReader, stdinWriter, err := os.Pipe()
4862
if err != nil {
4963
log.Err(err).Msg("Error creating stdin pipe: ")
5064
return err
5165
}
66+
t.stateMu.Lock()
5267
t.stdin = stdinWriter
68+
t.stateMu.Unlock()
5369
defer func() {
70+
// Remove the active reference before closing the specific pipe for
71+
// this attempt. In-flight os.File writes return an error on close.
72+
t.stateMu.Lock()
73+
if t.stdin == stdinWriter {
74+
t.stdin = nil
75+
}
76+
t.stateMu.Unlock()
77+
_ = stdinWriter.Close()
5478
_ = stdinReader.Close()
55-
_ = t.stdin.Close()
56-
t.stdin = nil
5779
}()
5880

5981
err = execx.CommandContext(ctx, "plumesign", "account", "login", "-u", account, "-p", password).
@@ -76,9 +98,12 @@ func (t *LoginManager) Start(ctx context.Context, account, password string) erro
7698
}
7799

78100
func (t *LoginManager) Close() {
79-
if t.cancel != nil {
80-
t.cancel()
81-
t.cancel = nil
101+
t.stateMu.Lock()
102+
cancel := t.cancel
103+
t.cancel = nil
104+
t.stateMu.Unlock()
105+
if cancel != nil {
106+
cancel()
82107
}
83108
if t.em != nil {
84109
_ = t.em.CloseWait()
@@ -93,7 +118,14 @@ func (t *LoginManager) OnOutput(fn func(string)) {
93118
}
94119

95120
func (t *LoginManager) Write(p []byte) {
96-
_, _ = t.stdin.Write(p)
121+
t.stateMu.Lock()
122+
writer := t.stdin
123+
t.stateMu.Unlock()
124+
// A late 2FA reply is harmless after the subprocess exits. Never keep
125+
// stateMu across Write: an OS pipe can block until its reader exits.
126+
if writer != nil {
127+
_, _ = writer.Write(p)
128+
}
97129
}
98130

99131
func (t *LoginManager) ErrorLog() string {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package manager
2+
3+
import (
4+
"context"
5+
"io"
6+
"os"
7+
"strings"
8+
"sync"
9+
"testing"
10+
)
11+
12+
func TestLoginWriteWithoutActiveProcess(t *testing.T) {
13+
mgr := &LoginManager{}
14+
mgr.Write([]byte("123456\n"))
15+
}
16+
17+
func TestLoginWriteToActivePipe(t *testing.T) {
18+
reader, writer, err := os.Pipe()
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
defer func() { _ = reader.Close() }()
23+
defer func() { _ = writer.Close() }()
24+
25+
mgr := &LoginManager{stdin: writer}
26+
mgr.Write([]byte("123456\n"))
27+
buf := make([]byte, len("123456\n"))
28+
if _, err := io.ReadFull(reader, buf); err != nil {
29+
t.Fatal(err)
30+
}
31+
if string(buf) != "123456\n" {
32+
t.Fatalf("wrong input: %q", buf)
33+
}
34+
}
35+
36+
func TestLoginWriteConcurrentShutdown(t *testing.T) {
37+
reader, writer, err := os.Pipe()
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
defer func() { _ = reader.Close() }()
42+
mgr := &LoginManager{stdin: writer}
43+
44+
var wg sync.WaitGroup
45+
for n := 0; n < 8; n++ {
46+
wg.Add(1)
47+
go func() {
48+
defer wg.Done()
49+
for i := 0; i < 100; i++ {
50+
mgr.Write([]byte("x"))
51+
}
52+
}()
53+
}
54+
wg.Add(1)
55+
go func() {
56+
defer wg.Done()
57+
mgr.stateMu.Lock()
58+
mgr.stdin = nil
59+
mgr.stateMu.Unlock()
60+
_ = writer.Close()
61+
}()
62+
wg.Wait()
63+
mgr.Write([]byte("late 2FA"))
64+
}
65+
66+
func TestLoginConcurrentStartIsRejected(t *testing.T) {
67+
mgr := &LoginManager{}
68+
mgr.runMu.Lock()
69+
defer mgr.runMu.Unlock()
70+
err := mgr.Start(context.Background(), "user", "password")
71+
if err == nil || !strings.Contains(err.Error(), "already in progress") {
72+
t.Fatalf("expected busy error, got %v", err)
73+
}
74+
}
75+
76+
func TestLoginCloseCancelsActiveAttempt(t *testing.T) {
77+
ctx, cancel := context.WithCancel(context.Background())
78+
defer cancel()
79+
mgr := &LoginManager{cancel: cancel}
80+
mgr.Close()
81+
if ctx.Err() != context.Canceled {
82+
t.Fatalf("Close did not cancel active attempt: %v", ctx.Err())
83+
}
84+
mgr.Close()
85+
}

0 commit comments

Comments
 (0)
⌂ 换网址