|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// newHelperAuthTestApp returns an app with empty device / app-password state so |
| 10 | +// each test can construct the exact binding scenario it needs without the seed |
| 11 | +// data created by newApp(). securityStateFile is cleared so no state is persisted |
| 12 | +// to disk during the test. |
| 13 | +func newHelperAuthTestApp() *app { |
| 14 | + a := newApp() |
| 15 | + a.devices = map[int]device{} |
| 16 | + a.appPasswords = map[string]appPassword{} |
| 17 | + a.nextDeviceID = 1 |
| 18 | + a.nextAppPasswordID = 1 |
| 19 | + a.securityStateFile = "" |
| 20 | + return a |
| 21 | +} |
| 22 | + |
| 23 | +// mintHelperAppPassword creates an app-password and returns its record ID along |
| 24 | +// with the compact form a helper would present for authentication. |
| 25 | +func mintHelperAppPassword(t *testing.T, a *app, now time.Time) (string, string) { |
| 26 | + t.Helper() |
| 27 | + record, plain := a.createAppPasswordLocked("test-helper", now) |
| 28 | + _, compact, ok := normalizeAppPasswordInput(plain) |
| 29 | + if !ok { |
| 30 | + t.Fatalf("failed to normalize generated app password %q", plain) |
| 31 | + } |
| 32 | + return record.ID, compact |
| 33 | +} |
| 34 | + |
| 35 | +// An app-password that is correctly bound to the requesting device must |
| 36 | +// authenticate cleanly (this is the regular happy-path upload). |
| 37 | +func TestAuthenticateHelperKeyCorrectlyBoundSucceeds(t *testing.T) { |
| 38 | + a := newHelperAuthTestApp() |
| 39 | + now := time.Now().UTC() |
| 40 | + a.devices[1] = device{ID: 1, DeviceType: "linux-x86", Fingerprint: "deck-1", LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 41 | + a.nextDeviceID = 2 |
| 42 | + |
| 43 | + keyID, compact := mintHelperAppPassword(t, a, now) |
| 44 | + rec := a.appPasswords[keyID] |
| 45 | + devID := 1 |
| 46 | + rec.BoundDeviceID = &devID |
| 47 | + rec.BoundDeviceType = "linux-x86" |
| 48 | + rec.BoundFingerprint = "deck-1" |
| 49 | + a.appPasswords[keyID] = rec |
| 50 | + d := a.devices[1] |
| 51 | + d.BoundAppPasswordID = &keyID |
| 52 | + a.devices[1] = d |
| 53 | + |
| 54 | + ctx, status, msg := a.authenticateHelperKey(compact, helperIdentity{DeviceType: "linux-x86", Fingerprint: "deck-1"}, helperMetadata{}) |
| 55 | + if status != 0 { |
| 56 | + t.Fatalf("expected success, got status %d (%s)", status, msg) |
| 57 | + } |
| 58 | + if ctx.AppPassword.BoundDeviceID == nil || *ctx.AppPassword.BoundDeviceID != 1 { |
| 59 | + t.Fatalf("expected app password bound to device 1, got %v", ctx.AppPassword.BoundDeviceID) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// The regression case: the record stores a stale device ID (the old device no |
| 64 | +// longer exists after a state reload) but the same physical fingerprint resolves |
| 65 | +// to a current device. The request must rebind rather than return 409. |
| 66 | +func TestAuthenticateHelperKeyRebindsStaleDeviceID(t *testing.T) { |
| 67 | + a := newHelperAuthTestApp() |
| 68 | + now := time.Now().UTC() |
| 69 | + // The live device is ID 7 (e.g. re-created with a new ID after a restart). |
| 70 | + a.devices[7] = device{ID: 7, DeviceType: "linux-x86", Fingerprint: "deck-1", LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 71 | + a.nextDeviceID = 8 |
| 72 | + |
| 73 | + keyID, compact := mintHelperAppPassword(t, a, now) |
| 74 | + rec := a.appPasswords[keyID] |
| 75 | + staleID := 99 // device 99 no longer exists |
| 76 | + rec.BoundDeviceID = &staleID |
| 77 | + rec.BoundDeviceType = "linux-x86" |
| 78 | + rec.BoundFingerprint = "deck-1" |
| 79 | + a.appPasswords[keyID] = rec |
| 80 | + |
| 81 | + ctx, status, msg := a.authenticateHelperKey(compact, helperIdentity{DeviceType: "linux-x86", Fingerprint: "deck-1"}, helperMetadata{}) |
| 82 | + if status != 0 { |
| 83 | + t.Fatalf("expected rebind success, got status %d (%s)", status, msg) |
| 84 | + } |
| 85 | + if got := a.appPasswords[keyID].BoundDeviceID; got == nil || *got != 7 { |
| 86 | + t.Fatalf("expected app password rebound to device 7, got %v", got) |
| 87 | + } |
| 88 | + if ctx.Device.ID != 7 { |
| 89 | + t.Fatalf("expected context device 7, got %d", ctx.Device.ID) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// A stale device ID that points to a device with a *different* fingerprint is a |
| 94 | +// genuinely different machine: the 409 guard must still fire. |
| 95 | +func TestAuthenticateHelperKeyRejectsDifferentDevice(t *testing.T) { |
| 96 | + a := newHelperAuthTestApp() |
| 97 | + now := time.Now().UTC() |
| 98 | + a.devices[1] = device{ID: 1, DeviceType: "linux-x86", Fingerprint: "deck-1", LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 99 | + a.devices[2] = device{ID: 2, DeviceType: "linux-x86", Fingerprint: "deck-2", LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 100 | + a.nextDeviceID = 3 |
| 101 | + |
| 102 | + keyID, compact := mintHelperAppPassword(t, a, now) |
| 103 | + rec := a.appPasswords[keyID] |
| 104 | + otherID := 2 // points at a device with a different fingerprint |
| 105 | + rec.BoundDeviceID = &otherID |
| 106 | + rec.BoundDeviceType = "linux-x86" |
| 107 | + rec.BoundFingerprint = "deck-1" |
| 108 | + a.appPasswords[keyID] = rec |
| 109 | + |
| 110 | + _, status, _ := a.authenticateHelperKey(compact, helperIdentity{DeviceType: "linux-x86", Fingerprint: "deck-1"}, helperMetadata{}) |
| 111 | + if status != http.StatusConflict { |
| 112 | + t.Fatalf("expected 409 conflict for a genuinely different device, got %d", status) |
| 113 | + } |
| 114 | + if got := a.appPasswords[keyID].BoundDeviceID; got == nil || *got != 2 { |
| 115 | + t.Fatalf("expected binding to remain on device 2 after rejection, got %v", got) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// Rebinding a device to a new app-password must delete the password it displaces |
| 120 | +// (now unbound and unusable) rather than leaving it to accumulate as an orphan. |
| 121 | +func TestBindAppPasswordDeletesDisplacedOrphan(t *testing.T) { |
| 122 | + a := newHelperAuthTestApp() |
| 123 | + now := time.Now().UTC() |
| 124 | + a.devices[1] = device{ID: 1, DeviceType: "linux-x86", Fingerprint: "deck-1", LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 125 | + a.nextDeviceID = 2 |
| 126 | + |
| 127 | + old, _ := a.createAppPasswordLocked("old", now) |
| 128 | + a.bindAppPasswordToDeviceLocked(old.ID, a.devices[1]) |
| 129 | + fresh, _ := a.createAppPasswordLocked("fresh", now) |
| 130 | + |
| 131 | + a.bindAppPasswordToDeviceLocked(fresh.ID, a.devices[1]) |
| 132 | + |
| 133 | + if _, ok := a.appPasswords[old.ID]; ok { |
| 134 | + t.Fatalf("expected displaced orphan %s to be deleted", old.ID) |
| 135 | + } |
| 136 | + if got := a.appPasswords[fresh.ID].BoundDeviceID; got == nil || *got != 1 { |
| 137 | + t.Fatalf("expected fresh password bound to device 1, got %v", got) |
| 138 | + } |
| 139 | + if got := a.devices[1].BoundAppPasswordID; got == nil || *got != fresh.ID { |
| 140 | + t.Fatalf("expected device 1 to reference fresh password, got %v", got) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// A displaced password that is still referenced by another device must NOT be |
| 145 | +// deleted by the orphan sweep. |
| 146 | +func TestBindAppPasswordKeepsPasswordReferencedElsewhere(t *testing.T) { |
| 147 | + a := newHelperAuthTestApp() |
| 148 | + now := time.Now().UTC() |
| 149 | + shared, _ := a.createAppPasswordLocked("shared", now) |
| 150 | + sharedID := shared.ID |
| 151 | + a.devices[1] = device{ID: 1, DeviceType: "linux-x86", Fingerprint: "deck-1", BoundAppPasswordID: &sharedID, LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 152 | + a.devices[2] = device{ID: 2, DeviceType: "linux-x86", Fingerprint: "deck-2", BoundAppPasswordID: &sharedID, LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 153 | + a.nextDeviceID = 3 |
| 154 | + |
| 155 | + fresh, _ := a.createAppPasswordLocked("fresh", now) |
| 156 | + a.bindAppPasswordToDeviceLocked(fresh.ID, a.devices[1]) |
| 157 | + |
| 158 | + if _, ok := a.appPasswords[sharedID]; !ok { |
| 159 | + t.Fatalf("password still referenced by device 2 must not be deleted") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// A helper may present a different device_type while keeping a stable fingerprint |
| 164 | +// (e.g. the SGM Steam Deck helper enrolls as "steamdeck" but uploads saves tagged |
| 165 | +// with the source emulator type "retroarch"). The fingerprint is the device |
| 166 | +// identity, so this must authenticate and stay bound to the same device. |
| 167 | +func TestAuthenticateHelperKeyDifferentDeviceTypeSameFingerprintSucceeds(t *testing.T) { |
| 168 | + a := newHelperAuthTestApp() |
| 169 | + now := time.Now().UTC() |
| 170 | + a.devices[1] = device{ID: 1, DeviceType: "steamdeck", Fingerprint: "deck-1", LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 171 | + a.nextDeviceID = 2 |
| 172 | + |
| 173 | + keyID, compact := mintHelperAppPassword(t, a, now) |
| 174 | + rec := a.appPasswords[keyID] |
| 175 | + devID := 1 |
| 176 | + rec.BoundDeviceID = &devID |
| 177 | + rec.BoundDeviceType = "steamdeck" |
| 178 | + rec.BoundFingerprint = "deck-1" |
| 179 | + a.appPasswords[keyID] = rec |
| 180 | + d := a.devices[1] |
| 181 | + d.BoundAppPasswordID = &keyID |
| 182 | + a.devices[1] = d |
| 183 | + |
| 184 | + ctx, status, msg := a.authenticateHelperKey(compact, helperIdentity{DeviceType: "retroarch", Fingerprint: "deck-1"}, helperMetadata{}) |
| 185 | + if status != 0 { |
| 186 | + t.Fatalf("expected success on same-fingerprint different-device_type, got %d (%s)", status, msg) |
| 187 | + } |
| 188 | + if ctx.AppPassword.BoundDeviceID == nil || *ctx.AppPassword.BoundDeviceID != 1 { |
| 189 | + t.Fatalf("expected app password to stay bound to device 1, got %v", ctx.AppPassword.BoundDeviceID) |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +// A genuinely different fingerprint is a different physical device and must still |
| 194 | +// be rejected — an app-password is bound to one device. |
| 195 | +func TestAuthenticateHelperKeyDifferentFingerprintRejected(t *testing.T) { |
| 196 | + a := newHelperAuthTestApp() |
| 197 | + now := time.Now().UTC() |
| 198 | + a.devices[1] = device{ID: 1, DeviceType: "steamdeck", Fingerprint: "deck-1", LastSeenAt: now, SyncAll: true, CreatedAt: now} |
| 199 | + a.nextDeviceID = 2 |
| 200 | + |
| 201 | + keyID, compact := mintHelperAppPassword(t, a, now) |
| 202 | + rec := a.appPasswords[keyID] |
| 203 | + devID := 1 |
| 204 | + rec.BoundDeviceID = &devID |
| 205 | + rec.BoundDeviceType = "steamdeck" |
| 206 | + rec.BoundFingerprint = "deck-1" |
| 207 | + a.appPasswords[keyID] = rec |
| 208 | + |
| 209 | + if _, status, _ := a.authenticateHelperKey(compact, helperIdentity{DeviceType: "steamdeck", Fingerprint: "other-deck"}, helperMetadata{}); status != http.StatusConflict { |
| 210 | + t.Fatalf("expected 409 on different fingerprint, got %d", status) |
| 211 | + } |
| 212 | +} |
0 commit comments