Files
magpie/internal/agent/zed_credential_windows.go
T
yetone bc320e249f agent: T3 Code, Air, OpenChamber and the rest send the LAN key to a gateway pinned beyond loopback (#1647, ggbdpq)
The gateway takes gateway.Token, and the keys that name an agent
(gateway.TokenFor), only from loopback. When MAGPIE_ADDR pins the
gateway to an address beyond loopback and sharing is on, keyAt gives the
LAN sharing key, and most writers use it. T3 Code's instance still wrote
gateway.Token, so every call it made got 401. Air's and OpenChamber's
Checks compared against gateway.Token, so they said the wiring Sync had
just written was off.

The same mistake was in every writer that pairs gateway.URL() or
gatewayV1() with a fixed token:
- agy, Alma, Cindy, Claude Desktop, Copilot for JetBrains, Cursor's
  local agent, OpenHanako, Pencil, VS Code / Insiders / VSCodium,
  WorkBuddy-family models.json, ZCode (both files), Zed's credential,
  Aside's provider, and the legacy Claude token rename.
- Fix: these now take agentKey(agent) or hereKey(), which are keyAt and
  agentKeyAt for gateway.URL(). On loopback they are unchanged.
- Alma re-keys a provider that holds any of magpie's keys other than the
  one it should have now.
- WorkBuddy and Aside also count the new key as theirs.
- ZCode treats a saved address equal to the pinned gateway as this
  machine's, not another magpie's.

What was run:
- New tests: TestT3CodeKeyBeyondLoopback and
  TestAirAndOpenChamberChecksTakeTheKeyForTheGateway (from the
  reporter's patch), and TestPinnedGatewayKeyForEveryWriter, which covers
  the other writers.
- With the product files reverted, all three are red. The T3 token reads
  "magpie", and each of the 11 writers shows its old key. They are green
  with the fix.
- go test -tags nogui ./internal/agent/ is green.
- gofmt -l and go vet are clean on darwin, linux and windows.

Not tried with a real T3 Code, Air, OpenChamber or a real pinned
MAGPIE_ADDR on a second machine.
2026-10-12 00:44:47 +08:00

48 lines
1.2 KiB
Go

package agent
import (
"fmt"
"runtime"
"unsafe"
"golang.org/x/sys/windows"
)
// zedWindowsCredential matches CREDENTIALW. Zed's blob is UTF-8, not UTF-16.
type zedWindowsCredential struct {
Flags uint32
Type uint32
TargetName *uint16
Comment *uint16
LastWritten windows.Filetime
CredentialBlobSize uint32
CredentialBlob *byte
Persist uint32
AttributeCount uint32
Attributes uintptr
TargetAlias *uint16
UserName *uint16
}
func saveZedCredential(url string) error {
target, err := windows.UTF16PtrFromString("zed:url=" + url)
if err != nil {
return err
}
user, _ := windows.UTF16PtrFromString("Bearer")
key := []byte(agentKey("zed"))
credential := zedWindowsCredential{
Type: 1, TargetName: target, UserName: user, // CRED_TYPE_GENERIC
CredentialBlobSize: uint32(len(key)), CredentialBlob: &key[0],
Persist: 2, // CRED_PERSIST_LOCAL_MACHINE
}
write := windows.NewLazySystemDLL("advapi32.dll").NewProc("CredWriteW")
ok, _, callErr := write.Call(uintptr(unsafe.Pointer(&credential)), 0)
runtime.KeepAlive(credential)
runtime.KeepAlive(key)
if ok == 0 {
return fmt.Errorf("CredWriteW: %w", callErr)
}
return nil
}