Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

Retry the password version on login when startup could not read it.

protonmatter7cac9c240b43
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 38b39b4..94aad22 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,7 +27,7 @@ All notable changes to this project are documented in this file. The format is b
### Changed
-- **Password changes revoke sessions**: a password change stores a monotonic `auth.password_version` in the same transaction as the hash. Tokens carry that version, so a token from an older version is rejected, including when two changes land in the same second. `reset-password.sh` writes the same counter and requires an ARTEX restart, because the process reads the version at startup. A failed write leaves the previous session valid. Log, activity, and side-question streams re-check the token and close after the version moves. If the version cannot be read at startup, sessions are rejected until a read succeeds.
+- **Password changes revoke sessions**: a password change stores a monotonic `auth.password_version` in the same transaction as the hash. Tokens carry that version, so a token from an older version is rejected, including when two changes land in the same second. `reset-password.sh` writes the same counter and requires an ARTEX restart, because the process reads the version at startup. A failed write leaves the previous session valid. Log, activity, and side-question streams re-check the token and close after the version moves. If the version cannot be read at startup, sessions are rejected until the next login reads it.
- **Untrusted-output boundary is now on by default**: Norma frames tool results for the worker, planner, chat and main agent unless `ARTEX_UNTRUSTED_BOUNDARY` is set to `0`, `false`, `off` or `no`. It was opt-in via `ARTEX_UNTRUSTED_BOUNDARY=1` in 0.6.0 (#166); the default flips here (#203).
- **Versioned migrations**: the `db` package applies numbered `db/migrations/NNNN_slug.sql` files once, in order, under the schema advisory lock, and records a SHA-256 checksum in `schema_migrations`; missing, renamed, changed or out-of-order migrations fail closed (#226).
- **Coverage graph performance**: the coverage graph batches its connector lookups (three queries instead of one per root and per company) and adds the partial index `idx_ts_icp`; on a task of about 3,000 assets the p50 dropped from 126 ms to 27 ms and the queries per call from 221 to 3 (#230).
diff --git a/server/auth.go b/server/auth.go
index 3f186f9..006d5fc 100644
--- a/server/auth.go
+++ b/server/auth.go
@@ -214,7 +214,8 @@ func verifyJWT(tokenStr string, key []byte, version int64) bool {
}
// sessionOK reports whether token is still a session for the password version
-// this process has loaded. A failed version read rejects every token.
+// this process has loaded. A failed version read rejects every token until
+// login reads the counter again.
func (s *Server) sessionOK(token string) bool {
if s.pwdState.Load() == pwdUnreadable {
return false
@@ -359,7 +360,8 @@ func (s *Server) initSetupToken() {
// loadPasswordVersion reads auth.password_version. A missing key leaves the
// version at zero, which accepts every valid token. A read that still fails
-// after three tries rejects every token until a later read succeeds.
+// after three tries rejects every token. Login calls this again when the
+// state is unreadable, and stays closed if that read fails too.
func (s *Server) loadPasswordVersion() {
if s.m == nil || s.m.pg == nil {
return
@@ -378,7 +380,7 @@ func (s *Server) loadPasswordVersion() {
}
}
if err != nil {
- log.Printf("[auth] password version unreadable; rejecting sessions until it can be read: %v", err)
+ log.Printf("[auth] password version unreadable; rejecting sessions until login can read it: %v", err)
s.pwdState.Store(pwdUnreadable)
return
}
@@ -389,7 +391,7 @@ func (s *Server) loadPasswordVersion() {
}
n, err := strconv.ParseInt(raw, 10, 64)
if err != nil || n < 0 {
- log.Printf("[auth] password version %q is not a counter; rejecting sessions", raw)
+ log.Printf("[auth] password version %q is not a counter; rejecting sessions until login can read it", raw)
s.pwdState.Store(pwdUnreadable)
return
}
@@ -570,6 +572,9 @@ func (s *Server) login(w http.ResponseWriter, r *http.Request, getSetting func(s
return
}
s.loginLimit.reset(ip)
+ if s.pwdState.Load() == pwdUnreadable {
+ s.loadPasswordVersion()
+ }
if s.pwdState.Load() == pwdUnreadable {
writeErr(w, 503, errDataSourceUnavailable)
return
diff --git a/server/auth_epoch_test.go b/server/auth_epoch_test.go
index 2f0188c..6ac3a9c 100644
--- a/server/auth_epoch_test.go
+++ b/server/auth_epoch_test.go
@@ -182,6 +182,69 @@ func TestUnreadablePasswordVersionRejectsSessions(t *testing.T) {
}
}
+func TestLoginRereadsPasswordVersionWhenUnreadable(t *testing.T) {
+ m, err := NewManager(t.TempDir(), "")
+ if err != nil {
+ t.Skipf("postgres unavailable (%v)", err)
+ }
+ defer m.Close()
+
+ stamp := time.Now().UnixNano()
+ s := &Server{
+ m: m,
+ jwtKey: []byte("0123456789abcdef0123456789abcdef"),
+ loginLimit: newLoginLimiter(time.Now),
+ authHashKey: fmt.Sprintf("test.password_hash.%d", stamp),
+ authVersionKey: fmt.Sprintf("test.password_version.%d", stamp),
+ }
+ t.Cleanup(func() {
+ _, _ = m.pg.Exec(`DELETE FROM settings WHERE key=$1 OR key=$2`, s.authHashKey, s.authVersionKey)
+ })
+ s.setupToken = "setup-ok"
+ w := httptest.NewRecorder()
+ s.authInit(w, httptest.NewRequest("POST", "/api/auth/init", strings.NewReader(`{"password":"correct-horse-1","setup_token":"setup-ok"}`)))
+ if w.Code != 200 {
+ t.Fatalf("init status=%d body=%s", w.Code, w.Body.String())
+ }
+ if _, err := m.pg.Exec(`UPDATE settings SET value='not-a-number' WHERE key=$1`, s.authVersionKey); err != nil {
+ t.Fatal(err)
+ }
+ s.pwdVersion.Store(99)
+ s.loadPasswordVersion()
+ if s.pwdState.Load() != pwdUnreadable {
+ t.Fatalf("state=%d want unreadable", s.pwdState.Load())
+ }
+ w = httptest.NewRecorder()
+ s.authLogin(w, httptest.NewRequest("POST", "/api/auth/login", strings.NewReader(`{"username":"ARTEX","password":"correct-horse-1"}`)))
+ if w.Code != 503 {
+ t.Fatalf("login while unreadable: status=%d want 503 body=%s", w.Code, w.Body.String())
+ }
+ if s.pwdState.Load() != pwdUnreadable {
+ t.Fatal("a failed reread opened sessions")
+ }
+
+ if _, err := m.pg.Exec(`UPDATE settings SET value='1' WHERE key=$1`, s.authVersionKey); err != nil {
+ t.Fatal(err)
+ }
+ w = httptest.NewRecorder()
+ s.authLogin(w, httptest.NewRequest("POST", "/api/auth/login", strings.NewReader(`{"username":"ARTEX","password":"correct-horse-1"}`)))
+ if w.Code != 200 {
+ t.Fatalf("login after the version can be read: status=%d body=%s", w.Code, w.Body.String())
+ }
+ if s.pwdState.Load() != pwdReady || s.pwdVersion.Load() != 1 {
+ t.Fatalf("state=%d version=%d, want ready at 1", s.pwdState.Load(), s.pwdVersion.Load())
+ }
+ var logged struct {
+ Token string `json:"token"`
+ }
+ if err := json.Unmarshal(w.Body.Bytes(), &logged); err != nil || logged.Token == "" {
+ t.Fatalf("login token: %v body=%s", err, w.Body.String())
+ }
+ if !s.sessionOK(logged.Token) {
+ t.Fatal("token from the healed login was rejected")
+ }
+}
+
func TestAbsentPasswordVersionKeepsSessions(t *testing.T) {
m, err := NewManager(t.TempDir(), "")
if err != nil {
diff --git a/server/server.go b/server/server.go
index 66d367f..aeba273 100644
--- a/server/server.go
+++ b/server/server.go
@@ -57,7 +57,7 @@ type Server struct {
// pwdVersion is auth.password_version, a counter. Zero means no password
// change is stored yet, and any valid token is accepted. pwdState is
// pwdUnreadable when that counter could not be read; every session is
- // rejected until a later read succeeds.
+ // rejected until login reads it again.
pwdVersion atomic.Int64
pwdState atomic.Int32
// authHashKey and authVersionKey override the settings keys. Empty uses