oficina / agent-access Public
Guard session freshness and content retention with runtime tests
1 file changed,
+81
−5
connection/server_test.go+81 −5
CI passed — run details
diff --git a/connection/server_test.go b/connection/server_test.go| index 3faaa1f..c56c19d 100644 |
| --- a/connection/server_test.go |
| +++ b/connection/server_test.go |
| @@ -1,6 +1,7 @@ |
| package connection |
| import ( |
| + "bytes" |
| "context" |
| "encoding/base64" |
| "encoding/json" |
| @@ -10,9 +11,11 @@ import ( |
| "net/http/cookiejar" |
| "net/http/httptest" |
| "net/url" |
| + "os" |
| "path/filepath" |
| "regexp" |
| "strings" |
| + "sync" |
| "sync/atomic" |
| "testing" |
| "time" |
| @@ -44,7 +47,8 @@ func (b bearerTransport) RoundTrip(r *http.Request) (*http.Response, error) { |
| func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| ctx := t.Context() |
| - logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| + logs := &lockedLog{} |
| + logger := slog.New(slog.NewTextHandler(logs, nil)) |
| key, err := family.Generate() |
| if err != nil { |
| t.Fatal(err) |
| @@ -52,6 +56,8 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| const subject = "pr_aaaaaaaaaaaaaaaaaaaaaaaaaa" |
| const home = "https://id.example.test" |
| const serviceOrigin = "https://connect.example.test" |
| + const inputCanary = "input_canary_910838cbe00b4f5db045" |
| + const outputCanary = "result_canary_c004907156a142f2a8f9" |
| var calls atomic.Int32 |
| var admitted atomic.Bool |
| admitted.Store(true) |
| @@ -59,13 +65,13 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| appLive.Store(true) |
| appRegistry, err := access.NewRegistry(access.Operation{Name: "notes_create", Description: "Create a note", Scope: "notes:write", Idempotent: true, |
| Input: json.RawMessage(`{"type":"object","required":["team","title"],"properties":{"team":{"type":"string"},"title":{"type":"string"}},"additionalProperties":false}`), |
| - Output: json.RawMessage(`{"type":"object","required":["id"],"properties":{"id":{"type":"integer"}}}`), |
| + Output: json.RawMessage(`{"type":"object","required":["id"],"properties":{"id":{"type":"integer"},"marker":{"type":"string"}}}`), |
| Invoke: func(_ context.Context, p access.Principal, _ json.RawMessage) (any, error) { |
| if p.Subject != subject || p.Client != client.ClientID || len(p.Teams) != 1 || p.Teams[0] != "acme" || len(p.Scopes) != 1 || p.Scopes[0] != "notes:write" { |
| t.Error("identity or attenuation changed") |
| } |
| calls.Add(1) |
| - return map[string]int64{"id": 9007199254740993}, nil |
| + return map[string]any{"id": int64(9007199254740993), "marker": outputCanary}, nil |
| }}) |
| if err != nil { |
| t.Fatal(err) |
| @@ -104,7 +110,8 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| service := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { serviceHandler.ServeHTTP(w, r) })) |
| defer service.Close() |
| transport := originTransport{service.Client().Transport, serviceOrigin, service.URL} |
| - d, err := db.Open(filepath.Join(t.TempDir(), "connect.db"), logger) |
| + databaseDir := t.TempDir() |
| + d, err := db.Open(filepath.Join(databaseDir, "connect.db"), logger) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -144,7 +151,9 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| } |
| return resp.Header.Get("Location") |
| } |
| + var savedAuthURL, savedProof string |
| browser := func(authURL string) error { |
| + savedAuthURL = authURL |
| start := redirect(get(authURL)) |
| toHome := redirect(get(start)) |
| u, err := url.Parse(toHome) |
| @@ -157,6 +166,7 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| if err != nil { |
| return err |
| } |
| + savedProof = proof |
| post, _ := http.NewRequest("POST", serviceOrigin+"/auth/assertion", strings.NewReader(url.Values{"assertion": {proof}}.Encode())) |
| post.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| post.Header.Set("Origin", home) |
| @@ -201,7 +211,7 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| if err != nil || len(info.Teams) != 1 || info.Teams[0] != "acme" { |
| t.Fatal("connection discovery", info, err) |
| } |
| - input := json.RawMessage(`{"team":"acme","title":"Agenda"}`) |
| + input := json.RawMessage(`{"team":"acme","title":"` + inputCanary + `"}`) |
| result, err := c.Call(ctx, "notes_create", input) |
| if err != nil { |
| t.Fatal(err) |
| @@ -250,6 +260,12 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| if _, err = remote.Verify(ctx, downstream); err != nil { |
| t.Fatal("live app credential", err) |
| } |
| + canaries := []string{inputCanary, outputCanary, serviceToken, parent, memory.value.Token.RefreshToken, downstream, savedProof} |
| + serviceURL, _ := url.Parse(serviceOrigin) |
| + for _, cookie := range c.HTTP.Jar.Cookies(serviceURL) { |
| + canaries = append(canaries, cookie.Value) |
| + } |
| + assertNoRuntimeRetention(t, databaseDir, logs, canaries) |
| appLive.Store(false) |
| if _, err = remote.Verify(ctx, downstream); err == nil { |
| t.Fatal("removed app retained downstream authority") |
| @@ -263,6 +279,23 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| t.Fatal("removed member reached app") |
| } |
| admitted.Store(true) |
| + // A session remains valid for an hour, but consent and connection |
| + // management must demand fresh Home authentication after five minutes. |
| + resp := get("/connections") |
| + resp.Body.Close() |
| + if resp.StatusCode != 200 { |
| + t.Fatal("fresh management", resp.StatusCode) |
| + } |
| + if _, err = d.Writer().Exec("UPDATE sessions SET auth_time=?", time.Now().Add(-6*time.Minute).UTC().Format(time.RFC3339)); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, path := range []string{"/connections", savedAuthURL} { |
| + toHome := redirect(get(redirect(get(path)))) |
| + u, err := url.Parse(toHome) |
| + if err != nil || !strings.HasPrefix(toHome, home+"/authorize?") || u.Query().Get("max_age") != "300" { |
| + t.Fatal("stale session bypassed fresh Home proof") |
| + } |
| + } |
| if err = c.Logout(ctx); err != nil { |
| t.Fatal(err) |
| } |
| @@ -294,3 +327,46 @@ func (o originTransport) RoundTrip(r *http.Request) (*http.Response, error) { |
| copy.Host = r.URL.Host |
| return o.base.RoundTrip(copy) |
| } |
| + |
| +type lockedLog struct { |
| + mu sync.Mutex |
| + data bytes.Buffer |
| +} |
| + |
| +func (l *lockedLog) Write(p []byte) (int, error) { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + return l.data.Write(p) |
| +} |
| +func (l *lockedLog) Bytes() []byte { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + return append([]byte(nil), l.data.Bytes()...) |
| +} |
| +func assertNoRuntimeRetention(t *testing.T, directory string, logs *lockedLog, canaries []string) { |
| + t.Helper() |
| + check := func(data []byte) { |
| + for _, canary := range canaries { |
| + if canary != "" && bytes.Contains(data, []byte(canary)) { |
| + t.Error("connection runtime retained raw content or credentials") |
| + } |
| + } |
| + } |
| + check(logs.Bytes()) |
| + if err := filepath.WalkDir(directory, func(path string, entry os.DirEntry, err error) error { |
| + if err != nil { |
| + return err |
| + } |
| + if entry.IsDir() { |
| + return nil |
| + } |
| + data, err := os.ReadFile(path) |
| + if err != nil { |
| + return err |
| + } |
| + check(data) |
| + return nil |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |