caelum0x cff329a525 fix(rithub): RIT-ACT stop leaking migrate auth token via git subprocess argv
Review of 369ab87a found realGitOps.run passing the Authorization header
to git as a `-c http.extraHeader=...` CLI flag, which lands in the git
child process's argv — readable by any other local process on
railyard-box via `ps -ef` or /proc/<pid>/cmdline for the full duration of
the clone/push (minutes, not milliseconds, for a real repo). This
directly contradicted the code's own doc comment claiming the token
never reaches argv. Verified live with a real `git clone` + `ps`: the
base64 Basic-auth token was plainly visible in process listing before
this fix, and gone after switching to GIT_CONFIG_COUNT/KEY/VALUE
environment variables (git >= 2.31), which only the owning uid/root can
read via /proc/<pid>/environ.

Also tightens the async-import job store file from 0644 to 0600 (and its
parent dir from 0755 to 0700): a Job's CloneAddr is stored unredacted,
so if a caller embeds credentials directly in the clone URL instead of
using auth_token, the persisted jobs.json file carried them world-
readable.

go build/vet/gofmt clean; go test ./... -race -count=1 passes all 3
packages. Still not deployed — same as 369ab87a, this is code-only, no
Caddy/pm2/systemd changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:15:07 +03:00

123 lines
3.3 KiB
Go

package jobs
import (
"crypto/rand"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"time"
)
// Store is a small, mutex-protected job registry with optional JSON-file
// persistence (so an async-import restart doesn't orphan an in-flight
// job's status — the worker goroutine itself is not resumed across a
// restart, only the last known status survives). nil persistPath means
// in-memory only, which is all the test suite needs.
type Store struct {
mu sync.Mutex
jobs map[string]Job
persistPath string
}
// NewStore builds a Store. persistPath may be empty for an in-memory-only
// store (used by tests and by callers that don't need restart-survival).
func NewStore(persistPath string) (*Store, error) {
s := &Store{jobs: make(map[string]Job), persistPath: persistPath}
if persistPath == "" {
return s, nil
}
if err := s.load(); err != nil {
return nil, fmt.Errorf("jobs: loading persisted store from %s: %w", persistPath, err)
}
return s, nil
}
func (s *Store) load() error {
data, err := os.ReadFile(s.persistPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if len(data) == 0 {
return nil
}
var jobs map[string]Job
if err := json.Unmarshal(data, &jobs); err != nil {
return err
}
s.jobs = jobs
return nil
}
// saveLocked persists the current job map. Caller must hold s.mu.
func (s *Store) saveLocked() error {
if s.persistPath == "" {
return nil
}
data, err := json.MarshalIndent(s.jobs, "", " ")
if err != nil {
return fmt.Errorf("jobs: marshaling store: %w", err)
}
if err := os.MkdirAll(filepath.Dir(s.persistPath), 0o700); err != nil {
return fmt.Errorf("jobs: creating store dir: %w", err)
}
// 0600, not 0644: a Job's CloneAddr is whatever clone_addr the caller
// sent, unredacted — if a caller embedded credentials directly in the
// URL (https://token@host/...) instead of using auth_token, this file
// carries them to disk. Only the service's own user should be able to
// read it.
tmp := s.persistPath + ".tmp"
if err := os.WriteFile(tmp, data, 0o600); err != nil {
return fmt.Errorf("jobs: writing store: %w", err)
}
if err := os.Rename(tmp, s.persistPath); err != nil {
return fmt.Errorf("jobs: committing store: %w", err)
}
return nil
}
// NewID returns a fresh random job id, safe to use in a URL path segment.
func NewID() (string, error) {
buf := make([]byte, 12)
if _, err := rand.Read(buf); err != nil {
return "", fmt.Errorf("jobs: generating id: %w", err)
}
return fmt.Sprintf("%x", buf), nil
}
// Create records a brand-new job. The caller sets ID/CreatedAt/UpdatedAt
// before calling.
func (s *Store) Create(job Job) error {
s.mu.Lock()
defer s.mu.Unlock()
s.jobs[job.ID] = job
return s.saveLocked()
}
// Get returns a copy of the job with the given id, and whether it existed.
func (s *Store) Get(id string) (Job, bool) {
s.mu.Lock()
defer s.mu.Unlock()
job, ok := s.jobs[id]
return job.Clone(), ok
}
// Update applies mutate to the stored job (if present) and persists the
// result. UpdatedAt is refreshed automatically; mutate should not set it.
func (s *Store) Update(id string, mutate func(*Job)) error {
s.mu.Lock()
defer s.mu.Unlock()
job, ok := s.jobs[id]
if !ok {
return fmt.Errorf("jobs: update %s: not found", id)
}
mutate(&job)
job.UpdatedAt = time.Now().UTC()
s.jobs[id] = job
return s.saveLocked()
}