diff --git a/History/2026-10-20.md b/History/2026-10-20.md index d1d3e1ec..245c76f5 100644 --- a/History/2026-10-20.md +++ b/History/2026-10-20.md @@ -39,3 +39,11 @@ Add an injected RSA signing capability and clock for the alpha controller, with ## 22:09 - Bind JWT verification to original bytes Reproduce both digest-mutation failures from PR219 review, then recompute SHA-256 from the original compact-JWS bytes after signing. A signature over a mutated digest is refused; a valid signature survives signer-side buffer clearing. Both cases retain one signing call with no retry. + +## 22:33 - Persist uncertain installation acquisition + +Add durable nonsecret token-mint intent, same-role cross-process ownership and explicit stopped-owner recovery. Actual local TLS tests observe committed intent before a token POST; killed-child restart tests prove both interrupted stages stay latched without HTTP replay. Faults cover pre/post-mint persistence, scope, proof, retention and malformed metadata. A case-alias regression failed before canonical field validation and now passes. The generated RSA JWT composes through both journaled role handshakes. Role generation handover and runtime composition remain separate; no live credentials or activation. + +## 22:52 - Bind journal retention to stored identity + +A self-review fault test reproduced a malformed terminal JSON record pointing at another unresolved attempt; retention deleted that evidence and made seven HTTP calls. Retention now validates every record against its SQLite role and attempt columns before any deletion or HTTP, preserving unresolved evidence on mismatch. diff --git a/reviewer/INSTALLATION-JOURNAL.md b/reviewer/INSTALLATION-JOURNAL.md new file mode 100644 index 00000000..34271cd5 --- /dev/null +++ b/reviewer/INSTALLATION-JOURNAL.md @@ -0,0 +1,101 @@ +# Durable installation acquisition + +`GitHubInstallationJournal` records explicit acquisition attempts for the +selected-installation credentials in [GITHUB-INSTALLATION.md](GITHUB-INSTALLATION.md). +It is source work for [issue #176](https://github.com/hemsoft-dev/set-it-free-loop/issues/176). +It uses synthetic keys, fake GitHub credentials and local TLS in qualification. +No actual key, account/profile, App, installation or activation is used. + +## Boundary and states + +The constructor takes an existing private SQLite `Store` and an explicit clock. +It creates a separate metadata table and private reader/publisher lock directories. +It performs no HTTP or background work. The existing service owner lock and +provider auth stores are separate. Each role holds a private cross-process flock +for its complete acquisition or recovery operation, without holding a SQL +transaction during HTTP or trusted recovery callbacks. + +`Acquire` binds the store's exact enrollment and the requested reader/publisher +role. It creates a fresh attempt before HTTP and commits `mint_pending` before +the non-replayable token POST. The underlying handshake retains all identity, +selected-repository, permission, destination, expiry and response checks. The +journal clock is authoritative for both its record and that handshake. + +| State | Meaning and next permitted action | +| --- | --- | +| `pre_mint` | Acquisition started. An interrupted attempt stays latched until explicit stopped-owner recovery. | +| `pre_mint_failed` | This invocation proved no token POST was dispatched and recorded the failure. A new explicit acquisition is allowed. | +| `mint_pending` | Durable intent precedes dispatch. A crash or failed later persistence leaves an uncertain attempt. | +| `unknown` | A token request or subsequent qualification failed after dispatch became possible. No automatic replay or fresh acquisition is allowed. | +| `qualified` | The complete handshake and nonsecret receipt were persisted. The caller receives the opaque role transport. | +| `retry_authorized` | A separate trusted stopped-work check and matching recovery evidence authorize a new explicit attempt. This operation sends no HTTP. | + +Qualified metadata never recreates a transport or proves readiness after restart. +An explicitly requested later acquisition uses a new attempt ID. This library +never renews credentials from a polling tick, a 401 response or constructor. +The original low-level `AcquireGitHubInstallation` remains an explicit, +unpersisted boundary; a service must use its durable lifecycle composition. + +If qualification persistence fails, the new transport is closed and withheld. +The journal attempts to retain uncertainty using a bounded cleanup context even +if the caller cancelled. If that write also fails, the preceding durable latch +remains. A crash before or after a token POST cannot erase the earlier intent. + +## Explicit recovery and retention + +`AuthorizeRetry` requires the exact attempt, immutable owner ID, a nonsecret +hash of retained evidence, its time, and a trusted callback that verifies stopped +prior work. It acquires the same role lock before checking the evidence and fences +the exact persisted attempt before authorization. It cannot race a still-running +acquisition. The callback must be bounded and must not mint credentials or launch +provider work. A model assertion or missing local transport is not stopped-work +evidence. The callback's error text is never persisted or returned. + +The prior token's remote creation can remain unknown even after its local owner +stops. An unknown token cannot be revoked without its lost value. Recovery records +that uncertainty and authorizes a separate qualification; it does not claim remote +revocation, retrieve the lost token or retry the old POST. + +Records contain only enrollment, role, random attempt ID, stage, times, optional +qualification metadata and recovery evidence. No JWT, token, key, remote error +body or provider charge is stored. Formatting the journal handle is redacted. +The JSON schema requires canonical field names and rejects duplicates, unknowns, +missing required fields, invalid identities, trailing data and oversized records. +Optional receipt/recovery objects follow their own bounded schemas. Records are +at most 4 KiB; reads enforce the same bound before materializing a stored value. + +At most 1,024 records are retained. A new attempt prunes terminal history older +than seven days, keeping each role's latest record and every unresolved attempt. +Retention validates every record against its stored row role and attempt ID +before selecting deletions. A full or malformed journal refuses new HTTP work. These records live in a +separate table, so ordinary legacy `Store` state writes cannot erase them. + +## Offline evidence and remaining integration + +Tests independently verify a generated RSA App JWT and send it through both +actual local TLS role handshakes, observing committed mint intent at the fake +server before its token response. They verify that acquisition changes no review +state or provider budget and that JWT/token bytes do not enter SQLite records, +database/WAL bytes or handle formatting. + +Fault tests cover lost responses, rejected post-mint identity, failed intent and +completion writes, a failed uncertainty write, fresh `Store` reopening, missing +or mismatched recovery proof, blocked recovery while a real owner is live, +process locks, bounded history, hostile JSON and namespace symlinks. Real child +processes are killed before a token POST and after fake-server acceptance while +the response is withheld. Both restart paths make zero HTTP calls until explicit +stopped-owner recovery. Every child is reaped on success or failure. + +```sh +go -C reviewer test -race -count=1 -run 'TestGitHubInstallationJournal|TestGitHubAppJWT|TestGitHubInstallation' . +go -C reviewer vet ./... +go -C reviewer test -race -count=1 ./... +``` + +Atomic reader/publisher generation handover, admitted-request cancellation/drain, +role refresh ownership, qualified object acquisition and packaged runtime +composition still need integration. This journal is not a live qualification, +credential import, remote revocation or activation grant. Both +[installation authentication](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token) +and [token revocation](https://docs.github.com/en/rest/apps/installations#revoke-an-installation-access-token) +remain distinct operations. diff --git a/reviewer/github_installation.go b/reviewer/github_installation.go index 9a47cefa..dc72a19e 100644 --- a/reviewer/github_installation.go +++ b/reviewer/github_installation.go @@ -183,8 +183,14 @@ func (r githubInstallationRepo) valid() bool { // and one role's minimal permissions. It never retries POST or follows redirects. // Caller cancellation/deadlines are honored; the complete handshake is bounded. func AcquireGitHubInstallation(ctx context.Context, o GitHubInstallationOptions) (*GitHubInstallationTransport, error) { + return acquireGitHubInstallation(ctx, o, nil) +} + +// beforeMint must durably record intent before the non-replayable token POST. +// A failed callback prevents dispatch; the helper never retries it. +func acquireGitHubInstallation(ctx context.Context, o GitHubInstallationOptions, beforeMint func(context.Context) error) (*GitHubInstallationTransport, error) { want := githubPermissions(o.Role) - if o.Enrollment.Validate() != nil || want == nil || !githubCredential(o.AppJWT) || o.Transport == nil || httptrace.ContextClientTrace(ctx) != nil { + if ctx == nil || o.Enrollment.Validate() != nil || want == nil || !githubCredential(o.AppJWT) || o.Transport == nil || httptrace.ContextClientTrace(ctx) != nil { return nil, ErrGitHubInstallation } if o.Now == nil { @@ -216,6 +222,9 @@ func AcquireGitHubInstallation(ctx context.Context, o GitHubInstallationOptions) endpoint := "/app/installations/" + strconv.FormatInt(o.Enrollment.InstallationID, 10) body := map[string]any{"repository_ids": []int64{RepositoryID}, "permissions": want} started := o.Now() + if beforeMint != nil && beforeMint(ctx) != nil { + return nil, ErrGitHubInstallation + } if installationRequest(ctx, client, jwt, "POST", endpoint+"/access_tokens", body, 201, &minted) != nil { return nil, ErrGitHubInstallation } diff --git a/reviewer/github_installation_journal_unix.go b/reviewer/github_installation_journal_unix.go new file mode 100644 index 00000000..8890ddac --- /dev/null +++ b/reviewer/github_installation_journal_unix.go @@ -0,0 +1,447 @@ +//go:build linux || darwin + +package reviewer + +import ( + "bytes" + "context" + "crypto/rand" + "database/sql" + "encoding/hex" + "encoding/json" + "errors" + "io" + "os" + "path/filepath" + "time" +) + +var ErrGitHubInstallationRecovery = errors.New("durable installation acquisition requires explicit recovery") + +const installationAttemptCap = 1024 + +// GitHubInstallationRecovery is nonsecret evidence supplied by the trusted +// operator layer. Its callback must establish stopped prior work; an assertion +// from a model or the mere absence of a local transport is not that proof. +type GitHubInstallationRecovery struct { + AttemptID string + OwnerID int64 + At time.Time + EvidenceHash string +} + +// GitHubInstallationAttempt records no JWT, token, key or remote error text. +// Receipt is qualification metadata only; it cannot recreate a transport. +type GitHubInstallationAttempt struct { + Version int + ID string + Enrollment Enrollment + Role GitHubCredentialRole + State string + StartedAt, UpdatedAt time.Time + MintAttempted bool + Receipt *GitHubInstallationReceipt + Recovery *GitHubInstallationRecovery +} + +// Durable metadata has optional receipt/recovery pointers, unlike model-output +// schemas that reject every null. Keep bounded, duplicate/unknown/trailing-key +// rejection and explicitly require every nonoptional field before semantics. +func installationJournalObject(raw []byte, required, optional []string) (map[string]json.RawMessage, error) { + var fields map[string]json.RawMessage + if json.Unmarshal(raw, &fields) != nil { + return nil, ErrGitHubInstallationRecovery + } + allowed := map[string]bool{} + for _, name := range required { + allowed[name] = true + value, ok := fields[name] + if !ok || bytes.Equal(bytes.TrimSpace(value), []byte("null")) { + return nil, ErrGitHubInstallationRecovery + } + } + for _, name := range optional { + allowed[name] = true + } + for name := range fields { + if !allowed[name] { + return nil, ErrGitHubInstallationRecovery + } + } + return fields, nil +} + +func decodeGitHubInstallationAttempt(raw []byte) (GitHubInstallationAttempt, error) { + var out GitHubInstallationAttempt + if len(raw) == 0 || len(raw) > 4096 || uniqueJSONKeys(json.NewDecoder(bytes.NewReader(raw)), 0) != nil { + return out, ErrGitHubInstallationRecovery + } + fields, err := installationJournalObject(raw, []string{"Version", "ID", "Enrollment", "Role", "State", "StartedAt", "UpdatedAt", "MintAttempted"}, []string{"Receipt", "Recovery"}) + if err != nil { + return out, err + } + enrollmentFields := []string{"RepositoryID", "InstallationID", "AppID", "BotID", "OwnerID"} + if _, err := installationJournalObject(fields["Enrollment"], enrollmentFields, nil); err != nil { + return out, err + } + for _, name := range []string{"Receipt", "Recovery"} { + value := fields[name] + if len(value) == 0 || bytes.Equal(bytes.TrimSpace(value), []byte("null")) { + continue + } + if name == "Receipt" { + receipt, err := installationJournalObject(value, []string{"Enrollment", "Role", "CheckedAt", "ExpiresAt"}, nil) + if err != nil { + return out, err + } + if _, err := installationJournalObject(receipt["Enrollment"], enrollmentFields, nil); err != nil { + return out, err + } + } else if _, err := installationJournalObject(value, []string{"AttemptID", "OwnerID", "At", "EvidenceHash"}, nil); err != nil { + return out, err + } + } + decoder := json.NewDecoder(bytes.NewReader(raw)) + decoder.DisallowUnknownFields() + if decoder.Decode(&out) != nil || decoder.Decode(new(any)) != io.EOF { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + return out, nil +} + +func (a GitHubInstallationAttempt) valid(e Enrollment, role GitHubCredentialRole) bool { + nonce, err := hex.DecodeString(a.ID) + if err != nil || len(nonce) != 16 || a.Version != 1 || a.Enrollment != e || a.Role != role || githubPermissions(role) == nil || a.StartedAt.IsZero() || a.UpdatedAt.Before(a.StartedAt) { + return false + } + switch a.State { + case "pre_mint", "pre_mint_failed": + if a.MintAttempted { + return false + } + case "mint_pending", "unknown", "qualified": + if !a.MintAttempted { + return false + } + case "retry_authorized": + if a.Recovery == nil || a.Recovery.AttemptID != a.ID || a.Recovery.OwnerID != OwnerID || a.Recovery.At.Before(a.StartedAt) || a.Recovery.At.After(a.UpdatedAt) || !validAuthHash(a.Recovery.EvidenceHash) { + return false + } + default: + return false + } + if a.State != "retry_authorized" && a.Recovery != nil { + return false + } + if !a.MintAttempted && a.Receipt != nil { + return false + } + if a.State == "qualified" && a.Receipt == nil { + return false + } + if a.Receipt != nil && (a.Receipt.Enrollment != e || a.Receipt.Role != role || a.Receipt.CheckedAt.Before(a.StartedAt) || a.Receipt.CheckedAt.After(a.UpdatedAt) || !a.Receipt.ExpiresAt.After(a.Receipt.CheckedAt)) { + return false + } + return true +} + +// GitHubInstallationJournal serializes each role's complete handshake across +// processes and keeps a SQLite write-ahead latch. The caller owns Store. No +// acquisition or background renewal occurs during construction or reopening. +type GitHubInstallationJournal struct { + store *Store + enrollment Enrollment + directory string + now func() time.Time +} + +func (GitHubInstallationJournal) String() string { return "GitHubInstallationJournal{redacted}" } +func (GitHubInstallationJournal) GoString() string { return "GitHubInstallationJournal{redacted}" } +func (GitHubInstallationJournal) MarshalJSON() ([]byte, error) { + return []byte(`"GitHubInstallationJournal{redacted}"`), nil +} + +func NewGitHubInstallationJournal(ctx context.Context, store *Store, now func() time.Time) (*GitHubInstallationJournal, error) { + if ctx == nil || store == nil || store.db == nil || now == nil { + return nil, ErrGitHubInstallationRecovery + } + state, err := store.Snapshot(ctx) + if err != nil || state.Enrollment.Validate() != nil { + return nil, ErrGitHubInstallationRecovery + } + directory := filepath.Join(filepath.Dir(store.path), "github-installation-attempts") + for _, dir := range []string{directory, filepath.Join(directory, string(GitHubReader)), filepath.Join(directory, string(GitHubPublisher))} { + if err := os.Mkdir(dir, 0700); err != nil && !os.IsExist(err) { + return nil, ErrGitHubInstallationRecovery + } + resolved, err := filepath.EvalSymlinks(dir) + if err != nil || resolved != dir || privateDirectory(dir) != nil { + return nil, ErrGitHubInstallationRecovery + } + } + if _, err = store.db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS github_installation_attempts (sequence INTEGER PRIMARY KEY AUTOINCREMENT, role TEXT NOT NULL CHECK(role IN ('reader','publisher')), attempt TEXT NOT NULL UNIQUE, record BLOB NOT NULL CHECK(length(record)<=4096))`); err != nil { + return nil, ErrGitHubInstallationRecovery + } + return &GitHubInstallationJournal{store, state.Enrollment, directory, now}, nil +} + +func (j *GitHubInstallationJournal) valid() bool { + return j != nil && j.store != nil && j.store.db != nil && j.enrollment.Validate() == nil && j.now != nil +} +func (j *GitHubInstallationJournal) lock(ctx context.Context, role GitHubCredentialRole) (*authLock, error) { + if !j.valid() || ctx == nil || githubPermissions(role) == nil { + return nil, ErrGitHubInstallationRecovery + } + // The existing private flock helper is used only in this dedicated metadata + // directory. No provider AuthStore/state/profile or credentials are opened. + held, err := (&AuthStore{directory: filepath.Join(j.directory, string(role))}).lock(ctx) + if err != nil { + return nil, ErrGitHubInstallationRecovery + } + return held, nil +} + +type installationRowReader interface { + QueryRowContext(context.Context, string, ...any) *sql.Row +} + +func (j *GitHubInstallationJournal) latest(ctx context.Context, db installationRowReader, role GitHubCredentialRole) (GitHubInstallationAttempt, error) { + var raw []byte + var id string + err := db.QueryRowContext(ctx, "SELECT attempt,CASE WHEN length(record)<=4096 THEN record ELSE NULL END FROM github_installation_attempts WHERE role=? ORDER BY sequence DESC LIMIT 1", string(role)).Scan(&id, &raw) + if errors.Is(err, sql.ErrNoRows) { + return GitHubInstallationAttempt{}, nil + } + if err != nil { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + record, err := decodeGitHubInstallationAttempt(raw) + if err != nil || record.ID != id || !record.valid(j.enrollment, role) { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + return record, nil +} + +// Latest reports durable metadata only, never a readiness or credential grant. +func (j *GitHubInstallationJournal) Latest(ctx context.Context, role GitHubCredentialRole) (GitHubInstallationAttempt, error) { + if !j.valid() || ctx == nil || githubPermissions(role) == nil { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + return j.latest(ctx, j.store.db, role) +} + +func (j *GitHubInstallationJournal) write(ctx context.Context, tx *sql.Tx, record GitHubInstallationAttempt, insert bool) error { + if !record.valid(j.enrollment, record.Role) { + return ErrGitHubInstallationRecovery + } + raw, err := json.Marshal(record) + if err != nil || len(raw) > 4096 { + return ErrGitHubInstallationRecovery + } + if insert { + _, err = tx.ExecContext(ctx, "INSERT INTO github_installation_attempts(role,attempt,record) VALUES(?,?,?)", string(record.Role), record.ID, raw) + } else { + var result sql.Result + result, err = tx.ExecContext(ctx, "UPDATE github_installation_attempts SET record=? WHERE role=? AND attempt=?", raw, string(record.Role), record.ID) + if err == nil { + n, e := result.RowsAffected() + if e != nil || n != 1 { + return ErrGitHubInstallationRecovery + } + } + } + if err != nil { + return ErrGitHubInstallationRecovery + } + return nil +} + +// Retain at least seven days of terminal history, every unresolved attempt and +// the latest record for each role. Excess attempts fail before any HTTP work. +func (j *GitHubInstallationJournal) prune(ctx context.Context, tx *sql.Tx, now time.Time) error { + rows, err := tx.QueryContext(ctx, "SELECT role,attempt,CASE WHEN length(record)<=4096 THEN record ELSE NULL END FROM github_installation_attempts ORDER BY sequence DESC LIMIT 1025") + if err != nil { + return ErrGitHubInstallationRecovery + } + seen := map[GitHubCredentialRole]bool{} + remove := []string{} + count := 0 + for rows.Next() { + count++ + var raw []byte + var role GitHubCredentialRole + var id string + if rows.Scan(&role, &id, &raw) != nil { + rows.Close() + return ErrGitHubInstallationRecovery + } + record, err := decodeGitHubInstallationAttempt(raw) + if err != nil || record.ID != id || !record.valid(j.enrollment, role) { + rows.Close() + return ErrGitHubInstallationRecovery + } + latest := !seen[record.Role] + seen[record.Role] = true + terminal := record.State == "qualified" || record.State == "pre_mint_failed" || record.State == "retry_authorized" + if !latest && terminal && record.UpdatedAt.Before(now.Add(-7*24*time.Hour)) { + remove = append(remove, record.ID) + } + } + err = rows.Err() + rows.Close() + if err != nil || count > installationAttemptCap { + return ErrGitHubInstallationRecovery + } + for _, id := range remove { + if _, err := tx.ExecContext(ctx, "DELETE FROM github_installation_attempts WHERE attempt=?", id); err != nil { + return ErrGitHubInstallationRecovery + } + } + if count-len(remove) >= installationAttemptCap { + return ErrGitHubInstallationRecovery + } + return nil +} + +func (j *GitHubInstallationJournal) begin(ctx context.Context, role GitHubCredentialRole) (GitHubInstallationAttempt, error) { + tx, err := j.store.db.BeginTx(ctx, nil) + if err != nil { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + defer tx.Rollback() + previous, err := j.latest(ctx, tx, role) + if err != nil { + return GitHubInstallationAttempt{}, err + } + if previous.ID != "" && previous.State != "qualified" && previous.State != "pre_mint_failed" && previous.State != "retry_authorized" { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + now := j.now().UTC() + if now.IsZero() || now.Before(previous.UpdatedAt) || j.prune(ctx, tx, now) != nil { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + var nonce [16]byte + if _, err := rand.Read(nonce[:]); err != nil { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + record := GitHubInstallationAttempt{Version: 1, ID: hex.EncodeToString(nonce[:]), Enrollment: j.enrollment, Role: role, State: "pre_mint", StartedAt: now, UpdatedAt: now} + if j.write(ctx, tx, record, true) != nil || tx.Commit() != nil { + return GitHubInstallationAttempt{}, ErrGitHubInstallationRecovery + } + return record, nil +} + +func (j *GitHubInstallationJournal) transition(ctx context.Context, attempt GitHubInstallationAttempt, change func(*GitHubInstallationAttempt) error) error { + tx, err := j.store.db.BeginTx(ctx, nil) + if err != nil { + return ErrGitHubInstallationRecovery + } + defer tx.Rollback() + record, err := j.latest(ctx, tx, attempt.Role) + if err != nil || record.ID != attempt.ID { + return ErrGitHubInstallationRecovery + } + now := j.now().UTC() + if now.IsZero() || now.Before(record.UpdatedAt) { + return ErrGitHubInstallationRecovery + } + if change(&record) != nil { + return ErrGitHubInstallationRecovery + } + record.UpdatedAt = now + if j.write(ctx, tx, record, false) != nil || tx.Commit() != nil { + return ErrGitHubInstallationRecovery + } + return nil +} + +// Acquire is explicit and never replays a token POST. Uncertain or interrupted +// work is latched across restart before a fresh attempt can reach HTTP. +func (j *GitHubInstallationJournal) Acquire(ctx context.Context, o GitHubInstallationOptions) (*GitHubInstallationTransport, error) { + if !j.valid() || ctx == nil || o.Enrollment != j.enrollment || githubPermissions(o.Role) == nil { + return nil, ErrGitHubInstallationRecovery + } + ctx, cancel := context.WithTimeout(ctx, 100*time.Second) + defer cancel() + held, err := j.lock(ctx, o.Role) + if err != nil { + return nil, err + } + defer held.close() + attempt, err := j.begin(ctx, o.Role) + if err != nil { + return nil, err + } + o.Now = j.now + transport, err := acquireGitHubInstallation(ctx, o, func(ctx context.Context) error { + return j.transition(ctx, attempt, func(a *GitHubInstallationAttempt) error { + if a.State != "pre_mint" { + return ErrGitHubInstallationRecovery + } + a.State = "mint_pending" + a.MintAttempted = true + return nil + }) + }) + if err == nil { + receipt := transport.Receipt() + err = j.transition(ctx, attempt, func(a *GitHubInstallationAttempt) error { + if a.State != "mint_pending" { + return ErrGitHubInstallationRecovery + } + a.State = "qualified" + a.Receipt = &receipt + return nil + }) + if err == nil { + return transport, nil + } + transport.Close() + } + // The caller's cancelled context cannot erase uncertainty. Failed recovery + // persistence leaves the earlier pre_mint/mint_pending latch intact. + cleanup, stop := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) + defer stop() + _ = j.transition(cleanup, attempt, func(a *GitHubInstallationAttempt) error { + if a.MintAttempted { + a.State = "unknown" + } else { + a.State = "pre_mint_failed" + } + return nil + }) + return nil, ErrGitHubInstallationRecovery +} + +// AuthorizeRetry is a separate trusted recovery operation. It holds the same +// process lock as acquisition, verifies stopped work outside the transaction, +// then fences the exact attempt before recording nonsecret recovery evidence. +// It does not mint/revoke a token or retry any transport operation. +func (j *GitHubInstallationJournal) AuthorizeRetry(ctx context.Context, role GitHubCredentialRole, evidence GitHubInstallationRecovery, verifyStopped func(context.Context, GitHubInstallationAttempt, GitHubInstallationRecovery) error) error { + if !j.valid() || ctx == nil || verifyStopped == nil || evidence.OwnerID != OwnerID || !validAuthHash(evidence.EvidenceHash) { + return ErrGitHubInstallationRecovery + } + held, err := j.lock(ctx, role) + if err != nil { + return err + } + defer held.close() + attempt, err := j.Latest(ctx, role) + if err != nil || attempt.ID == "" || attempt.ID != evidence.AttemptID || evidence.At.Before(attempt.UpdatedAt) || evidence.At.After(j.now()) { + return ErrGitHubInstallationRecovery + } + if attempt.State != "unknown" && attempt.State != "pre_mint" && attempt.State != "mint_pending" { + return ErrGitHubInstallationRecovery + } + if verifyStopped(ctx, attempt, evidence) != nil || ctx.Err() != nil { + return ErrGitHubInstallationRecovery + } + return j.transition(ctx, attempt, func(a *GitHubInstallationAttempt) error { + if a.State != attempt.State { + return ErrGitHubInstallationRecovery + } + a.State = "retry_authorized" + a.Recovery = &evidence + return nil + }) +} diff --git a/reviewer/github_installation_journal_unix_test.go b/reviewer/github_installation_journal_unix_test.go new file mode 100644 index 00000000..1d97b57d --- /dev/null +++ b/reviewer/github_installation_journal_unix_test.go @@ -0,0 +1,644 @@ +//go:build linux || darwin + +package reviewer + +import ( + "bytes" + "context" + "crypto/sha256" + "crypto/tls" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "net/http" + "net/url" + "os" + "os/exec" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "testing" + "time" +) + +func installationJournalFixture(t *testing.T) (*GitHubInstallationJournal, *Store, string) { + t.Helper() + store, path := openFixtureStore(t) + journal, err := NewGitHubInstallationJournal(context.Background(), store, func() time.Time { return controlTime }) + if err != nil { + t.Fatal(err) + } + return journal, store, path +} +func installationJournalReopen(t *testing.T, path string) *GitHubInstallationJournal { + t.Helper() + store, err := OpenStore(path, alphaEnrollment(), DefaultConfig()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { store.Close() }) + journal, err := NewGitHubInstallationJournal(context.Background(), store, func() time.Time { return controlTime }) + if err != nil { + t.Fatal(err) + } + return journal +} +func assertJournalContainsNoCredentials(t *testing.T, j *GitHubInstallationJournal, credentials ...[]byte) { + t.Helper() + containsCredential := func(raw []byte) bool { + if strings.Contains(string(raw), "synthetic-app-jwt") || strings.Contains(string(raw), "synthetic-installation-") { + return true + } + for _, credential := range credentials { + if len(credential) > 0 && bytes.Contains(raw, credential) { + return true + } + } + return false + } + rows, err := j.store.db.Query("SELECT record FROM github_installation_attempts") + if err != nil { + t.Fatal(err) + } + for rows.Next() { + var raw []byte + if rows.Scan(&raw) != nil { + t.Fatal("journal read failed") + } + if containsCredential(raw) { + t.Fatal("credential persisted in journal") + } + } + err = rows.Err() + rows.Close() + if err != nil { + t.Fatal(err) + } + for _, path := range []string{j.store.path, j.store.path + "-wal"} { + raw, err := os.ReadFile(path) + if err != nil && !os.IsNotExist(err) { + t.Fatal(err) + } + if containsCredential(raw) { + t.Fatal("credential persisted in SQLite bytes") + } + } + for _, value := range []any{j, *j} { + raw, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + for _, text := range []string{fmt.Sprint(value), fmt.Sprintf("%#v", value), string(raw)} { + if !strings.Contains(text, "redacted") || strings.Contains(text, j.directory) { + t.Fatal("private journal handle exposed") + } + } + } +} + +func TestGitHubInstallationJournalTLSWriteAheadBothRolesAndRestart(t *testing.T) { + j, _, path := installationJournalFixture(t) + before, err := j.store.Snapshot(context.Background()) + if err != nil { + t.Fatal(err) + } + key := appJWTTestKey(t) + signer, err := NewGitHubAppJWTSigner(alphaEnrollment(), key, func() time.Time { return controlTime }) + if err != nil { + t.Fatal(err) + } + jwt, err := signer.Sign(context.Background()) + if err != nil { + t.Fatal(err) + } + defer clear(jwt) + verifyAppJWT(t, jwt, &key.PublicKey, controlTime) + for _, role := range []GitHubCredentialRole{GitHubReader, GitHubPublisher} { + f, o := installationHTTPFixture(t, role, string(jwt)) + observed := false + f.mutate = func(p string, _ map[string]any) { + if strings.HasSuffix(p, "/access_tokens") { + record, err := j.Latest(context.Background(), role) + if err != nil || record.State != "mint_pending" || !record.MintAttempted || record.Receipt != nil { + t.Error("POST arrived without committed mint intent") + } + observed = true + } + } + transport, err := j.Acquire(context.Background(), o) + if err != nil { + t.Fatal(err) + } + transport.Close() + record, err := j.Latest(context.Background(), role) + if err != nil || record.State != "qualified" || record.Receipt == nil || record.Receipt.Role != role || record.Enrollment != alphaEnrollment() || f.mints != 1 || f.calls != 7 || !observed { + t.Fatal("qualified role metadata missing", err) + } + reopened := installationJournalReopen(t, path) + got, err := reopened.Latest(context.Background(), role) + if err != nil || Digest(got) != Digest(record) || f.mints != 1 { + t.Fatal("restart changed metadata or minted credentials") + } + } + assertJournalContainsNoCredentials(t, j, jwt) + after, err := j.store.Snapshot(context.Background()) + if err != nil || Digest(before) != Digest(after) { + t.Fatal("credential qualification changed review state or budgets") + } +} + +func TestGitHubInstallationJournalUnknownAndPostMintRefusalNeverReplay(t *testing.T) { + for _, mode := range []string{"lost mint", "bad post-mint identity", "qualification write failure", "unknown write failure"} { + t.Run(mode, func(t *testing.T) { + j, store, path := installationJournalFixture(t) + f, o := installationHTTPFixture(t, GitHubReader) + switch mode { + case "lost mint": + f.loseMint = true + case "bad post-mint identity": + f.mutate = func(p string, d map[string]any) { + if strings.HasPrefix(p, "/users/") { + d["id"] = int64(999) + } + } + case "qualification write failure", "unknown write failure": + states := "json_extract(NEW.record,'$.State')='qualified'" + if mode == "unknown write failure" { + states += " OR json_extract(NEW.record,'$.State')='unknown'" + } + _, err := store.db.Exec("CREATE TRIGGER reject_completion BEFORE UPDATE ON github_installation_attempts WHEN " + states + " BEGIN SELECT RAISE(ABORT,'synthetic-private-db-error'); END") + if err != nil { + t.Fatal(err) + } + } + transport, err := j.Acquire(context.Background(), o) + if transport != nil || !errors.Is(err, ErrGitHubInstallationRecovery) || strings.Contains(err.Error(), "synthetic-private") || f.mints != 1 { + t.Fatal("failed mint/qualification returned transport or replayed", err) + } + record, err := j.Latest(context.Background(), GitHubReader) + if err != nil || (record.State != "unknown" && record.State != "mint_pending") || !record.MintAttempted { + t.Fatal("uncertainty not retained", record.State, err) + } + calls := f.calls + reopened := installationJournalReopen(t, path) + transport, err = reopened.Acquire(context.Background(), o) + if transport != nil || !errors.Is(err, ErrGitHubInstallationRecovery) || f.calls != calls || f.mints != 1 { + t.Fatal("restart replayed uncertain token POST") + } + assertJournalContainsNoCredentials(t, j) + }) + } +} + +func TestGitHubInstallationJournalPersistenceRefusalPreventsMint(t *testing.T) { + for _, stage := range []string{"begin", "mint intent"} { + t.Run(stage, func(t *testing.T) { + j, store, _ := installationJournalFixture(t) + f, o := installationHTTPFixture(t, GitHubReader) + statement := "CREATE TRIGGER refuse BEFORE INSERT ON github_installation_attempts BEGIN SELECT RAISE(ABORT,'private-write-error'); END" + if stage == "mint intent" { + statement = "CREATE TRIGGER refuse BEFORE UPDATE ON github_installation_attempts WHEN json_extract(NEW.record,'$.State')='mint_pending' BEGIN SELECT RAISE(ABORT,'private-write-error'); END" + } + if _, err := store.db.Exec(statement); err != nil { + t.Fatal(err) + } + if transport, err := j.Acquire(context.Background(), o); transport != nil || !errors.Is(err, ErrGitHubInstallationRecovery) || strings.Contains(err.Error(), "private-write-error") { + t.Fatal("persistence failure leaked or admitted HTTP") + } + expectedCalls := 0 + if stage == "mint intent" { + expectedCalls = 2 + } + if f.mints != 0 || f.calls != expectedCalls { + t.Fatal("token dispatched before durable intent", f.calls, f.mints) + } + }) + } +} + +func TestGitHubInstallationJournalExplicitRecoveryRequiresMatchingProof(t *testing.T) { + j, _, _ := installationJournalFixture(t) + f, o := installationHTTPFixture(t, GitHubReader) + f.loseMint = true + if transport, err := j.Acquire(context.Background(), o); transport != nil || err == nil { + t.Fatal("lost response unexpectedly qualified") + } + attempt, err := j.Latest(context.Background(), GitHubReader) + if err != nil { + t.Fatal(err) + } + proof := GitHubInstallationRecovery{AttemptID: attempt.ID, OwnerID: OwnerID, At: controlTime, EvidenceHash: Digest("synthetic stopped acquisition")} + verified := 0 + check := func(ctx context.Context, a GitHubInstallationAttempt, e GitHubInstallationRecovery) error { + verified++ + if a.ID != attempt.ID || Digest(e) != Digest(proof) { + return errors.New("foreign proof") + } + return nil + } + for _, mutate := range []func(*GitHubInstallationRecovery){ + func(p *GitHubInstallationRecovery) { p.OwnerID++ }, func(p *GitHubInstallationRecovery) { p.AttemptID = strings.Repeat("f", 32) }, func(p *GitHubInstallationRecovery) { p.At = controlTime.Add(-time.Second) }, func(p *GitHubInstallationRecovery) { p.At = controlTime.Add(time.Second) }, func(p *GitHubInstallationRecovery) { p.EvidenceHash = "" }, + } { + bad := proof + mutate(&bad) + if j.AuthorizeRetry(context.Background(), GitHubReader, bad, check) == nil { + t.Fatal("foreign or missing recovery proof accepted") + } + } + if verified != 0 || f.mints != 1 { + t.Fatal("invalid proof reached callback/network") + } + if j.AuthorizeRetry(context.Background(), GitHubReader, proof, nil) == nil { + t.Fatal("missing stopped-work check accepted") + } + if j.AuthorizeRetry(context.Background(), GitHubReader, proof, func(context.Context, GitHubInstallationAttempt, GitHubInstallationRecovery) error { + return errors.New("not stopped") + }) == nil { + t.Fatal("failed stop check accepted") + } + if err := j.AuthorizeRetry(context.Background(), GitHubReader, proof, check); err != nil { + t.Fatal(err) + } + if verified != 1 || f.mints != 1 { + t.Fatal("recovery itself performed network work") + } + authorized, _ := j.Latest(context.Background(), GitHubReader) + if authorized.State != "retry_authorized" || Digest(authorized.Recovery) != Digest(&proof) { + t.Fatal("proof not persisted") + } + f.loseMint = false + transport, err := j.Acquire(context.Background(), o) + if err != nil { + t.Fatal(err) + } + transport.Close() + current, _ := j.Latest(context.Background(), GitHubReader) + if current.ID == attempt.ID || current.State != "qualified" || f.mints != 2 { + t.Fatal("explicit fresh qualification not recorded") + } + if j.AuthorizeRetry(context.Background(), GitHubReader, proof, check) == nil { + t.Fatal("old recovery proof reused") + } +} + +func TestGitHubInstallationJournalChild(t *testing.T) { + mode := os.Getenv("SFL_JOURNAL_TEST_MODE") + if mode == "" { + t.Skip("explicit child fixture only") + } + store, err := OpenStore(os.Getenv("SFL_JOURNAL_TEST_DB"), alphaEnrollment(), DefaultConfig()) + if err != nil { + t.Fatal(err) + } + defer store.Close() + j, err := NewGitHubInstallationJournal(context.Background(), store, func() time.Time { return controlTime }) + if err != nil { + t.Fatal(err) + } + var calls atomic.Int32 + var transport http.RoundTripper = egressRoundTrip(func(q *http.Request) (*http.Response, error) { + calls.Add(1) + return nil, errors.New("unexpected child HTTP") + }) + if mode == "crash" { + base, err := url.Parse(os.Getenv("SFL_JOURNAL_TEST_SERVER")) + if err != nil || base.Scheme != "https" || base.Hostname() != "127.0.0.1" { + t.Fatal("only explicit fake loopback TLS allowed") + } + pin := os.Getenv("SFL_JOURNAL_TEST_CERT") + if !validAuthHash(pin) { + t.Fatal("missing fake certificate pin") + } + inner := &http.Transport{TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12, InsecureSkipVerify: true, VerifyConnection: func(state tls.ConnectionState) error { + if len(state.PeerCertificates) != 1 { + return errors.New("fake certificate count") + } + sum := sha256.Sum256(state.PeerCertificates[0].Raw) + if hex.EncodeToString(sum[:]) != pin { + return errors.New("fake certificate mismatch") + } + return nil + }}} + defer inner.CloseIdleConnections() + transport = installationRoute{base, inner, t} + } + timeout := 10 * time.Second + if mode == "locked" { + timeout = 150 * time.Millisecond + } + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + got, err := j.Acquire(ctx, GitHubInstallationOptions{alphaEnrollment(), GitHubReader, []byte("synthetic-app-jwt"), transport, func() time.Time { return controlTime }}) + if got != nil { + got.Close() + } + if mode == "crash" { + t.Fatal("crash child returned before parent killed it") + } + if !errors.Is(err, ErrGitHubInstallationRecovery) || calls.Load() != 0 { + t.Fatal("process lock permitted HTTP") + } +} +func journalChild(t *testing.T, mode, path string, extra ...string) *exec.Cmd { + t.Helper() + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + cmd := exec.Command(exe, "-test.run=^TestGitHubInstallationJournalChild$", "-test.v") + cmd.Env = append([]string{"PATH=/usr/bin:/bin", "SFL_JOURNAL_TEST_MODE=" + mode, "SFL_JOURNAL_TEST_DB=" + path}, extra...) + return cmd +} + +func TestGitHubInstallationJournalSameRoleLockAcrossProcesses(t *testing.T) { + j, _, path := installationJournalFixture(t) + held, err := j.lock(context.Background(), GitHubReader) + if err != nil { + t.Fatal(err) + } + defer held.close() + cmd := journalChild(t, "locked", path) + raw, err := cmd.CombinedOutput() + if err != nil { + t.Fatal("child bypassed or failed private lock", err, string(raw)) + } + attempt, err := j.Latest(context.Background(), GitHubReader) + if err != nil || attempt.ID != "" { + t.Fatal("blocked child created attempt") + } +} + +func TestGitHubInstallationJournalKilledOwnerRetainsUnknownAcrossRestart(t *testing.T) { + for _, point := range []string{"pre_mint", "mint_pending"} { + t.Run(point, func(t *testing.T) { + j, _, path := installationJournalFixture(t) + f, o := installationHTTPFixture(t, GitHubReader) + route := o.Transport.(installationRoute) + conn, err := tls.Dial("tcp", route.base.Host, route.inner.(*http.Transport).TLSClientConfig) + if err != nil { + t.Fatal(err) + } + sum := sha256.Sum256(conn.ConnectionState().PeerCertificates[0].Raw) + conn.Close() + accepted, release := make(chan struct{}), make(chan struct{}) + var once sync.Once + unblock := func() { once.Do(func() { close(release) }) } + t.Cleanup(unblock) + f.mu.Lock() + f.mutate = func(path string, _ map[string]any) { + if point == "pre_mint" && path == "/app" || point == "mint_pending" && strings.HasSuffix(path, "/access_tokens") { + close(accepted) + <-release + } + } + f.mu.Unlock() + cmd := journalChild(t, "crash", path, "SFL_JOURNAL_TEST_SERVER="+route.base.String(), "SFL_JOURNAL_TEST_CERT="+hex.EncodeToString(sum[:])) + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + waited := make(chan error, 1) + var waitComplete atomic.Bool + go func() { err := cmd.Wait(); waitComplete.Store(true); waited <- err }() + t.Cleanup(func() { + if !waitComplete.Load() { + _ = cmd.Process.Kill() + select { + case <-waited: + case <-time.After(3 * time.Second): + t.Error("fixture child not reaped") + } + } + }) + select { + case <-accepted: + case err := <-waited: + unblock() + t.Fatal("child exited before target", err) + case <-time.After(5 * time.Second): + unblock() + t.Fatal("child never reached fake target") + } + record, err := j.Latest(context.Background(), GitHubReader) + if err != nil || record.State != point || record.MintAttempted != (point == "mint_pending") { + t.Fatal("child HTTP preceded durable stage", record.State, err) + } + proof := GitHubInstallationRecovery{AttemptID: record.ID, OwnerID: OwnerID, At: controlTime, EvidenceHash: Digest("stopped synthetic child")} + checked := false + short, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + err = j.AuthorizeRetry(short, GitHubReader, proof, func(context.Context, GitHubInstallationAttempt, GitHubInstallationRecovery) error { + checked = true + return nil + }) + cancel() + if err == nil || checked { + t.Fatal("recovery raced a live owner") + } + if err := cmd.Process.Kill(); err != nil { + t.Fatal(err) + } + select { + case err := <-waited: + if err == nil { + t.Fatal("child was not killed") + } + case <-time.After(3 * time.Second): + t.Fatal("killed child did not stop") + } + unblock() + f.mu.Lock() + f.mutate = nil + calls, mints := f.calls, f.mints + f.mu.Unlock() + reopened := installationJournalReopen(t, path) + if got, err := reopened.Acquire(context.Background(), o); got != nil || !errors.Is(err, ErrGitHubInstallationRecovery) { + t.Fatal("killed acquisition replayed") + } + f.mu.Lock() + same := f.calls == calls && f.mints == mints + f.mu.Unlock() + if !same { + t.Fatal("restart made HTTP request") + } + if err := reopened.AuthorizeRetry(context.Background(), GitHubReader, proof, func(context.Context, GitHubInstallationAttempt, GitHubInstallationRecovery) error { + if cmd.ProcessState == nil { + return errors.New("child not waited") + } + return nil + }); err != nil { + t.Fatal(err) + } + got, err := reopened.Acquire(context.Background(), o) + if err != nil { + t.Fatal(err) + } + got.Close() + f.mu.Lock() + newMints := f.mints + f.mu.Unlock() + if newMints != mints+1 { + t.Fatal("explicit recovery did not make one new mint") + } + assertJournalContainsNoCredentials(t, reopened) + }) + } +} + +func TestGitHubInstallationJournalHistoryCapAndTerminalRetention(t *testing.T) { + j, store, _ := installationJournalFixture(t) + tx, err := store.db.Begin() + if err != nil { + t.Fatal(err) + } + for i := 0; i < installationAttemptCap; i++ { + receipt := &GitHubInstallationReceipt{alphaEnrollment(), GitHubReader, controlTime, controlTime.Add(time.Hour)} + a := GitHubInstallationAttempt{Version: 1, ID: fmt.Sprintf("%032x", i+1), Enrollment: alphaEnrollment(), Role: GitHubReader, State: "qualified", StartedAt: controlTime, UpdatedAt: controlTime, MintAttempted: true, Receipt: receipt} + if i == 0 { + a.State = "unknown" + a.Receipt = nil + } + if err := j.write(context.Background(), tx, a, true); err != nil { + tx.Rollback() + t.Fatal(err) + } + } + if err := tx.Commit(); err != nil { + t.Fatal(err) + } + f, o := installationHTTPFixture(t, GitHubReader) + if got, err := j.Acquire(context.Background(), o); got != nil || !errors.Is(err, ErrGitHubInstallationRecovery) || f.calls != 0 { + t.Fatal("cap admitted HTTP") + } + later := controlTime.Add(8 * 24 * time.Hour) + j.now = func() time.Time { return later } + f.mutate = func(path string, doc map[string]any) { + if strings.HasSuffix(path, "/access_tokens") { + doc["expires_at"] = later.Add(time.Hour) + } + } + got, err := j.Acquire(context.Background(), o) + if err != nil { + t.Fatal(err) + } + got.Close() + var count int + if store.db.QueryRow("SELECT count(*) FROM github_installation_attempts").Scan(&count) != nil || count != 3 { + t.Fatal("terminal retention removed latest or unresolved evidence", count) + } + var raw []byte + if store.db.QueryRow("SELECT record FROM github_installation_attempts WHERE attempt=?", fmt.Sprintf("%032x", 1)).Scan(&raw) != nil || !strings.Contains(string(raw), "unknown") { + t.Fatal("unresolved old attempt pruned") + } +} + +func TestGitHubInstallationJournalRefusesForeignAndMalformedState(t *testing.T) { + j, store, _ := installationJournalFixture(t) + f, o := installationHTTPFixture(t, GitHubReader) + bad := o + bad.Enrollment.InstallationID++ + if got, err := j.Acquire(context.Background(), bad); got != nil || err == nil || f.calls != 0 { + t.Fatal("foreign enrollment admitted") + } + if _, err := NewGitHubInstallationJournal(context.Background(), store, nil); err == nil { + t.Fatal("ambient clock accepted") + } + if _, err := store.db.Exec("INSERT INTO github_installation_attempts(role,attempt,record) VALUES(?,?,?)", string(GitHubReader), strings.Repeat("a", 32), []byte(`{"Version":1,"State":"pre_mint_failed","State":"qualified"}`)); err != nil { + t.Fatal(err) + } + if got, err := j.Acquire(context.Background(), o); got != nil || err == nil || f.calls != 0 { + t.Fatal("malformed journal admitted HTTP") + } +} + +func TestGitHubInstallationJournalRefusesSymlinkNamespace(t *testing.T) { + store, _ := openFixtureStore(t) + outside := t.TempDir() + path := filepath.Join(filepath.Dir(store.path), "github-installation-attempts") + if err := os.Symlink(outside, path); err != nil { + t.Fatal(err) + } + if _, err := NewGitHubInstallationJournal(context.Background(), store, func() time.Time { return controlTime }); err == nil { + t.Fatal("symlink journal namespace accepted") + } + entries, err := os.ReadDir(outside) + if err != nil || len(entries) != 0 { + t.Fatal("symlink target mutated") + } +} + +func TestGitHubInstallationJournalCaseAliasCannotRetireUnknownState(t *testing.T) { + j, store, _ := installationJournalFixture(t) + attempt, err := j.begin(context.Background(), GitHubReader) + if err != nil { + t.Fatal(err) + } + raw, err := json.Marshal(attempt) + if err != nil { + t.Fatal(err) + } + raw = bytes.Replace(raw, []byte(`"State":"pre_mint"`), []byte(`"State":"unknown","state":"pre_mint_failed"`), 1) + if _, err := store.db.Exec("UPDATE github_installation_attempts SET record=? WHERE attempt=?", raw, attempt.ID); err != nil { + t.Fatal(err) + } + f, o := installationHTTPFixture(t, GitHubReader) + got, err := j.Acquire(context.Background(), o) + if got != nil { + got.Close() + } + if got != nil || !errors.Is(err, ErrGitHubInstallationRecovery) || f.calls != 0 { + t.Fatal("ambiguous state field escaped the durable latch", f.calls) + } +} + +func TestGitHubInstallationJournalRetentionRefusesMismatchedRowIdentity(t *testing.T) { + j, store, _ := installationJournalFixture(t) + tx, err := store.db.Begin() + if err != nil { + t.Fatal(err) + } + records := []GitHubInstallationAttempt{} + for i := 1; i <= 3; i++ { + record := GitHubInstallationAttempt{Version: 1, ID: fmt.Sprintf("%032x", i), Enrollment: alphaEnrollment(), Role: GitHubReader, State: "qualified", StartedAt: controlTime, UpdatedAt: controlTime, MintAttempted: true, Receipt: &GitHubInstallationReceipt{alphaEnrollment(), GitHubReader, controlTime, controlTime.Add(time.Hour)}} + if i == 1 { + record.State = "unknown" + record.Receipt = nil + } + if err := j.write(context.Background(), tx, record, true); err != nil { + tx.Rollback() + t.Fatal(err) + } + records = append(records, record) + } + if err := tx.Commit(); err != nil { + t.Fatal(err) + } + // A malformed terminal row points at a different unresolved attempt. Its + // JSON identity must never choose the row deleted by retention. + corrupt := records[1] + corrupt.ID = records[0].ID + raw, err := json.Marshal(corrupt) + if err != nil { + t.Fatal(err) + } + if _, err := store.db.Exec("UPDATE github_installation_attempts SET record=? WHERE attempt=?", raw, records[1].ID); err != nil { + t.Fatal(err) + } + later := controlTime.Add(8 * 24 * time.Hour) + j.now = func() time.Time { return later } + f, o := installationHTTPFixture(t, GitHubReader) + f.mutate = func(path string, doc map[string]any) { + if strings.HasSuffix(path, "/access_tokens") { + doc["expires_at"] = later.Add(time.Hour) + } + } + got, err := j.Acquire(context.Background(), o) + if got != nil { + got.Close() + } + var preserved int + readErr := store.db.QueryRow("SELECT count(*) FROM github_installation_attempts WHERE attempt=?", records[0].ID).Scan(&preserved) + if got != nil || !errors.Is(err, ErrGitHubInstallationRecovery) || f.calls != 0 || readErr != nil || preserved != 1 { + t.Fatal("mismatched terminal row retired unresolved evidence", f.calls, preserved) + } +}