From c7fb9fdb55ebc366ae005d94991c10980b3540cc Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Mon, 16 Nov 2026 20:07:07 +0800 Subject: [PATCH 1/5] fix(totp): distinguish two accepted secrets during rotation Preserve uncertain recovery state with allowlisted diagnostics when enrollment leaves both secrets usable. Factor binding and recovery follow-up remain pending. Refs #881 --- .gitignore | 1 + .../internal/service/openai_totp_rotation.go | 3 +- .../service/openai_totp_rotation_test.go | 21 +++++++++-- docs/openai-totp-factor-recovery.md | 36 +++++++++++++++++++ tools/openai_totp_rotation.py | 10 ++++-- tools/test_openai_totp_rotation.py | 18 +++++++++- 6 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 docs/openai-totp-factor-recovery.md diff --git a/.gitignore b/.gitignore index 21a6d3aaaec0..641fdb783ab0 100644 --- a/.gitignore +++ b/.gitignore @@ -135,6 +135,7 @@ deploy/docker-compose.override.yml vite.config.js user-traffic-pending/ docs/* +!docs/openai-totp-factor-recovery.md !docs/ai-ops-codex.md !docs/reauth-identity-validation.md !docs/openai-context-limit-errors.md diff --git a/backend/internal/service/openai_totp_rotation.go b/backend/internal/service/openai_totp_rotation.go index 1d43ba0ffd8b..c2febaca4579 100644 --- a/backend/internal/service/openai_totp_rotation.go +++ b/backend/internal/service/openai_totp_rotation.go @@ -294,7 +294,8 @@ func (s *OpenAIOAuthReauthService) FinishTOTPRotation(ctx context.Context, task code = "" } else { switch code { - case "login_failed", "identity_mismatch", "enrollment_failed", "activation_unconfirmed", "verification_failed", "worker_failed": + case "login_failed", "identity_mismatch", "enrollment_failed", "activation_unconfirmed", "verification_failed", "worker_failed", + "new_accepted_old_valid", "old_accepted_new_valid": default: code = "worker_failed" } diff --git a/backend/internal/service/openai_totp_rotation_test.go b/backend/internal/service/openai_totp_rotation_test.go index 54977faea865..a89a4dfe1e1b 100644 --- a/backend/internal/service/openai_totp_rotation_test.go +++ b/backend/internal/service/openai_totp_rotation_test.go @@ -12,8 +12,9 @@ import ( type rotationTestRepo struct { *reauthTestRepo - rotation *OpenAITOTPRotation - candidate string + rotation *OpenAITOTPRotation + candidate string + finishCode string } func (r *rotationTestRepo) GetTOTPRotation(context.Context, int64) (*OpenAITOTPRotation, error) { @@ -29,7 +30,8 @@ func (r *rotationTestRepo) TransitionTOTPRotation(_ context.Context, _ int64, _ r.candidate = candidate return nil } -func (r *rotationTestRepo) FinishTOTPRotation(context.Context, int64, string, bool, string) error { +func (r *rotationTestRepo) FinishTOTPRotation(_ context.Context, _ int64, _ string, _ bool, code string) error { + r.finishCode = code return nil } func (r *rotationTestRepo) RetryTOTPRotation(context.Context, int64, int64, string) error { return nil } @@ -118,3 +120,16 @@ func TestTOTPRotationRequiresCapableWorker(t *testing.T) { _, err = svc.CreateTOTPRotation(context.Background(), 42) require.NoError(t, err) } + +func TestTOTPRotationFinishPreservesSafeMultiFactorDiagnostics(t *testing.T) { + for _, code := range []string{"new_accepted_old_valid", "old_accepted_new_valid", "verification_failed"} { + t.Run(code, func(t *testing.T) { + svc, repo := newRotationService(t) + require.NoError(t, svc.FinishTOTPRotation(context.Background(), 7, "worker", false, code)) + require.Equal(t, code, repo.finishCode) + }) + } + svc, repo := newRotationService(t) + require.NoError(t, svc.FinishTOTPRotation(context.Background(), 7, "worker", false, "untrusted-secret-body")) + require.Equal(t, "worker_failed", repo.finishCode) +} diff --git a/docs/openai-totp-factor-recovery.md b/docs/openai-totp-factor-recovery.md new file mode 100644 index 000000000000..4f81df10bede --- /dev/null +++ b/docs/openai-totp-factor-recovery.md @@ -0,0 +1,36 @@ +# 多 TOTP 验证器恢复边界 + +Refs #881。本页描述首个诊断修复检查点,尚未完成 factor 绑定与恢复方案。 + +本地托管 Worker 的 enroll/activate 流程新增验证器,没有撤销旧验证器。 +新旧密钥都被登录协议接受时,任务保持 `uncertain` 并保留候选密钥, +普通登录配置不会自动切换。状态 API 的 `error_code` 区分以下结果: + +| 错误码 | 已观察到的结果 | 未证明的事项 | +| --- | --- | --- | +| `new_accepted_old_valid` | 本次先验证候选密钥成功,随后旧密钥也被接受 | 尚无持久化 factor 绑定,未撤销旧验证器 | +| `old_accepted_new_valid` | 恢复检查先接受旧密钥,随后候选密钥也被接受 | 尚无持久化 factor 绑定,未撤销候选验证器 | +| `verification_failed` | 验证未获得足够成功证据 | 不能据此断言候选无效或旧验证器已撤销 | + +这些诊断不是成功替换或撤销的证明。不要再次 enroll 来消除不确定状态, +不要删除唯一可用验证器。已有账号可通过现有管理员恢复导出接口获取候选 +或旧凭据;导出只提供恢复材料,不证明该密钥可完成登录。妥善保护导出材料。 +旧账号删除后的任务访问和重新导入路径仍待实现,不能把重新添加账号当作 +清除原任务或重新 enroll 的依据。 + +## 协议归属 + +本地 runtime 的构建入口是 `tools/reauth-runtime/Dockerfile`,下载 +`poxiao33/toSub2` 的固定提交 `8548397e89bf80e508eda64a87e0d556d43abc84`。 +其中 `src/protocol-login.mjs` 当前选取返回列表中的第一个 TOTP factor。 +本检查点没有修改这个上游文件,也没有声称发布了登录协议修复。 + +“2FA 登录导入”的首次登录由外部 Session Studio 执行。外部服务的部署源码 +及 factor 选择行为不在本仓库控制范围内;本地 Worker 修复不能证明该服务 +已修复。后续需分别验证本地绑定登录和外部导入边界。 + +## 验收限制 + +当前只有合成凭据的离线回归。真实授权、登录和 2FA 验收 **NOT EXECUTED**, +需要另行明确授权。Go 服务测试、完整 runtime 构建和隔离数据库集成由远程 +CI 验证,不能把本地 Python GREEN 当作这些检查通过。Issue #881 保持未解决。 diff --git a/tools/openai_totp_rotation.py b/tools/openai_totp_rotation.py index 34e5372b12b4..ac72a501de63 100644 --- a/tools/openai_totp_rotation.py +++ b/tools/openai_totp_rotation.py @@ -170,7 +170,10 @@ def verify_pair(config, claim, candidate, action, login): if rejected: time.sleep(31 - time.time() % 30) if rejected and login(config, claim, rejected) is not None: - raise RotationError('verification_failed') + # Enrollment adds a factor; it does not revoke the previous one. Keep + # recovery available until factor binding can establish provenance, + # but do not misreport a successfully verified secret as invalid. + raise RotationError('old_accepted_new_valid' if action == 'verify_old' else 'new_accepted_old_valid') def rotate(api, claim, *, session_factory=WebSession, login=login_web, journal_factory=Journal): @@ -213,7 +216,10 @@ def rotate(api, claim, *, session_factory=WebSession, login=login_web, journal_f stage = 'verification_failed' verify_pair(api.config, claim, candidate, action, login) api.totp_finish(task, True) - except Exception: + except Exception as exc: + if stage == 'verification_failed' and isinstance(exc, RotationError) and exc.code in ( + 'new_accepted_old_valid', 'old_accepted_new_valid'): + stage = exc.code # Never expose network bodies, login traces, candidates, passwords, or exceptions. try: api.totp_finish(task, False, stage) diff --git a/tools/test_openai_totp_rotation.py b/tools/test_openai_totp_rotation.py index 6aac1b36ca38..2374f3344a4e 100644 --- a/tools/test_openai_totp_rotation.py +++ b/tools/test_openai_totp_rotation.py @@ -65,10 +65,26 @@ def test_activation_timeout_does_not_reenroll_and_keeps_candidate(self): self.api.totp_finish.assert_called_once_with(7, False, 'activation_unconfirmed') self.login.assert_not_called() - def test_old_secret_still_works_never_reports_success(self): + def test_two_valid_factors_preserve_candidate_with_specific_diagnostic(self): self.login.side_effect = [{}, {}] self.run_rotation() + self.api.totp_finish.assert_called_once_with(7, False, 'new_accepted_old_valid') + self.assertEqual(self.journal.save.call_args.args[1], NEW) + self.assertEqual(self.session.request.call_count, 2) + self.assertEqual([call.args[2] for call in self.login.call_args_list], [NEW, OLD]) + + def test_candidate_rejected_remains_verification_failed(self): + self.login.side_effect = [None] + self.run_rotation() self.api.totp_finish.assert_called_once_with(7, False, 'verification_failed') + self.assertEqual(self.login.call_count, 1) + + def test_old_recovery_distinguishes_two_valid_factors(self): + self.claim.update(action='verify_old', candidate_secret=NEW) + self.login.side_effect = [{}, {}] + self.run_rotation() + self.api.totp_finish.assert_called_once_with(7, False, 'old_accepted_new_valid') + self.session.request.assert_not_called() def test_network_error_is_not_evidence_old_secret_was_rejected(self): self.login.side_effect = [{}, RotationError('login_failed')] From f963c3d362ef09fbcde55d1ce97cd39ee62f7016 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Mon, 16 Nov 2026 20:30:31 +0800 Subject: [PATCH 2/5] fix(totp): persist factor bindings and recover deleted-account credentials safely Bound local challenges, atomically preserve encrypted factor provenance, and require fresh binding before promotion. Add insert-only recovery import, idempotent migration, and isolated PostgreSQL/API acceptance. Refs #881 --- .github/workflows/issue881-totp.yml | 144 +++++++++++++ .github/workflows/reauth-runtime.yml | 28 ++- .../admin/openai_totp_rotation_handler.go | 64 +++++- .../openai_totp_rotation_handler_test.go | 2 +- .../oauth429_weekly_only_integration_test.go | 1 + .../repository/openai_totp_recovery.go | 29 +++ .../openai_totp_recovery_integration_test.go | 86 ++++++++ .../repository/openai_totp_rotation.go | 74 ++++++- .../openai_totp_rotation_integration_test.go | 29 +++ backend/internal/server/routes/admin.go | 2 + .../internal/service/openai_oauth_reauth.go | 21 +- .../internal/service/openai_session_export.go | 7 +- .../internal/service/openai_totp_binding.go | 203 ++++++++++++++++++ .../internal/service/openai_totp_rotation.go | 115 +++++++--- .../service/openai_totp_rotation_test.go | 113 +++++++++- .../migrations/382_openai_totp_binding.sql | 7 + docs/openai-totp-factor-recovery.md | 102 ++++++--- tools/fixtures/codex-http-transport.mjs | 3 +- tools/fixtures/totp-http-transport.mjs | 31 +++ tools/openai_codex_diagnostics.mjs | 13 ++ tools/openai_codex_oauth_adapter.py | 9 +- tools/openai_oauth_reauth_worker.py | 13 +- tools/openai_totp_journal.mjs | 3 + tools/openai_totp_rotation.py | 67 ++++-- tools/reauth-runtime/Dockerfile | 10 +- tools/reauth-runtime/apply_totp_patch.py | 13 ++ tools/reauth-runtime/package.py | 1 + tools/reauth-runtime/test_deployment.py | 63 +++++- .../reauth-runtime/totp-factor-binding.patch | 95 ++++++++ tools/test_openai_codex_diagnostics.mjs | 10 + tools/test_openai_codex_oauth_adapter.py | 4 +- tools/test_openai_totp_integration.py | 113 ++++++++++ tools/test_openai_totp_journal.mjs | 9 + tools/test_openai_totp_protocol.mjs | 85 ++++++++ tools/test_openai_totp_rotation.py | 72 ++++--- 35 files changed, 1489 insertions(+), 152 deletions(-) create mode 100644 .github/workflows/issue881-totp.yml create mode 100644 backend/internal/repository/openai_totp_recovery.go create mode 100644 backend/internal/repository/openai_totp_recovery_integration_test.go create mode 100644 backend/internal/service/openai_totp_binding.go create mode 100644 backend/migrations/382_openai_totp_binding.sql create mode 100644 tools/fixtures/totp-http-transport.mjs create mode 100644 tools/reauth-runtime/apply_totp_patch.py create mode 100644 tools/reauth-runtime/totp-factor-binding.patch create mode 100644 tools/test_openai_totp_integration.py create mode 100644 tools/test_openai_totp_protocol.mjs diff --git a/.github/workflows/issue881-totp.yml b/.github/workflows/issue881-totp.yml new file mode 100644 index 000000000000..2f5880fab9ce --- /dev/null +++ b/.github/workflows/issue881-totp.yml @@ -0,0 +1,144 @@ +name: Issue881 TOTP persistence acceptance +on: + pull_request: + paths: + - 'backend/**' + - 'tools/*totp*' + - 'tools/reauth-runtime/**' + - '.github/workflows/issue881-totp.yml' + workflow_dispatch: +permissions: + contents: read +jobs: + persistence: + if: github.head_ref == 'fix/issue881-totp-factor-binding' || github.ref_name == 'fix/issue881-totp-factor-binding' + runs-on: ubuntu-24.04 + timeout-minutes: 45 + services: + postgres: + image: postgres:16-alpine + env: + POSTGRES_USER: fixture + POSTGRES_PASSWORD: fixture + POSTGRES_DB: issue881 + ports: ['15432:5432'] + options: --health-cmd "pg_isready -U fixture" --health-interval 5s --health-retries 20 + redis: + image: redis:7-alpine + ports: ['16379:6379'] + env: + EXPECTED_HEAD: ${{ github.event.pull_request.head.sha || github.sha }} + SUB2API_TEST_POSTGRES_DSN: postgres://fixture:fixture@127.0.0.1:15432/issue881?sslmode=disable + SUB2API_TEST_REDIS_URL: redis://127.0.0.1:16379/0 + CI: 'true' + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + - uses: actions/setup-go@v6 + with: + go-version-file: backend/go.mod + cache-dependency-path: backend/go.sum + - name: Freeze identity + run: | + test "$(git rev-parse HEAD)" = "$EXPECTED_HEAD" + mkdir -p evidence + git rev-parse HEAD HEAD^{tree} > evidence/head.txt + - name: Real PG RED against pre-fix recovery Upsert SQL + shell: bash + run: | + set -euo pipefail + source=backend/internal/repository/openai_totp_recovery.go + cp "$source" "$RUNNER_TEMP/recovery-green.go" + trap 'cp "$RUNNER_TEMP/recovery-green.go" "$source"' EXIT + python3 - <<'PY' + from pathlib import Path + Path('backend/internal/repository/openai_totp_recovery.go').write_text('''package repository + import ("context"; "github.com/Wei-Shaw/sub2api/internal/service") + func (r *openAIOAuthReauthRepository) InsertTOTPRecoveryConfig(ctx context.Context, c *service.OpenAIOAuthReauthStoredConfig) error {return r.UpsertConfig(ctx,c)} + ''') + PY + set +e + (cd backend && go test -tags=integration ./internal/repository -run '^TestTOTPRecoveryInsertNeverOverwritesExistingOrConcurrentConfig$' -count=1 -v -timeout=10m) > evidence/red-recovery-pg.log 2>&1 + result=$? + set -e + echo "$result" > evidence/red-recovery-pg.exit + cat evidence/red-recovery-pg.log + test "$result" -ne 0 + ! grep -Eq 'build failed|setup failed|undefined:|no tests to run' evidence/red-recovery-pg.log + grep -F 'recovery loser must conflict' evidence/red-recovery-pg.log + - name: Real PG RED against pre-fix migration reload + shell: bash + run: | + set -euo pipefail + sed 's/ADD COLUMN IF NOT EXISTS/ADD COLUMN/g' backend/migrations/382_openai_totp_binding.sql > "$RUNNER_TEMP/migration-red.sql" + set +e + (cd backend && ISSUE881_MIGRATION_TEST_FILE="$RUNNER_TEMP/migration-red.sql" go test -tags=integration ./internal/repository -run '^TestTOTPBindingMigrationCanReloadWithoutReplacingSnapshot$' -count=1 -v -timeout=10m) > evidence/red-migration-pg.log 2>&1 + result=$? + set -e + echo "$result" > evidence/red-migration-pg.exit + cat evidence/red-migration-pg.log + test "$result" -ne 0 + grep -F 'migration reload must be idempotent' evidence/red-migration-pg.log + - name: Actual handler and service GREEN + shell: bash + run: | + set -euo pipefail + git diff --exit-code + (cd backend && go test -tags=unit ./internal/handler/admin ./internal/service -run '^Test(TOTP|OpenAIOAuthReauth|OpenAISessionExport)' -count=1 -v -timeout=10m) 2>&1 | tee evidence/green-handler-service.log + ! grep -Eq -- '--- SKIP:|no tests to run' evidence/green-handler-service.log + - name: Real PG GREEN migration reload, binding, CAS, recovery conflicts + shell: bash + run: | + set -euo pipefail + (cd backend && go test -tags=integration ./internal/repository -run '^Test(TOTP|OAuth429)' -count=1 -v -timeout=10m) 2>&1 | tee evidence/green-persistence.log + ! grep -Eq -- '--- SKIP:|no tests to run' evidence/green-persistence.log + - name: Production Go build + shell: bash + run: | + set -euo pipefail + (cd backend && CGO_ENABLED=0 go build -trimpath -o "$RUNNER_TEMP/sub2api-production" ./cmd/server) 2>&1 | tee evidence/production-build.log + sha256sum "$RUNNER_TEMP/sub2api-production" > evidence/production-build.sha256 + git diff --exit-code + - uses: actions/upload-artifact@v7 + if: always() + with: + name: issue881-persistence-${{ github.event.pull_request.head.sha || github.sha }} + path: evidence/ + + production-build: + if: github.head_ref == 'fix/issue881-totp-factor-binding' || github.ref_name == 'fix/issue881-totp-factor-binding' + runs-on: ubuntu-24.04 + timeout-minutes: 35 + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + - uses: actions/setup-go@v6 + with: + go-version-file: backend/go.mod + cache-dependency-path: backend/go.sum + - uses: pnpm/action-setup@v6 + with: + version: 9 + - uses: actions/setup-node@v6 + with: + node-version: '24' + cache: pnpm + cache-dependency-path: frontend/pnpm-lock.yaml + - name: Production frontend and embedded Linux binary + shell: bash + run: | + set -euo pipefail + test "$(git rev-parse HEAD)" = "${{ github.event.pull_request.head.sha || github.sha }}" + mkdir -p evidence + git rev-parse HEAD > evidence/head.txt + pnpm --dir frontend install --frozen-lockfile 2>&1 | tee evidence/dependencies.log + pnpm --dir frontend run build 2>&1 | tee evidence/frontend-build.log + (cd backend && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags embed -trimpath -ldflags="-s -w -X main.Commit=${{ github.event.pull_request.head.sha || github.sha }}" -o "$RUNNER_TEMP/sub2api-production" ./cmd/server) 2>&1 | tee evidence/production-build.log + sha256sum "$RUNNER_TEMP/sub2api-production" > evidence/binary.sha256 + - uses: actions/upload-artifact@v7 + if: always() + with: + name: issue881-production-build-${{ github.event.pull_request.head.sha || github.sha }} + path: evidence/ diff --git a/.github/workflows/reauth-runtime.yml b/.github/workflows/reauth-runtime.yml index 7ef920e1b56a..05be39e8a654 100644 --- a/.github/workflows/reauth-runtime.yml +++ b/.github/workflows/reauth-runtime.yml @@ -8,6 +8,11 @@ on: - 'tools/fixtures/codex-http-transport.mjs' - 'tools/fixtures/session-studio/**' - 'tools/fixtures/credential-operation-errors.json' + - 'backend/internal/service/openai_totp*' + - 'backend/internal/repository/openai_totp*' + - 'backend/internal/handler/admin/openai_totp*' + - 'backend/migrations/382_openai_totp_binding.sql' + - 'tools/fixtures/totp-http-transport.mjs' - 'tools/openai_totp_*' - 'tools/test_openai_totp_*' - 'tools/openai_siwc_*' @@ -39,6 +44,10 @@ jobs: runs-on: ${{ matrix.runner }} steps: - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + - name: Assert exact head + run: test "$(git rev-parse HEAD)" = "${{ github.event.pull_request.head.sha || github.sha }}" - uses: docker/setup-buildx-action@v3 - name: Package and validate without publishing env: @@ -47,7 +56,7 @@ jobs: run: bash tools/reauth-runtime/build.sh - uses: actions/upload-artifact@v7 with: - name: reauth-runtime-test-${{ matrix.arch }} + name: reauth-runtime-test-${{ matrix.arch }}-${{ github.event.pull_request.head.sha || github.sha }} path: reauth-output/*.tar.gz retention-days: 3 @@ -75,6 +84,10 @@ jobs: --health-interval 5s --health-timeout 5s --health-retries 20 steps: - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + - name: Assert exact head + run: test "$(git rev-parse HEAD)" = "${{ github.event.pull_request.head.sha || github.sha }}" - uses: actions/setup-go@v6 with: go-version-file: backend/go.mod @@ -84,7 +97,7 @@ jobs: python-version: '3.12' - uses: actions/download-artifact@v8 with: - name: reauth-runtime-test-amd64 + name: reauth-runtime-test-amd64-${{ github.event.pull_request.head.sha || github.sha }} path: reauth-input - name: Build local API working-directory: backend @@ -125,10 +138,15 @@ jobs: sleep 2 done curl -fsS "$STAGING_API_URL/health" - python tools/reauth-runtime/test_deployment.py + set -o pipefail + python tools/reauth-runtime/test_deployment.py 2>&1 | tee "$STAGING_ROOT/acceptance.log" - uses: actions/upload-artifact@v7 + if: always() with: - name: totp-deployment-verification - path: ${{ runner.temp }}/totp-staging/verification.json + name: totp-deployment-verification-${{ github.event.pull_request.head.sha || github.sha }} + path: | + ${{ runner.temp }}/totp-staging/verification.json + ${{ runner.temp }}/totp-staging/acceptance.log + ${{ runner.temp }}/totp-staging/api.log if-no-files-found: error retention-days: 3 diff --git a/backend/internal/handler/admin/openai_totp_rotation_handler.go b/backend/internal/handler/admin/openai_totp_rotation_handler.go index 3cd0a90c35d7..b2402351af1b 100644 --- a/backend/internal/handler/admin/openai_totp_rotation_handler.go +++ b/backend/internal/handler/admin/openai_totp_rotation_handler.go @@ -121,16 +121,18 @@ func (h *OpenAIOAuthReauthHandler) TOTPPhase(c *gin.Context) { return } var req struct { - WorkerID string `json:"worker_id"` - Phase string `json:"phase"` - Secret string `json:"secret"` + WorkerID string `json:"worker_id"` + Phase string `json:"phase"` + Secret string `json:"secret"` + FactorID string `json:"factor_id"` + EnrollmentSessionID string `json:"enrollment_session_id"` } c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4096) if err = c.ShouldBindJSON(&req); err != nil { response.BadRequest(c, "Invalid phase request") return } - if err = h.service.TOTPRotationPhase(c.Request.Context(), task, req.WorkerID, req.Phase, req.Secret); err != nil { + if err = h.service.TOTPRotationPhase(c.Request.Context(), task, req.WorkerID, req.Phase, req.Secret, req.FactorID, req.EnrollmentSessionID); err != nil { response.ErrorFrom(c, err) return } @@ -173,16 +175,66 @@ func (h *OpenAIOAuthReauthHandler) TOTPRecover(c *gin.Context) { return } var req struct { - Secret string `json:"secret"` + Secret string `json:"secret"` + FactorID string `json:"factor_id"` + EnrollmentSessionID string `json:"enrollment_session_id"` } c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4096) if err = c.ShouldBindJSON(&req); err != nil { response.BadRequest(c, "Invalid recovery request") return } - if err = h.service.RecoverTOTPCandidate(c.Request.Context(), task, req.Secret); err != nil { + if err = h.service.RecoverTOTPCandidate(c.Request.Context(), task, req.Secret, req.FactorID, req.EnrollmentSessionID); err != nil { response.ErrorFrom(c, err) return } response.Success(c, gin.H{"accepted": true}) } + +// Recovery uses a task ID, since the original account may have been soft-deleted. +func (h *OpenAIOAuthReauthHandler) ExportTOTPRecovery(c *gin.Context) { + if !rotationPrivate(c) { + return + } + var req struct { + TaskID int64 `json:"task_id"` + Email string `json:"expected_email"` + Source string `json:"source"` + IncludePassword bool `json:"include_password"` + } + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4096) + if err := c.ShouldBindJSON(&req); err != nil || req.TaskID <= 0 { + response.BadRequest(c, "Invalid recovery request") + return + } + result, err := h.service.ExportTOTPRecovery(c.Request.Context(), req.TaskID, req.Email, req.Source, req.IncludePassword) + if err != nil { + response.ErrorFrom(c, err) + return + } + response.Success(c, result) +} +func (h *OpenAIOAuthReauthHandler) ImportTOTPRecovery(c *gin.Context) { + if !rotationPrivate(c) { + return + } + id, ok := parseReauthAccountID(c) + if !ok { + return + } + var req struct { + TaskID int64 `json:"task_id"` + Email string `json:"expected_email"` + Source string `json:"source"` + } + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4096) + if err := c.ShouldBindJSON(&req); err != nil || req.TaskID <= 0 { + response.BadRequest(c, "Invalid recovery request") + return + } + if err := h.service.ImportTOTPRecovery(c.Request.Context(), id, req.TaskID, req.Email, req.Source); err != nil { + response.ErrorFrom(c, err) + return + } + response.Success(c, gin.H{"imported": true, "verified": false, "engine": "local_worker"}) +} diff --git a/backend/internal/handler/admin/openai_totp_rotation_handler_test.go b/backend/internal/handler/admin/openai_totp_rotation_handler_test.go index 627744f8553b..8720d75616fa 100644 --- a/backend/internal/handler/admin/openai_totp_rotation_handler_test.go +++ b/backend/internal/handler/admin/openai_totp_rotation_handler_test.go @@ -14,7 +14,7 @@ import ( func TestTOTPRotationAdminEndpointsRejectObserversAndDisableCaching(t *testing.T) { gin.SetMode(gin.TestMode) h := &OpenAIOAuthReauthHandler{} - for _, endpoint := range []gin.HandlerFunc{h.RotationStatus, h.RotateTOTP, h.RetryTOTP, h.ExportTOTP} { + for _, endpoint := range []gin.HandlerFunc{h.RotationStatus, h.RotateTOTP, h.RetryTOTP, h.ExportTOTP, h.ExportTOTPRecovery, h.ImportTOTPRecovery} { recorder := httptest.NewRecorder() c, _ := gin.CreateTestContext(recorder) c.Request = httptest.NewRequest(http.MethodPost, "/accounts/42", strings.NewReader("{}")) diff --git a/backend/internal/repository/oauth429_weekly_only_integration_test.go b/backend/internal/repository/oauth429_weekly_only_integration_test.go index ca2f84ec6b7a..c551afd740d4 100644 --- a/backend/internal/repository/oauth429_weekly_only_integration_test.go +++ b/backend/internal/repository/oauth429_weekly_only_integration_test.go @@ -56,6 +56,7 @@ func TestOAuth429WeeklyOnlyRotationPreservesScheduling(t *testing.T) { for _, step := range [][3]string{{"running", "enrolling", ""}, {"enrolling", "prepared", "enc:fictional-new"}, {"prepared", "activating", ""}, {"activating", "verifying", ""}} { require.NoError(t, rotation.TransitionTOTPRotation(ctx, task.TaskID, "fictional-worker", step[0], step[1], step[2])) } + require.NoError(t, rotation.BindTOTPRotation(ctx, task.TaskID, "fictional-worker", "enc:fictional-new", "enc:fictional-new")) } require.NoError(t, rotation.FinishTOTPRotation(ctx, task.TaskID, "fictional-worker", success, "")) after, err := accounts.GetByID(ctx, a.ID) diff --git a/backend/internal/repository/openai_totp_recovery.go b/backend/internal/repository/openai_totp_recovery.go new file mode 100644 index 000000000000..17b634bced8d --- /dev/null +++ b/backend/internal/repository/openai_totp_recovery.go @@ -0,0 +1,29 @@ +package repository + +import ( + "context" + + infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" + "github.com/Wei-Shaw/sub2api/internal/service" +) + +// Recovery never updates a destination row, regardless of its version or a +// concurrent normal configuration save. Keep UpsertConfig's editing semantics. +func (r *openAIOAuthReauthRepository) InsertTOTPRecoveryConfig(ctx context.Context, config *service.OpenAIOAuthReauthStoredConfig) error { + result, err := r.db.ExecContext(ctx, `INSERT INTO openai_oauth_reauth_configs + (account_id,login_email,credential_mode,engine,proxy_source,proxy_id,password_ciphertext,totp_secret_ciphertext,otp_url_ciphertext) + VALUES ($1,$2,$3,$4,$5,$6,$7,$8,NULLIF($9,'')) ON CONFLICT (account_id) DO NOTHING`, + config.AccountID, config.LoginEmail, config.CredentialMode, config.Engine, config.ProxySource, config.ProxyID, + config.PasswordCiphertext, config.TOTPSecretCiphertext, config.OTPURLCiphertext) + if err != nil { + return err + } + n, err := result.RowsAffected() + if err != nil { + return err + } + if n != 1 { + return infraerrors.Conflict("TOTP_CONFIG_EXISTS", "Recovery import requires an unconfigured destination account") + } + return nil +} diff --git a/backend/internal/repository/openai_totp_recovery_integration_test.go b/backend/internal/repository/openai_totp_recovery_integration_test.go new file mode 100644 index 000000000000..503d30fc9854 --- /dev/null +++ b/backend/internal/repository/openai_totp_recovery_integration_test.go @@ -0,0 +1,86 @@ +//go:build integration + +package repository + +import ( + "os" + "testing" + "time" + + infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" + "github.com/Wei-Shaw/sub2api/internal/service" + "github.com/stretchr/testify/require" +) + +func TestTOTPRecoveryInsertNeverOverwritesExistingOrConcurrentConfig(t *testing.T) { + for _, concurrent := range []bool{false, true} { + name := "existing" + if concurrent { + name = "concurrent-insert" + } + t.Run(name, func(t *testing.T) { + ctx, repo, cfg := totpRotationFixture(t) + // Even a destination with the same zero version must be preserved. Recovery + // is insert-only, not a timestamp-dependent variant of ordinary editing. + _, err := integrationDB.ExecContext(ctx, `DELETE FROM openai_oauth_reauth_configs WHERE account_id=$1`, cfg.AccountID) + require.NoError(t, err) + loser := *cfg + loser.LoginEmail = "loser@example.test" + loser.PasswordCiphertext = "enc:loser-password" + loser.TOTPSecretCiphertext = "enc:loser-pair" + loser.UpdatedAt = time.Time{} + tx, err := integrationDB.BeginTx(ctx, nil) + require.NoError(t, err) + defer func() { _ = tx.Rollback() }() + var winnerPID int + require.NoError(t, tx.QueryRowContext(ctx, `SELECT pg_backend_pid()`).Scan(&winnerPID)) + _, err = tx.ExecContext(ctx, `INSERT INTO openai_oauth_reauth_configs(account_id,login_email,credential_mode,engine,proxy_source,password_ciphertext,totp_secret_ciphertext,otp_url_ciphertext,updated_at) + VALUES($1,'winner@example.test','password_totp','local_worker','account','enc:winner-password','enc:winner-pair','enc:untouched',$2)`, cfg.AccountID, time.Time{}) + require.NoError(t, err) + done := make(chan error, 1) + if concurrent { + go func() { done <- repo.InsertTOTPRecoveryConfig(ctx, &loser) }() + require.Eventually(t, func() bool { + var blocked bool + e := integrationDB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM pg_stat_activity WHERE $1=ANY(pg_blocking_pids(pid)))`, winnerPID).Scan(&blocked) + return e == nil && blocked + }, 5*time.Second, 10*time.Millisecond, "recovery insert must encounter the uncommitted winner") + require.NoError(t, tx.Commit()) + } else { + require.NoError(t, tx.Commit()) + done <- repo.InsertTOTPRecoveryConfig(ctx, &loser) + } + require.Equal(t, 409, infraerrors.Code(<-done), "recovery loser must conflict") + winner, err := repo.GetConfig(ctx, cfg.AccountID) + require.NoError(t, err) + require.Equal(t, &service.OpenAIOAuthReauthStoredConfig{AccountID: cfg.AccountID, LoginEmail: "winner@example.test", CredentialMode: "password_totp", Engine: "local_worker", ProxySource: "account", PasswordCiphertext: "enc:winner-password", TOTPSecretCiphertext: "enc:winner-pair", OTPURLCiphertext: "enc:untouched", UpdatedAt: time.Time{}}, winner) + winner.LoginEmail = "normal-save@example.test" + require.NoError(t, repo.UpsertConfig(ctx, winner), "ordinary explicit configuration edits retain their existing CAS semantics") + reloaded, err := repo.GetConfig(ctx, cfg.AccountID) + require.NoError(t, err) + require.Equal(t, "normal-save@example.test", reloaded.LoginEmail) + require.Equal(t, "enc:winner-pair", reloaded.TOTPSecretCiphertext) + }) + } +} + +func TestTOTPBindingMigrationCanReloadWithoutReplacingSnapshot(t *testing.T) { + ctx, repo, cfg := totpRotationFixture(t) + r, err := repo.CreateTOTPRotation(ctx, cfg.AccountID, "snapshot", cfg.UpdatedAt) + require.NoError(t, err) + migrationPath := os.Getenv("ISSUE881_MIGRATION_TEST_FILE") + if migrationPath == "" { + migrationPath = "../../migrations/382_openai_totp_binding.sql" + } + migration, err := os.ReadFile(migrationPath) + require.NoError(t, err) + for range 2 { + _, err = integrationDB.ExecContext(ctx, string(migration)) + require.NoError(t, err, "migration reload must be idempotent") + } + saved, err := NewOpenAIOAuthReauthRepository(integrationDB).(*openAIOAuthReauthRepository).GetTOTPRotationByTask(ctx, r.TaskID) + require.NoError(t, err) + require.Equal(t, cfg.LoginEmail, saved.LoginEmail) + require.Equal(t, cfg.TOTPSecretCiphertext, saved.PreviousCiphertext) + require.False(t, saved.BindingVerified) +} diff --git a/backend/internal/repository/openai_totp_rotation.go b/backend/internal/repository/openai_totp_rotation.go index 3a95d9309939..a32e26955ca1 100644 --- a/backend/internal/repository/openai_totp_rotation.go +++ b/backend/internal/repository/openai_totp_rotation.go @@ -12,12 +12,12 @@ import ( ) const rotationSelect = `SELECT task_id, account_id, state, action, worker_id, candidate_ciphertext, - config_updated_at, error_code, updated_at FROM openai_totp_rotations ` + config_updated_at, error_code, updated_at, previous_ciphertext, password_ciphertext, login_email, binding_verified FROM openai_totp_rotations ` func scanRotation(row openAIOAuthReauthRow) (*service.OpenAITOTPRotation, error) { var r service.OpenAITOTPRotation err := row.Scan(&r.TaskID, &r.AccountID, &r.State, &r.Action, &r.WorkerID, &r.CandidateCiphertext, - &r.ConfigUpdatedAt, &r.ErrorCode, &r.UpdatedAt) + &r.ConfigUpdatedAt, &r.ErrorCode, &r.UpdatedAt, &r.PreviousCiphertext, &r.PasswordCiphertext, &r.LoginEmail, &r.BindingVerified) if errors.Is(err, sql.ErrNoRows) { return nil, nil } @@ -55,6 +55,18 @@ func (r *openAIOAuthReauthRepository) createTOTPRotation(ctx context.Context, ac if !current.Equal(version) { return nil, infraerrors.Conflict("TOTP_CONFIG_CHANGED", "Login configuration changed; reload before retrying") } + // A deleted-account recovery must not turn reimport into another enrollment. + var unresolvedIdentity bool + err = tx.QueryRowContext(ctx, `SELECT EXISTS (SELECT 1 FROM openai_totp_rotations r + JOIN openai_oauth_reauth_configs c ON c.account_id=$1 + WHERE r.account_id<>$1 AND lower(r.login_email)=lower(c.login_email) + AND r.state NOT IN ('succeeded','failed'))`, accountID).Scan(&unresolvedIdentity) + if err != nil { + return nil, err + } + if unresolvedIdentity { + return nil, infraerrors.Conflict("TOTP_RECOVERY_UNRESOLVED", "Resolve the existing identity's 2FA recovery before another enrollment") + } if automatic { // The config row lock serializes this check with creation on every API // replica. Never replace an unresolved task, or rotate repeatedly on a @@ -77,8 +89,8 @@ func (r *openAIOAuthReauthRepository) createTOTPRotation(ctx context.Context, ac } return nil, err } - _, err = tx.ExecContext(ctx, `INSERT INTO openai_totp_rotations (task_id,account_id,config_updated_at,previous_ciphertext,password_ciphertext) - SELECT $1,account_id,$3,totp_secret_ciphertext,password_ciphertext FROM openai_oauth_reauth_configs WHERE account_id=$2`, taskID, accountID, version) + _, err = tx.ExecContext(ctx, `INSERT INTO openai_totp_rotations (task_id,account_id,config_updated_at,previous_ciphertext,password_ciphertext,login_email) + SELECT $1,account_id,$3,totp_secret_ciphertext,password_ciphertext,login_email FROM openai_oauth_reauth_configs WHERE account_id=$2`, taskID, accountID, version) if err != nil { return nil, err } @@ -94,9 +106,9 @@ func (r *openAIOAuthReauthRepository) ClaimTOTPRotation(ctx context.Context, wor } return scanRotation(r.db.QueryRowContext(ctx, `WITH next AS (SELECT task_id FROM openai_totp_rotations WHERE state='queued' ORDER BY task_id FOR UPDATE SKIP LOCKED LIMIT 1) - UPDATE openai_totp_rotations r SET state='running',worker_id=$1,updated_at=NOW(),error_code='' + UPDATE openai_totp_rotations r SET state='running',worker_id=$1,updated_at=NOW(),error_code='',binding_verified=FALSE FROM next WHERE r.task_id=next.task_id - RETURNING r.task_id,r.account_id,r.state,r.action,r.worker_id,r.candidate_ciphertext,r.config_updated_at,r.error_code,r.updated_at`, worker)) + RETURNING r.task_id,r.account_id,r.state,r.action,r.worker_id,r.candidate_ciphertext,r.config_updated_at,r.error_code,r.updated_at,r.previous_ciphertext,r.password_ciphertext,r.login_email,r.binding_verified`, worker)) } func (r *openAIOAuthReauthRepository) TransitionTOTPRotation(ctx context.Context, taskID int64, worker, from, to, candidate string) error { @@ -149,7 +161,7 @@ func (r *openAIOAuthReauthRepository) FinishTOTPRotation(ctx context.Context, ta } state := "uncertain" if success { - if rotation.State != "verifying" || !version.Equal(rotation.ConfigUpdatedAt) { + if rotation.State != "verifying" || !rotation.BindingVerified || !version.Equal(rotation.ConfigUpdatedAt) { return sql.ErrNoRows } if rotation.Action != "verify_old" && rotation.CandidateCiphertext == "" { @@ -163,8 +175,12 @@ func (r *openAIOAuthReauthRepository) FinishTOTPRotation(ctx context.Context, ta if err != nil { return err } - if success && rotation.Action != "verify_old" { - _, err = tx.ExecContext(ctx, `UPDATE openai_oauth_reauth_configs SET totp_secret_ciphertext=$2,updated_at=NOW() WHERE account_id=$1`, rotation.AccountID, rotation.CandidateCiphertext) + if success { + selected := rotation.CandidateCiphertext + if rotation.Action == "verify_old" { + selected = rotation.PreviousCiphertext + } + _, err = tx.ExecContext(ctx, `UPDATE openai_oauth_reauth_configs SET totp_secret_ciphertext=$2,updated_at=NOW() WHERE account_id=$1`, rotation.AccountID, selected) if err != nil { return err } @@ -199,3 +215,43 @@ func (r *openAIOAuthReauthRepository) expireTOTPRotations(ctx context.Context, a AND updated_at < NOW()-INTERVAL '15 minutes'`, accountID) return err } + +// Read immutable recovery snapshots even when the original account is soft-deleted. +func (r *openAIOAuthReauthRepository) GetTOTPRotationByTask(ctx context.Context, taskID int64) (*service.OpenAITOTPRotation, error) { + if err := r.expireTOTPRotations(ctx, 0); err != nil { + return nil, err + } + return scanRotation(r.db.QueryRowContext(ctx, rotationSelect+`WHERE task_id=$1`, taskID)) +} + +func (r *openAIOAuthReauthRepository) BindTOTPRotation(ctx context.Context, taskID int64, worker, expected, updated string) error { + return r.writeTOTPProvenance(ctx, `UPDATE openai_totp_rotations SET + candidate_ciphertext=CASE WHEN action='verify_old' THEN candidate_ciphertext ELSE $4 END, + previous_ciphertext=CASE WHEN action='verify_old' THEN $4 ELSE previous_ciphertext END, + binding_verified=TRUE,updated_at=NOW() + WHERE task_id=$1 AND worker_id=$2 AND state='verifying' + AND CASE WHEN action='verify_old' THEN previous_ciphertext ELSE candidate_ciphertext END=$3 + AND $3<>'' AND $4<>''`, taskID, worker, expected, updated) +} + +func (r *openAIOAuthReauthRepository) RestoreTOTPProvenance(ctx context.Context, taskID int64, expected, candidate string) error { + return r.writeTOTPProvenance(ctx, `UPDATE openai_totp_rotations SET candidate_ciphertext=$3, + binding_verified=FALSE,error_code='candidate_recovered',updated_at=NOW() + WHERE task_id=$1 AND state='uncertain' AND candidate_ciphertext=$2`, taskID, expected, candidate) +} + +func (r *openAIOAuthReauthRepository) writeTOTPProvenance(ctx context.Context, query string, args ...any) error { + tx, err := r.db.BeginTx(ctx, nil) + if err != nil { + return err + } + defer func() { _ = tx.Rollback() }() + if _, err = tx.ExecContext(ctx, "SET LOCAL synchronous_commit = on"); err != nil { + return err + } + result, err := tx.ExecContext(ctx, query, args...) + if err = requireOpenAIOAuthReauthTaskUpdate(result, err); err != nil { + return err + } + return tx.Commit() +} diff --git a/backend/internal/repository/openai_totp_rotation_integration_test.go b/backend/internal/repository/openai_totp_rotation_integration_test.go index 628ec1d189c0..ac61acec773a 100644 --- a/backend/internal/repository/openai_totp_rotation_integration_test.go +++ b/backend/internal/repository/openai_totp_rotation_integration_test.go @@ -138,6 +138,8 @@ func TestTOTPRotationAtomicActivationAndExclusion(t *testing.T) { unchanged, err := repo.GetConfig(ctx, cfg.AccountID) require.NoError(t, err) require.Equal(t, "enc:old", unchanged.TOTPSecretCiphertext) + require.Error(t, repo.FinishTOTPRotation(ctx, r.TaskID, "worker", true, ""), "unbound verification must not promote a candidate") + require.NoError(t, repo.BindTOTPRotation(ctx, r.TaskID, "worker", "enc:new", "enc:new")) // Force the effective-config write to fail after the rotation state UPDATE. _, err = integrationDB.ExecContext(ctx, fmt.Sprintf(`ALTER TABLE openai_oauth_reauth_configs ADD CONSTRAINT totp_test_write_failure CHECK(account_id <> %d OR totp_secret_ciphertext <> 'enc:new') NOT VALID`, cfg.AccountID)) require.NoError(t, err) @@ -194,6 +196,7 @@ func TestTOTPRotationExpirationPreservesCandidateAndFencesWorker(t *testing.T) { require.Equal(t, "verify_new", next.Action) require.Error(t, repo.TransitionTOTPRotation(ctx, r.TaskID, "new", "running", "enrolling", "")) require.NoError(t, repo.TransitionTOTPRotation(ctx, r.TaskID, "new", "running", "verifying", "")) + require.NoError(t, repo.BindTOTPRotation(ctx, r.TaskID, "new", "enc:new", "enc:new")) require.NoError(t, repo.FinishTOTPRotation(ctx, r.TaskID, "new", true, "")) } @@ -210,6 +213,7 @@ func TestTOTPRotationLocalJournalRecoveryAndOriginalVerification(t *testing.T) { _, err = repo.ClaimTOTPRotation(ctx, "recovery") require.NoError(t, err) require.NoError(t, repo.TransitionTOTPRotation(ctx, r.TaskID, "recovery", "running", "verifying", "")) + require.NoError(t, repo.BindTOTPRotation(ctx, r.TaskID, "recovery", "enc:old", "enc:old")) require.NoError(t, repo.FinishTOTPRotation(ctx, r.TaskID, "recovery", true, "")) current, err := repo.GetConfig(ctx, cfg.AccountID) require.NoError(t, err) @@ -241,3 +245,28 @@ func TestTOTPRotationConcurrentCreationHasSingleWinner(t *testing.T) { } require.Equal(t, 1, successes) } + +func TestTOTPBindingCASAndSoftDeletedRecoverySnapshot(t *testing.T) { + ctx, repo, cfg := totpRotationFixture(t) + r, err := repo.CreateTOTPRotation(ctx, cfg.AccountID, "snapshot", cfg.UpdatedAt) + require.NoError(t, err) + _, err = repo.ClaimTOTPRotation(ctx, "owner") + require.NoError(t, err) + for _, step := range [][3]string{{"running", "enrolling", ""}, {"enrolling", "prepared", "enc:paired"}, {"prepared", "activating", ""}, {"activating", "verifying", ""}} { + require.NoError(t, repo.TransitionTOTPRotation(ctx, r.TaskID, "owner", step[0], step[1], step[2])) + } + require.Error(t, repo.BindTOTPRotation(ctx, r.TaskID, "other", "enc:paired", "enc:bound")) + require.Error(t, repo.BindTOTPRotation(ctx, r.TaskID, "owner", "enc:stale", "enc:bound")) + require.NoError(t, repo.BindTOTPRotation(ctx, r.TaskID, "owner", "enc:paired", "enc:bound")) + require.NoError(t, repo.FinishTOTPRotation(ctx, r.TaskID, "owner", false, "verification_failed")) + _, err = integrationDB.ExecContext(ctx, `UPDATE accounts SET deleted_at=NOW() WHERE id=$1`, cfg.AccountID) + require.NoError(t, err) + saved, err := repo.GetTOTPRotationByTask(ctx, r.TaskID) + require.NoError(t, err) + require.Equal(t, "owner@example.test", saved.LoginEmail) + require.Equal(t, "enc:bound", saved.CandidateCiphertext) + require.Equal(t, "enc:password", saved.PasswordCiphertext) + require.Equal(t, "uncertain", saved.State) + require.NoError(t, repo.RestoreTOTPProvenance(ctx, r.TaskID, "enc:bound", "enc:recovered")) + require.Error(t, repo.RestoreTOTPProvenance(ctx, r.TaskID, "enc:bound", "enc:stale")) +} diff --git a/backend/internal/server/routes/admin.go b/backend/internal/server/routes/admin.go index b4f73aa266ac..200680d7664e 100644 --- a/backend/internal/server/routes/admin.go +++ b/backend/internal/server/routes/admin.go @@ -490,6 +490,8 @@ func registerAccountRoutes(admin *gin.RouterGroup, h *handler.Handlers, stepUpAu accounts.POST("/:id/totp-rotation", h.Admin.OpenAIOAuthReauth.RotateTOTP) accounts.POST("/:id/totp-rotation/verify", h.Admin.OpenAIOAuthReauth.RetryTOTP) accounts.POST("/:id/totp-export", h.Admin.OpenAIOAuthReauth.ExportTOTP) + accounts.POST("/totp-recovery-export", h.Admin.OpenAIOAuthReauth.ExportTOTPRecovery) + accounts.POST("/:id/totp-recovery-import", h.Admin.OpenAIOAuthReauth.ImportTOTPRecovery) accounts.PUT("/:id/openai-reauth/email", h.Admin.OpenAIOAuthReauth.SaveConfig) accounts.POST("/:id/openai-reauth", h.Admin.OpenAIOAuthReauth.CreateTask) } diff --git a/backend/internal/service/openai_oauth_reauth.go b/backend/internal/service/openai_oauth_reauth.go index 6edfeb2b6d3b..33a6b4c25601 100644 --- a/backend/internal/service/openai_oauth_reauth.go +++ b/backend/internal/service/openai_oauth_reauth.go @@ -504,6 +504,16 @@ func (s *OpenAIOAuthReauthService) SaveCredentialConfig(ctx context.Context, acc } } } + if existing != nil && !strings.EqualFold(existing.LoginEmail, email) && stored.TOTPSecretCiphertext != "" { + binding, err := s.decryptTOTP(stored.TOTPSecretCiphertext) + if err != nil { + return nil, err + } + stored.TOTPSecretCiphertext, err = s.encryptor.Encrypt(binding.Secret) + if err != nil { + return nil, infraerrors.InternalServer("TOTP_ENCRYPT_FAILED", "Could not preserve TOTP secret") + } + } if err := s.repo.UpsertConfig(ctx, stored); err != nil { if infraerrors.Code(err) == 409 { return nil, err @@ -548,11 +558,11 @@ func (s *OpenAIOAuthReauthService) configView(ctx context.Context, stored *OpenA } view.PasswordConfigured = password != "" if stored.TOTPSecretCiphertext != "" { - totp, decryptErr := s.decryptReauthSecret(stored.TOTPSecretCiphertext, "saved TOTP secret cannot be decrypted") + totp, decryptErr := s.decryptTOTP(stored.TOTPSecretCiphertext) if decryptErr != nil { return nil, decryptErr } - view.TOTPConfigured = totp != "" + view.TOTPConfigured = totp.Secret != "" } view.Configured = view.PasswordConfigured default: @@ -700,6 +710,7 @@ type OpenAIOAuthReauthClaim struct { ReloginHeaders map[string]string `json:"relogin_headers,omitempty"` Password string `json:"password,omitempty"` TOTPSecret string `json:"totp_secret,omitempty"` + TOTPFactorID string `json:"totp_factor_id,omitempty"` OTPURL string `json:"otp_url,omitempty"` AuthURL string `json:"auth_url,omitempty"` ProxyURL string `json:"proxy_url,omitempty"` @@ -890,7 +901,9 @@ func (s *OpenAIOAuthReauthService) ClaimTaskWithEngines(ctx context.Context, wor return nil, nil } if stored.TOTPSecretCiphertext != "" { - claim.TOTPSecret, err = s.decryptReauthSecret(stored.TOTPSecretCiphertext, "TOTP secret cannot be decrypted") + var binding openAITOTPBinding + binding, err = s.decryptTOTP(stored.TOTPSecretCiphertext) + claim.TOTPSecret, claim.TOTPFactorID = binding.Secret, binding.FactorID if err != nil { _ = s.repo.MarkFailed(ctx, record.ID, workerID, "TOTP secret cannot be decrypted") return nil, nil @@ -1311,7 +1324,7 @@ func sanitizeReauthError(raw string) string { return code } switch msg { - case "password/TOTP protocol failed (exit 1)", "proxy_unavailable", "password_rejected", "totp_rejected", "upstream_rejected", "worker_timeout": + case "password/TOTP protocol failed (exit 1)", "proxy_unavailable", "password_rejected", "totp_rejected", "upstream_rejected", "worker_timeout", "factor_binding_runtime_required": return msg } return "re-login failed; provider details omitted" diff --git a/backend/internal/service/openai_session_export.go b/backend/internal/service/openai_session_export.go index 775f7a9979b2..5b1916330aaf 100644 --- a/backend/internal/service/openai_session_export.go +++ b/backend/internal/service/openai_session_export.go @@ -63,6 +63,7 @@ type OpenAISessionExportClaim struct { LoginEmail string `json:"login_email"` Password string `json:"password"` TOTPSecret string `json:"totp_secret"` + TOTPFactorID string `json:"totp_factor_id,omitempty"` ProxyURL string `json:"proxy_url,omitempty"` ExpectedUserID string `json:"expected_user_id,omitempty"` } @@ -242,8 +243,8 @@ func (s *OpenAIOAuthReauthService) ClaimSessionExportTask(ctx context.Context, w _ = s.sessionExportRepo.FinishSessionExportTask(ctx, record.ID, workerID, OpenAISessionExportStatusFailed, "password_unavailable") return nil, nil } - totpSecret, err := s.decryptReauthSecret(config.TOTPSecretCiphertext, "TOTP secret cannot be decrypted") - if err != nil || totpSecret == "" { + totpSecret, err := s.decryptTOTP(config.TOTPSecretCiphertext) + if err != nil || totpSecret.Secret == "" { _ = s.sessionExportRepo.FinishSessionExportTask(ctx, record.ID, workerID, OpenAISessionExportStatusFailed, "totp_unavailable") return nil, nil } @@ -254,7 +255,7 @@ func (s *OpenAIOAuthReauthService) ClaimSessionExportTask(ctx context.Context, w } expectedUserID := strings.TrimSpace(account.GetCredential("chatgpt_user_id")) s.workerLastSeen.Store(time.Now().UnixNano()) - return &OpenAISessionExportClaim{Operation: OpenAISessionExportOperation, TaskID: record.ID, AccountID: record.AccountID, LoginEmail: config.LoginEmail, Password: password, TOTPSecret: totpSecret, ProxyURL: proxyURL, ExpectedUserID: expectedUserID}, nil + return &OpenAISessionExportClaim{Operation: OpenAISessionExportOperation, TaskID: record.ID, AccountID: record.AccountID, LoginEmail: config.LoginEmail, Password: password, TOTPSecret: totpSecret.Secret, TOTPFactorID: totpSecret.FactorID, ProxyURL: proxyURL, ExpectedUserID: expectedUserID}, nil } } return nil, nil diff --git a/backend/internal/service/openai_totp_binding.go b/backend/internal/service/openai_totp_binding.go new file mode 100644 index 000000000000..4f0856769362 --- /dev/null +++ b/backend/internal/service/openai_totp_binding.go @@ -0,0 +1,203 @@ +package service + +import ( + "context" + "encoding/json" + "strings" + + infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" +) + +// Secret and provenance share one authenticated ciphertext and one database write. +// Legacy plaintext-inside-ciphertext records remain readable as unbound secrets. +type openAITOTPBinding struct { + Version int `json:"v"` + Secret string `json:"secret"` + FactorID string `json:"factor_id,omitempty"` + EnrollmentSessionID string `json:"enrollment_session_id,omitempty"` + RecoveryTaskID int64 `json:"recovery_task_id,omitempty"` +} + +func validTOTPFactorID(id string) bool { + if len(id) < 1 || len(id) > 256 { + return false + } + for _, c := range id { + if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' || c == '-') { + return false + } + } + return true +} +func (s *OpenAIOAuthReauthService) decryptTOTP(ciphertext string) (openAITOTPBinding, error) { + raw, err := s.decryptReauthSecret(ciphertext, "TOTP recovery record cannot be decrypted") + if err != nil { + return openAITOTPBinding{}, err + } + b := openAITOTPBinding{Secret: raw} + if strings.HasPrefix(strings.TrimSpace(raw), "{") { + if json.Unmarshal([]byte(raw), &b) != nil || b.Version != 1 || b.Secret == "" || (b.FactorID != "" && !validTOTPFactorID(b.FactorID)) { + return openAITOTPBinding{}, infraerrors.BadRequest("TOTP_BINDING_INVALID", "Invalid saved TOTP binding") + } + } + return b, nil +} +func (s *OpenAIOAuthReauthService) encryptTOTP(b openAITOTPBinding) (string, error) { + if err := s.ensureDurableEncryption(); err != nil { + return "", err + } + secret, err := normalizeRotationSecret(b.Secret) + if err != nil { + return "", err + } + if b.FactorID != "" && !validTOTPFactorID(b.FactorID) || len(b.EnrollmentSessionID) > 512 { + return "", infraerrors.BadRequest("TOTP_BINDING_INVALID", "Invalid TOTP provenance") + } + b.Version, b.Secret = 1, secret + raw, err := json.Marshal(b) + if err != nil { + return "", err + } + ciphertext, err := s.encryptor.Encrypt(string(raw)) + if err != nil { + return "", infraerrors.InternalServer("TOTP_ENCRYPT_FAILED", "Could not preserve TOTP provenance") + } + return ciphertext, nil +} + +type openAITOTPBindingRepository interface { + InsertTOTPRecoveryConfig(context.Context, *OpenAIOAuthReauthStoredConfig) error + GetTOTPRotationByTask(context.Context, int64) (*OpenAITOTPRotation, error) + BindTOTPRotation(context.Context, int64, string, string, string) error + RestoreTOTPProvenance(context.Context, int64, string, string) error +} + +func (s *OpenAIOAuthReauthService) bindingRepo() (openAITOTPBindingRepository, error) { + repo, err := s.rotationRepo() + if err != nil { + return nil, err + } + b, ok := repo.(openAITOTPBindingRepository) + if !ok { + return nil, infraerrors.ServiceUnavailable("TOTP_UNAVAILABLE", "TOTP binding storage is unavailable") + } + return b, nil +} +func (s *OpenAIOAuthReauthService) bindTOTPRotation(ctx context.Context, task int64, worker, secret, factor string) error { + repo, err := s.bindingRepo() + if err != nil { + return err + } + if !validTOTPFactorID(factor) { + return infraerrors.BadRequest("TOTP_BINDING_INVALID", "Verified factor ID is required") + } + r, err := repo.GetTOTPRotationByTask(ctx, task) + if err != nil || r == nil || r.WorkerID != worker || r.State != "verifying" { + return rotationError() + } + ciphertext := r.CandidateCiphertext + if r.Action == "verify_old" { + ciphertext = r.PreviousCiphertext + } + b, err := s.decryptTOTP(ciphertext) + if err != nil { + return err + } + secret, err = normalizeRotationSecret(secret) + if err != nil { + return err + } + old, err := normalizeRotationSecret(b.Secret) + if err != nil || secret != old || (b.FactorID != "" && b.FactorID != factor) { + return rotationError() + } + b.FactorID = factor + updated, err := s.encryptTOTP(b) + if err != nil { + return err + } + if err = repo.BindTOTPRotation(ctx, task, worker, ciphertext, updated); err != nil { + return rotationError() + } + return nil +} + +// Task-addressed recovery works after a soft delete. Snapshot identity is checked +// explicitly; ordinary account APIs continue to reject deleted accounts. +func (s *OpenAIOAuthReauthService) ExportTOTPRecovery(ctx context.Context, task int64, email, source string, includePassword bool) (*OpenAITOTPExport, error) { + repo, err := s.bindingRepo() + if err != nil { + return nil, err + } + r, err := repo.GetTOTPRotationByTask(ctx, task) + if err != nil || r == nil || r.State != "uncertain" || r.LoginEmail == "" || !strings.EqualFold(strings.TrimSpace(email), r.LoginEmail) { + return nil, rotationError() + } + ciphertext := r.CandidateCiphertext + if source == "previous" { + ciphertext = r.PreviousCiphertext + } else if source != "candidate" { + return nil, rotationError() + } + if ciphertext == "" { + return nil, rotationError() + } + b, err := s.decryptTOTP(ciphertext) + if err != nil { + return nil, err + } + out, err := totpExport(r.LoginEmail, b, r.State, source) + if err != nil { + return nil, err + } + if includePassword { + out.Password, err = s.decryptReauthSecret(r.PasswordCiphertext, "Recovery password cannot be decrypted") + } + return out, err +} + +// Import config only into an explicitly selected, matching, unconfigured account. +// It neither activates a factor nor claims that a candidate has been verified. +func (s *OpenAIOAuthReauthService) ImportTOTPRecovery(ctx context.Context, id, task int64, email, source string) error { + out, err := s.ExportTOTPRecovery(ctx, task, email, source, true) + if err != nil { + return err + } + account, err := s.accountFor(ctx, id) + if err != nil { + return err + } + actual, _ := account.Credentials["email"].(string) + if actual == "" { + actual, _ = account.Extra["email"].(string) + } + if actual == "" { + actual, _ = account.Extra["email_address"].(string) + } + if !strings.EqualFold(strings.TrimSpace(actual), out.Email) { + return infraerrors.BadRequest("TOTP_IDENTITY_MISMATCH", "Recovery identity must match the destination account") + } + cfg, err := s.repo.GetConfig(ctx, id) + if err != nil { + return err + } + if cfg != nil { + return infraerrors.Conflict("TOTP_CONFIG_EXISTS", "Recovery import requires an unconfigured destination account") + } + if out.Password == "" { + return infraerrors.BadRequest("TOTP_CONFIG_REQUIRED", "Recovery snapshot has no saved password") + } + password, err := s.encryptor.Encrypt(out.Password) + if err != nil { + return infraerrors.InternalServer("TOTP_ENCRYPT_FAILED", "Could not preserve recovery credentials") + } + ciphertext, err := s.encryptTOTP(openAITOTPBinding{Secret: out.Secret, FactorID: out.FactorID, RecoveryTaskID: task}) + if err != nil { + return err + } + repo, err := s.bindingRepo() + if err != nil { + return err + } + return repo.InsertTOTPRecoveryConfig(ctx, &OpenAIOAuthReauthStoredConfig{AccountID: id, LoginEmail: out.Email, CredentialMode: OpenAIOAuthReauthModePasswordTOTP, Engine: OpenAIOAuthReauthEngineLocal, ProxySource: OpenAIOAuthReauthProxySourceAccount, PasswordCiphertext: password, TOTPSecretCiphertext: ciphertext}) +} diff --git a/backend/internal/service/openai_totp_rotation.go b/backend/internal/service/openai_totp_rotation.go index c2febaca4579..ce3f5a107536 100644 --- a/backend/internal/service/openai_totp_rotation.go +++ b/backend/internal/service/openai_totp_rotation.go @@ -23,6 +23,11 @@ type OpenAITOTPRotation struct { HasCandidate bool `json:"has_candidate"` WorkerID string `json:"-"` CandidateCiphertext string `json:"-"` + PreviousCiphertext string `json:"-"` + PasswordCiphertext string `json:"-"` + LoginEmail string `json:"-"` + BindingVerified bool `json:"-"` + Outcome string `json:"outcome,omitempty"` ConfigUpdatedAt time.Time `json:"-"` } @@ -55,6 +60,9 @@ func safeRotation(r *OpenAITOTPRotation) *OpenAITOTPRotation { } copy := *r copy.HasCandidate = copy.CandidateCiphertext != "" + if copy.State == "succeeded" && copy.BindingVerified { + copy.Outcome = "selected_factor_verified_no_revocation" + } return © } func normalizeRotationSecret(raw string) (string, error) { @@ -133,6 +141,13 @@ func (s *OpenAIOAuthReauthService) createTOTPRotation(ctx context.Context, id in if err != nil { return nil, err } + binding, err := s.decryptTOTP(cfg.TOTPSecretCiphertext) + if err != nil { + return nil, err + } + if binding.RecoveryTaskID != 0 { + return nil, infraerrors.Conflict("TOTP_RECOVERY_UNRESOLVED", "Imported recovery credentials allow login only; resolve the original factor set before enrolling again") + } // Re-read the account instead of trusting the gateway's scheduling snapshot. if automatic && (!openAITOTPRotateOn429Enabled(account) || !account.IsActive()) { return nil, nil @@ -168,15 +183,18 @@ func (s *OpenAIOAuthReauthService) createTOTPRotation(ctx context.Context, id in } type OpenAITOTPRotationClaim struct { - Operation string `json:"operation"` - TaskID int64 `json:"task_id"` - AccountID int64 `json:"account_id"` - Action string `json:"action"` - LoginEmail string `json:"login_email"` - Password string `json:"password"` - TOTPSecret string `json:"totp_secret"` - Candidate string `json:"candidate_secret,omitempty"` - ProxyURL string `json:"proxy_url,omitempty"` + Operation string `json:"operation"` + TaskID int64 `json:"task_id"` + AccountID int64 `json:"account_id"` + Action string `json:"action"` + LoginEmail string `json:"login_email"` + Password string `json:"password"` + TOTPSecret string `json:"totp_secret"` + TOTPFactorID string `json:"totp_factor_id,omitempty"` + CandidateFactorID string `json:"candidate_factor_id,omitempty"` + CandidateEnrollmentSessionID string `json:"candidate_enrollment_session_id,omitempty"` + Candidate string `json:"candidate_secret,omitempty"` + ProxyURL string `json:"proxy_url,omitempty"` } func (s *OpenAIOAuthReauthService) ClaimTOTPRotation(ctx context.Context, worker string) (*OpenAITOTPRotationClaim, error) { @@ -207,10 +225,14 @@ func (s *OpenAIOAuthReauthService) ClaimTOTPRotation(ctx context.Context, worker claim.Password, err = s.decryptReauthSecret(cfg.PasswordCiphertext, "Password cannot be decrypted") } if err == nil { - claim.TOTPSecret, err = s.decryptReauthSecret(cfg.TOTPSecretCiphertext, "TOTP cannot be decrypted") + var b openAITOTPBinding + b, err = s.decryptTOTP(record.PreviousCiphertext) + claim.TOTPSecret, claim.TOTPFactorID = b.Secret, b.FactorID } if err == nil && record.CandidateCiphertext != "" { - claim.Candidate, err = s.decryptReauthSecret(record.CandidateCiphertext, "Candidate TOTP cannot be decrypted") + var b openAITOTPBinding + b, err = s.decryptTOTP(record.CandidateCiphertext) + claim.Candidate, claim.CandidateFactorID, claim.CandidateEnrollmentSessionID = b.Secret, b.FactorID, b.EnrollmentSessionID } if err == nil { source, sourceErr := normalizeReauthProxySource(cfg.ProxySource, cfg.ProxyID) @@ -247,13 +269,23 @@ func (s *OpenAIOAuthReauthService) ClaimTOTPRotation(ctx context.Context, worker } // Phase is an acknowledged write barrier. Enrollment/activation must not run until it succeeds. -func (s *OpenAIOAuthReauthService) TOTPRotationPhase(ctx context.Context, task int64, worker, phase, secret string) error { +func (s *OpenAIOAuthReauthService) TOTPRotationPhase(ctx context.Context, task int64, worker, phase, secret string, provenance ...string) error { repo, err := s.rotationRepo() if err != nil { return err } from := "" candidate := "" + factor, session := "", "" + if len(provenance) > 0 { + factor = provenance[0] + } + if len(provenance) > 1 { + session = provenance[1] + } + if phase == "bound" { + return s.bindTOTPRotation(ctx, task, worker, secret, factor) + } switch phase { case "enrolling": from = "running" @@ -266,7 +298,7 @@ func (s *OpenAIOAuthReauthService) TOTPRotationPhase(ctx context.Context, task i if err != nil { return err } - candidate, err = s.encryptor.Encrypt(secret) + candidate, err = s.encryptTOTP(openAITOTPBinding{Secret: secret, FactorID: factor, EnrollmentSessionID: session}) if err != nil { return infraerrors.InternalServer("TOTP_ENCRYPT_FAILED", "Could not preserve candidate TOTP") } @@ -295,7 +327,7 @@ func (s *OpenAIOAuthReauthService) FinishTOTPRotation(ctx context.Context, task } else { switch code { case "login_failed", "identity_mismatch", "enrollment_failed", "activation_unconfirmed", "verification_failed", "worker_failed", - "new_accepted_old_valid", "old_accepted_new_valid": + "new_accepted_old_valid", "old_accepted_new_valid", "factor_binding_unconfirmed": default: code = "worker_failed" } @@ -330,6 +362,7 @@ type OpenAITOTPExport struct { OTPAuthURI string `json:"otpauth_uri"` State string `json:"state"` Source string `json:"source"` + FactorID string `json:"factor_id,omitempty"` } func (s *OpenAIOAuthReauthService) ExportTOTP(ctx context.Context, id int64, source string, includePassword bool) (*OpenAITOTPExport, error) { @@ -364,20 +397,18 @@ func (s *OpenAIOAuthReauthService) ExportTOTP(ctx context.Context, id int64, sou if rotation == nil || rotation.State != "uncertain" { return nil, rotationError() } + ciphertext = rotation.PreviousCiphertext default: return nil, infraerrors.BadRequest("TOTP_EXPORT_INVALID", "Invalid export source") } - secret, err := s.decryptReauthSecret(ciphertext, "TOTP cannot be decrypted") + binding, err := s.decryptTOTP(ciphertext) if err != nil { return nil, err } - secret, err = normalizeRotationSecret(secret) + result, err := totpExport(cfg.LoginEmail, binding, state, source) if err != nil { return nil, err } - result := &OpenAITOTPExport{Email: cfg.LoginEmail, Secret: secret, State: state, Source: source} - q := url.Values{"secret": {secret}, "issuer": {"OpenAI"}, "algorithm": {"SHA1"}, "digits": {"6"}, "period": {"30"}} - result.OTPAuthURI = "otpauth://totp/" + url.PathEscape("OpenAI:"+cfg.LoginEmail) + "?" + q.Encode() if includePassword { result.Password, err = s.decryptReauthSecret(cfg.PasswordCiphertext, "Password cannot be decrypted") if err != nil { @@ -387,23 +418,55 @@ func (s *OpenAIOAuthReauthService) ExportTOTP(ctx context.Context, id int64, sou return result, nil } -func (s *OpenAIOAuthReauthService) RecoverTOTPCandidate(ctx context.Context, task int64, secret string) error { - repo, err := s.rotationRepo() +func totpExport(email string, binding openAITOTPBinding, state, source string) (*OpenAITOTPExport, error) { + secret, err := normalizeRotationSecret(binding.Secret) if err != nil { - return err + return nil, err } - if err = s.ensureDurableEncryption(); err != nil { + result := &OpenAITOTPExport{Email: email, Secret: secret, FactorID: binding.FactorID, State: state, Source: source} + q := url.Values{"secret": {secret}, "issuer": {"OpenAI"}, "algorithm": {"SHA1"}, "digits": {"6"}, "period": {"30"}} + result.OTPAuthURI = "otpauth://totp/" + url.PathEscape("OpenAI:"+email) + "?" + q.Encode() + return result, nil +} + +func (s *OpenAIOAuthReauthService) RecoverTOTPCandidate(ctx context.Context, task int64, secret string, provenance ...string) error { + repo, err := s.bindingRepo() + if err != nil { return err } + r, err := repo.GetTOTPRotationByTask(ctx, task) + if err != nil || r == nil || r.State != "uncertain" { + return rotationError() + } + factor, session := "", "" + if len(provenance) > 0 { + factor = provenance[0] + } + if len(provenance) > 1 { + session = provenance[1] + } secret, err = normalizeRotationSecret(secret) if err != nil { return err } - candidate, err := s.encryptor.Encrypt(secret) + if r.CandidateCiphertext != "" { + old, e := s.decryptTOTP(r.CandidateCiphertext) + if e != nil { + return e + } + normalized, e := normalizeRotationSecret(old.Secret) + if e != nil || normalized != secret || old.FactorID != "" && old.FactorID != factor || old.EnrollmentSessionID != "" && old.EnrollmentSessionID != session { + return rotationError() + } + if old.FactorID == factor && old.EnrollmentSessionID == session { + return nil + } + } + candidate, err := s.encryptTOTP(openAITOTPBinding{Secret: secret, FactorID: factor, EnrollmentSessionID: session}) if err != nil { - return infraerrors.InternalServer("TOTP_ENCRYPT_FAILED", "Could not preserve candidate TOTP") + return err } - if err = repo.RecoverTOTPCandidate(ctx, task, candidate); err != nil { + if err = repo.RestoreTOTPProvenance(ctx, task, r.CandidateCiphertext, candidate); err != nil { return rotationError() } return nil diff --git a/backend/internal/service/openai_totp_rotation_test.go b/backend/internal/service/openai_totp_rotation_test.go index a89a4dfe1e1b..7c1b76dfc3c0 100644 --- a/backend/internal/service/openai_totp_rotation_test.go +++ b/backend/internal/service/openai_totp_rotation_test.go @@ -44,7 +44,7 @@ func newRotationService(t *testing.T) (*OpenAIOAuthReauthService, *rotationTestR t.Helper() svc, _, repo, _, _, _ := newReauthTestService("acct-1") repo.config = &OpenAIOAuthReauthStoredConfig{AccountID: 42, LoginEmail: "user@example.com", CredentialMode: "password_totp", ProxySource: "account", PasswordCiphertext: "encrypted:secret-password", TOTPSecretCiphertext: "encrypted:JBSWY3DPEHPK3PXP"} - wrapped := &rotationTestRepo{reauthTestRepo: repo, rotation: &OpenAITOTPRotation{TaskID: 7, AccountID: 42, State: "uncertain", WorkerID: "worker-private", CandidateCiphertext: "encrypted:GEZDGNBVGY3TQOJQ"}} + wrapped := &rotationTestRepo{reauthTestRepo: repo, rotation: &OpenAITOTPRotation{TaskID: 7, AccountID: 42, State: "uncertain", WorkerID: "worker-private", CandidateCiphertext: "encrypted:GEZDGNBVGY3TQOJQ", PreviousCiphertext: repo.config.TOTPSecretCiphertext, PasswordCiphertext: repo.config.PasswordCiphertext, LoginEmail: repo.config.LoginEmail}} svc.repo = wrapped return svc, wrapped } @@ -82,7 +82,9 @@ func TestTOTPRotationExportSeparatesCurrentAndRecovery(t *testing.T) { func TestTOTPRotationCandidateEncryptedBeforeRepository(t *testing.T) { svc, repo := newRotationService(t) require.NoError(t, svc.TOTPRotationPhase(context.Background(), 7, "worker", "prepared", "GEZDGNBVGY3TQOJQ")) - require.Equal(t, "encrypted:GEZDGNBVGY3TQOJQ", repo.candidate) + binding, err := svc.decryptTOTP(repo.candidate) + require.NoError(t, err) + require.Equal(t, "GEZDGNBVGY3TQOJQ", binding.Secret) repo.candidate = "" svc.encryptionKeyConfigured = false require.Error(t, svc.TOTPRotationPhase(context.Background(), 7, "worker", "prepared", "GEZDGNBVGY3TQOJQ")) @@ -133,3 +135,110 @@ func TestTOTPRotationFinishPreservesSafeMultiFactorDiagnostics(t *testing.T) { require.NoError(t, svc.FinishTOTPRotation(context.Background(), 7, "worker", false, "untrusted-secret-body")) require.Equal(t, "worker_failed", repo.finishCode) } + +func (r *rotationTestRepo) GetTOTPRotationByTask(context.Context, int64) (*OpenAITOTPRotation, error) { + return r.rotation, nil +} +func (r *rotationTestRepo) BindTOTPRotation(_ context.Context, _ int64, _ string, expected, updated string) error { + if r.rotation.Action == "verify_old" { + if r.rotation.PreviousCiphertext != expected { + return errors.New("CAS") + } + r.rotation.PreviousCiphertext = updated + } else { + if r.rotation.CandidateCiphertext != expected { + return errors.New("CAS") + } + r.rotation.CandidateCiphertext = updated + } + r.rotation.BindingVerified = true + return nil +} +func (r *rotationTestRepo) RestoreTOTPProvenance(_ context.Context, _ int64, expected, updated string) error { + if r.rotation.CandidateCiphertext != expected { + return errors.New("CAS") + } + r.rotation.CandidateCiphertext = updated + return nil +} + +func TestTOTPCandidateBindingPreservesSecretAndRejectsWrongFactor(t *testing.T) { + svc, repo := newRotationService(t) + ctx := context.Background() + require.NoError(t, svc.TOTPRotationPhase(ctx, 7, "worker-private", "prepared", "GEZDGNBVGY3TQOJQ", "F2", "enrollment")) + repo.rotation.CandidateCiphertext = repo.candidate + repo.rotation.State = "verifying" + require.Error(t, svc.TOTPRotationPhase(ctx, 7, "worker-private", "bound", "GEZDGNBVGY3TQOJQ", "F1")) + require.False(t, repo.rotation.BindingVerified) + require.NoError(t, svc.TOTPRotationPhase(ctx, 7, "worker-private", "bound", "GEZDGNBVGY3TQOJQ", "F2")) + b, err := svc.decryptTOTP(repo.rotation.CandidateCiphertext) + require.NoError(t, err) + require.Equal(t, "F2", b.FactorID) + require.Equal(t, "enrollment", b.EnrollmentSessionID) + require.True(t, repo.rotation.BindingVerified) + require.Error(t, svc.TOTPRotationPhase(ctx, 7, "stale-worker", "bound", b.Secret, "F2")) +} +func TestTOTPJournalRecoveryIsIdempotentAndCannotReplaceProvenance(t *testing.T) { + svc, repo := newRotationService(t) + ctx := context.Background() + require.NoError(t, svc.RecoverTOTPCandidate(ctx, 7, "GEZDGNBVGY3TQOJQ", "F2", "session")) + saved := repo.rotation.CandidateCiphertext + require.NoError(t, svc.RecoverTOTPCandidate(ctx, 7, "GEZDGNBVGY3TQOJQ", "F2", "session")) + require.Equal(t, saved, repo.rotation.CandidateCiphertext) + for _, args := range [][3]string{{"JBSWY3DPEHPK3PXP", "F2", "session"}, {"GEZDGNBVGY3TQOJQ", "F1", "session"}, {"GEZDGNBVGY3TQOJQ", "F2", "different"}} { + require.Error(t, svc.RecoverTOTPCandidate(ctx, 7, args[0], args[1], args[2])) + } + require.Equal(t, saved, repo.rotation.CandidateCiphertext) +} +func TestTOTPRecoveryExportAfterAccountDeletionUsesTaskSnapshot(t *testing.T) { + svc, repo := newRotationService(t) + ctx := context.Background() + svc.accounts.(*reauthTestAccountReader).account = nil + out, err := svc.ExportTOTPRecovery(ctx, 7, "USER@example.com", "candidate", true) + require.NoError(t, err) + require.Equal(t, "GEZDGNBVGY3TQOJQ", out.Secret) + require.Equal(t, "secret-password", out.Password) + _, err = svc.ExportTOTPRecovery(ctx, 7, "other@example.com", "candidate", true) + require.Error(t, err) + repo.rotation.State = "succeeded" + _, err = svc.ExportTOTPRecovery(ctx, 7, "user@example.com", "candidate", true) + require.Error(t, err) +} +func TestTOTPRecoveryImportBindsMatchingUnconfiguredAccountWithoutEnrollment(t *testing.T) { + svc, repo := newRotationService(t) + ctx := context.Background() + require.NoError(t, svc.RecoverTOTPCandidate(ctx, 7, "GEZDGNBVGY3TQOJQ", "F2", "session")) + repo.config = nil + reader := svc.accounts.(*reauthTestAccountReader) + reader.account.Credentials["email"] = "wrong@example.com" + require.Error(t, svc.ImportTOTPRecovery(ctx, 42, 7, "user@example.com", "candidate")) + reader.account.Credentials["email"] = "user@example.com" + require.NoError(t, svc.ImportTOTPRecovery(ctx, 42, 7, "user@example.com", "candidate")) + b, err := svc.decryptTOTP(repo.config.TOTPSecretCiphertext) + require.NoError(t, err) + require.Equal(t, "F2", b.FactorID) + require.EqualValues(t, 7, b.RecoveryTaskID) + require.Equal(t, "local_worker", repo.config.Engine) + require.Equal(t, "uncertain", repo.rotation.State) + require.Error(t, svc.ImportTOTPRecovery(ctx, 42, 7, "user@example.com", "candidate")) + _, err = svc.CreateTOTPRotation(ctx, 42) + require.ErrorContains(t, err, "allow login only") +} +func TestTOTPLegacyAndBoundClaimCompatibility(t *testing.T) { + svc, repo := newRotationService(t) + binding, err := svc.encryptTOTP(openAITOTPBinding{Secret: "JBSWY3DPEHPK3PXP", FactorID: "F1"}) + require.NoError(t, err) + repo.rotation.PreviousCiphertext = binding + claim, err := svc.ClaimTOTPRotation(context.Background(), "worker") + require.NoError(t, err) + require.Equal(t, "JBSWY3DPEHPK3PXP", claim.TOTPSecret) + require.Equal(t, "F1", claim.TOTPFactorID) + require.Empty(t, claim.CandidateFactorID) +} + +func (r *rotationTestRepo) InsertTOTPRecoveryConfig(ctx context.Context, cfg *OpenAIOAuthReauthStoredConfig) error { + if r.config != nil { + return errors.New("destination exists") + } + return r.UpsertConfig(ctx, cfg) +} diff --git a/backend/migrations/382_openai_totp_binding.sql b/backend/migrations/382_openai_totp_binding.sql new file mode 100644 index 000000000000..8781ddb9789c --- /dev/null +++ b/backend/migrations/382_openai_totp_binding.sql @@ -0,0 +1,7 @@ +-- Binding metadata is encrypted together with the secret in existing ciphertext +-- columns. This flag records a fresh verification for this claimed task only. +ALTER TABLE openai_totp_rotations + ADD COLUMN IF NOT EXISTS binding_verified BOOLEAN NOT NULL DEFAULT FALSE, + ADD COLUMN IF NOT EXISTS login_email TEXT NOT NULL DEFAULT ''; +UPDATE openai_totp_rotations r SET login_email=c.login_email +FROM openai_oauth_reauth_configs c WHERE c.account_id=r.account_id AND r.login_email=''; diff --git a/docs/openai-totp-factor-recovery.md b/docs/openai-totp-factor-recovery.md index 4f81df10bede..e98df46547fe 100644 --- a/docs/openai-totp-factor-recovery.md +++ b/docs/openai-totp-factor-recovery.md @@ -1,36 +1,84 @@ -# 多 TOTP 验证器恢复边界 +# 多 TOTP 验证器绑定与恢复 -Refs #881。本页描述首个诊断修复检查点,尚未完成 factor 绑定与恢复方案。 +Refs #881。真实授权、登录和 2FA 验收 **NOT EXECUTED**,需要另行明确授权。 +本地托管 Worker 的修复不能证明外部 Session Studio 已修复。 -本地托管 Worker 的 enroll/activate 流程新增验证器,没有撤销旧验证器。 -新旧密钥都被登录协议接受时,任务保持 `uncertain` 并保留候选密钥, -普通登录配置不会自动切换。状态 API 的 `error_code` 区分以下结果: +## 登录和新增验证器 -| 错误码 | 已观察到的结果 | 未证明的事项 | -| --- | --- | --- | -| `new_accepted_old_valid` | 本次先验证候选密钥成功,随后旧密钥也被接受 | 尚无持久化 factor 绑定,未撤销旧验证器 | -| `old_accepted_new_valid` | 恢复检查先接受旧密钥,随后候选密钥也被接受 | 尚无持久化 factor 绑定,未撤销候选验证器 | -| `verification_failed` | 验证未获得足够成功证据 | 不能据此断言候选无效或旧验证器已撤销 | +托管 runtime 在构建时对 `poxiao33/toSub2` 固定提交 +`8548397e89bf80e508eda64a87e0d556d43abc84` 应用本仓库的 +`tools/reauth-runtime/totp-factor-binding.patch`。应用前校验原文件 SHA256, +使用 zero-fuzz patch;源码漂移会中止构建。未修改或发布上游仓库。 -这些诊断不是成功替换或撤销的证明。不要再次 enroll 来消除不确定状态, -不要删除唯一可用验证器。已有账号可通过现有管理员恢复导出接口获取候选 -或旧凭据;导出只提供恢复材料,不证明该密钥可完成登录。妥善保护导出材料。 -旧账号删除后的任务访问和重新导入路径仍待实现,不能把重新添加账号当作 -清除原任务或重新 enroll 的依据。 +保存了 factor ID 的登录只挑战该 ID,ID 不在返回列表中时立即停止。 +旧记录没有绑定时,最多匹配两个不同 TOTP factor,各挑战一次;超过两个时 +要求先取得明确绑定。只有明确的 400/401 OTP 拒绝允许尝试下一个 factor。 +429、网络错误、非 OTP 错误、含糊响应或继续要求 MFA 的 HTTP 200 都停止。 +不会靠重复 OTP、无限挑战或删除验证器恢复。factor ID 当前接受 1–256 位 +ASCII 字母、数字、下划线和连字符,未知格式保持未决,不作猜测。 -## 协议归属 +新增时,secret、明确返回的 `factor_id` 和 enrollment `session_id` 同时写入 +加密 journal 和数据库加密记录,收到持久化确认后才 activate。`session_id` +不是 factor ID;没有返回明确 ID 时,先保留 enrollment 来源,随后由有限登录 +匹配取得实际接受的 factor,再原子写入绑定。activation 消耗 OTP 后等待一个 +新的时间窗口进行一次验证。绑定写入与恢复使用 ciphertext CAS,不允许替换 +另一份密钥或已知 factor。journal 也拒绝覆盖或清除已有来源信息。 -本地 runtime 的构建入口是 `tools/reauth-runtime/Dockerfile`,下载 -`poxiao33/toSub2` 的固定提交 `8548397e89bf80e508eda64a87e0d556d43abc84`。 -其中 `src/protocol-login.mjs` 当前选取返回列表中的第一个 TOTP factor。 -本检查点没有修改这个上游文件,也没有声称发布了登录协议修复。 +成功表示“所选 factor 已验证并保存”,`outcome=selected_factor_verified_no_revocation`。 +它不表示旧验证器已撤销;流程没有删除 factor 的请求。两个 factor 都有效时, +不再要求另一个密钥失败。缺少绑定证据时返回 `factor_binding_unconfirmed`; +网络或响应不确定仍保留 `uncertain` 与候选记录。旧检查点的 +`new_accepted_old_valid` / `old_accepted_new_valid` 仍可读取,但新流程不再以 +这两个结果阻止已验证的新绑定。 -“2FA 登录导入”的首次登录由外部 Session Studio 执行。外部服务的部署源码 -及 factor 选择行为不在本仓库控制范围内;本地 Worker 修复不能证明该服务 -已修复。后续需分别验证本地绑定登录和外部导入边界。 +## 未决任务与删除后恢复 -## 验收限制 +已有账号仍使用原来的状态、验证和恢复导出接口。重试只接受 `verify_new` +或 `verify_old`,不会重复 enroll。普通重登与 Session 导出均从同一加密记录 +解出 secret + factor ID,并把绑定传给本地协议。 -当前只有合成凭据的离线回归。真实授权、登录和 2FA 验收 **NOT EXECUTED**, -需要另行明确授权。Go 服务测试、完整 runtime 构建和隔离数据库集成由远程 -CI 验证,不能把本地 Python GREEN 当作这些检查通过。Issue #881 保持未解决。 +旧账号软删除后,管理员可按保留的 task ID 访问快照;观察员禁止访问,响应 +使用 `Cache-Control: no-store`。以下是接口契约,不是已执行的生产恢复步骤: + +- `POST /api/v1/admin/accounts/totp-recovery-export`:请求 + `task_id`、`expected_email`、`source`(`candidate` / `previous`),可选 + `include_password`。只允许未决任务及匹配的快照邮箱,返回 secret、factor ID + 和恢复状态;默认不返回密码。 +- `POST /api/v1/admin/accounts/:id/totp-recovery-import`:请求同样的 task、邮箱和 + source。目标必须是邮箱匹配且尚未保存登录配置的 OpenAI OAuth 账号。 + 服务端通过专用 `INSERT ... ON CONFLICT DO NOTHING` 写入完整加密配置; + 已存在或并发先写入的配置返回 409,绝不覆盖赢家。 +- 导入选择 `local_worker` 并明确返回 `verified: false`。它准备本地登录配置, + 不执行真实登录,也不改变原任务状态。新账号的普通重登使用恢复绑定。 + 原身份仍有未决任务时禁止再次 enroll;导入本身不能清除这个门槛。 + +恢复导入需要一个通过受支持 OAuth/Session 导入路径建立的目标账号。 +如果外部首次登录仍失败,可在另行授权下,使用管理员掌握的有效恢复凭据和 +官方登录流程取得 OAuth/Session,再通过已有导入入口建立目标账号,随后使用 +上述本地恢复配置接口。不能声称发送给外部 Session Studio 的 `mfa_secret` +携带本地 factor 绑定,或声称外部提供者具备同样的有限匹配能力。 + +硬删除仍受现有 FK cascade 影响,不承诺数据库任务恢复。journal、journal key +与应用加密 key 都须保留;缺失 key 时不生成新 key 覆盖旧记录。迁移为历史任务 +从仍保留的配置补齐邮箱;没有身份快照的历史任务拒绝恢复,不猜测归属。 + +## 数据兼容与回滚 + +migration 382 只新增 `binding_verified` 和快照 `login_email`,使用 +`IF NOT EXISTS`,重复执行不覆盖已有身份快照。新代码同时读取旧 secret +ciphertext 和带版本的加密绑定记录;所有普通 claim、Session 导出和 TOTP +导出消费者均已适配。新任务每次 claim 清除确认标记,未收到本次绑定确认不能 +成功提交;完成时在同一事务内更新登录配置。 + +**不能只回滚旧 server/worker 二进制**:旧代码不识别加密记录中的版本化 +JSON,也不支持绑定确认协议。需要保留支持该格式的服务端/runtime,或另行 +审核数据兼容转换;不要清空候选、覆盖 journal 或恢复丢失新凭据的旧数据库 +快照来绕过不确定状态。旧 worker 配合新服务端不能完成未确认绑定的任务。 + +## 验收 + +本地已运行实际 pinned 模块、完整 CLI、真实 worker HTTP 客户端与加密 journal +的离线 RED/GREEN;上游响应为合成 mock。真实 Go handler/service/repository、 +PostgreSQL migration/reload/CAS、完整 API 和 production build 由 PR 的远程 CI +执行。具体 SHA、原始日志及未闭门见 issue881 artifacts handoff;CI 通过也不能 +替代尚未授权、尚未执行的真实登录/2FA 与外部 Session Studio 验收。 diff --git a/tools/fixtures/codex-http-transport.mjs b/tools/fixtures/codex-http-transport.mjs index 2c71371ff7ad..7a89cd6ba63b 100644 --- a/tools/fixtures/codex-http-transport.mjs +++ b/tools/fixtures/codex-http-transport.mjs @@ -33,11 +33,12 @@ export class TlsFingerprintTransport { assert.equal(JSON.parse(options.body).password, 'synthetic-password'); if (mode === 'operation-error') return Response.json(JSON.parse(process.env.CODEX_TEST_ERROR), {status: 400}); return Response.json({page: {type: 'mfa_challenge'}, 'oai-client-auth-session': { - mfa_factors: [{factor_type: 'totp', id: 'synthetic-factor'}], + mfa_factors: [...(mode === 'multi-factor' ? [{factor_type:'totp',id:'old-factor'}] : []), {factor_type: 'totp', id: 'synthetic-factor'}], }}); } if (route === 'POST /api/accounts/mfa/issue_challenge') return Response.json({}); if (route === 'POST /api/accounts/mfa/verify') { + if (JSON.parse(options.body).id === 'old-factor') return Response.json({error:{code:'invalid_otp'}},{status:400}); assert.match(JSON.parse(options.body).code, /^\d{6}$/); return Response.json({continue_url: 'https://chatgpt.com/auth-complete'}); } diff --git a/tools/fixtures/totp-http-transport.mjs b/tools/fixtures/totp-http-transport.mjs new file mode 100644 index 000000000000..a90c7872bb6f --- /dev/null +++ b/tools/fixtures/totp-http-transport.mjs @@ -0,0 +1,31 @@ +// Strict offline HTTP boundary for the actual CLI. Never delegates to fetch. +import fs from 'node:fs'; +import assert from 'node:assert/strict'; +const fixture=JSON.parse(fs.readFileSync(new URL('./totp-fixture.json',import.meta.url))); +Date.now=()=>59000; +export const shouldUseTlsTransport=()=>true; +export const browserIdentityForTlsProfile=()=>({userAgent:'offline',acceptLanguage:'en-US'}); +export class TlsFingerprintTransport { + enabled=true; + async prepareProxy() {} async configure() {} async close() {} + async generateSentinelTokens(){return {token:'synthetic'};} + async fetch(url,options) { + const target=new URL(url); + assert.ok(['https://chatgpt.com','https://auth.openai.com'].includes(target.origin)); + const route=`${options.method} ${target.pathname}`; + const body=options.body && JSON.parse(options.headers.get('content-type')?.includes('application/json')?options.body:'{}'); + fs.appendFileSync(fixture.trace,JSON.stringify({route,id:body?.id})+'\n'); + if(route==='GET /'||route==='GET /log-in/password'||route==='GET /auth-complete') return new Response('offline'); + if(route==='GET /api/auth/providers') return Response.json({}); + if(route==='GET /api/auth/csrf') return Response.json({csrfToken:'synthetic'}, {headers:{'set-cookie':'__Host-next-auth.csrf-token=synthetic; Path=/; Secure'}}); + if(route==='POST /api/auth/signin/openai') return Response.json({url:'https://auth.openai.com/log-in/password'}); + if(route==='POST /api/accounts/password/verify') return Response.json({page:{type:'mfa_challenge'},'oai-client-auth-session':{mfa_factors:fixture.factors.map(id=>({id,factor_type:'totp'}))}}); + if(route==='POST /api/accounts/mfa/issue_challenge') return Response.json({}); + if(route==='POST /api/accounts/mfa/verify') { + if(fixture.failure) return Response.json({error:'invalid_otp'},{status:fixture.failure}); + if(body.code!==fixture.codes[body.id]) return Response.json({error:'invalid_otp'},{status:400}); + return Response.json({continue_url:'https://chatgpt.com/auth-complete'}); + } + throw Error('UNEXPECTED_OFFLINE_HTTP_REQUEST'); + } +} diff --git a/tools/openai_codex_diagnostics.mjs b/tools/openai_codex_diagnostics.mjs index 44524be115a6..494fb93b7df9 100644 --- a/tools/openai_codex_diagnostics.mjs +++ b/tools/openai_codex_diagnostics.mjs @@ -76,6 +76,17 @@ export function parseOperationJSON(raw) { } catch { return null; } } +// Preserve only an explicit OTP rejection at the MFA verification boundary. +// Generic diagnostic mapping must not erase the evidence needed for bounded matching. +export function isTotpRejectionResponse(result) { + if (![400,401].includes(Number(result?.res?.status))) return false; + const payload = parseOperationJSON(result?.text); + const error = payload?.error; + const code = typeof error === 'string' ? error : error?.code; + if (['invalid_otp','incorrect_otp','wrong_otp','invalid_totp','incorrect_totp','wrong_totp','invalid_code','incorrect_code','wrong_code'].includes(code)) return true; + return code === undefined && typeof error?.message === 'string' && /^(?:invalid|incorrect|wrong)[ _-](?:totp|otp|code)$/i.test(error.message); +} + export function observeProtocolResponse(result, fallback = 'protocol_error') { const payload = parseOperationJSON(result?.text); const status = Number(result?.res?.status); @@ -91,6 +102,8 @@ function reasonFor(error, stage) { try { if (reasons.has(error?.message)) return error.message; const text = typeof error?.message === 'string' ? error.message : ''; + if (text.startsWith('TOTP_REJECTED:')) return 'totp_rejected'; + if (text.startsWith('TOTP_FACTOR_') || text === 'TOTP_VERIFICATION_UNCONFIRMED') return 'interaction_required'; if (text.startsWith('PROXY_RISK_CONTROL: ')) return 'security_check'; if (/^(?:CODEX_AUTH_LOGIN_REQUIRED|SESSION_SELECTION_INVALID):/.test(text)) return 'session_unavailable'; if (['ECONNREFUSED', 'ENOTFOUND', 'ETIMEDOUT'].includes(error?.code) || ['fetch failed', 'timed out'].includes(text)) return 'transport_error'; diff --git a/tools/openai_codex_oauth_adapter.py b/tools/openai_codex_oauth_adapter.py index d0cfe5714fe6..44a4545560ee 100644 --- a/tools/openai_codex_oauth_adapter.py +++ b/tools/openai_codex_oauth_adapter.py @@ -16,6 +16,7 @@ from openai_excel_oauth_adapter import replace_exact PINNED_PROTOCOL_BLOB = 'ff2db2507e401c29fa78705e44f5b40a3108a8c6' +BOUND_PROTOCOL_BLOB = 'ffcbb56992f0e34a90821586c63d9bbe27c890d9' STAGES = frozenset(('starting', 'web_login', 'password_accepted', 'mfa_challenge', 'mfa_accepted', 'web_redirect', 'oauth_authorize', 'session_select', 'workspace_select', 'callback_validation', 'token_exchange', 'oauth_output', 'completed')) @@ -31,15 +32,15 @@ def codex_source(source): raw = source.encode('utf-8') - if hashlib.sha1(b'blob ' + str(len(raw)).encode() + b'\0' + raw).hexdigest() != PINNED_PROTOCOL_BLOB: + if hashlib.sha1(b'blob ' + str(len(raw)).encode() + b'\0' + raw).hexdigest() not in (PINNED_PROTOCOL_BLOB, BOUND_PROTOCOL_BLOB): raise ValueError('unsupported_codex_runtime_layout') source = replace_exact(source, 'import crypto from "node:crypto";', '''import crypto from "node:crypto"; -import {createProtocolDiagnostics, validateCodexCallback, observeProtocolResponse} from "./openai_codex_diagnostics.mjs"; +import {createProtocolDiagnostics, validateCodexCallback, observeProtocolResponse, isTotpRejectionResponse} from "./openai_codex_diagnostics.mjs"; const protocolDiagnostic = createProtocolDiagnostics(process.env.SUB2API_PROTOCOL_DIAGNOSTIC_PATH, Number(process.env.SUB2API_PROTOCOL_TASK_ID)); console.log = console.warn = console.error = () => {};''') for old, new in ( - (' assertOk(result, `${method} ${url}`);', ' observeProtocolResponse(result);\n assertOk(result, `${method} ${url}`);'), + (' assertOk(result, `${method} ${url}`);', ' if (new URL(url).pathname === "/api/accounts/mfa/verify" && isTotpRejectionResponse(result)) throw new Error(`HTTP ${result.res.status} invalid_otp`);\n observeProtocolResponse(result);\n assertOk(result, `${method} ${url}`);'), ('async function loginChatgptWeb(client, { chatgptBase, authBase, email, rl, password, totpSecret }) {', 'async function loginChatgptWeb(client, { chatgptBase, authBase, email, rl, password, totpSecret }) {\n protocolDiagnostic.mark("web_login");'), (' console.log("[ok] Password accepted");', ' protocolDiagnostic.mark("password_accepted");'), @@ -68,6 +69,8 @@ def codex_source(source): (' if (!data[key]) throw new Error(`Token response missing ${key}.`);', ' if (typeof data[key] !== "string" || !data[key].trim()) throw new Error("credentials_incomplete");'), ): + if old == ' if (!isRejectedTotpError(error)) throw error;' and 'TOTP_FACTOR_BINDING_REQUIRED' in source: + continue # The tracked bounded MFA flow already terminates explicit rejection. source = replace_exact(source, old, new) # Both saved-session and freshly authenticated-session failures must end the # queued attempt. Do not turn a rejected OAuth result into another login. diff --git a/tools/openai_oauth_reauth_worker.py b/tools/openai_oauth_reauth_worker.py index d7c37011d220..fa0e16c6121b 100644 --- a/tools/openai_oauth_reauth_worker.py +++ b/tools/openai_oauth_reauth_worker.py @@ -446,16 +446,17 @@ def runtime_concurrency(self) -> int: raise WorkerError("invalid worker concurrency from server") return value - def totp_phase(self, task_id, phase, secret=""): + def totp_phase(self, task_id, phase, secret="", factor_id="", enrollment_session_id=""): return self._post(f"/api/v1/internal/openai-reauth/{task_id}/totp-phase", - {"worker_id": self.config.worker_id, "phase": phase, "secret": secret}) + {"worker_id": self.config.worker_id, "phase": phase, "secret": secret, + "factor_id": factor_id, "enrollment_session_id": enrollment_session_id}) def totp_finish(self, task_id, success, error_code=""): return self._post(f"/api/v1/internal/openai-reauth/{task_id}/totp-finish", {"worker_id": self.config.worker_id, "success": success, "error_code": error_code}) - def totp_recover(self, task_id, secret): - return self._post(f"/api/v1/internal/openai-reauth/{task_id}/totp-recover", {"secret": secret}) + def totp_recover(self, task_id, secret, factor_id="", enrollment_session_id=""): + return self._post(f"/api/v1/internal/openai-reauth/{task_id}/totp-recover", {"secret": secret, "factor_id": factor_id, "enrollment_session_id": enrollment_session_id}) def claim_totp(self): if self.config.tosub2_root is None or not os.getenv("OPENAI_TOTP_JOURNAL_DIR"): @@ -1313,6 +1314,8 @@ def process_password_claim(api: WorkerAPI, claim: dict[str, Any]) -> None: if not script.is_file(): raise WorkerError("toSub2 protocol-login.mjs is unavailable") + if claim.get("totp_factor_id") and "TOTP_FACTOR_BINDING_REQUIRED" not in script.read_text(): + raise WorkerError("factor_binding_runtime_required") LOGGER.info("task=%s account=%s started (password protocol)", task_id, account_id) _best_effort_progress(api, task_id, "starting") _best_effort_progress(api, task_id, "protocol_connecting") @@ -1398,6 +1401,7 @@ def codex_diagnostic(): for key in ("CHATGPT_BASE", "AUTH_BASE", "CHATGPT_PROXY_URL"): child_env.pop(key, None) child_env["CHATGPT_PROXY_MAX_ATTEMPTS"] = "1" + child_env["CHATGPT_TOTP_FACTOR_ID"] = str(claim.get("totp_factor_id") or "") child_env["CHATGPT_LOGIN_PASSWORD"] = password if totp_secret: child_env["CHATGPT_TOTP_SECRET"] = totp_secret @@ -1537,6 +1541,7 @@ def process_session_export_claim(api: WorkerAPI, claim: dict[str, Any]) -> None: login_claim = { "login_email": email, "password": password, + "totp_factor_id": str(claim.get("totp_factor_id") or ""), "proxy_url": str(claim.get("proxy_url") or ""), } _best_effort_session_progress(api, task_id, "password_submitted") diff --git a/tools/openai_totp_journal.mjs b/tools/openai_totp_journal.mjs index 0ea9d6e88e6a..eb60e25ba4db 100644 --- a/tools/openai_totp_journal.mjs +++ b/tools/openai_totp_journal.mjs @@ -40,6 +40,9 @@ export function openJournal(root) { if (fs.existsSync(target)) { const old = read(id) if (old.candidate && old.candidate !== record.candidate) throw Error('candidate_overwrite_refused') + for (const key of ['factor_id', 'session_id', 'previous_factor_id', 'email', 'previous']) { + if (old[key] && old[key] !== record[key]) throw Error('provenance_overwrite_refused') + } } const nonce = crypto.randomBytes(12) const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce) diff --git a/tools/openai_totp_rotation.py b/tools/openai_totp_rotation.py index ac72a501de63..6107466bfd20 100644 --- a/tools/openai_totp_rotation.py +++ b/tools/openai_totp_rotation.py @@ -61,10 +61,11 @@ def call(self, payload): raise RotationError('journal_unavailable') return json.loads(child.stdout) - def save(self, claim, candidate='', session_id=''): + def save(self, claim, candidate='', session_id='', factor_id=''): self.call({'action': 'write', 'record': {'task_id': claim['task_id'], 'account_id': claim['account_id'], 'email': claim['login_email'], 'password': claim['password'], 'previous': claim['totp_secret'], - 'candidate': candidate, 'session_id': session_id}}) + 'candidate': candidate, 'session_id': session_id, 'factor_id': factor_id, + 'previous_factor_id': claim.get('totp_factor_id', '')}}) def acknowledge(self, task_id): self.call({'action': 'acknowledge', 'task_id': task_id}) @@ -74,7 +75,7 @@ def recover(self, api): if record.get('candidate') and not record.get('database_acknowledged'): # The API accepts this only for an uncertain task with no different candidate. try: - api.totp_recover(record['task_id'], record['candidate']) + api.totp_recover(record['task_id'], record['candidate'], record.get('factor_id', ''), record.get('session_id', '')) self.acknowledge(record['task_id']) except Exception: pass # Keep the encrypted journal for a later reconciliation. @@ -91,7 +92,8 @@ def login_web(config, claim, secret): 'PATH', 'LANG', 'SYSTEMROOT', 'TEMP', 'TMP', 'PYTHONHOME', 'LD_LIBRARY_PATH', 'SSL_CERT_FILE', 'CURL_CA_BUNDLE', 'TOSUB2_PYTHON', 'NODE_EXECUTABLE')} child_env.update(CHATGPT_LOGIN_PASSWORD=claim['password'], CHATGPT_TOTP_SECRET=secret, - CHATGPT_PROXY_MAX_ATTEMPTS='1', CHATGPT_PROXY_URL=claim.get('proxy_url', '')) + CHATGPT_PROXY_MAX_ATTEMPTS='1', CHATGPT_PROXY_URL=claim.get('proxy_url', ''), + CHATGPT_TOTP_FACTOR_ID=claim.get('totp_factor_id', '')) command = [os.getenv('NODE_EXECUTABLE', 'node'), str(root / 'src/protocol-login.mjs'), '--email', claim['login_email'], '--web-only', '--output-mode', 'session', '--out', str(session_file)] try: @@ -103,6 +105,8 @@ def login_web(config, claim, secret): output = result.stdout + '\n' + result.stderr if 'The code generated from the configured 2FA key was rejected' in output: return None + if 'TOTP_FACTOR_' in output: + raise RotationError('factor_binding_unconfirmed') if 'Password was rejected' in output: raise RotationError('password_rejected') if result.returncode or not session_file.exists(): @@ -110,8 +114,11 @@ def login_web(config, claim, secret): raise RotationError('proxy_unavailable') raise RotationError('login_failed') data = json.loads(session_file.read_text()) - if data.get('web', {}).get('mfaVerified') is not True: + web = data.get('web', {}) + if web.get('mfaVerified') is not True: raise RotationError('verification_failed') + if not valid_factor_id(web.get('mfaFactorId')) or (claim.get('totp_factor_id') and web['mfaFactorId'] != claim['totp_factor_id']): + raise RotationError('factor_binding_unconfirmed') return data @@ -163,23 +170,33 @@ def close(self): def verify_pair(config, claim, candidate, action, login): - accepted, rejected = (claim['totp_secret'], candidate) if action == 'verify_old' else (candidate, claim['totp_secret']) - if not accepted or login(config, claim, accepted) is None: + """Verify the selected credential; adding a factor does not revoke another.""" + old = action == 'verify_old' + secret = claim['totp_secret'] if old else candidate + factor = claim.get('totp_factor_id' if old else 'candidate_factor_id', '') + if not secret: raise RotationError('verification_failed') - # A fresh time window avoids mistaking replay rejection of the initial OTP for revocation. - if rejected: - time.sleep(31 - time.time() % 30) - if rejected and login(config, claim, rejected) is not None: - # Enrollment adds a factor; it does not revoke the previous one. Keep - # recovery available until factor binding can establish provenance, - # but do not misreport a successfully verified secret as invalid. - raise RotationError('old_accepted_new_valid' if action == 'verify_old' else 'new_accepted_old_valid') + data = login(config, {**claim, 'totp_factor_id': factor}, secret) + if data is None: + raise RotationError('verification_failed') + web = data.get('web', {}) + verified = web.get('mfaFactorId') + if web.get('mfaVerified') is not True or not valid_factor_id(verified) or (factor and verified != factor): + raise RotationError('factor_binding_unconfirmed') + return secret, verified + + +def valid_factor_id(value): + import re + return isinstance(value, str) and re.fullmatch(r'[A-Za-z0-9_-]{1,256}', value) is not None def rotate(api, claim, *, session_factory=WebSession, login=login_web, journal_factory=Journal): task = claim['task_id'] action = claim.get('action', 'rotate') + claim = dict(claim) candidate = claim.get('candidate_secret', '') + session_id = claim.get('candidate_enrollment_session_id', '') session = None stage = 'login_failed' try: @@ -193,14 +210,18 @@ def rotate(api, claim, *, session_factory=WebSession, login=login_web, journal_f candidate = data.get('secret', '') session_id = data.get('session_id', '') # Preserve the raw returned candidate before further validation or activation. - journal.save(claim, candidate, session_id) + factor_id = data.get('factor_id', '') + journal.save(claim, candidate, session_id, factor_id) + if factor_id and not valid_factor_id(factor_id): + raise RotationError('enrollment_failed') + claim['candidate_factor_id'] = factor_id if not isinstance(candidate, str) or not isinstance(session_id, str) or not candidate or not session_id: raise RotationError('enrollment_failed') normalized = lambda value: ''.join(value.upper().split()).rstrip('=') if normalized(candidate) == normalized(claim['totp_secret']): raise RotationError('enrollment_failed') code = totp(candidate) - api.totp_phase(task, 'prepared', candidate) # Database commit ACK is required. + api.totp_phase(task, 'prepared', candidate, factor_id, session_id) # Database commit ACK is required. journal.acknowledge(task) api.totp_phase(task, 'activating') stage = 'activation_unconfirmed' @@ -209,16 +230,24 @@ def rotate(api, claim, *, session_factory=WebSession, login=login_web, journal_f if activation.get('success') is not True: raise RotationError('activation_unconfirmed') api.totp_phase(task, 'verifying') + # Activation may consume this time window's OTP. Verify once in a + # fresh window instead of treating replay rejection as a bad binding. + time.sleep(31 - time.time() % 30) elif action in ('verify_new', 'verify_old'): api.totp_phase(task, 'verify_recovery') else: raise RotationError('worker_failed') stage = 'verification_failed' - verify_pair(api.config, claim, candidate, action, login) + secret, verified_factor = verify_pair(api.config, claim, candidate, action, login) + if action != 'verify_old': + journal.save(claim, candidate, session_id, verified_factor) + api.totp_phase(task, 'bound', secret, verified_factor) + if action != 'verify_old': + journal.acknowledge(task) api.totp_finish(task, True) except Exception as exc: if stage == 'verification_failed' and isinstance(exc, RotationError) and exc.code in ( - 'new_accepted_old_valid', 'old_accepted_new_valid'): + 'factor_binding_unconfirmed', 'new_accepted_old_valid', 'old_accepted_new_valid'): stage = exc.code # Never expose network bodies, login traces, candidates, passwords, or exceptions. try: diff --git a/tools/reauth-runtime/Dockerfile b/tools/reauth-runtime/Dockerfile index 694476dae860..b49a9e837182 100644 --- a/tools/reauth-runtime/Dockerfile +++ b/tools/reauth-runtime/Dockerfile @@ -2,7 +2,7 @@ FROM node:22.22.2-bookworm-slim AS node FROM python:3.12.12-slim-bookworm AS runtime ARG TOSUB2_COMMIT=8548397e89bf80e508eda64a87e0d556d43abc84 -RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates libstdc++6 curl \ +RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates libstdc++6 curl patch \ && rm -rf /var/lib/apt/lists/* COPY --from=node /usr/local/bin/node /usr/local/bin/node COPY --from=node /usr/local/lib/node_modules/npm /usr/local/lib/node_modules/npm @@ -10,25 +10,29 @@ RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm WORKDIR /build/tosub2 RUN curl -fsSL --retry 3 "https://codeload.github.com/poxiao33/toSub2/tar.gz/$TOSUB2_COMMIT" \ | tar -xz --strip-components=1 -C /build/tosub2 +COPY tools/reauth-runtime/apply_totp_patch.py tools/reauth-runtime/totp-factor-binding.patch /build/patches/ +RUN python /build/patches/apply_totp_patch.py /build/tosub2 COPY tools/reauth-runtime/package*.json ./ RUN npm ci --omit=dev --ignore-scripts --no-audit --no-fund COPY tools/reauth-runtime/requirements.txt /build/requirements.txt RUN python -m pip install --no-cache-dir --only-binary=:all: -r /build/requirements.txt COPY tools/openai_oauth_reauth_worker.py /build/worker.py -COPY tools/openai_totp_rotation.py tools/openai_totp_journal.mjs tools/test_openai_totp_rotation.py tools/test_openai_totp_journal.mjs /build/tools/ +COPY tools/openai_totp_rotation.py tools/openai_totp_journal.mjs tools/test_openai_totp_rotation.py tools/test_openai_totp_journal.mjs tools/test_openai_totp_protocol.mjs tools/test_openai_totp_integration.py /build/tools/ COPY tools/test_openai_oauth_reauth_worker.py tools/test_openai_oauth_reauth_engines.py tools/test_openai_oauth_reauth_runtime_controls.py /build/tools/ COPY tools/openai_codex_oauth_adapter.py tools/openai_codex_diagnostics.mjs tools/test_openai_codex_oauth_adapter.py tools/test_openai_codex_diagnostics.mjs /build/tools/ COPY tools/openai_excel_oauth_adapter.py tools/openai_excel_2fa_login.py tools/test_openai_excel_oauth_adapter.py /build/tools/ COPY tools/openai_excel_password_flow.mjs tools/test_openai_excel_password_flow.mjs /build/tools/ COPY tools/openai_siwc_oauth_adapter.py tools/openai_siwc_password_flow.mjs tools/test_openai_siwc_oauth_adapter.py tools/test_openai_siwc_password_flow.mjs /build/tools/ COPY tools/fixtures/siwc-http-transport.mjs /build/tools/fixtures/ -COPY tools/fixtures/codex-http-transport.mjs /build/tools/fixtures/ +COPY tools/fixtures/codex-http-transport.mjs tools/fixtures/totp-http-transport.mjs /build/tools/fixtures/ COPY tools/fixtures/session-studio/ /build/tools/fixtures/session-studio/ COPY tools/fixtures/credential-operation-errors.json /build/tools/fixtures/credential-operation-errors.json RUN cp /build/worker.py /build/tools/openai_oauth_reauth_worker.py \ && cd /build && EXCEL_TEST_TOSUB2_ROOT=/build/tosub2 python -m unittest tools.test_openai_totp_rotation tools.test_openai_oauth_reauth_worker tools.test_openai_oauth_reauth_engines tools.test_openai_oauth_reauth_runtime_controls tools.test_openai_excel_oauth_adapter tools.test_openai_siwc_oauth_adapter tools.test_openai_codex_oauth_adapter -q \ && node --test /build/tools/test_openai_codex_diagnostics.mjs \ + && TOTP_TEST_PROTOCOL=/build/tosub2/src/protocol-login.mjs python -m unittest tools.test_openai_totp_integration -v \ && node --test /build/tools/test_openai_totp_journal.mjs \ + && TOTP_TEST_PROTOCOL=/build/tosub2/src/protocol-login.mjs node --test /build/tools/test_openai_totp_protocol.mjs \ && node /build/tools/test_openai_excel_password_flow.mjs \ && node --test /build/tools/test_openai_siwc_password_flow.mjs \ && cd /build/tosub2 && node test/password-mfa-workspace-smoke.mjs diff --git a/tools/reauth-runtime/apply_totp_patch.py b/tools/reauth-runtime/apply_totp_patch.py new file mode 100644 index 000000000000..1a333b6eb642 --- /dev/null +++ b/tools/reauth-runtime/apply_totp_patch.py @@ -0,0 +1,13 @@ +"""Apply our reviewed MFA patch only to the pinned toSub2 source, fail on drift.""" +import hashlib +from pathlib import Path +import subprocess +import sys + +root = Path(sys.argv[1]).resolve() +source = root / 'src/protocol-login.mjs' +expected = 'b7e5ed0a4dcc2955607815f01ea291d1708c661af0796b19dc5dab23ff9c4752' +if hashlib.sha256(source.read_bytes()).hexdigest() != expected: + raise SystemExit('unsupported TOTP protocol source; review the pin and patch') +subprocess.run(['patch', '--batch', '--fuzz=0', '-p1', '-i', + str(Path(__file__).with_name('totp-factor-binding.patch').resolve())], cwd=root, check=True) diff --git a/tools/reauth-runtime/package.py b/tools/reauth-runtime/package.py index 04cdb9331f7a..f25b687578d2 100644 --- a/tools/reauth-runtime/package.py +++ b/tools/reauth-runtime/package.py @@ -59,6 +59,7 @@ ) script.chmod(0o755) manifest = {"tosub2_commit": "8548397e89bf80e508eda64a87e0d556d43abc84", + "totp_factor_binding_patch": "issue881-v1", "architecture": platform.machine(), "python": platform.python_version(), "node": subprocess.check_output(["node", "--version"], text=True).strip(), "python_packages": subprocess.check_output([sys.executable, "-m", "pip", "freeze"], text=True).splitlines()} diff --git a/tools/reauth-runtime/test_deployment.py b/tools/reauth-runtime/test_deployment.py index bf8e852b8cae..00cb3489f828 100644 --- a/tools/reauth-runtime/test_deployment.py +++ b/tools/reauth-runtime/test_deployment.py @@ -63,10 +63,10 @@ class MockMFA: def request(self, method, route, payload=None): if route.endswith('/enroll'): phases.append('enroll') - return {'secret': NEW, 'session_id': 'synthetic-enrollment'} + return {'secret': NEW, 'session_id': 'synthetic-enrollment', 'factor_id': 'F2'} assert route.endswith('/activate_enrollment') saved = next(x for x in journal.call({'action': 'list'}) if x['task_id'] == task['task_id']) - assert saved['candidate'] == NEW and saved['database_acknowledged'] + assert saved['candidate'] == NEW and saved['factor_id'] == 'F2' and saved['database_acknowledged'] status = request('GET', f'/admin/accounts/{ID}/totp-rotation') assert status['state'] == 'activating' and status['has_candidate'] phases.append('activate-after-local-and-database-save') @@ -76,16 +76,18 @@ def close(self): pass def mock_login(_config, _claim, secret): - phases.append('verify-new' if secret == NEW else 'verify-old-rejected') - return {'cookies': []} if secret == NEW else None + assert _claim['totp_factor_id'] == 'F2' + phases.append('verify-new') + # The old factor remains valid. Success verifies the selected factor only. + return {'web': {'mfaVerified': True, 'mfaFactorId': 'F2'}} with patch('openai_totp_rotation.time.sleep'): rotate(api, claim, session_factory=lambda *_: MockMFA(), login=mock_login) status = request('GET', f'/admin/accounts/{ID}/totp-rotation') -assert status['state'] == 'succeeded', status +assert status['state'] == 'succeeded' and status['outcome'] == 'selected_factor_verified_no_revocation', status exported = request('POST', f'/admin/accounts/{ID}/totp-export', {'source': 'current', 'include_password': False}) -assert exported['secret'] == NEW and 'password' not in exported -assert phases == ['enroll', 'activate-after-local-and-database-save', 'verify-new', 'verify-old-rejected'], phases +assert exported['secret'] == NEW and exported['factor_id'] == 'F2' and 'password' not in exported +assert phases == ['enroll', 'activate-after-local-and-database-save', 'verify-new'], phases print('Real API/PostgreSQL rotation: succeeded; export and encrypted configuration updated.', flush=True) task2 = request('POST', f'/admin/accounts/{ID}/totp-rotation', {}) @@ -95,7 +97,7 @@ def mock_login(_config, _claim, secret): class TimeoutMFA: def request(self, method, route, payload=None): if route.endswith('/enroll'): - return {'secret': candidate2, 'session_id': 'synthetic-timeout'} + return {'secret': candidate2, 'session_id': 'synthetic-timeout', 'factor_id': 'F3'} raise TimeoutError('synthetic activation response loss') def close(self): pass @@ -114,12 +116,51 @@ def close(self): print('Activation timeout: uncertain, new rotation blocked, candidate survives journal reopening.', flush=True) request('POST', f'/admin/accounts/{ID}/totp-rotation/verify', {'task_id': task2['task_id'], 'action': 'verify_new'}) recovery = api.claim_totp() -assert recovery['action'] == 'verify_new' and recovery['candidate_secret'] == candidate2 +assert recovery['action'] == 'verify_new' and recovery['candidate_secret'] == candidate2 and recovery['candidate_factor_id'] == 'F3' with patch('openai_totp_rotation.time.sleep'): rotate(api, recovery, session_factory=lambda *_: (_ for _ in ()).throw(AssertionError('Recovery must not enroll')), - login=lambda _config, _claim, secret: {} if secret == candidate2 else None) + login=lambda _config, _claim, secret: {'web': {'mfaVerified': True, 'mfaFactorId': 'F3'}} if secret == candidate2 and _claim['totp_factor_id'] == 'F3' else None) assert request('GET', f'/admin/accounts/{ID}/totp-rotation')['state'] == 'succeeded' print('Recovery verifies the saved candidate and completes without another enrollment.', flush=True) -report = {'account_id': ID, 'successful_task': task['task_id'], 'uncertain_task': task2['task_id'], 'checks': ['packaged-worker-current-symlink', 'real-api-heartbeat', 'postgres-task-creation-and-transitions', 'dual-save-before-activation', 'configuration-update-and-export', 'activation-timeout-and-restart-recovery', 'verify-new-without-reenrollment'], 'upstream': 'mock', 'production_changes': False} +# An unresolved task retains its own identity/credential snapshot after soft delete. +task3 = request('POST', f'/admin/accounts/{ID}/totp-rotation', {}) +claim3 = api.claim_totp() +class DeletedTimeoutMFA: + def request(self, method, route, payload=None): + if route.endswith('/enroll'): + return {'secret': OLD, 'session_id': 'deleted-enrollment', 'factor_id': 'F4'} + raise TimeoutError('synthetic activation response loss') + def close(self): pass +rotate(api, claim3, session_factory=lambda *_: DeletedTimeoutMFA(), login=mock_login) +assert request('GET', f'/admin/accounts/{ID}/totp-rotation')['state'] == 'uncertain' +request('DELETE', f'/admin/accounts/{ID}') +recovery_request = {'task_id': task3['task_id'], 'expected_email': 'rotation@example.test', 'source': 'candidate'} +restored = request('POST', '/admin/accounts/totp-recovery-export', recovery_request) +assert restored['secret'] == OLD and restored['factor_id'] == 'F4' and 'password' not in restored +try: + request('POST', '/admin/accounts/totp-recovery-export', {**recovery_request, 'expected_email': 'wrong@example.test'}) +except RuntimeError as error: + assert str(error).startswith('409 ') +else: + raise AssertionError('wrong recovery identity must fail') +destination = request('POST', '/admin/accounts', {'name': 'Recovered synthetic', 'platform': 'openai', 'type': 'oauth', 'credentials': {'email': 'rotation@example.test', 'access_token': 'synthetic-token'}, 'extra': {'email': 'rotation@example.test'}, 'concurrency': 1, 'priority': 50}) +new_id = destination['id'] +result = request('POST', f'/admin/accounts/{new_id}/totp-recovery-import', recovery_request) +assert result['imported'] and result['verified'] is False and result['engine'] == 'local_worker' +try: + request('POST', f'/admin/accounts/{new_id}/totp-rotation', {}) +except RuntimeError as error: + assert str(error).startswith('409 ') +else: + raise AssertionError('reimport must not reenroll an unresolved identity') +normal_task = request('POST', f'/admin/accounts/{new_id}/openai-reauth', {}) +normal_claim = api.claim() +assert normal_claim['task_id'] == normal_task['id'] and normal_claim['account_id'] == new_id +assert normal_claim['totp_secret'] == OLD and normal_claim['totp_factor_id'] == 'F4' +api.fail(normal_claim['task_id'], 'worker_failed') # No real authentication is performed. +print('Soft-delete recovery: task snapshot exported, matching new account imported, normal claim bound; reenrollment blocked.', flush=True) +report = {'account_id': ID, 'successful_task': task['task_id'], 'uncertain_task': task2['task_id'], 'deleted_task': task3['task_id'], 'reimport_account': new_id, + 'checks': ['packaged-worker-current-symlink', 'real-api-heartbeat', 'postgres-task-creation-and-transitions', 'paired-dual-save-before-activation', 'bound-configuration-update-and-export', 'activation-timeout-and-restart-recovery', 'verify-new-without-reenrollment', 'soft-deleted-task-snapshot-export', 'identity-checked-recovery-import', 'normal-worker-claim-retains-binding', 'reimport-does-not-reenroll'], + 'upstream': 'mock', 'production_changes': False, 'real_login_2fa': 'NOT EXECUTED'} (ROOT / 'verification.json').write_text(json.dumps(report, indent=2) + '\n') print(json.dumps(report, indent=2)) diff --git a/tools/reauth-runtime/totp-factor-binding.patch b/tools/reauth-runtime/totp-factor-binding.patch new file mode 100644 index 000000000000..7252ed71a86e --- /dev/null +++ b/tools/reauth-runtime/totp-factor-binding.patch @@ -0,0 +1,95 @@ +--- a/src/protocol-login.mjs ++++ b/src/protocol-login.mjs +@@ -1090,6 +1090,7 @@ + emailVerified: true, + loginMethod, + mfaVerified: mfaRequired, ++ mfaFactorId: client.totpFactorId || null, + }; + } + +@@ -1133,52 +1134,45 @@ + } + + async function completeTotpMfaIfNeeded(client, { authBase, rl, deviceId, payload, totpSecret, referer }) { ++ delete client.totpFactorId; + if (!isMfaChallengePayload(payload)) return payload; +- const factor = pickTotpFactor(payload); +- if (!factor?.id) throw new Error("2FA is required, but the response does not contain a TOTP factor ID"); +- ++ const session = payload?.["oai-client-auth-session"] || {}; ++ const factors = [...new Map([ ++ ...(Array.isArray(session.mfa_challenge_factors) ? session.mfa_challenge_factors : []), ++ ...(Array.isArray(session.mfa_factors) ? session.mfa_factors : []), ++ ].filter(f => f?.factor_type === "totp" && typeof f.id === "string" && /^[A-Za-z0-9_-]{1,256}$/.test(f.id)) ++ .map(f => [f.id, f])).values()]; + console.log("[mfa] TOTP 2FA challenge reached."); +- await authJsonStep(client, authBase, "POST", "/api/accounts/mfa/issue_challenge", { +- type: "totp", +- id: factor.id, +- force_fresh_challenge: false, +- }, { referer }); +- +- let code; +- let generatedFromSecret = false; +- if (totpSecret) { +- code = generateTotp(totpSecret); +- generatedFromSecret = true; +- console.log("[mfa] Generated a 6-digit code from the configured 2FA key."); +- } else { +- code = await askTotpOtp(rl); +- } +- const challengeUrl = getContinueUrl(payload) || `${authBase}/mfa-challenge/${factor.id}`; +- for (;;) { ++ const bound = process.env.CHATGPT_TOTP_FACTOR_ID || ""; ++ const selected = bound ? factors.filter(f => f.id === bound) : factors; ++ if (!selected.length) throw new Error("TOTP_FACTOR_NOT_FOUND"); ++ // A code cannot identify its factor without a challenge. Bound the legacy ++ // discovery budget before sending anything, and never retry uncertain I/O. ++ if (!bound && selected.length > 2) throw new Error("TOTP_FACTOR_BINDING_REQUIRED"); ++ if (!totpSecret) throw new Error("TOTP_SECRET_REQUIRED"); ++ for (const factor of selected) { ++ await authJsonStep(client, authBase, "POST", "/api/accounts/mfa/issue_challenge", { ++ type: "totp", id: factor.id, force_fresh_challenge: false, ++ }, { referer }); ++ const code = generateTotp(totpSecret); ++ const sentinelHeaders = await createSentinelHeaders(client, {authBase, deviceId, flow: "password_verify"}); + try { +- const sentinelHeaders = await createSentinelHeaders(client, { +- authBase, +- deviceId, +- flow: "password_verify", +- }); + const { data } = await authJsonStep(client, authBase, "POST", "/api/accounts/mfa/verify", { +- type: "totp", +- id: factor.id, +- code, +- }, { referer: challengeUrl, headers: sentinelHeaders }); ++ type: "totp", id: factor.id, code, ++ }, { referer: `${authBase}/mfa-challenge/${encodeURIComponent(factor.id)}`, headers: sentinelHeaders }); ++ if (data?.error || isMfaChallengePayload(data) || !getContinueUrl(data)) { ++ throw new Error("TOTP_VERIFICATION_UNCONFIRMED"); ++ } ++ client.totpFactorId = factor.id; + console.log("[ok] 2FA verification accepted"); + return data; + } catch (error) { +- if (!isRejectedTotpError(error)) throw error; +- console.log( +- generatedFromSecret +- ? "[warn] The code generated from the configured 2FA key was rejected; enter a current code manually." +- : "[warn] The 2FA code was rejected; enter a new code.", +- ); +- generatedFromSecret = false; +- code = await askTotpOtp(rl); +- } +- } ++ // Only an explicit 400/401 OTP rejection permits trying another ID. ++ // In particular a 429/5xx body mentioning an OTP is not a rejection. ++ if (!/HTTP (?:400|401)\b/.test(String(error?.message || "")) || !isRejectedTotpError(error)) throw error; ++ } ++ } ++ throw new Error("TOTP_REJECTED: The code generated from the configured 2FA key was rejected"); + } + + function isRejectedPasswordError(error) { diff --git a/tools/test_openai_codex_diagnostics.mjs b/tools/test_openai_codex_diagnostics.mjs index 112c9ab8d366..eafaa939df84 100644 --- a/tools/test_openai_codex_diagnostics.mjs +++ b/tools/test_openai_codex_diagnostics.mjs @@ -81,3 +81,13 @@ test('private snapshots contain only bounded enums, time and the task binding', assert.equal(JSON.parse(fs.readFileSync(file, 'utf8')).reason, 'protocol_error'); } finally { fs.rmSync(root, {recursive: true, force: true}); } }); + +test('only explicit 400/401 OTP rejection permits another factor', async () => { + const {isTotpRejectionResponse}=await import('./openai_codex_diagnostics.mjs'); + const response=(status,error)=>({res:{status},text:JSON.stringify({error})}); + assert.equal(isTotpRejectionResponse(response(400,{code:'invalid_otp'})),true); + assert.equal(isTotpRejectionResponse(response(401,{message:'invalid totp'})),true); + for (const value of [response(429,{code:'invalid_otp'}),response(500,{code:'invalid_otp'}),response(400,{code:'unknown',message:'invalid totp'}),response(400,{message:'nested invalid otp data'})]) { + assert.equal(isTotpRejectionResponse(value),false); + } +}); diff --git a/tools/test_openai_codex_oauth_adapter.py b/tools/test_openai_codex_oauth_adapter.py index dae51f49e0a3..45588f11e795 100644 --- a/tools/test_openai_codex_oauth_adapter.py +++ b/tools/test_openai_codex_oauth_adapter.py @@ -140,7 +140,7 @@ def test_structured_failure_does_not_send_mfa_or_tokens(self): def test_real_pinned_cli_with_only_mock_http(self): dependency = Path(os.environ['EXCEL_TEST_TOSUB2_ROOT']) original = (dependency / 'src/protocol-login.mjs').read_bytes() - for mode, expected in [('success', None), ('workspace-success', None), ('missing-callback', 'callback_missing'), + for mode, expected in [('success', None), ('multi-factor', None), ('workspace-success', None), ('missing-callback', 'callback_missing'), ('wrong-state', 'callback_state_mismatch'), ('exchange-failed', 'token_exchange_failed'), ('incomplete', 'credentials_incomplete'), ('session-rejected', 'session_unavailable'), ('workspace-mismatch', 'workspace_mismatch')]: @@ -173,7 +173,7 @@ def test_real_pinned_cli_with_only_mock_http(self): self.assertFalse(output.exists()) trace = routes.read_text() self.assertEqual(trace.count('POST /api/accounts/password/verify'), 1) - self.assertEqual(trace.count('POST /api/accounts/mfa/verify'), 1) + self.assertEqual(trace.count('POST /api/accounts/mfa/verify'), 2 if mode == 'multi-factor' else 1) if mode in ('missing-callback', 'wrong-state', 'session-rejected', 'workspace-mismatch'): self.assertNotIn('POST /oauth/token', trace) self.assertEqual((dependency / 'src/protocol-login.mjs').read_bytes(), original) diff --git a/tools/test_openai_totp_integration.py b/tools/test_openai_totp_integration.py new file mode 100644 index 000000000000..391f1bf9e368 --- /dev/null +++ b/tools/test_openai_totp_integration.py @@ -0,0 +1,113 @@ +"""Real WorkerAPI HTTP + rotate + sealed journal + actual patched CLI; upstream/API storage mocked.""" +import importlib +import json +import os +from pathlib import Path +import shutil +import sys +import tempfile +import threading +import unittest +from http.server import BaseHTTPRequestHandler, HTTPServer +from unittest.mock import patch + +TOOLS = Path(__file__).resolve().parent +sys.path.insert(0, os.environ.get('TOTP_TEST_WORKER_ROOT', str(TOOLS))) +worker = importlib.import_module('openai_oauth_reauth_worker') +rotation = importlib.import_module('openai_totp_rotation') +OLD='JBSWY3DPEHPK3PXP' +NEW='GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ' + +class WorkerIntegrationTests(unittest.TestCase): + def setUp(self): + self.temp=tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root=Path(self.temp.name) + src=self.root/'protocol/src';src.mkdir(parents=True) + shutil.copyfile(os.environ['TOTP_TEST_PROTOCOL'],src/'protocol-login.mjs') + shutil.copyfile(TOOLS/'fixtures/totp-http-transport.mjs',src/'tls-transport.mjs') + self.src=src + self.trace=self.root/'http.jsonl' + self.fixture={'factors':['F1','F2'],'codes':{'F1':rotation.totp(OLD,59),'F2':rotation.totp(NEW,59)},'trace':str(self.trace)} + self.claim=dict(task_id=7,account_id=42,login_email='owner@example.test',password='synthetic',totp_secret=OLD) + self.requests=[];self.finish=None;self.errors=[] + outer=self + class API(BaseHTTPRequestHandler): + def log_message(self,*args): pass + def do_POST(self): + data=json.loads(self.rfile.read(int(self.headers['Content-Length']))) + status=200 + try: + assert self.headers['X-OpenAI-Reauth-Worker-Token']=='synthetic-worker' + outer.requests.append((self.path,data)) + if self.path.endswith('/totp-phase') and data['phase']=='prepared': + saved=outer.journal.call({'action':'list'})[0] + assert saved['candidate']==data['secret']==NEW + assert saved['factor_id']==data['factor_id']=='F2' + assert saved['session_id']==data['enrollment_session_id']=='enrollment' + if self.path.endswith('/totp-phase') and data['phase']=='bound': + assert data['secret']==NEW and data['factor_id']=='F2' + if self.path.endswith('/totp-finish'): outer.finish=data + except Exception as exc: + outer.errors.append(type(exc).__name__);status=400 + self.send_response(status);self.send_header('Content-Type','application/json');self.end_headers() + self.wfile.write(json.dumps({'code':0 if status==200 else 1,'data':{}}).encode()) + self.server=HTTPServer(('127.0.0.1',0),API) + thread=threading.Thread(target=self.server.serve_forever,daemon=True);thread.start() + def close(): self.server.shutdown();self.server.server_close();thread.join() + self.addCleanup(close) + env=patch.dict(os.environ,{'OPENAI_TOTP_JOURNAL_DIR':str(self.root/'journal')}) + env.start();self.addCleanup(env.stop) + self.journal=rotation.Journal() + self.api=worker.WorkerAPI(worker.WorkerConfig(base_url=f'http://127.0.0.1:{self.server.server_port}',worker_token='synthetic-worker',worker_id='writer',protocol_root=None,tosub2_root=src.parent)) + + def run_rotation(self, timeout=False): + (self.src/'totp-fixture.json').write_text(json.dumps(self.fixture)) + outer=self + class MFA: + def request(self,method,route,payload): + if route.endswith('/enroll'): return {'secret':NEW,'session_id':'enrollment','factor_id':'F2'} + assert route.endswith('/activate_enrollment') + saved=outer.journal.call({'action':'list'})[0] + assert saved['database_acknowledged'] + if timeout: raise TimeoutError('ambiguous activation') + return {'success':True} + def close(self): pass + with patch.object(rotation.time, 'sleep'): + rotation.rotate(self.api,self.claim,session_factory=lambda *_:MFA()) + + def test_actual_worker_http_and_patched_cli_promote_only_verified_pair(self): + self.run_rotation() + self.assertEqual(self.errors,[]) + self.assertTrue(self.finish['success']) + phases=[body['phase'] for route,body in self.requests if route.endswith('/totp-phase')] + self.assertEqual(phases,['enrolling','prepared','activating','verifying','bound']) + requests=[json.loads(line) for line in self.trace.read_text().splitlines()] + self.assertEqual([r['id'] for r in requests if '/mfa/' in r['route']],['F2','F2']) + saved=rotation.Journal().call({'action':'list'})[0] + self.assertEqual(saved['factor_id'],'F2') + for file in (self.root/'journal').iterdir(): + self.assertNotIn(NEW.encode(),file.read_bytes()) + self.assertNotIn(b'enrollment',file.read_bytes()) + + def test_activation_uncertainty_preserves_pair_and_recovery_does_not_enroll(self): + self.run_rotation(timeout=True) + self.assertEqual(self.errors,[]) + self.assertFalse(self.finish['success']) + saved=rotation.Journal().call({'action':'list'})[0] + self.assertEqual((saved['candidate'],saved['factor_id']),(NEW,'F2')) + self.requests.clear() + self.claim.update(action='verify_new',candidate_secret=NEW,candidate_factor_id='F2',candidate_enrollment_session_id='enrollment') + self.run_rotation() + self.assertTrue(self.finish['success']) + self.assertEqual([body['phase'] for route,body in self.requests if route.endswith('/totp-phase')],['verify_recovery','bound']) + + def test_legacy_unbound_candidate_matches_in_both_factor_orders(self): + for ids in [['F1','F2'],['F2','F1']]: + with self.subTest(order=ids): + self.fixture['factors']=ids + (self.src/'totp-fixture.json').write_text(json.dumps(self.fixture)) + data=rotation.login_web(self.api.config,self.claim,NEW) + self.assertEqual(data['web']['mfaFactorId'],'F2') + +if __name__=='__main__': unittest.main() diff --git a/tools/test_openai_totp_journal.mjs b/tools/test_openai_totp_journal.mjs index f8308ea32281..99a76a3c851e 100644 --- a/tools/test_openai_totp_journal.mjs +++ b/tools/test_openai_totp_journal.mjs @@ -71,3 +71,12 @@ test('CLI invoked through a current symlink executes journal operations', t => { assert.equal(list.status, 0, list.stderr) assert.deepEqual(JSON.parse(list.stdout), [record]) }) + +test('candidate provenance cannot be replaced or stripped while keeping the secret', t => { + const journal = openJournal(setup(t)); + journal.write({task_id:7,candidate:'NEW',factor_id:'F2',session_id:'enrollment'}); + for (const change of [{factor_id:'F1'},{factor_id:''},{session_id:'other'}]) { + assert.throws(()=>journal.write({task_id:7,candidate:'NEW',factor_id:'F2',session_id:'enrollment',...change}), /overwrite/); + } + assert.equal(journal.read(7).factor_id,'F2'); +}); diff --git a/tools/test_openai_totp_protocol.mjs b/tools/test_openai_totp_protocol.mjs new file mode 100644 index 000000000000..eadaf0852d9d --- /dev/null +++ b/tools/test_openai_totp_protocol.mjs @@ -0,0 +1,85 @@ +// Execute the entire pinned protocol module with an offline HTTP client. +// Only CLI startup is disabled; production MFA, TOTP and HTTP-step functions run. +import {test} from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'node:fs/promises'; +import os from 'node:os'; +import path from 'node:path'; +import {pathToFileURL} from 'node:url'; + +const sourcePath = process.env.TOTP_TEST_PROTOCOL; +assert.ok(sourcePath, 'TOTP_TEST_PROTOCOL must name the pinned or patched real module'); +const root = await fs.mkdtemp(path.join(os.tmpdir(), 'totp-protocol-')); +let source = await fs.readFile(sourcePath, 'utf8'); +assert.equal(source.split('run().catch((error) => {').length, 2); +source = source.replace('run().catch((error) => {', 'Promise.resolve().catch((error) => {'); +await fs.writeFile(path.join(root, 'protocol.mjs'), source + '\nexport {completeTotpMfaIfNeeded, generateTotp, loginChatgptWeb};\n'); +await fs.writeFile(path.join(root, 'tls-transport.mjs'), `export const browserIdentityForTlsProfile=()=>({}); export const shouldUseTlsTransport=()=>false; export class TlsFingerprintTransport { constructor(){throw Error('network forbidden')} }`); +process.env.NODE_ENV = 'test'; +process.env.TOSUB2_TEST_SENTINEL_TOKEN = 'synthetic'; +const protocol = await import(pathToFileURL(path.join(root, 'protocol.mjs'))); +process.on('exit', () => {}); +const S1='JBSWY3DPEHPK3PXP', S2='GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'; +function fixture(ids, secret, bound='', failure='') { + const calls=[]; + const client={async getJson(method,url,{json}) { + const route=new URL(url).pathname; + calls.push([route,json.id]); + if(route.endsWith('/issue_challenge')) return {data:{}}; + assert.ok(route.endsWith('/mfa/verify')); + if(failure==='ambiguous-success') return {data:{page:{type:'mfa_challenge'}}}; + if(failure) throw Error(failure); + if(json.code !== protocol.generateTotp(json.id==='F1'?S1:S2)) throw Error('HTTP 400 invalid_otp'); + return {data:{continue_url:'https://auth.openai.com/done'}}; + }}; + const options={authBase:'https://auth.openai.com',rl:{question(){throw Error('unexpected manual prompt')}},deviceId:'synthetic', + payload:{page:{type:'mfa_challenge'},'oai-client-auth-session':{mfa_factors:ids.map(id=>({id,factor_type:'totp'}))}}, + totpSecret:secret,referer:'https://auth.openai.com/log-in/password'}; + process.env.CHATGPT_TOTP_FACTOR_ID=bound; + return {client,options,calls}; +} +for(const ids of [['F1','F2'],['F2','F1']]) for(const [secret,id] of [[S1,'F1'],[S2,'F2']]) { + test(`actual MFA matches ${id} with order ${ids.join(',')}`,async()=>{ + const f=fixture(ids,secret); + await protocol.completeTotpMfaIfNeeded(f.client,f.options); + assert.equal(f.client.totpFactorId,id); + assert.ok(f.calls.length<=4); + }); +} +test('bound login challenges only the bound factor',async()=>{ + const f=fixture(['F1','F2'],S2,'F2'); + await protocol.completeTotpMfaIfNeeded(f.client,f.options); + assert.deepEqual(f.calls.map(x=>x[1]),['F2','F2']); +}); +test('missing bound factor aborts without trying others',async()=>{ + const f=fixture(['F1'],S1,'absent'); + await assert.rejects(protocol.completeTotpMfaIfNeeded(f.client,f.options),/TOTP_FACTOR_NOT_FOUND/); + assert.equal(f.calls.length,0); +}); +test('more than two unbound factors requires explicit binding before challenge',async()=>{ + const f=fixture(['F1','F2','F3'],S1); + await assert.rejects(protocol.completeTotpMfaIfNeeded(f.client,f.options),/TOTP_FACTOR_BINDING_REQUIRED/); + assert.equal(f.calls.length,0); +}); +test('duplicate factor IDs do not consume extra challenges',async()=>{ + const f=fixture(['F1','F1','F2'],S2); + await protocol.completeTotpMfaIfNeeded(f.client,f.options); + assert.deepEqual(f.calls.map(x=>x[1]),['F1','F1','F2','F2']); +}); +for(const error of ['HTTP 429 invalid_otp','HTTP 503 invalid_otp','connection reset']) test(`uncertain ${error} never falls back`,async()=>{ + const f=fixture(['F1','F2'],S2,'',error); + await assert.rejects(protocol.completeTotpMfaIfNeeded(f.client,f.options)); + assert.equal(f.calls.length,2); +}); +test('all explicitly rejected stops after two attempts without prompting',async()=>{ + const f=fixture(['F1','F2'],S2,'','HTTP 400 invalid_otp'); + await assert.rejects(protocol.completeTotpMfaIfNeeded(f.client,f.options),/TOTP_REJECTED/); + assert.equal(f.calls.length,4); +}); +test('HTTP 200 with another MFA challenge is not binding evidence',async()=>{ + const f=fixture(['F1','F2'],S2,'','ambiguous-success'); + await assert.rejects(protocol.completeTotpMfaIfNeeded(f.client,f.options),/TOTP_VERIFICATION_UNCONFIRMED/); + assert.equal(f.client.totpFactorId,undefined); + assert.equal(f.calls.length,2); +}); +test.after(async()=>{delete process.env.CHATGPT_TOTP_FACTOR_ID;await fs.rm(root,{recursive:true,force:true});}); diff --git a/tools/test_openai_totp_rotation.py b/tools/test_openai_totp_rotation.py index 2374f3344a4e..48849e401b53 100644 --- a/tools/test_openai_totp_rotation.py +++ b/tools/test_openai_totp_rotation.py @@ -28,19 +28,19 @@ def setUp(self): self.session = Mock() def request(method, path, payload): self.events.append('activate' if path.endswith('activate_enrollment') else 'enroll') - return {'success': True} if path.endswith('activate_enrollment') else {'secret': NEW, 'session_id': 'fake-session'} + return {'success': True} if path.endswith('activate_enrollment') else {'secret': NEW, 'session_id': 'fake-session', 'factor_id': 'F2'} self.session.request.side_effect = request - self.login = Mock(side_effect=[{'cookies': []}, None]) + self.login = Mock(return_value={'web': {'mfaVerified': True, 'mfaFactorId': 'F2'}}) def run_rotation(self): rotate(self.api, self.claim, session_factory=lambda *a: self.session, login=self.login, journal_factory=lambda: self.journal) - def test_activation_requires_both_durable_copies_and_both_verifications(self): + def test_activation_requires_durable_pair_and_bound_candidate_verification(self): self.run_rotation() self.assertLess(self.events.index('journal_candidate'), self.events.index('prepared')) self.assertLess(self.events.index('prepared'), self.events.index('activate')) self.api.totp_finish.assert_called_once_with(7, True) - self.assertEqual([call.args[2] for call in self.login.call_args_list], [NEW, OLD]) + self.assertEqual([call.args[2] for call in self.login.call_args_list], [NEW]) def test_local_write_failure_never_activates(self): self.journal.save.side_effect = [None, OSError('disk full')] @@ -65,46 +65,66 @@ def test_activation_timeout_does_not_reenroll_and_keeps_candidate(self): self.api.totp_finish.assert_called_once_with(7, False, 'activation_unconfirmed') self.login.assert_not_called() - def test_two_valid_factors_preserve_candidate_with_specific_diagnostic(self): - self.login.side_effect = [{}, {}] + def test_two_valid_factors_accepts_addition_without_requiring_revocation(self): + self.login.side_effect = lambda _cfg, claim, secret: {'web': {'mfaVerified': True, 'mfaFactorId': 'F2' if secret == NEW else 'F1'}} self.run_rotation() - self.api.totp_finish.assert_called_once_with(7, False, 'new_accepted_old_valid') - self.assertEqual(self.journal.save.call_args.args[1], NEW) - self.assertEqual(self.session.request.call_count, 2) - self.assertEqual([call.args[2] for call in self.login.call_args_list], [NEW, OLD]) + self.api.totp_finish.assert_called_once_with(7, True) + self.assertEqual(self.login.call_count, 1) + self.assertEqual(self.journal.save.call_args.args[3], 'F2') + self.api.totp_phase.assert_any_call(7, 'bound', NEW, 'F2') - def test_candidate_rejected_remains_verification_failed(self): - self.login.side_effect = [None] + def test_candidate_rejected_remains_uncertain(self): + self.login.return_value = None self.run_rotation() self.api.totp_finish.assert_called_once_with(7, False, 'verification_failed') self.assertEqual(self.login.call_count, 1) - def test_old_recovery_distinguishes_two_valid_factors(self): - self.claim.update(action='verify_old', candidate_secret=NEW) - self.login.side_effect = [{}, {}] + def test_missing_factor_evidence_cannot_finish(self): + self.login.return_value = {'web': {'mfaVerified': True}} self.run_rotation() - self.api.totp_finish.assert_called_once_with(7, False, 'old_accepted_new_valid') - self.session.request.assert_not_called() + self.api.totp_finish.assert_called_once_with(7, False, 'factor_binding_unconfirmed') + + def test_wrong_bound_factor_cannot_finish(self): + self.login.return_value = {'web': {'mfaVerified': True, 'mfaFactorId': 'F1'}} + self.run_rotation() + self.api.totp_finish.assert_called_once_with(7, False, 'factor_binding_unconfirmed') - def test_network_error_is_not_evidence_old_secret_was_rejected(self): - self.login.side_effect = [{}, RotationError('login_failed')] + def test_network_error_does_not_verify_candidate(self): + self.login.side_effect = RotationError('login_failed') self.run_rotation() self.api.totp_finish.assert_called_once_with(7, False, 'verification_failed') - def test_recovery_only_verifies_saved_candidate(self): - self.claim.update(action='verify_new', candidate_secret=NEW) + def test_recovery_only_verifies_saved_candidate_with_its_factor(self): + self.claim.update(action='verify_new', candidate_secret=NEW, candidate_factor_id='F2') self.run_rotation() self.session.request.assert_not_called() - self.journal.save.assert_not_called() - self.api.totp_phase.assert_called_once_with(7, 'verify_recovery') + self.assertEqual(self.login.call_args.args[1]['totp_factor_id'], 'F2') self.api.totp_finish.assert_called_once_with(7, True) - def test_old_recovery_requires_new_secret_rejection(self): - self.claim.update(action='verify_old', candidate_secret=NEW) + def test_old_recovery_does_not_require_candidate_rejection(self): + self.claim.update(action='verify_old', candidate_secret=NEW, totp_factor_id='F1') + self.login.return_value = {'web': {'mfaVerified': True, 'mfaFactorId': 'F1'}} self.run_rotation() - self.assertEqual([call.args[2] for call in self.login.call_args_list], [OLD, NEW]) + self.assertEqual([call.args[2] for call in self.login.call_args_list], [OLD]) + self.api.totp_phase.assert_any_call(7, 'bound', OLD, 'F1') self.api.totp_finish.assert_called_once_with(7, True) + def test_enrollment_session_id_is_not_guessed_to_be_factor_id(self): + self.session.request.side_effect = [{'secret': NEW, 'session_id': 'not-a-factor'}, {'success': True}] + self.run_rotation() + self.assertEqual(self.login.call_args.args[1].get('totp_factor_id', ''), '') + self.api.totp_phase.assert_any_call(7, 'prepared', NEW, '', 'not-a-factor') + self.api.totp_phase.assert_any_call(7, 'bound', NEW, 'F2') + + def test_bound_write_failure_preserves_encrypted_recovery_and_never_reenrolls(self): + def phase(task, stage, *args): + if stage == 'bound': raise TimeoutError('lost bound ack') + self.api.totp_phase.side_effect = phase + self.run_rotation() + self.api.totp_finish.assert_called_once_with(7, False, 'verification_failed') + self.assertEqual(self.session.request.call_count, 2) + self.assertEqual(self.journal.save.call_args.args[3], 'F2') + def test_missing_journal_prevents_all_upstream_calls(self): rotate(self.api, self.claim, session_factory=self.session, journal_factory=Mock(side_effect=OSError('disk unavailable'))) From e8c39e0f91de30925ebfe5809ff2c8836a1fa4b1 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Mon, 16 Nov 2026 20:34:11 +0800 Subject: [PATCH 3/5] ci(totp): fail closed on contaminated PostgreSQL RED evidence Require both recovery subtests to fail their own conflict assertion and replace inverted grep checks with explicit failures. Label the old-Upsert compatibility reproduction accurately. Refs #881 --- .github/scripts/issue881-red-evidence.py | 37 +++++++++++++++++++ .github/scripts/test_issue881_red_evidence.py | 27 ++++++++++++++ .github/workflows/issue881-totp.yml | 17 ++++++--- 3 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 .github/scripts/issue881-red-evidence.py create mode 100644 .github/scripts/test_issue881_red_evidence.py diff --git a/.github/scripts/issue881-red-evidence.py b/.github/scripts/issue881-red-evidence.py new file mode 100644 index 000000000000..f0835fcd3c93 --- /dev/null +++ b/.github/scripts/issue881-red-evidence.py @@ -0,0 +1,37 @@ +"""Validate behavioral RED, including per-subtest diagnostic ownership.""" +import re +import sys +from pathlib import Path + +POLLUTION = r'build failed|setup failed|undefined:|no tests to run|--- SKIP:|panic:|fatal error:|data race|race detected|\btimeout\b|timed out|deadline exceeded' +PARENT = 'TestTOTPRecoveryInsertNeverOverwritesExistingOrConcurrentConfig' + + +def validate(raw): + if re.search(POLLUTION, raw, re.I): + raise ValueError('RED contains infrastructure or execution contamination') + expected = [PARENT + '/existing', PARENT + '/concurrent-insert'] + sections = {} + current = None + for line in raw.splitlines(): + started = re.fullmatch(r'=== RUN\s+(\S+)\s*', line) + if started: + current = started.group(1) + sections.setdefault(current, []) + elif current: + sections[current].append(line) + failures = re.findall(r'^\s*--- FAIL: (\S+) \(', raw, re.M) + if sorted(failures) != sorted([PARENT, *expected]): + raise ValueError('RED must fail exactly the parent and both named subtests') + for name in expected: + section = '\n'.join(sections.get(name, [])) + if not re.search(r'Test:\s+' + re.escape(name) + r'\s*$', section, re.M): + raise ValueError('missing assertion ownership for ' + name) + if not re.search(r'Messages:\s+recovery loser must conflict\s*$', section, re.M): + raise ValueError('missing recovery conflict assertion for ' + name) + return True + + +if __name__ == '__main__': + validate(Path(sys.argv[1]).read_text()) + print('Both recovery subtests failed their own conflict assertion; no contaminated RED.') diff --git a/.github/scripts/test_issue881_red_evidence.py b/.github/scripts/test_issue881_red_evidence.py new file mode 100644 index 000000000000..b8ba6265afcd --- /dev/null +++ b/.github/scripts/test_issue881_red_evidence.py @@ -0,0 +1,27 @@ +import importlib.util +from pathlib import Path +import unittest + +spec=importlib.util.spec_from_file_location('evidence',Path(__file__).with_name('issue881-red-evidence.py')) +evidence=importlib.util.module_from_spec(spec);spec.loader.exec_module(evidence) +P=evidence.PARENT + +def valid(): + return '\n'.join([f'=== RUN {P}', *[f'=== RUN {P}/{name}\n Test: {P}/{name}\n Messages: recovery loser must conflict' for name in ['existing','concurrent-insert']], + f'--- FAIL: {P} (0.1s)',f' --- FAIL: {P}/existing (0.0s)',f' --- FAIL: {P}/concurrent-insert (0.1s)']) + +class EvidenceTests(unittest.TestCase): + def test_accepts_both_owned_behavioral_failures(self): + self.assertTrue(evidence.validate(valid())) + + def test_rejects_missing_failure_or_borrowed_diagnostic(self): + for raw in [valid().replace(f'--- FAIL: {P}/existing','--- PASS: '+P+'/existing'), + valid().replace('Messages: recovery loser must conflict','Messages: unrelated',1), + valid().replace(f'Test: {P}/existing',f'Test: {P}/concurrent-insert')]: + with self.assertRaises(ValueError): evidence.validate(raw) + + def test_rejects_each_execution_contamination(self): + for marker in ['[build failed]','[setup failed]','undefined: name','no tests to run','--- SKIP: x','panic: x','WARNING: DATA RACE','race detected','timeout','test timed out','deadline exceeded']: + with self.subTest(marker=marker), self.assertRaises(ValueError): evidence.validate(valid()+'\n'+marker) + +if __name__=='__main__': unittest.main() diff --git a/.github/workflows/issue881-totp.yml b/.github/workflows/issue881-totp.yml index 2f5880fab9ce..fc83bd60b0ad 100644 --- a/.github/workflows/issue881-totp.yml +++ b/.github/workflows/issue881-totp.yml @@ -6,6 +6,7 @@ on: - 'tools/*totp*' - 'tools/reauth-runtime/**' - '.github/workflows/issue881-totp.yml' + - '.github/scripts/*issue881*' workflow_dispatch: permissions: contents: read @@ -44,10 +45,14 @@ jobs: test "$(git rev-parse HEAD)" = "$EXPECTED_HEAD" mkdir -p evidence git rev-parse HEAD HEAD^{tree} > evidence/head.txt - - name: Real PG RED against pre-fix recovery Upsert SQL + - name: Validate RED evidence gate + run: python3 -m unittest discover -s .github/scripts -p 'test_issue881_red_evidence.py' -v + - name: Real PG RED compatibility reproduction using existing Upsert SQL shell: bash run: | set -euo pipefail + # Compatibility shim delegates to the real existing Upsert SQL; this is + # not a full historical-blob restoration. Restore candidate before GREEN. source=backend/internal/repository/openai_totp_recovery.go cp "$source" "$RUNNER_TEMP/recovery-green.go" trap 'cp "$RUNNER_TEMP/recovery-green.go" "$source"' EXIT @@ -65,8 +70,8 @@ jobs: echo "$result" > evidence/red-recovery-pg.exit cat evidence/red-recovery-pg.log test "$result" -ne 0 - ! grep -Eq 'build failed|setup failed|undefined:|no tests to run' evidence/red-recovery-pg.log - grep -F 'recovery loser must conflict' evidence/red-recovery-pg.log + if grep -Eqi -- 'build failed|setup failed|undefined:|no tests to run|--- SKIP:|panic:|fatal error:|data race|race detected|(^|[^[:alnum:]_])timeout([^[:alnum:]_]|$)|timed out|deadline exceeded' evidence/red-recovery-pg.log; then exit 1; fi + python3 .github/scripts/issue881-red-evidence.py evidence/red-recovery-pg.log - name: Real PG RED against pre-fix migration reload shell: bash run: | @@ -79,6 +84,8 @@ jobs: echo "$result" > evidence/red-migration-pg.exit cat evidence/red-migration-pg.log test "$result" -ne 0 + if grep -Eqi -- 'build failed|setup failed|undefined:|no tests to run|--- SKIP:|panic:|fatal error:|data race|race detected|(^|[^[:alnum:]_])timeout([^[:alnum:]_]|$)|timed out|deadline exceeded' evidence/red-migration-pg.log; then exit 1; fi + grep -Eq -- '^--- FAIL: TestTOTPBindingMigrationCanReloadWithoutReplacingSnapshot \(' evidence/red-migration-pg.log grep -F 'migration reload must be idempotent' evidence/red-migration-pg.log - name: Actual handler and service GREEN shell: bash @@ -86,13 +93,13 @@ jobs: set -euo pipefail git diff --exit-code (cd backend && go test -tags=unit ./internal/handler/admin ./internal/service -run '^Test(TOTP|OpenAIOAuthReauth|OpenAISessionExport)' -count=1 -v -timeout=10m) 2>&1 | tee evidence/green-handler-service.log - ! grep -Eq -- '--- SKIP:|no tests to run' evidence/green-handler-service.log + if grep -Eqi -- 'build failed|setup failed|undefined:|no tests to run|--- SKIP:|panic:|fatal error:|data race|race detected|(^|[^[:alnum:]_])timeout([^[:alnum:]_]|$)|timed out|deadline exceeded' evidence/green-handler-service.log; then exit 1; fi - name: Real PG GREEN migration reload, binding, CAS, recovery conflicts shell: bash run: | set -euo pipefail (cd backend && go test -tags=integration ./internal/repository -run '^Test(TOTP|OAuth429)' -count=1 -v -timeout=10m) 2>&1 | tee evidence/green-persistence.log - ! grep -Eq -- '--- SKIP:|no tests to run' evidence/green-persistence.log + if grep -Eqi -- 'build failed|setup failed|undefined:|no tests to run|--- SKIP:|panic:|fatal error:|data race|race detected|(^|[^[:alnum:]_])timeout([^[:alnum:]_]|$)|timed out|deadline exceeded' evidence/green-persistence.log; then exit 1; fi - name: Production Go build shell: bash run: | From 6f68ca8429a6726ed2a7403b779980cee575e9bd Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Mon, 16 Nov 2026 20:48:19 +0800 Subject: [PATCH 4/5] fix(totp): satisfy binding validation and recovery test lint Check fixture type assertions and express the accepted factor alphabet directly. Repairs exact-head golangci-lint findings without changing the binding contract. Refs #881 --- backend/internal/service/openai_totp_binding.go | 5 +++-- backend/internal/service/openai_totp_rotation_test.go | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/backend/internal/service/openai_totp_binding.go b/backend/internal/service/openai_totp_binding.go index 4f0856769362..3b0f2750a0dd 100644 --- a/backend/internal/service/openai_totp_binding.go +++ b/backend/internal/service/openai_totp_binding.go @@ -23,9 +23,10 @@ func validTOTPFactorID(id string) bool { return false } for _, c := range id { - if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' || c == '-') { - return false + if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' || c == '-' { + continue } + return false } return true } diff --git a/backend/internal/service/openai_totp_rotation_test.go b/backend/internal/service/openai_totp_rotation_test.go index 7c1b76dfc3c0..da6c8f912ef5 100644 --- a/backend/internal/service/openai_totp_rotation_test.go +++ b/backend/internal/service/openai_totp_rotation_test.go @@ -193,7 +193,9 @@ func TestTOTPJournalRecoveryIsIdempotentAndCannotReplaceProvenance(t *testing.T) func TestTOTPRecoveryExportAfterAccountDeletionUsesTaskSnapshot(t *testing.T) { svc, repo := newRotationService(t) ctx := context.Background() - svc.accounts.(*reauthTestAccountReader).account = nil + reader, ok := svc.accounts.(*reauthTestAccountReader) + require.True(t, ok) + reader.account = nil out, err := svc.ExportTOTPRecovery(ctx, 7, "USER@example.com", "candidate", true) require.NoError(t, err) require.Equal(t, "GEZDGNBVGY3TQOJQ", out.Secret) @@ -209,7 +211,8 @@ func TestTOTPRecoveryImportBindsMatchingUnconfiguredAccountWithoutEnrollment(t * ctx := context.Background() require.NoError(t, svc.RecoverTOTPCandidate(ctx, 7, "GEZDGNBVGY3TQOJQ", "F2", "session")) repo.config = nil - reader := svc.accounts.(*reauthTestAccountReader) + reader, ok := svc.accounts.(*reauthTestAccountReader) + require.True(t, ok) reader.account.Credentials["email"] = "wrong@example.com" require.Error(t, svc.ImportTOTPRecovery(ctx, 42, 7, "user@example.com", "candidate")) reader.account.Credentials["email"] = "user@example.com" From 8fa077cac1ed6c85cff7389a8190debed56963a7 Mon Sep 17 00:00:00 2001 From: ranxi2001 Date: Mon, 16 Nov 2026 21:13:33 +0800 Subject: [PATCH 5/5] fix(totp): classify raw MFA rejection from structured HTTP errors Only exact allowlisted OTP codes at the MFA verify HTTP boundary produce a typed rejection. Explicit non-OTP codes, message-only and duplicate fields stop matching. Cover the independent session_expired counterexample through real getJson/assertOk and the complete CLI. Refs #881 --- docs/openai-totp-factor-recovery.md | 4 +- tools/fixtures/totp-http-transport.mjs | 2 +- tools/openai_codex_diagnostics.mjs | 2 +- tools/openai_codex_oauth_adapter.py | 2 +- .../reauth-runtime/totp-factor-binding.patch | 68 ++++++++++++------- tools/test_openai_codex_diagnostics.mjs | 2 +- tools/test_openai_totp_integration.py | 8 +++ tools/test_openai_totp_protocol.mjs | 28 ++++++-- 8 files changed, 80 insertions(+), 36 deletions(-) diff --git a/docs/openai-totp-factor-recovery.md b/docs/openai-totp-factor-recovery.md index e98df46547fe..c635dc383f19 100644 --- a/docs/openai-totp-factor-recovery.md +++ b/docs/openai-totp-factor-recovery.md @@ -12,7 +12,9 @@ Refs #881。真实授权、登录和 2FA 验收 **NOT EXECUTED**,需要另行 保存了 factor ID 的登录只挑战该 ID,ID 不在返回列表中时立即停止。 旧记录没有绑定时,最多匹配两个不同 TOTP factor,各挑战一次;超过两个时 -要求先取得明确绑定。只有明确的 400/401 OTP 拒绝允许尝试下一个 factor。 +要求先取得明确绑定。只有 HTTP 400/401 的结构化 `error` / `error.code` 精确命中允许的 OTP +拒绝码才允许尝试下一个 factor;显式非 OTP code 优先于描述文字。只有 message、 +重复字段或异常文本中的 OTP 字样均不构成重试依据。 429、网络错误、非 OTP 错误、含糊响应或继续要求 MFA 的 HTTP 200 都停止。 不会靠重复 OTP、无限挑战或删除验证器恢复。factor ID 当前接受 1–256 位 ASCII 字母、数字、下划线和连字符,未知格式保持未决,不作猜测。 diff --git a/tools/fixtures/totp-http-transport.mjs b/tools/fixtures/totp-http-transport.mjs index a90c7872bb6f..866f4fdbba28 100644 --- a/tools/fixtures/totp-http-transport.mjs +++ b/tools/fixtures/totp-http-transport.mjs @@ -22,7 +22,7 @@ export class TlsFingerprintTransport { if(route==='POST /api/accounts/password/verify') return Response.json({page:{type:'mfa_challenge'},'oai-client-auth-session':{mfa_factors:fixture.factors.map(id=>({id,factor_type:'totp'}))}}); if(route==='POST /api/accounts/mfa/issue_challenge') return Response.json({}); if(route==='POST /api/accounts/mfa/verify') { - if(fixture.failure) return Response.json({error:'invalid_otp'},{status:fixture.failure}); + if(fixture.failure) return Response.json(fixture.error || {error:'invalid_otp'},{status:fixture.failure}); if(body.code!==fixture.codes[body.id]) return Response.json({error:'invalid_otp'},{status:400}); return Response.json({continue_url:'https://chatgpt.com/auth-complete'}); } diff --git a/tools/openai_codex_diagnostics.mjs b/tools/openai_codex_diagnostics.mjs index 494fb93b7df9..459384b47d5a 100644 --- a/tools/openai_codex_diagnostics.mjs +++ b/tools/openai_codex_diagnostics.mjs @@ -84,7 +84,7 @@ export function isTotpRejectionResponse(result) { const error = payload?.error; const code = typeof error === 'string' ? error : error?.code; if (['invalid_otp','incorrect_otp','wrong_otp','invalid_totp','incorrect_totp','wrong_totp','invalid_code','incorrect_code','wrong_code'].includes(code)) return true; - return code === undefined && typeof error?.message === 'string' && /^(?:invalid|incorrect|wrong)[ _-](?:totp|otp|code)$/i.test(error.message); + return false; // Descriptive text alone is not structured OTP rejection evidence. } export function observeProtocolResponse(result, fallback = 'protocol_error') { diff --git a/tools/openai_codex_oauth_adapter.py b/tools/openai_codex_oauth_adapter.py index 44a4545560ee..85a1970899ab 100644 --- a/tools/openai_codex_oauth_adapter.py +++ b/tools/openai_codex_oauth_adapter.py @@ -16,7 +16,7 @@ from openai_excel_oauth_adapter import replace_exact PINNED_PROTOCOL_BLOB = 'ff2db2507e401c29fa78705e44f5b40a3108a8c6' -BOUND_PROTOCOL_BLOB = 'ffcbb56992f0e34a90821586c63d9bbe27c890d9' +BOUND_PROTOCOL_BLOB = '7d5143da87e36ad79c2d050003df99319736b866' STAGES = frozenset(('starting', 'web_login', 'password_accepted', 'mfa_challenge', 'mfa_accepted', 'web_redirect', 'oauth_authorize', 'session_select', 'workspace_select', 'callback_validation', 'token_exchange', 'oauth_output', 'completed')) diff --git a/tools/reauth-runtime/totp-factor-binding.patch b/tools/reauth-runtime/totp-factor-binding.patch index 7252ed71a86e..6dece3f251b5 100644 --- a/tools/reauth-runtime/totp-factor-binding.patch +++ b/tools/reauth-runtime/totp-factor-binding.patch @@ -1,19 +1,44 @@ --- a/src/protocol-login.mjs +++ b/src/protocol-login.mjs -@@ -1090,6 +1090,7 @@ - emailVerified: true, - loginMethod, - mfaVerified: mfaRequired, +@@ -269,0 +270,3 @@ ++ if (method === "POST" && new URL(url).pathname === "/api/accounts/mfa/verify" && isExplicitTotpRejection(result)) { ++ throw new TotpRejectionError("TOTP_CHALLENGE_REJECTED"); ++ } +@@ -1092,0 +1096 @@ + mfaFactorId: client.totpFactorId || null, - }; - } - -@@ -1133,52 +1134,45 @@ - } - - async function completeTotpMfaIfNeeded(client, { authBase, rl, deviceId, payload, totpSecret, referer }) { +@@ -1134,0 +1139,29 @@ ++class TotpRejectionError extends Error {} ++ ++function isExplicitTotpRejection(result) { ++ if (![400, 401].includes(result?.res?.status)) return false; ++ const raw = result?.text; ++ if (typeof raw !== "string" || Buffer.byteLength(raw) > 32768) return false; ++ try { ++ const payload = JSON.parse(raw); ++ // Duplicate keys make even a known code ambiguous. Reject before using it. ++ const tokens = raw.match(/"(?:[^"\\]|\\.)*"|[{}[\]:,]|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null/g) || []; ++ const stack = []; ++ for (let i = 0; i < tokens.length; i++) { ++ const token = tokens[i]; ++ if (token === "{") stack.push(new Set()); ++ else if (token === "[") stack.push(null); ++ else if (token === "}" || token === "]") stack.pop(); ++ else if (token.startsWith('"') && tokens[i + 1] === ":") { ++ const keys = stack.at(-1), key = JSON.parse(token); ++ if (!keys || keys.has(key)) return false; ++ keys.add(key); ++ } ++ if (stack.length > 32) return false; ++ } ++ const error = payload?.error; ++ const code = typeof error === "string" ? error : error?.code; ++ return ["invalid_otp", "incorrect_otp", "wrong_otp", "invalid_totp", "incorrect_totp", "wrong_totp", "invalid_code", "incorrect_code", "wrong_code"].includes(code); ++ } catch { return false; } ++} ++ +@@ -1135,0 +1169 @@ + delete client.totpFactorId; - if (!isMfaChallengePayload(payload)) return payload; +@@ -1137,3 +1171,6 @@ - const factor = pickTotpFactor(payload); - if (!factor?.id) throw new Error("2FA is required, but the response does not contain a TOTP factor ID"); - @@ -23,7 +48,7 @@ + ...(Array.isArray(session.mfa_factors) ? session.mfa_factors : []), + ].filter(f => f?.factor_type === "totp" && typeof f.id === "string" && /^[A-Za-z0-9_-]{1,256}$/.test(f.id)) + .map(f => [f.id, f])).values()]; - console.log("[mfa] TOTP 2FA challenge reached."); +@@ -1141,17 +1178,13 @@ - await authJsonStep(client, authBase, "POST", "/api/accounts/mfa/issue_challenge", { - type: "totp", - id: factor.id, @@ -54,13 +79,13 @@ + }, { referer }); + const code = generateTotp(totpSecret); + const sentinelHeaders = await createSentinelHeaders(client, {authBase, deviceId, flow: "password_verify"}); - try { +@@ -1159,5 +1191,0 @@ - const sentinelHeaders = await createSentinelHeaders(client, { - authBase, - deviceId, - flow: "password_verify", - }); - const { data } = await authJsonStep(client, authBase, "POST", "/api/accounts/mfa/verify", { +@@ -1165,4 +1193,6 @@ - type: "totp", - id: factor.id, - code, @@ -71,9 +96,7 @@ + throw new Error("TOTP_VERIFICATION_UNCONFIRMED"); + } + client.totpFactorId = factor.id; - console.log("[ok] 2FA verification accepted"); - return data; - } catch (error) { +@@ -1172,10 +1202,6 @@ - if (!isRejectedTotpError(error)) throw error; - console.log( - generatedFromSecret @@ -84,12 +107,9 @@ - code = await askTotpOtp(rl); - } - } -+ // Only an explicit 400/401 OTP rejection permits trying another ID. -+ // In particular a 429/5xx body mentioning an OTP is not a rejection. -+ if (!/HTTP (?:400|401)\b/.test(String(error?.message || "")) || !isRejectedTotpError(error)) throw error; ++ // Only the raw HTTP boundary can establish an explicit OTP rejection. ++ // Never infer it from exception prose or a provider's descriptive message. ++ if (!(error instanceof TotpRejectionError)) throw error; + } + } + throw new Error("TOTP_REJECTED: The code generated from the configured 2FA key was rejected"); - } - - function isRejectedPasswordError(error) { diff --git a/tools/test_openai_codex_diagnostics.mjs b/tools/test_openai_codex_diagnostics.mjs index eafaa939df84..79e971a2c80a 100644 --- a/tools/test_openai_codex_diagnostics.mjs +++ b/tools/test_openai_codex_diagnostics.mjs @@ -86,7 +86,7 @@ test('only explicit 400/401 OTP rejection permits another factor', async () => { const {isTotpRejectionResponse}=await import('./openai_codex_diagnostics.mjs'); const response=(status,error)=>({res:{status},text:JSON.stringify({error})}); assert.equal(isTotpRejectionResponse(response(400,{code:'invalid_otp'})),true); - assert.equal(isTotpRejectionResponse(response(401,{message:'invalid totp'})),true); + assert.equal(isTotpRejectionResponse(response(401,{message:'invalid totp'})),false); for (const value of [response(429,{code:'invalid_otp'}),response(500,{code:'invalid_otp'}),response(400,{code:'unknown',message:'invalid totp'}),response(400,{message:'nested invalid otp data'})]) { assert.equal(isTotpRejectionResponse(value),false); } diff --git a/tools/test_openai_totp_integration.py b/tools/test_openai_totp_integration.py index 391f1bf9e368..e3ee9be89724 100644 --- a/tools/test_openai_totp_integration.py +++ b/tools/test_openai_totp_integration.py @@ -110,4 +110,12 @@ def test_legacy_unbound_candidate_matches_in_both_factor_orders(self): data=rotation.login_web(self.api.config,self.claim,NEW) self.assertEqual(data['web']['mfaFactorId'],'F2') + def test_raw_cli_explicit_nonotp_code_stops_before_second_factor(self): + self.fixture.update(failure=400,error={'error':{'code':'session_expired','message':'incorrect code'}}) + (self.src/'totp-fixture.json').write_text(json.dumps(self.fixture)) + with self.assertRaises(rotation.RotationError): + rotation.login_web(self.api.config,self.claim,NEW) + requests=[json.loads(line) for line in self.trace.read_text().splitlines()] + self.assertEqual([r['id'] for r in requests if '/mfa/' in r['route']],['F1','F1']) + if __name__=='__main__': unittest.main() diff --git a/tools/test_openai_totp_protocol.mjs b/tools/test_openai_totp_protocol.mjs index eadaf0852d9d..0fd38914c18b 100644 --- a/tools/test_openai_totp_protocol.mjs +++ b/tools/test_openai_totp_protocol.mjs @@ -13,7 +13,7 @@ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'totp-protocol-')); let source = await fs.readFile(sourcePath, 'utf8'); assert.equal(source.split('run().catch((error) => {').length, 2); source = source.replace('run().catch((error) => {', 'Promise.resolve().catch((error) => {'); -await fs.writeFile(path.join(root, 'protocol.mjs'), source + '\nexport {completeTotpMfaIfNeeded, generateTotp, loginChatgptWeb};\n'); +await fs.writeFile(path.join(root, 'protocol.mjs'), source + '\nexport {ProtocolClient, completeTotpMfaIfNeeded, generateTotp, loginChatgptWeb};\n'); await fs.writeFile(path.join(root, 'tls-transport.mjs'), `export const browserIdentityForTlsProfile=()=>({}); export const shouldUseTlsTransport=()=>false; export class TlsFingerprintTransport { constructor(){throw Error('network forbidden')} }`); process.env.NODE_ENV = 'test'; process.env.TOSUB2_TEST_SENTINEL_TOKEN = 'synthetic'; @@ -22,15 +22,18 @@ process.on('exit', () => {}); const S1='JBSWY3DPEHPK3PXP', S2='GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'; function fixture(ids, secret, bound='', failure='') { const calls=[]; - const client={async getJson(method,url,{json}) { + const reply=(status,payload)=>({res:{status},text:typeof payload==='string'?payload:JSON.stringify(payload)}); + const client={getJson:protocol.ProtocolClient.prototype.getJson,async request(method,url,{json}) { const route=new URL(url).pathname; calls.push([route,json.id]); - if(route.endsWith('/issue_challenge')) return {data:{}}; + if(route.endsWith('/issue_challenge')) return reply(200,{}); assert.ok(route.endsWith('/mfa/verify')); - if(failure==='ambiguous-success') return {data:{page:{type:'mfa_challenge'}}}; - if(failure) throw Error(failure); - if(json.code !== protocol.generateTotp(json.id==='F1'?S1:S2)) throw Error('HTTP 400 invalid_otp'); - return {data:{continue_url:'https://auth.openai.com/done'}}; + if(failure==='ambiguous-success') return reply(200,{page:{type:'mfa_challenge'}}); + if(typeof failure==='object') return reply(failure.status,failure.payload); + if(failure==='connection reset') throw Error(failure); + if(failure) return reply(Number(failure.split(' ')[1]),{error:{code:'invalid_otp'}}); + if(json.code !== protocol.generateTotp(json.id==='F1'?S1:S2)) return reply(400,{error:{code:'invalid_otp'}}); + return reply(200,{continue_url:'https://auth.openai.com/done'}); }}; const options={authBase:'https://auth.openai.com',rl:{question(){throw Error('unexpected manual prompt')}},deviceId:'synthetic', payload:{page:{type:'mfa_challenge'},'oai-client-auth-session':{mfa_factors:ids.map(id=>({id,factor_type:'totp'}))}}, @@ -82,4 +85,15 @@ test('HTTP 200 with another MFA challenge is not binding evidence',async()=>{ assert.equal(f.client.totpFactorId,undefined); assert.equal(f.calls.length,2); }); +for (const [name,payload] of [ + ['explicit session code overrides OTP-like message',{error:{code:'session_expired',message:'incorrect code'}}], + ['explicit auth code overrides OTP-like message',{error:{code:'authorization_denied',message:'invalid totp'}}], + ['message alone is not a structured rejection',{error:{message:'incorrect code'}}], + ['duplicate code is ambiguous','{"error":{"code":"session_expired","code":"invalid_otp","message":"incorrect code"}}'], +]) test(`raw HTTP boundary stops: ${name}`,async()=>{ + const f=fixture(['F1','F2'],S2,'',{status:400,payload}); + await assert.rejects(protocol.completeTotpMfaIfNeeded(f.client,f.options)); + assert.deepEqual(f.calls.map(x=>x[1]),['F1','F1']); + assert.equal(f.client.totpFactorId,undefined); +}); test.after(async()=>{delete process.env.CHATGPT_TOTP_FACTOR_ID;await fs.rm(root,{recursive:true,force:true});});