Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software:2222/vole.git

A free and open-source forge that lives in your Git repo.

Make the audit log one shape that a privileged change cannot skip

b1499c729a026cfda32d8a90374b3b8391ca0090 · Keenan Nemetz · 13d ago · parent d74d4de8

Make the audit log one shape that a privileged change cannot skip

Every writer used to format its own line and open its own file, so
data/audit.log held six different column layouts and a write that failed
changed nothing. A log that can be bypassed by a full disk, or by a key
comment containing a tab, is not the security promise the docs make.

internal/audit is now the only writer: one line per action, always
timestamp, action, actor, subject, detail. Every field is escaped and
capped at 4 KiB, so a key comment, an LFS lock path, a repository name or
an admin argument cannot add a column or a record. One write per line
under O_APPEND keeps the web, the SSH server and each hook process from
interleaving, and secure.OpenAppend keeps the file at 0600 including on an
upgrade from 0644.

Privileged changes now fail closed, and they are recorded before they
happen, so a failed write means the change did not occur:

  - roster, key and instance-role changes (inside the transaction)
  - access-rule changes, and a repository deletion, which records before
    removing content and forgets the rules only afterwards
  - session creation and revocation
  - a forced LFS unlock
  - an admin command over SSH, which reports audit_unavailable
  - a push that overrides a lock, an approval, a release or history

Ordinary pushes and web actions are reported instead: their changes are
already durable in git, so a write failure is logged at error level (or on
the pusher's terminal from post-receive) rather than making a working push
look rejected.

Opening an instance now checks the log is a regular file at 0600 owned by
the service account and appendable, and refuses to start otherwise. There
is no setting that disables the log or returns it to best effort.

Regression tests cover field injection through key comments, lock paths,
admin argv and repository names; 0600 on create and on upgrade; an
unwritable sink failing an ACL change, an account change, a session, a
forced unlock, an admin command and a push override while leaving the
underlying state unchanged; concurrent appends keeping whole lines; and
startup refusing a symlinked log.

forge/access/store_test.go +54−2

@@ -19,6 +19,7 @@ func open(t *testing.T) (*Store, string) {
19 19 if err != nil {
20 20 t.Fatal(err)
21 21 }
22 s.AuditPath = filepath.Join(dir, "audit.log")
22 23 t.Cleanup(func() { s.Close() })
23 24 return s, dir
24 25 }
@@ -131,7 +132,58 @@ func TestChangesAreAudited(t *testing.T) {
131 132 if err != nil {
132 133 t.Fatalf("no audit line: %v", err)
133 134 }
134 if !strings.Contains(string(raw), "acl repo-1 updated by keenan") {
135 t.Fatalf("audit log = %q", raw)
135 fields := strings.Split(strings.TrimSuffix(string(raw), "\n"), "\t")
136 if len(fields) != 5 {
137 t.Fatalf("audit line has %d columns, want 5: %q", len(fields), raw)
138 }
139 if fields[1] != "acl.update" || fields[2] != "keenan" || fields[3] != "repo-1" {
140 t.Fatalf("audit line = %q, want action acl.update by keenan on repo-1", raw)
141 }
142 }
143
144 // An ACL change that cannot be recorded must not happen: the rule set is the
145 // instance's only copy, and an unrecorded grant is a grant nobody can explain.
146 func TestChangesFailWhenTheLogCannotBeWritten(t *testing.T) {
147 t.Parallel()
148 s, dir := open(t)
149 blocker := filepath.Join(dir, "blocked")
150 if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil {
151 t.Fatal(err)
152 }
153 s.AuditPath = filepath.Join(blocker, "audit.log")
154 owner := "01M5OWNER0000000000000000A"
155 if err := s.Set("repo-1", Empty(owner), "keenan", time.Now().UTC()); err == nil {
156 t.Fatal("Set succeeded with an unwritable audit log")
157 }
158 if got, err := s.Get("repo-1"); err != nil || got != nil {
159 t.Fatalf("rules were written anyway: %+v, %v", got, err)
160 }
161 // The deletion is recorded separately because the repository's content has to
162 // survive a failed audit, so it fails the same way and for the same reason.
163 if err := s.RecordDelete("repo-1", "keenan"); err == nil {
164 t.Fatal("RecordDelete succeeded with an unwritable audit log")
165 }
166 }
167
168 // A key comment, a repository name and an account name are user content; none may
169 // add a column or a record of its own.
170 func TestAuditFieldsCannotInjectColumns(t *testing.T) {
171 t.Parallel()
172 s, dir := open(t)
173 audit := filepath.Join(dir, "audit.log")
174 s.AuditPath = audit
175 evile := "a\tb\nc"
176 if err := s.Set(evile, Empty("01M5OWNER0000000000000000A"), evile, time.Now().UTC()); err != nil {
177 t.Fatal(err)
178 }
179 raw, err := os.ReadFile(audit)
180 if err != nil {
181 t.Fatal(err)
182 }
183 if n := strings.Count(string(raw), "\n"); n != 1 {
184 t.Fatalf("injected field wrote %d lines: %q", n, raw)
185 }
186 if got := len(strings.Split(strings.TrimSuffix(string(raw), "\n"), "\t")); got != 5 {
187 t.Fatalf("injected field wrote %d columns: %q", got, raw)
136 188 }
137 189 }