Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/scripts/issue881-red-evidence.py
Original file line number Diff line number Diff line change
@@ -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.')
27 changes: 27 additions & 0 deletions .github/scripts/test_issue881_red_evidence.py
Original file line number Diff line number Diff line change
@@ -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()
151 changes: 151 additions & 0 deletions .github/workflows/issue881-totp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
name: Issue881 TOTP persistence acceptance
on:
pull_request:
paths:
- 'backend/**'
- 'tools/*totp*'
- 'tools/reauth-runtime/**'
- '.github/workflows/issue881-totp.yml'
- '.github/scripts/*issue881*'
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: 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
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
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: |
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
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
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
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
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: |
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/
28 changes: 23 additions & 5 deletions .github/workflows/reauth-runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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_*'
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 58 additions & 6 deletions backend/internal/handler/admin/openai_totp_rotation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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"})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("{}"))
Expand Down
Loading
Loading