Files
magpie/internal/usage/client_ip_test.go
T
yetone 68865032c3 usage: a gateway row keeps the address its request came from, on this computer only
geesehowardt7 on X asked to see a request's source IP. No usage row
kept it.

Record.ClientIP (json client_ip) is set in appendUsage from peerAddr:
the connection's own address, never a forwarding header (the gateway
trusts none), so behind a reverse proxy it is the proxy's. Loopback is
127.0.0.1/::1. Rows kept before this, reported calls and magpie's own
calls have none. rowText packs it, or the ledger cache gave it back
empty (the cache's file is keyed on this package's source, so it is
rebuilt once).

It stays here:
- sync's OwnDays blanks it, as it does Archive and Computer;
- GET /v1/magpie/usage/requests (other computers, Remote magpie, key
  holders) takes it out for every caller (withoutClientIPs);
- the CSV export, OTel and middleware points name their own columns
  and don't carry it; telemetry sends ids only.
The window's /api/usage/requests keeps it for the Usage page.

Run (mote, at base e3f1efdaa):
- go test -count=1 -timeout 45m ./internal/usage ./internal/usageapi: ok
- go test -run 'TestUsageRowsKeepTheClientsAddress|TestUsageOverGateway'
  ./internal/gateway: ok
- go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet on
  gateway and usage: ok; gofmt -l clean.
Broken by hand, each restored:
- no ClientIP in appendUsage: TestUsageRowsKeepTheClientsAddress red
  (addresses kept: map[:true]).
- no withoutClientIPs: the same test red (the API told 127.0.0.1:5000
  the address).
- OwnDays not blanking it: TestClientIPKeptHereNotShared red.
- before rowText packed it: TestClientIPKeptHereNotShared red (empty
  from the ledger).
The full suite runs with the GUI commit that shows it.
2026-11-10 04:44:07 +08:00

36 lines
998 B
Go

package usage
import (
"encoding/json"
"strings"
"testing"
"time"
)
// The address a call came from (geesehowardt7 on X) is kept with the call
// and read back on the Usage page, and stays on this computer: the day
// sync shares with the others leaves it out.
func TestClientIPKeptHereNotShared(t *testing.T) {
reportedHome(t)
now := time.Now().Add(-time.Hour)
Append(Record{Time: now, Agent: "claude", Provider: "anthropic", Model: "claude-opus-5-5", Input: 1000, Output: 100, Status: 200, ClientIP: "192.168.1.9"})
page := QueryPage(All, Filter{}, 0, 50)
if len(page.Rows) != 1 || page.Rows[0].ClientIP != "192.168.1.9" {
t.Fatalf("the ledger's row: %+v", page.Rows)
}
n := 0
for _, calls := range OwnDays(now.Add(-time.Hour)) {
for _, c := range calls {
n++
b, _ := json.Marshal(c)
if c.ClientIP != "" || strings.Contains(string(b), "192.168.1.9") {
t.Fatalf("a shared call has the address: %s", b)
}
}
}
if n != 1 {
t.Fatalf("%d shared calls", n)
}
}