diff --git a/.github/scripts/batch-whitelist-ui.mjs b/.github/scripts/batch-whitelist-ui.mjs new file mode 100644 index 000000000000..11fb5e6b7354 --- /dev/null +++ b/.github/scripts/batch-whitelist-ui.mjs @@ -0,0 +1,100 @@ +// Run from frontend/. The Vite dev server serves the real modal, locales and Tailwind styles. +import assert from 'node:assert/strict' +import { execFileSync } from 'node:child_process' +import { createHash } from 'node:crypto' +import { readFileSync, writeFileSync, rmSync, mkdirSync } from 'node:fs' +import { createRequire } from 'node:module' +import { resolve, join } from 'node:path' +import { pathToFileURL } from 'node:url' + +const [mode, out] = process.argv.slice(2) +assert.ok(['before', 'after'].includes(mode) && out) +mkdirSync(out, { recursive: true }) +const require = createRequire(resolve('package.json')) +const { createServer } = await import(pathToFileURL(require.resolve('vite').replace(/index\.cjs$/, 'dist/node/index.js'))) +// Resolve the browser from an isolated installation, outside the application. +const { chromium } = createRequire(resolve(process.env.PLAYWRIGHT_ROOT, 'package.json'))('playwright') +const vue = require('@vitejs/plugin-vue') +const html = 'batch-whitelist-evidence.html' +const entry = 'batch-whitelist-evidence.js' +execFileSync('git', ['diff', '--quiet', 'HEAD', '--', 'src']) +const sha = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim() +const manifest = { sha, mode, screenshots: [], fixture: 'Real BulkEditAccountModal and CSS. Synthetic admin, selected IDs and Axios adapter.' } +let browser, server +try { + writeFileSync(html, '
', { flag: 'wx' }) + writeFileSync(entry, ` + import {createApp, h} from 'vue' + import {createPinia} from 'pinia' + import Modal from './src/components/account/BulkEditAccountModal.vue' + import {i18n, loadLocaleMessages} from './src/i18n' + import {useAuthStore} from './src/stores/auth' + import {apiClient} from './src/api/client' + import './src/style.css' + await loadLocaleMessages('zh') + i18n.global.locale.value='zh' + window.calls=[] + window.requests=[] + apiClient.defaults.adapter=async config=>{ + const url=config.url + window.requests.push({method:config.method,url}) + if(config.method !== 'get' && !url.endsWith('/models/sync-upstream')) throw Error('Unexpected fixture write '+url) + let data={} + if(url.endsWith('/models/sync-upstream')){ + window.calls.push(url) + data=url.includes('/42/') && window.calls.length===2 ? {models:[],skipped:'empty'} : {models:['synthetic'],applied:true,changed:true} + } + return {data:{code:0,data},status:200,statusText:'OK',headers:{},config} + } + const pinia=createPinia() + useAuthStore(pinia).user={id:9,role:'admin'} + createApp({render:()=>h(Modal,{show:true,accountIds:[41,42],selectedPlatforms:['anthropic'],selectedTypes:['apikey'],groups:[],proxies:[]})}).use(pinia).use(i18n).mount('#app') + `, { flag: 'wx' }) + server = await createServer({ configFile: false, plugins: [vue()], cacheDir: join(out, 'vite-cache'), + resolve: { alias: { '@': resolve('src') } }, server: { host: '127.0.0.1', port: 0 } }) + await server.listen() + browser = await chromium.launch() + for (const [name, viewport] of [['desktop',{width:1366,height:900}],['mobile',{width:390,height:844}]]) { + const page = await browser.newPage({viewport}) + const errors = [] + page.on('pageerror', e => errors.push(e.message)) + await page.route('**/*', route => new URL(route.request().url()).hostname === '127.0.0.1' ? route.continue() : route.abort()) + await page.goto(server.resolvedUrls.local[0] + html) + await page.locator('#bulk-edit-model-restriction-enabled').check() + const shared = page.getByRole('button', {name:'同步最新支持模型',exact:true}) + const batch = page.getByRole('button', {name:'批量同步上游模型',exact:true}) + assert.equal(await batch.count(), mode === 'after' ? 1 : 0) + const focus = mode === 'before' ? shared : batch + await focus.evaluate(el => el.scrollIntoView({block:'center',behavior:'instant'})) + await page.evaluate(async () => { + await document.fonts.ready + await Promise.all(document.getAnimations().filter(a=>a.effect.getTiming().iterations!==Infinity).map(a=>a.finished.catch(()=>{}))) + }) + if (mode === 'after') { + await batch.click() + await page.getByRole('button',{name:'仅重试失败账号'}).waitFor() + } + const box = await focus.boundingBox() + assert.ok(box && box.x >= 0 && box.x + box.width <= viewport.width && box.y > 0 && box.y + box.height < viewport.height) + assert.ok(await focus.evaluate(el => { + const b=el.getBoundingClientRect() + return el.contains(document.elementFromPoint(b.x+b.width/2,b.y+b.height/2)) + })) + assert.deepEqual(errors, []) + const path = join(out, mode+'-'+name+'.png') + await page.screenshot({path}) + if(mode === 'after') { + await page.getByRole('button',{name:'仅重试失败账号'}).click() + await page.waitForFunction(()=>window.calls.length===3) + assert.deepEqual(await page.evaluate(()=>window.calls),[41,42,42].map(id=>'/admin/accounts/'+id+'/models/sync-upstream')) + } + manifest.screenshots.push({name,viewport,box,sha256:createHash('sha256').update(readFileSync(path)).digest('hex'), requests:await page.evaluate(()=>window.requests)}) + await page.close() + } + writeFileSync(join(out, mode+'-manifest.json'), JSON.stringify(manifest,null,2)+'\n') +} finally { + await browser?.close() + await server?.close() + rmSync(entry,{force:true}) + rmSync(html,{force:true}) +} diff --git a/.github/workflows/account-request-proxy.yml b/.github/workflows/account-request-proxy.yml index 5d891bd36c62..f580e962b733 100644 --- a/.github/workflows/account-request-proxy.yml +++ b/.github/workflows/account-request-proxy.yml @@ -127,13 +127,24 @@ jobs: test "$(git hash-object "$target")" = "$baseline_blob" printf 'path=%s candidate_blob=%s restored_baseline_blob=%s\n' "$target" "$candidate_blob" "$baseline_blob" >> "$manifest" done + # Restoring the token lifecycle above removes its later read-only + # whitelist helper. Pin only the dependent catalog caller to the + # exact pre-helper source; keep the original config-switch defects. + caller_baseline=add606e964972ded3d6389790586c781c89a58c1 + target=backend/internal/service/openai_excel_bps_model_access.go + candidate_blob=$(git rev-parse "$candidate:$target") + test "$(git hash-object "$target")" = "$candidate_blob" + git show "$caller_baseline:$target" > "$target" + baseline_blob=$(git rev-parse "$caller_baseline:$target") + test "$(git hash-object "$target")" = "$baseline_blob" + printf 'compatibility_caller=%s baseline=%s candidate_blob=%s restored_baseline_blob=%s\n' "$target" "$caller_baseline" "$candidate_blob" "$baseline_blob" >> "$manifest" for file in pr885_config_switch_refresh_test.go pr885_siwc_config_switch_test.go pr885_oauth_config_switch_test.go; do target="backend/internal/service/$file" blob=$(git rev-parse "$candidate:$target") test "$(git hash-object "$target")" = "$blob" printf 'unchanged_candidate_fixture=%s blob=%s\n' "$target" "$blob" >> "$manifest" done - expected=$(printf '%s\n' backend/internal/service/openai_excel_oauth.go backend/internal/service/openai_maintenance_proxy.go backend/internal/service/openai_oauth_service.go backend/internal/service/openai_siwc.go backend/internal/service/openai_token_provider.go) + expected=$(printf '%s\n' backend/internal/service/openai_excel_bps_model_access.go backend/internal/service/openai_excel_oauth.go backend/internal/service/openai_maintenance_proxy.go backend/internal/service/openai_oauth_service.go backend/internal/service/openai_siwc.go backend/internal/service/openai_token_provider.go) test "$(git diff --name-only -- 'backend/**/*.go' ':!backend/**/*_test.go')" = "$expected" - name: Require all eight config switch failures to be behavioral shell: bash diff --git a/.github/workflows/batch-upstream-whitelist.yml b/.github/workflows/batch-upstream-whitelist.yml new file mode 100644 index 000000000000..3f9df09c1783 --- /dev/null +++ b/.github/workflows/batch-upstream-whitelist.yml @@ -0,0 +1,163 @@ +name: Batch upstream whitelist +on: + pull_request: + paths: + - 'frontend/src/components/account/*UpstreamWhitelist*' + - 'frontend/src/composables/useBatchUpstreamWhitelist.ts' + - 'frontend/src/components/account/BulkEditAccountModal.vue' + - 'frontend/src/components/account/__tests__/BatchUpstreamWhitelist*' + - 'backend/internal/**/**batch_whitelist*' + - 'backend/internal/service/upstream_model_mapping_sync.go' + - 'backend/internal/service/upstream_models.go' + - 'backend/internal/service/openai_excel_bps_model_access.go' + - 'backend/internal/service/openai_models_list.go' + - 'backend/internal/service/openai_codex_models_service.go' + - 'backend/internal/service/openai_excel_oauth.go' + - 'backend/internal/handler/admin/account_handler.go' + - '.github/workflows/batch-upstream-whitelist.yml' + - '.github/scripts/batch-whitelist-ui.mjs' +permissions: + contents: read +concurrency: + group: batch-upstream-whitelist-${{ github.event.pull_request.number }} + cancel-in-progress: true +env: + BASE: 701612f6e9f7b697eb36397b9fb49bf7324e56a1 +jobs: + contract: + runs-on: ubuntu-latest + timeout-minutes: 55 + env: + CI: true + GOMAXPROCS: 4 + GOFLAGS: -p=4 + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 + - uses: actions/setup-go@v6 + with: + go-version-file: backend/go.mod + cache-dependency-path: backend/go.sum + - name: Same handler oracle on baseline + shell: bash + run: | + set -euo pipefail + git worktree add --detach "$RUNNER_TEMP/before" "$BASE" + file=backend/internal/handler/admin/account_handler_batch_whitelist_test.go + cp "$file" "$RUNNER_TEMP/before/$file" + cd "$RUNNER_TEMP/before/backend" + if go test -v -tags=unit ./internal/handler/admin -run '^TestAccountHandlerBatchUpstreamWhitelist$' -count=1 > "$RUNNER_TEMP/handler-red.log" 2>&1; then exit 1; fi + cat "$RUNNER_TEMP/handler-red.log" + grep -F 'whitelist-only apply must not persist catalog metadata or quality state' "$RUNNER_TEMP/handler-red.log" + grep -F -- '--- FAIL: TestAccountHandlerBatchUpstreamWhitelist' "$RUNNER_TEMP/handler-red.log" + - name: Same incomplete-catalog oracle on baseline + shell: bash + run: | + set -euo pipefail + file=backend/internal/service/upstream_batch_whitelist_test.go + cp "$file" "$RUNNER_TEMP/before/$file" + cd "$RUNNER_TEMP/before/backend" + if go test -v -tags=unit ./internal/service -run 'TestBatchWhitelistSafety/unconsumed|TestBatchWhitelistBPSPartialCatalog' -count=1 > "$RUNNER_TEMP/catalog-red.log" 2>&1; then exit 1; fi + cat "$RUNNER_TEMP/catalog-red.log" + grep -F -- '--- FAIL: TestBatchWhitelistSafety/unconsumed' "$RUNNER_TEMP/catalog-red.log" + grep -F 'partial BPS/native catalogs cannot replace the entire whitelist' "$RUNNER_TEMP/catalog-red.log" + - name: Mapping-only fresh read and account state regression on reviewed head + shell: bash + run: | + set -euo pipefail + reviewed=add606e964972ded3d6389790586c781c89a58c1 + git worktree add --detach "$RUNNER_TEMP/whitelist-read-before" "$reviewed" + file=backend/internal/service/upstream_batch_whitelist_read_policy_test.go + cp "$file" "$RUNNER_TEMP/whitelist-read-before/$file" + cd "$RUNNER_TEMP/whitelist-read-before/backend" + if go test -v -tags=unit ./internal/service -run '^TestBatchWhitelistBPS(NativeStructured401IsReadOnly|NativeStaleFailureKeepsMapping|NativeFreshCompleteSuccess|FreshCacheCannotHideFailure|ExpiredGrantDoesNotRefresh)$' -count=1 > "$RUNNER_TEMP/read-policy-red.log" 2>&1; then exit 1; fi + cat "$RUNNER_TEMP/read-policy-red.log" + grep -F 'whitelist discovery must not disable an account' "$RUNNER_TEMP/read-policy-red.log" + grep -F 'stale native cache cannot authorize a replacement whitelist' "$RUNNER_TEMP/read-policy-red.log" + grep -F 'BPS fresh cache must also be bypassed' "$RUNNER_TEMP/read-policy-red.log" + if grep -Eq 'build failed|setup failed|undefined:|no tests to run|panic:|WARNING: DATA RACE|--- SKIP:' "$RUNNER_TEMP/read-policy-red.log"; then exit 1; fi + - name: Candidate handler and catalog policies + working-directory: backend + run: | + set -o pipefail + go test -v -tags=unit ./internal/handler/admin ./internal/service ./internal/server/middleware -run 'Test(AccountHandler(Batch|SyncUpstreamModels)|BatchWhitelist|ApplyUpstreamModels|AutomaticUpstreamModels|FetchUpstreamSupportedModels|FetchOpenAIModelsList|FetchCodexModelsManifest(OAuth|AgentIdentity401|APIKey401)|SyncUpstreamModelCatalog|ReconciledUpstreamModels|ExcelBPSAccess|ExcelBPSMixed|ExcelOAuthRefresh|AdminAuth)' -count=1 | tee "$RUNNER_TEMP/handler-green.log" + - name: Build production server + run: make -C backend build + - name: PostgreSQL application, scheduler invalidation and stale edit protection + working-directory: backend + run: | + set -o pipefail + go test -v -tags=integration ./internal/repository ./internal/handler/admin -run '^Test(BatchWhitelistPostgresHandlerReadback|ApplyUpstreamModelsPreservesCredentialsAndInvalidatesScheduler)$' -count=1 | tee "$RUNNER_TEMP/postgres.log" + grep -F -- '--- PASS: TestBatchWhitelistPostgresHandlerReadback' "$RUNNER_TEMP/postgres.log" + grep -F -- '--- PASS: TestApplyUpstreamModelsPreservesCredentialsAndInvalidatesScheduler' "$RUNNER_TEMP/postgres.log" + - uses: actions/upload-artifact@v7 + if: always() + with: + name: whitelist-contract-${{ github.event.pull_request.head.sha }} + path: | + ${{ runner.temp }}/handler-red.log + ${{ runner.temp }}/handler-green.log + ${{ runner.temp }}/catalog-red.log + ${{ runner.temp }}/read-policy-red.log + ${{ runner.temp }}/postgres.log + browser: + runs-on: ubuntu-24.04 + timeout-minutes: 35 + env: + NODE_OPTIONS: --max-old-space-size=6144 + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 + - uses: pnpm/action-setup@v6 + with: + version: 9 + - uses: actions/setup-node@v6 + with: + node-version: 22 + cache: pnpm + cache-dependency-path: frontend/pnpm-lock.yaml + - run: pnpm --dir frontend install --frozen-lockfile + - name: Mounted action before and after + shell: bash + run: | + set -euo pipefail + out="$RUNNER_TEMP/evidence" + mkdir -p "$out" + base="$RUNNER_TEMP/before" + git worktree add --detach "$base" "$BASE" + pnpm --dir "$base/frontend" install --frozen-lockfile + oracle=src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts + cp "frontend/$oracle" "$base/frontend/$oracle" + if pnpm --dir "$base/frontend" exec vitest run "$oracle" -t 'submits each selected ID' > "$out/ui-red.log" 2>&1; then exit 1; fi + grep -F 'selected accounts need a per-account upstream action: expected false to be true' "$out/ui-red.log" + pnpm --dir frontend exec vitest run "$oracle" src/views/admin/__tests__/AccountsView.bulkEdit.spec.ts | tee "$out/ui-green.log" + - name: Install isolated Chromium + run: | + npm install --prefix "$RUNNER_TEMP/browser" --no-save --ignore-scripts playwright@1.56.1 + "$RUNNER_TEMP/browser/node_modules/.bin/playwright" install --with-deps chromium + sudo apt-get install -y fonts-noto-cjk + - name: Render original and candidate components + env: + PLAYWRIGHT_ROOT: ${{ runner.temp }}/browser + run: | + script="$PWD/.github/scripts/batch-whitelist-ui.mjs" + (cd "$RUNNER_TEMP/before/frontend" && node "$script" before "$RUNNER_TEMP/evidence") + (cd frontend && node "$script" after "$RUNNER_TEMP/evidence") + - uses: actions/upload-artifact@v7 + if: always() + with: + name: whitelist-ui-${{ github.event.pull_request.head.sha }} + path: | + ${{ runner.temp }}/evidence/*.png + ${{ runner.temp }}/evidence/*.json + ${{ runner.temp }}/evidence/*.log + - name: Complete frontend CI (lint, typecheck, critical manifest) + run: make test-frontend + - name: Build production frontend + run: pnpm --dir frontend run build + env: + NODE_OPTIONS: --max-old-space-size=6144 diff --git a/.github/workflows/issue715-agent-recovery.yml b/.github/workflows/issue715-agent-recovery.yml index 0844a7787598..f0a9c336d6ae 100644 --- a/.github/workflows/issue715-agent-recovery.yml +++ b/.github/workflows/issue715-agent-recovery.yml @@ -60,8 +60,9 @@ jobs: original=6df0cd5bb0ce4d774d34004008f9dd661d236468 bash .github/scripts/historical-recovery-timezone-callers.sh targets=(backend/internal/service/openai_codex_model_metadata.go backend/internal/service/openai_codex_models_service.go backend/internal/service/openai_excel_bps_models_manifest.go) + models_caller=backend/internal/service/openai_models_list.go git fetch --no-tags --depth=1 origin "$original" - trap 'git restore --source=HEAD -- "${targets[@]}" backend/internal/service/openai_request_timezone_egress.go backend/internal/service/openai_ws_client.go; git diff --exit-code' EXIT + trap 'git restore --source=HEAD -- "${targets[@]}" "$models_caller" backend/internal/service/openai_request_timezone_egress.go backend/internal/service/openai_ws_client.go; git diff --exit-code' EXIT git restore --source="$original" -- "${targets[@]}" for target in "${targets[@]}"; do expected=$(git rev-parse "$original:$target") @@ -69,6 +70,21 @@ jobs: echo "baseline=$original path=$target expected_blob=$expected tested_blob=$actual" test "$expected" = "$actual" done + # The historical manifest predates the whitelist-only policy API. + # Reconnect only its caller; retain candidate policy declarations and + # the exact historical capability implementation and test assertions. + python3 - "$models_caller" <<'PYTHON' + from pathlib import Path + import sys + path = Path(sys.argv[1]) + source = path.read_text() + current = 's.fetchNativeCodexModelsManifestWithPolicy(ctx, account, clientVersion, "", policy)' + historical = 's.fetchNativeCodexModelsManifest(ctx, account, clientVersion, "")' + if source.count(current) != 1: + raise SystemExit("missing or ambiguous historical manifest caller") + path.write_text(source.replace(current, historical, 1)) + PYTHON + echo "historical manifest compatibility caller path=$models_caller blob=$(git hash-object "$models_caller")" set +e (cd backend && go test -tags=unit ./internal/service -run '^TestIssue715(BridgeDoesNotAdvertiseEncryptedAgent|FetchedAndPinnedBridgeCapabilities|LiveBridgeCapabilityCannotOverrideWire)$' -count=1 -v -timeout=10m) > "$RUNNER_TEMP/issue715-red.log" 2>&1 result=$? diff --git a/.github/workflows/quality-live-regressions.yml b/.github/workflows/quality-live-regressions.yml index fa3e5e039e8c..91f190c4f6a0 100644 --- a/.github/workflows/quality-live-regressions.yml +++ b/.github/workflows/quality-live-regressions.yml @@ -36,15 +36,32 @@ jobs: set -euo pipefail sources=(backend/internal/service/quality_supported_models.go backend/internal/service/openai_codex_models_service.go backend/internal/service/pelican_scheduled.go) for source in "${sources[@]}"; do cp "$source" "$RUNNER_TEMP/$(basename "$source")"; done + models_caller=backend/internal/service/openai_models_list.go + cp "$models_caller" "$RUNNER_TEMP/quality-models-caller-current.go" # This later regression calls eligibleQualityModels, absent from the pinned # original sources. Keep it out of the historical overlay only; exercise # it explicitly with current sources in the next step. later_test=backend/internal/service/oauth_degradation_scope_test.go cp "$later_test" "$RUNNER_TEMP/oauth-degradation-scope-current.go" - trap 'for source in "${sources[@]}"; do cp "$RUNNER_TEMP/$(basename "$source")" "$source"; done; cp "$RUNNER_TEMP/oauth-degradation-scope-current.go" "$later_test"' EXIT + trap 'for source in "${sources[@]}"; do cp "$RUNNER_TEMP/$(basename "$source")" "$source"; done; cp "$RUNNER_TEMP/quality-models-caller-current.go" "$models_caller"; cp "$RUNNER_TEMP/oauth-degradation-scope-current.go" "$later_test"' EXIT rm "$later_test" git fetch --no-tags --depth=1 origin e082313679d5269eef3e3cce4101c22692695371 for source in "${sources[@]}"; do git show "e082313679d5269eef3e3cce4101c22692695371:$source" > "$source"; done + # Keep the selected historical manifest, with only its dependent + # caller using the API present in that baseline. Candidate policy + # declarations remain available to unrelated retained callers/tests. + python3 - "$models_caller" <<'PYTHON' + from pathlib import Path + import sys + path = Path(sys.argv[1]) + source = path.read_text() + current = 's.fetchNativeCodexModelsManifestWithPolicy(ctx, account, clientVersion, "", policy)' + historical = 's.fetchNativeCodexModelsManifest(ctx, account, clientVersion, "")' + if source.count(current) != 1: + raise SystemExit("missing or ambiguous historical manifest caller") + path.write_text(source.replace(current, historical, 1)) + PYTHON + echo "historical manifest compatibility caller path=$models_caller blob=$(git hash-object "$models_caller")" python3 .github/scripts/quality-live-red-compat.py "$RUNNER_TEMP/quality_supported_models.go" set +e (cd backend && go test -tags=unit ./internal/service -run '^TestQualityLive' -count=1 -v) > evidence/red-service.log 2>&1 diff --git a/Makefile b/Makefile index d9514a622215..4d3c4778c2fc 100644 --- a/Makefile +++ b/Makefile @@ -124,6 +124,7 @@ FRONTEND_CRITICAL_VITEST := \ src/components/account/__tests__/OpenAIReferralCell.transport.spec.ts \ src/components/account/__tests__/OpenAIQuotaResetCell.spark_shadow.spec.ts \ src/components/account/__tests__/ExcelBPS403Badge.spec.ts \ + src/components/account/__tests__/BatchUpstreamWhitelist.spec.ts \ src/components/account/__tests__/BulkEditAccountModal.spec.ts \ src/constants/__tests__/platforms.spec.ts \ src/components/account/__tests__/credentialsBuilder.platformCatalog.spec.ts \ diff --git a/backend/internal/handler/admin/account_handler.go b/backend/internal/handler/admin/account_handler.go index 4bad70a4d8c5..414a0d41bba7 100644 --- a/backend/internal/handler/admin/account_handler.go +++ b/backend/internal/handler/admin/account_handler.go @@ -3335,8 +3335,8 @@ func (h *AccountHandler) SyncUpstreamModels(c *gin.Context) { response.BadRequest(c, "Invalid upstream model sync options") return } - if options.Automatic && !options.Apply { - response.BadRequest(c, "Automatic model sync requires apply=true") + if err := options.Validate(); err != nil { + response.BadRequest(c, err.Error()) return } if _, observer := service.ObserverGroupIDs(c.Request.Context()); observer && options.Apply { @@ -3344,7 +3344,7 @@ func (h *AccountHandler) SyncUpstreamModels(c *gin.Context) { return } accountID, err := strconv.ParseInt(c.Param("id"), 10, 64) - if err != nil { + if err != nil || accountID <= 0 { response.BadRequest(c, "Invalid account ID") return } @@ -3380,7 +3380,7 @@ func (h *AccountHandler) SyncUpstreamModels(c *gin.Context) { response.Error(c, http.StatusBadGateway, "Failed to sync upstream models from upstream") return } - if catalog != nil && account != nil && account.IsGrok() && account.ID > 0 && h.accountTestService != nil { + if !options.WhitelistOnly && catalog != nil && account != nil && account.IsGrok() && account.ID > 0 && h.accountTestService != nil { if latest, readErr := h.adminService.GetAccount(c.Request.Context(), account.ID); readErr == nil && latest != nil { catalog.GrokModelDiagnostics = h.accountTestService.GetGrokModelDiagnostics(c.Request.Context(), latest) } diff --git a/backend/internal/handler/admin/account_handler_batch_whitelist_integration_test.go b/backend/internal/handler/admin/account_handler_batch_whitelist_integration_test.go new file mode 100644 index 000000000000..9f3f4a83d361 --- /dev/null +++ b/backend/internal/handler/admin/account_handler_batch_whitelist_integration_test.go @@ -0,0 +1,122 @@ +//go:build integration + +package admin + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "database/sql" + "entgo.io/ent/dialect" + entsql "entgo.io/ent/dialect/sql" + dbent "github.com/Wei-Shaw/sub2api/ent" + _ "github.com/Wei-Shaw/sub2api/ent/runtime" + "github.com/Wei-Shaw/sub2api/internal/config" + "github.com/Wei-Shaw/sub2api/internal/pkg/tlsfingerprint" + "github.com/Wei-Shaw/sub2api/internal/repository" + "github.com/Wei-Shaw/sub2api/internal/service" + "github.com/gin-gonic/gin" + _ "github.com/lib/pq" + "github.com/stretchr/testify/require" + tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres" +) + +type batchWhitelistAdmin struct { + service.AdminService + repo service.AccountRepository +} + +func (s *batchWhitelistAdmin) GetAccount(ctx context.Context, id int64) (*service.Account, error) { + return s.repo.GetByID(ctx, id) +} + +type batchWhitelistHTTP struct { + t *testing.T + failID int64 +} + +func (u *batchWhitelistHTTP) Do(req *http.Request, _ string, id int64, _ int) (*http.Response, error) { + require.Equal(u.t, http.MethodGet, req.Method) + require.Equal(u.t, fmt.Sprintf("https://up-%d.example/v1/models", id), req.URL.String()) + body := fmt.Sprintf(`{"data":[{"id":"live-%d"}]}`, id) + code := 200 + if id == u.failID { + body = `{"error":"unavailable"}` + code = 503 + } + return &http.Response{StatusCode: code, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}, nil +} +func (u *batchWhitelistHTTP) DoWithTLS(req *http.Request, proxy string, id int64, concurrency int, _ *tlsfingerprint.Profile) (*http.Response, error) { + return u.Do(req, proxy, id, concurrency) +} + +func TestBatchWhitelistPostgresHandlerReadback(t *testing.T) { + ctx := context.Background() + container, err := tcpostgres.Run(ctx, "postgres:18.1-alpine3.23", + tcpostgres.WithDatabase("whitelist"), tcpostgres.WithUsername("fixture"), tcpostgres.WithPassword("fixture"), tcpostgres.BasicWaitStrategies()) + require.NoError(t, err) + t.Cleanup(func() { _ = container.Terminate(context.Background()) }) + dsn, err := container.ConnectionString(ctx, "sslmode=disable", "TimeZone=UTC") + require.NoError(t, err) + db, err := sql.Open("postgres", dsn) + require.NoError(t, err) + t.Cleanup(func() { _ = db.Close() }) + require.NoError(t, repository.ApplyMigrations(ctx, db)) + client := dbent.NewClient(dbent.Driver(entsql.OpenDB(dialect.Postgres, db))) + repo := repository.NewAccountRepository(client, db, nil) + ids := make([]int64, 0, 4) + for range 4 { + a := &service.Account{ + Name: "synthetic-whitelist", Platform: service.PlatformOpenAI, Type: service.AccountTypeAPIKey, Status: service.StatusActive, Schedulable: true, + Credentials: map[string]any{"api_key": "synthetic", "model_mapping": map[string]any{"old": "old"}}, + Extra: map[string]any{"keep": "unchanged"}, Concurrency: 3, Priority: 19, + } + require.NoError(t, repo.Create(ctx, a)) + a.Credentials["base_url"] = fmt.Sprintf("https://up-%d.example/v1", a.ID) + require.NoError(t, repo.Update(ctx, a)) + ids = append(ids, a.ID) + } + svc := service.NewAccountTestService(repo, nil, nil, nil, nil, &batchWhitelistHTTP{t: t, failID: ids[1]}, &config.Config{}, nil) + h := NewAccountHandler(&batchWhitelistAdmin{repo: repo}, nil, nil, nil, nil, nil, nil, nil, svc, nil, nil, nil, nil, nil) + r := gin.New() + r.POST("/accounts/:id/models/sync-upstream", h.SyncUpstreamModels) + r.GET("/accounts/:id", h.GetByID) + read := func(id int64) map[string]any { + w := httptest.NewRecorder() + r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/accounts/%d", id), nil)) + require.Equal(t, 200, w.Code, w.Body.String()) + var response struct{ Data map[string]any } + require.NoError(t, json.Unmarshal(w.Body.Bytes(), &response)) + return response.Data + } + before := make(map[int64]map[string]any) + for _, id := range ids { + before[id] = read(id) + } + for _, id := range ids[:3] { + w := httptest.NewRecorder() + r.ServeHTTP(w, httptest.NewRequest(http.MethodPost, fmt.Sprintf("/accounts/%d/models/sync-upstream", id), strings.NewReader(`{"apply":true,"whitelist_only":true}`))) + if id == ids[1] { + require.Equal(t, 502, w.Code) + } else { + require.Equal(t, 200, w.Code, w.Body.String()) + } + } + for _, id := range ids { + after := read(id) + if id == ids[0] || id == ids[2] { + model := fmt.Sprintf("live-%d", id) + require.Equal(t, map[string]any{model: model}, after["credentials"].(map[string]any)["model_mapping"]) + after["credentials"].(map[string]any)["model_mapping"] = before[id]["credentials"].(map[string]any)["model_mapping"] + // The existing CAS intentionally bumps the account revision. + after["updated_at"] = before[id]["updated_at"] + } + require.Equal(t, before[id], after, "only selected successful white lists may change") + } +} diff --git a/backend/internal/handler/admin/account_handler_batch_whitelist_test.go b/backend/internal/handler/admin/account_handler_batch_whitelist_test.go new file mode 100644 index 000000000000..f3026ac11c03 --- /dev/null +++ b/backend/internal/handler/admin/account_handler_batch_whitelist_test.go @@ -0,0 +1,176 @@ +package admin + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/Wei-Shaw/sub2api/internal/config" + "github.com/Wei-Shaw/sub2api/internal/pkg/tlsfingerprint" + "github.com/Wei-Shaw/sub2api/internal/server/middleware" + "github.com/Wei-Shaw/sub2api/internal/service" + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/require" +) + +// Only the storage and outbound HTTP boundaries are replaced. The same fixture +// drives Gin binding, credential selection, extraction, policy and apply for A/B. +type whitelistStore struct { + service.AccountRepository + accounts map[int64]*service.Account + extraWrites int +} + +func (s *whitelistStore) UpdateExtra(context.Context, int64, map[string]any) error { + s.extraWrites++ + return nil +} + +func (s *whitelistStore) ClaimAutomaticUpstreamModelSync(context.Context, int64, time.Time, time.Time) (bool, error) { + return false, fmt.Errorf("unexpected automatic claim") +} + +func (s *whitelistStore) UpdateUpstreamModelMappingIfUnchanged(_ context.Context, account *service.Account, mapping map[string]string) (bool, error) { + s.accounts[account.ID].Credentials["model_mapping"] = mapping + return true, nil +} + +type whitelistAdmin struct { + *stubAdminService + store *whitelistStore +} + +func (s *whitelistAdmin) GetAccount(_ context.Context, id int64) (*service.Account, error) { + if account := s.store.accounts[id]; account != nil { + return account, nil + } + return nil, service.ErrAccountNotFound +} + +type whitelistTransport struct { + syncUpstreamHTTPUpstream + t *testing.T + lookups []int64 +} + +func (u *whitelistTransport) DoWithTLS(req *http.Request, proxy string, id int64, _ int, _ *tlsfingerprint.Profile) (*http.Response, error) { + body, status := "{}", http.StatusOK + if req.URL.Host != "models.dev" { + require.Equal(u.t, http.MethodGet, req.Method) + require.Equal(u.t, fmt.Sprintf("https://up-%d.example/v1/models", id), req.URL.String()) + require.Equal(u.t, fmt.Sprintf("Bearer fixture-%d", id), req.Header.Get("Authorization")) + require.Equal(u.t, fmt.Sprintf("http://proxy-%d.example:80", id), proxy) + u.lookups = append(u.lookups, id) + switch id { + case 43: + status = http.StatusForbidden + case 44: + body = `{"data":[]}` + case 45: + status = http.StatusNotFound + default: + body = fmt.Sprintf(`{"data":[{"id":"model-%d","reasoning":true,"default_reasoning_level":"high","supported_reasoning_levels":["high"],"input_modalities":["text"],"context_window":8192},{"id":"model-%d"},{"id":"*"}]}`, id, id) + } + } + return &http.Response{StatusCode: status, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}, nil +} + +func TestAccountHandlerBatchUpstreamWhitelist(t *testing.T) { + store := &whitelistStore{accounts: make(map[int64]*service.Account)} + for id := int64(41); id <= 46; id++ { + proxy := &service.Proxy{ID: id, Host: fmt.Sprintf("proxy-%d.example", id), Port: 80, Protocol: "http"} + store.accounts[id] = &service.Account{ID: id, Platform: service.PlatformOpenAI, Type: service.AccountTypeAPIKey, Status: service.StatusActive, + ProxyID: &proxy.ID, Proxy: proxy, Extra: map[string]any{"keep": true}, + Credentials: map[string]any{"base_url": fmt.Sprintf("https://up-%d.example/v1", id), "api_key": fmt.Sprintf("fixture-%d", id), "model_mapping": map[string]string{"old": "old", "public": fmt.Sprintf("model-%d", id)}}} + } + up := &whitelistTransport{t: t} + svc := service.NewAccountTestService(store, nil, nil, nil, nil, up, &config.Config{}, nil) + h := NewAccountHandler(&whitelistAdmin{newStubAdminService(), store}, nil, nil, nil, nil, nil, nil, nil, svc, nil, nil, nil, nil, nil) + r := gin.New() + r.POST("/accounts/:id/models/sync-upstream", h.SyncUpstreamModels) + body := `{"apply":true,"whitelist_only":true}` + post := func(id string, payload string, observer bool) *httptest.ResponseRecorder { + req := httptest.NewRequest(http.MethodPost, "/accounts/"+id+"/models/sync-upstream", strings.NewReader(payload)) + req.Header.Set("Content-Type", "application/json") + if observer { + req = req.WithContext(service.WithObserverScope(req.Context(), []int64{1})) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + return w + } + for _, id := range []int64{43, 41, 44, 42, 45} { + w := post(fmt.Sprint(id), body, false) + a := store.accounts[id] + if id == 41 || id == 42 { + require.Equal(t, 200, w.Code, w.Body.String()) + var result struct{ Data service.UpstreamModelCatalog } + require.NoError(t, json.Unmarshal(w.Body.Bytes(), &result)) + require.True(t, result.Data.Applied) + model := fmt.Sprintf("model-%d", id) + require.Equal(t, map[string]string{model: model, "public": model}, a.Credentials["model_mapping"]) + } else { + if w.Code == 200 { + require.Contains(t, w.Body.String(), `"skipped"`) + } else { + require.Equal(t, http.StatusBadGateway, w.Code) + } + require.Contains(t, a.Credentials["model_mapping"], "old") + } + } + require.Zero(t, store.extraWrites, "whitelist-only apply must not persist catalog metadata or quality state") + require.Contains(t, store.accounts[46].Credentials["model_mapping"], "old", "unselected account") + require.Equal(t, 403, post("41", body, true).Code) + for _, invalid := range []string{"0", "-1", "not-an-id"} { + require.Equal(t, 400, post(invalid, body, false).Code) + } + require.Equal(t, 404, post("47", body, false).Code) + require.Equal(t, 400, post("46", `{"whitelist_only":true}`, false).Code) + require.Equal(t, 400, post("46", `{"whitelist_only":true,"apply":true,"automatic":true}`, false).Code) + require.Equal(t, []int64{43, 41, 44, 42, 45}, up.lookups) +} + +type whitelistActor struct { + service.UserRepository + user *service.User +} + +func (r *whitelistActor) GetByID(context.Context, int64) (*service.User, error) { return r.user, nil } + +func (r *whitelistActor) GetUserAvatar(context.Context, int64) (*service.UserAvatar, error) { + return nil, nil +} + +func TestAccountHandlerBatchWhitelistAuthentication(t *testing.T) { + for _, tc := range []struct { + role string + code int + }{{"", 401}, {service.RoleUser, 403}, {service.RoleObserver, 403}, {service.RoleAdmin, 404}} { + t.Run(tc.role, func(t *testing.T) { + cfg := &config.Config{JWT: config.JWTConfig{Secret: "synthetic-whitelist", ExpireHour: 1}} + auth := service.NewAuthService(nil, nil, nil, nil, cfg, nil, nil, nil, nil, nil, nil, nil, nil) + actor := &service.User{ID: 19, Role: tc.role, Status: service.StatusActive} + user := service.NewUserService(&whitelistActor{user: actor}, nil, nil, nil) + h := NewAccountHandler(&whitelistAdmin{newStubAdminService(), &whitelistStore{accounts: map[int64]*service.Account{}}}, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) + r := gin.New() + r.Use(gin.HandlerFunc(middleware.NewAdminAuthMiddleware(auth, user, nil, nil))) + r.POST("/api/v1/admin/accounts/:id/models/sync-upstream", h.SyncUpstreamModels) + req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/accounts/41/models/sync-upstream", strings.NewReader(`{"apply":true,"whitelist_only":true}`)) + req.Header.Set("Content-Type", "application/json") + if tc.role != "" { + token, err := auth.GenerateToken(context.Background(), actor) + require.NoError(t, err) + req.Header.Set("Authorization", "Bearer "+token) + } + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + require.Equal(t, tc.code, w.Code, w.Body.String()) + }) + } +} diff --git a/backend/internal/service/openai_codex_models_service.go b/backend/internal/service/openai_codex_models_service.go index eb87ac7ee52e..198b36e2a787 100644 --- a/backend/internal/service/openai_codex_models_service.go +++ b/backend/internal/service/openai_codex_models_service.go @@ -1739,7 +1739,11 @@ func (s *OpenAIGatewayService) FetchCodexModelsManifest(ctx context.Context, acc } func (s *OpenAIGatewayService) fetchNativeCodexModelsManifest(ctx context.Context, account *Account, clientVersion, ifNoneMatch string) (*OpenAIModelsResponse, error) { - useCache := !isFreshOpenAICredentialProbe(ctx) + return s.fetchNativeCodexModelsManifestWithPolicy(ctx, account, clientVersion, ifNoneMatch, openAIModelsReadDefault) +} + +func (s *OpenAIGatewayService) fetchNativeCodexModelsManifestWithPolicy(ctx context.Context, account *Account, clientVersion, ifNoneMatch string, policy openAIModelsReadPolicy) (*OpenAIModelsResponse, error) { + useCache := policy != openAIModelsReadWhitelist && !isFreshOpenAICredentialProbe(ctx) if account == nil { return nil, infraerrors.New(http.StatusInternalServerError, "OPENAI_CODEX_MODELS_ACCOUNT_REQUIRED", "account is required") } @@ -1751,6 +1755,11 @@ func (s *OpenAIGatewayService) fetchNativeCodexModelsManifest(ctx context.Contex if credAccount.IsExcelOAuth() { return nil, errExcelOAuthRouteUnavailable } + if policy == openAIModelsReadWhitelist && credAccount.IsOpenAIAgentIdentity() { + // Even building these headers can create/persist a task. Whitelist + // discovery never owns the agent identity task lifecycle. + return nil, infraerrors.BadRequest("OPENAI_MODELS_READ_ONLY_ACCOUNT_UNSUPPORTED", "Read-only model discovery does not support agent identity accounts") + } clientVersion = strings.TrimSpace(clientVersion) if clientVersion == "" { clientVersion = CodexCanonicalClientVersion() @@ -1851,6 +1860,11 @@ func (s *OpenAIGatewayService) fetchNativeCodexModelsManifest(ctx context.Contex request.headers.Del("If-None-Match") request.headers.Set("Cache-Control", "no-cache") } + if policy == openAIModelsReadWhitelist { + // Bypass the shared display cache and its detached refresh completely. + // Return the actual response before OAuth health handling/task recovery. + return s.fetchCodexModelsManifestUpstream(ctx, request, "") + } if useAPIKeyUpstream { if !useCache { return s.fetchCodexModelsManifestUpstream(ctx, request, "") diff --git a/backend/internal/service/openai_excel_bps_model_access.go b/backend/internal/service/openai_excel_bps_model_access.go index 1ceb7eeb37a9..26ecd0521021 100644 --- a/backend/internal/service/openai_excel_bps_model_access.go +++ b/backend/internal/service/openai_excel_bps_model_access.go @@ -87,13 +87,23 @@ func excelBPSAccessModelsBody(raw []byte) ([]byte, error) { // FetchExcelBPSModelsList uses a separate fresh-only cache scoped by bearer, // account and proxy identity. Expired permissions are never served stale. func (s *OpenAIGatewayService) FetchExcelBPSModelsList(ctx context.Context, account *Account) (*OpenAIModelsResponse, error) { + return s.fetchExcelBPSModelsListWithPolicy(ctx, account, openAIModelsReadDefault) +} + +func (s *OpenAIGatewayService) fetchExcelBPSModelsListWithPolicy(ctx context.Context, account *Account, policy openAIModelsReadPolicy) (*OpenAIModelsResponse, error) { if err := ctx.Err(); err != nil { return nil, err } if s == nil || s.httpUpstream == nil || account == nil || !account.IsOpenAI() || !account.IsExcelBPSEnabled() { return nil, fmt.Errorf("excel BPS model discovery is unavailable") } - token, err := s.getExcelBPSAccessToken(ctx, account) + var token string + var err error + if policy == openAIModelsReadWhitelist { + token, err = s.getExcelBPSAccessTokenReadOnly(ctx, account) + } else { + token, err = s.getExcelBPSAccessToken(ctx, account) + } if err != nil { if IsExcelAuthorizationError(err) { return nil, err @@ -112,17 +122,25 @@ func (s *OpenAIGatewayService) FetchExcelBPSModelsList(ctx context.Context, acco request.Body = nil request.ContentLength = 0 request.Header.Set("Accept", "application/json") + if policy == openAIModelsReadWhitelist { + request.Header.Set("Cache-Control", "no-cache") + request.Header.Del("If-None-Match") + } request.URL.Path = "/basispoints/api/responses/access" request.URL.RawQuery = "include_models=true" proxy := upstreamModelsProxyURL(account) identity := openAIModelsRequest{url: request.URL.String(), headers: request.Header, proxyURL: fmt.Sprintf("%s|bps_pool:%s|managed:%t", proxy, account.ExcelBPSProxySource(), account.IsExcelBPSMihomoEnabled()), accountID: account.ID, credentialAccountID: account.ID, standardModelsList: true} key := buildOpenAIModelsCacheKey(identity) - if cached, state := s.excelBPSModelsCache.get(key, time.Now()); state == openAIModelsCacheFresh { - return openAIModelsResponseForClient(cached, ""), nil + if policy != openAIModelsReadWhitelist { + if cached, state := s.excelBPSModelsCache.get(key, time.Now()); state == openAIModelsCacheFresh { + return openAIModelsResponseForClient(cached, ""), nil + } } fetch := func(fetchCtx context.Context) (*OpenAIModelsResponse, error) { - if cached, state := s.excelBPSModelsCache.get(key, time.Now()); state == openAIModelsCacheFresh { - return cached, nil + if policy != openAIModelsReadWhitelist { + if cached, state := s.excelBPSModelsCache.get(key, time.Now()); state == openAIModelsCacheFresh { + return cached, nil + } } fetchCtx, cancel := context.WithTimeout(fetchCtx, 5*time.Second) defer cancel() @@ -166,9 +184,16 @@ func (s *OpenAIGatewayService) FetchExcelBPSModelsList(ctx context.Context, acco return nil, err } manifest := &OpenAIModelsResponse{Body: body, ETag: codexModelsManifestBodyETag(body)} - s.excelBPSModelsCache.set(key, manifest, time.Now()) + if policy != openAIModelsReadWhitelist { + s.excelBPSModelsCache.set(key, manifest, time.Now()) + } return manifest, nil } + if policy == openAIModelsReadWhitelist { + // No shared cache, detached singleflight or background work can verify + // the current destructive edit. Cancellation belongs to this caller. + return fetch(ctx) + } if HasAPIKeyAdmissionOwner(ctx) { result, err := fetch(ctx) if err != nil { @@ -281,21 +306,31 @@ func applyExcelBPSEffortsToManifest(body, access []byte, account *Account) ([]by // Independent BPS discovery applies to the actual selected model route. Native // models keep their own source; a native source failure cannot invent BPS access. func (s *OpenAIGatewayService) fetchExcelBPSAccountModels(ctx context.Context, account *Account) (*OpenAIModelsResponse, error) { - access, accessErr := s.FetchExcelBPSModelsList(ctx, account) + return s.fetchExcelBPSAccountModelsWithPolicy(ctx, account, false) +} + +func (s *OpenAIGatewayService) fetchExcelBPSAccountModelsWithPolicy(ctx context.Context, account *Account, requireComplete bool) (*OpenAIModelsResponse, error) { + policy := openAIModelsReadDefault + if requireComplete { + policy = openAIModelsReadWhitelist + } + access, accessErr := s.fetchExcelBPSModelsListWithPolicy(ctx, account, policy) if accessErr != nil { if ctx.Err() != nil { return nil, ctx.Err() } - if account.IsExcelOAuth() || account.isExcelBPSAllModelsEnabled() { + if requireComplete || account.IsExcelOAuth() || account.isExcelBPSAllModelsEnabled() { return nil, accessErr } access = &OpenAIModelsResponse{Body: []byte(`{"object":"list","data":[]}`)} } var native []byte if !account.IsExcelOAuth() && !account.isExcelBPSAllModelsEnabled() { - response, err := s.fetchNativeOpenAIModelsList(ctx, account) + response, err := s.fetchNativeOpenAIModelsListWithPolicy(ctx, account, policy) if err == nil { native = response.Body + } else if requireComplete { + return nil, err } else if ctx.Err() != nil { return nil, ctx.Err() } diff --git a/backend/internal/service/openai_excel_oauth.go b/backend/internal/service/openai_excel_oauth.go index a9f833cc4b04..f12f5b0c584f 100644 --- a/backend/internal/service/openai_excel_oauth.go +++ b/backend/internal/service/openai_excel_oauth.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "log/slog" + "strconv" "strings" "sync" "time" @@ -412,6 +413,80 @@ func (s *OpenAIGatewayService) getExcelBPSAccessToken(ctx context.Context, accou return token, err } +// A whitelist edit may read a saved grant, but must not refresh/rotate it or +// change its authorization state. Unready credentials fail this edit only. +func (s *OpenAIGatewayService) getExcelBPSAccessTokenReadOnly(ctx context.Context, account *Account) (string, error) { + if account == nil { + return "", errors.New("account is missing") + } + if err := validateOpenAIConnectionKind(account); err != nil { + return "", err + } + if s.excelOAuthReauth != nil { + if err := s.excelOAuthReauth.validateAuthConnectionPolicy(ctx, account); err != nil { + return "", err + } + } + if account.AuthConnectionID != "" { + reauth := s.excelOAuthReauth + if reauth == nil { + return "", errors.New("authorization connection service unavailable") + } + if err := reauth.validateConnectionMaintenanceProxyMode(ctx, account.ID); err != nil { + return "", err + } + if err := reauth.ValidateAuthConnectionSend(ctx, account); err != nil { + return "", err + } + connection, err := reauth.authConnection(ctx, account.ID, account.AuthConnectionID) + if err != nil { + return "", err + } + if connection.Legacy || connection.Kind != "bps" || connection.Revision != account.AuthConnectionRevision || + connection.Status != "active" || (connection.RefreshState != "" && connection.RefreshState != "idle") || + connection.RefreshCandidateCiphertext != "" || len(connection.Capabilities) == 0 || + (connection.CooldownUntil != nil && time.Now().Before(*connection.CooldownUntil)) { + return "", connectionRefreshUnavailable() + } + credentials, err := reauth.readConnectionCredentials(connection) + if err != nil { + return "", err + } + expiry, _ := strconv.ParseInt(stringCredential(credentials, "expires_at"), 10, 64) + token := strings.TrimSpace(stringCredential(credentials, "access_token")) + if token == "" || expiry <= time.Now().Add(openAITokenRefreshSkew).Unix() { + return "", connectionRefreshUnavailable() + } + return token, nil + } + if account.HasManagedOpenAIConnections() || account.IsOpenAIAgentIdentity() { + return "", errors.New("read-only BPS discovery requires a saved authorization") + } + if s.excelOAuthReauth != nil { + _, credentials, fresh, err := s.excelOAuthReauth.readExcelGrant(ctx, account) + if !errors.Is(err, errExcelLegacyCredentials) { + if err != nil { + return "", err + } + claims, err := openai.DecodeIDToken(reauthMapString(credentials, "access_token")) + if err != nil || credentials[excelAccessTokenRejectedKey] == true || claims.Exp <= time.Now().Add(openAITokenRefreshSkew).Unix() { + return "", errors.New("saved Excel access token requires refresh") + } + return validatedExcelAccessToken(fresh, credentials) + } + } + // Legacy/pure gateway accounts already carry their saved token. Do not + // enter GetAccessToken: its provider may refresh or disable the account. + if expiry := account.GetCredentialAsTime("expires_at"); expiry != nil && !expiry.After(time.Now().Add(openAITokenRefreshSkew)) { + return "", errors.New("saved BPS access token requires refresh") + } + token := strings.TrimSpace(account.GetOpenAIAccessToken()) + if token == "" { + return "", errors.New("saved BPS access token is unavailable") + } + return token, nil +} + // Reject only the access token actually used by this request. Preserve its RT // for the normal refresh flow, and never overwrite a concurrently rotated grant. func (s *OpenAIOAuthReauthService) invalidateExcelAccessToken(ctx context.Context, id int64, token string, cache OpenAITokenCache) error { diff --git a/backend/internal/service/openai_models_list.go b/backend/internal/service/openai_models_list.go index 2a423ffffdc8..0cd825bc83c0 100644 --- a/backend/internal/service/openai_models_list.go +++ b/backend/internal/service/openai_models_list.go @@ -13,6 +13,15 @@ import ( infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" ) +type openAIModelsReadPolicy uint8 + +const ( + openAIModelsReadDefault openAIModelsReadPolicy = iota + // A replacement whitelist needs this request's complete upstream response. + // It must not refresh credentials, recover tasks or change account health. + openAIModelsReadWhitelist +) + // FetchOpenAIModelsList discovers a single account's raw public model catalog. // API keys use the standard endpoint; OAuth reuses the authenticated, cached // Codex source. Account mappings and group policy are applied after this cache. @@ -24,6 +33,10 @@ func (s *OpenAIGatewayService) FetchOpenAIModelsList(ctx context.Context, accoun } func (s *OpenAIGatewayService) fetchNativeOpenAIModelsList(ctx context.Context, account *Account) (*OpenAIModelsResponse, error) { + return s.fetchNativeOpenAIModelsListWithPolicy(ctx, account, openAIModelsReadDefault) +} + +func (s *OpenAIGatewayService) fetchNativeOpenAIModelsListWithPolicy(ctx context.Context, account *Account, policy openAIModelsReadPolicy) (*OpenAIModelsResponse, error) { if s == nil || account == nil { return nil, infraerrors.New(http.StatusInternalServerError, "OPENAI_MODELS_ACCOUNT_REQUIRED", "OpenAI account is required") } @@ -39,10 +52,13 @@ func (s *OpenAIGatewayService) fetchNativeOpenAIModelsList(ctx context.Context, if s.settingService != nil { clientVersion = s.settingService.GetOpenAICodexClientVersion(ctx) } - response, err := s.fetchNativeCodexModelsManifest(ctx, account, clientVersion, "") + response, err := s.fetchNativeCodexModelsManifestWithPolicy(ctx, account, clientVersion, "", policy) if err != nil { return nil, err } + if response == nil || response.NotModified { + return nil, invalidOpenAIModelsList(fmt.Errorf("upstream returned no current model catalog")) + } body, err := standardOpenAIModelsBody(response.Body, true) if err != nil { return nil, invalidOpenAIModelsList(err) @@ -60,7 +76,7 @@ func (s *OpenAIGatewayService) fetchNativeOpenAIModelsList(ctx context.Context, accountConcurrency: account.Concurrency, useAPIKeyUpstream: true, standardModelsList: true, } - response, err := s.fetchCachedOpenAIModels(ctx, request, func(fetchCtx context.Context, etag string) (*OpenAIModelsResponse, error) { + fetch := func(fetchCtx context.Context, etag string) (*OpenAIModelsResponse, error) { response, err := s.fetchOpenAIModelsUpstream(fetchCtx, request, etag) if err != nil || response.NotModified { return response, err @@ -72,7 +88,15 @@ func (s *OpenAIGatewayService) fetchNativeOpenAIModelsList(ctx context.Context, response.Body = body response.ETag = codexModelsManifestBodyETag(body) return response, nil - }, "") + } + var response *OpenAIModelsResponse + if policy == openAIModelsReadWhitelist { + request.headers.Del("If-None-Match") + request.headers.Set("Cache-Control", "no-cache") + response, err = fetch(ctx, "") + } else { + response, err = s.fetchCachedOpenAIModels(ctx, request, fetch, "") + } if err != nil { return nil, err } diff --git a/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go b/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go new file mode 100644 index 000000000000..8b60dfad9f46 --- /dev/null +++ b/backend/internal/service/upstream_batch_whitelist_identity_policy_test.go @@ -0,0 +1,109 @@ +//go:build unit + +package service + +import ( + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestBatchWhitelistNativePolicyRejectsTaskLifecycle(t *testing.T) { + for _, task := range []string{"", "existing-task"} { + t.Run(task, func(t *testing.T) { + key, privateKey := newTestAgentIdentityKey(t) + account := newCodexModelsTestAccount() + account.Credentials["auth_mode"] = OpenAIAuthModeAgentIdentity + account.Credentials["agent_runtime_id"] = key.runtimeID + account.Credentials["agent_private_key"] = privateKey + account.Credentials["task_id"] = task + repo := &batchWhitelistReadOnlyRepo{upstreamModelMappingRepoStub: &upstreamModelMappingRepoStub{account: account}} + gateway := newCodexModels401TestService(repo) + gateway.accountRepo = repo + _, err := gateway.fetchNativeOpenAIModelsListWithPolicy(context.Background(), account, openAIModelsReadWhitelist) + require.Error(t, err, "read-only discovery cannot create or recover an identity task") + require.Zero(t, repo.credentialWrites.Load()) + require.Equal(t, privateKey, account.GetCredential("agent_private_key")) + require.Equal(t, task, account.GetCredential("task_id")) + }) + } +} + +func TestBatchWhitelistSyncAgentIdentityTaskLifecycle(t *testing.T) { + for _, tc := range []struct { + name, task string + whitelist bool + }{ + {"whitelist without task", "", true}, + {"whitelist with task", "existing-task", true}, + {"legacy discovery still registers task", "", false}, + } { + t.Run(tc.name, func(t *testing.T) { + key, privateKey := newTestAgentIdentityKey(t) + account := newCodexModelsTestAccount() + account.Status, account.Schedulable = StatusActive, true + account.Credentials["auth_mode"] = OpenAIAuthModeAgentIdentity + account.Credentials["agent_runtime_id"] = key.runtimeID + account.Credentials["agent_private_key"] = privateKey + account.Credentials["task_id"] = tc.task + account.Credentials["model_mapping"] = map[string]any{"old": "old"} + require.False(t, account.IsExcelBPSEnabled(), "exercise the non-BPS sync entry") + before, err := json.Marshal(account) + require.NoError(t, err) + var registrations atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + registrations.Add(1) + _, _ = io.WriteString(w, `{"task_id":"registered-task"}`) + })) + original := openAIAgentIdentityAuthAPIBaseURL + openAIAgentIdentityAuthAPIBaseURL = server.URL + t.Cleanup(func() { openAIAgentIdentityAuthAPIBaseURL = original; server.Close() }) + repo := &batchWhitelistReadOnlyRepo{upstreamModelMappingRepoStub: &upstreamModelMappingRepoStub{account: account}} + ws := &agentIdentityWSInvalidationRecorder{} + upstream := &httpUpstreamRecorder{resp: &http.Response{ + StatusCode: http.StatusServiceUnavailable, Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(`{"error":"synthetic catalog failure"}`)), + }} + svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream, cfg: upstreamModelSyncTestConfig(), agentIdentityWS: ws} + options := UpstreamModelSyncOptions{} + if tc.whitelist { + options = whitelistOnlyOptions(t) + } + _, err = svc.SyncUpstreamModelCatalogWithOptions(context.Background(), account, options) + require.Error(t, err) + if tc.whitelist { + var syncErr *UpstreamModelSyncError + require.True(t, errors.As(err, &syncErr)) + require.Equal(t, UpstreamModelSyncErrorUnsupported, syncErr.Kind) + require.Zero(t, registrations.Load(), "whitelist sync must reject before task registration") + require.Zero(t, repo.credentialWrites.Load(), "whitelist sync must not persist a task") + require.Empty(t, ws.accountIDs) + require.Empty(t, upstream.requests) + after, marshalErr := json.Marshal(account) + require.NoError(t, marshalErr) + require.JSONEq(t, string(before), string(after)) + } else { + // Legacy discovery retains task setup even if its subsequent + // model-list request fails; the guard is whitelist-only. + require.EqualValues(t, 1, registrations.Load()) + require.EqualValues(t, 1, repo.credentialWrites.Load()) + require.Equal(t, "registered-task", account.GetCredential("task_id")) + require.Equal(t, []int64{account.ID}, ws.accountIDs) + require.Len(t, upstream.requests, 1) + } + require.Zero(t, repo.authErrors.Load()) + require.Zero(t, repo.tempUnsched.Load()) + require.Zero(t, repo.extraWrites.Load()) + require.Zero(t, repo.applyCalls) + require.Zero(t, repo.claimCalls) + }) + } +} diff --git a/backend/internal/service/upstream_batch_whitelist_read_policy_test.go b/backend/internal/service/upstream_batch_whitelist_read_policy_test.go new file mode 100644 index 000000000000..9ac82911ee4b --- /dev/null +++ b/backend/internal/service/upstream_batch_whitelist_read_policy_test.go @@ -0,0 +1,270 @@ +//go:build unit + +package service + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type batchWhitelistReadOnlyRepo struct { + *upstreamModelMappingRepoStub + authErrors atomic.Int32 + tempUnsched atomic.Int32 + credentialWrites atomic.Int32 + extraWrites atomic.Int32 +} + +func (r *batchWhitelistReadOnlyRepo) SetError(context.Context, int64, string) error { + r.authErrors.Add(1) + return nil +} + +func (r *batchWhitelistReadOnlyRepo) SetTempUnschedulable(context.Context, int64, time.Time, string) error { + r.tempUnsched.Add(1) + return nil +} + +func (r *batchWhitelistReadOnlyRepo) UpdateCredentials(context.Context, int64, map[string]any) error { + r.credentialWrites.Add(1) + return nil +} + +func (r *batchWhitelistReadOnlyRepo) UpdateExtra(context.Context, int64, map[string]any) error { + r.extraWrites.Add(1) + return nil +} + +type batchWhitelistTokenInvalidator struct{ calls atomic.Int32 } + +func (i *batchWhitelistTokenInvalidator) InvalidateToken(context.Context, *Account) error { + i.calls.Add(1) + return nil +} + +// Only persistence and BPS HTTP are replaced. Native discovery traverses the +// real Codex request builder, HTTP client, envelope parser and error handling. +type batchWhitelistReadFixture struct { + service *AccountTestService + gateway *OpenAIGatewayService + repo *batchWhitelistReadOnlyRepo + invalidator *batchWhitelistTokenInvalidator + nativeCalls atomic.Int32 + bpsCalls atomic.Int32 + nativeStatus atomic.Int32 + bpsStatus atomic.Int32 + nativeBody atomic.Value + bpsBody atomic.Value +} + +func newBatchWhitelistReadFixture(t *testing.T) *batchWhitelistReadFixture { + t.Helper() + f := &batchWhitelistReadFixture{} + f.nativeStatus.Store(http.StatusOK) + f.bpsStatus.Store(http.StatusOK) + f.nativeBody.Store(`{"models":[{"slug":"gpt-native-a"},{"slug":"gpt-native-b"}]}`) + f.bpsBody.Store(`{"allowed":true,"model_catalog":{"models":[{"id":"gpt-7-new"}]}}`) + account := excelAccount() + account.Credentials["refresh_token"] = "synthetic-refresh-token" + account.Credentials["model_mapping"] = map[string]any{ + "gpt-native-b": "gpt-native-b", "native-alias": "gpt-native-b", "obsolete": "obsolete", + } + account.Extra["openai_excel_bps_models"] = []string{"gpt-7-new", "gpt-7-cached"} + account.Extra["keep"] = "unchanged" + f.repo = &batchWhitelistReadOnlyRepo{upstreamModelMappingRepoStub: &upstreamModelMappingRepoStub{account: account}} + f.invalidator = &batchWhitelistTokenInvalidator{} + f.gateway = newCodexModels401TestService(f.repo) + f.gateway.accountRepo = f.repo + f.gateway.rateLimitService.SetTokenCacheInvalidator(f.invalidator) + f.gateway.httpUpstream = &codexModelsHTTPUpstreamStub{do: func(req *http.Request, _ string, id int64, _ int) (*http.Response, error) { + f.bpsCalls.Add(1) + assert.Equal(t, http.MethodGet, req.Method) + assert.Equal(t, "/basispoints/api/responses/access", req.URL.Path) + assert.Equal(t, account.ID, id) + response := ordinaryModelsUpstreamResponse(f.bpsBody.Load().(string)) + response.StatusCode = int(f.bpsStatus.Load()) + return response, nil + }} + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + f.nativeCalls.Add(1) + assert.Equal(t, http.MethodGet, req.Method) + assert.Equal(t, "Bearer test-token", req.Header.Get("Authorization")) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(int(f.nativeStatus.Load())) + _, _ = w.Write([]byte(f.nativeBody.Load().(string))) + })) + original := chatgptCodexModelsURL + chatgptCodexModelsURL = server.URL + t.Cleanup(func() { chatgptCodexModelsURL = original; server.Close() }) + f.service = &AccountTestService{accountRepo: f.repo, openaiGatewayService: f.gateway} + return f +} + +func (f *batchWhitelistReadFixture) assertNoAccountSideEffects(t *testing.T, before []byte) { + t.Helper() + after, err := json.Marshal(f.repo.account) + require.NoError(t, err) + require.JSONEq(t, string(before), string(after), "discovery must preserve credentials, mapping, status, cooldowns and Extra") + require.Zero(t, f.repo.authErrors.Load(), "whitelist discovery must not disable an account") + require.Zero(t, f.repo.tempUnsched.Load(), "whitelist discovery must not park an account") + require.Zero(t, f.repo.credentialWrites.Load(), "whitelist discovery must not rotate credentials or recover tasks") + require.Zero(t, f.repo.extraWrites.Load(), "whitelist discovery must not write health or catalog state") + require.Zero(t, f.invalidator.calls.Load(), "whitelist discovery must not invalidate token caches") + require.Zero(t, f.repo.claimCalls) + require.False(t, f.gateway.isOpenAIAccountRuntimeBlocked(f.repo.account)) + require.Zero(t, f.gateway.openaiAccountRuntimeBlockSequence.Load(), "discovery must not mutate runtime health generations") +} + +func TestBatchWhitelistBPSNativeStructured401IsReadOnly(t *testing.T) { + for _, body := range []string{ + `{"error":{"code":"token_revoked","message":"synthetic revoked token"}}`, + `{"error":{"code":"token_invalidated"}}`, + `{"detail":{"message":"invalid token"}}`, + } { + t.Run(body, func(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + f.nativeStatus.Store(http.StatusUnauthorized) + f.nativeBody.Store(body) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + _, err = f.service.SyncUpstreamModelCatalogWithOptions(context.Background(), f.repo.account, whitelistOnlyOptions(t)) + require.Error(t, err) + require.EqualValues(t, 1, f.bpsCalls.Load()) + require.EqualValues(t, 1, f.nativeCalls.Load()) + require.Zero(t, f.repo.applyCalls) + f.assertNoAccountSideEffects(t, before) + }) + } +} + +func TestBatchWhitelistBPSNativeStaleFailureKeepsMapping(t *testing.T) { + for _, admissionOwner := range []bool{false, true} { + t.Run(map[bool]string{false: "admin", true: "admission owner"}[admissionOwner], func(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + f.nativeBody.Store(`{"models":[{"slug":"gpt-native-a"}]}`) + _, err := f.gateway.fetchNativeOpenAIModelsList(context.Background(), f.repo.account) + require.NoError(t, err) + expireCodexModelsManifestCache(f.gateway, 2*time.Minute) + f.nativeStatus.Store(http.StatusServiceUnavailable) + f.nativeBody.Store(`{"error":{"code":"unavailable"}}`) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + ctx := context.Background() + if admissionOwner { + var cancel context.CancelFunc + ctx, cancel = WithAPIKeyAdmissionOwner(ctx) + defer cancel() + } + _, err = f.service.SyncUpstreamModelCatalogWithOptions(ctx, f.repo.account, whitelistOnlyOptions(t)) + require.Error(t, err, "stale native cache cannot authorize a replacement whitelist") + require.EqualValues(t, 2, f.nativeCalls.Load(), "this request must wait for its own native response") + require.Zero(t, f.repo.applyCalls, "failed live discovery must keep native-b and its alias") + f.assertNoAccountSideEffects(t, before) + }) + } +} + +func TestBatchWhitelistBPSNativeFreshCompleteSuccess(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + // Both caches contain old but still fresh display catalogs. A destructive + // sync must observe this round's successful responses from both sources. + f.nativeBody.Store(`{"models":[{"slug":"gpt-native-a"}]}`) + f.bpsBody.Store(`{"allowed":true,"model_catalog":{"models":[{"id":"gpt-7-cached"}]}}`) + _, err := f.gateway.fetchNativeOpenAIModelsList(context.Background(), f.repo.account) + require.NoError(t, err) + _, err = f.gateway.FetchExcelBPSModelsList(context.Background(), f.repo.account) + require.NoError(t, err) + f.nativeBody.Store(`{"models":[{"slug":"gpt-native-a"},{"slug":"gpt-native-b"}]}`) + f.bpsBody.Store(`{"allowed":true,"model_catalog":{"models":[{"id":"gpt-7-new"}]}}`) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + catalog, err := f.service.SyncUpstreamModelCatalogWithOptions(context.Background(), f.repo.account, whitelistOnlyOptions(t)) + require.NoError(t, err) + require.True(t, catalog.Applied) + assert.EqualValues(t, 2, f.nativeCalls.Load(), "native fresh cache must also be bypassed") + assert.EqualValues(t, 2, f.bpsCalls.Load(), "BPS fresh cache must also be bypassed") + require.Equal(t, map[string]string{ + "gpt-native-a": "gpt-native-a", "gpt-native-b": "gpt-native-b", + "native-alias": "gpt-native-b", "gpt-7-new": "gpt-7-new", + }, f.repo.mapping) + require.Equal(t, 1, f.repo.applyCalls) + f.assertNoAccountSideEffects(t, before) +} + +func TestBatchWhitelistBPSFreshCacheCannotHideFailure(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + _, err := f.gateway.FetchExcelBPSModelsList(context.Background(), f.repo.account) + require.NoError(t, err) + f.bpsStatus.Store(http.StatusUnauthorized) + f.bpsBody.Store(`{"error":{"code":"token_revoked"}}`) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + _, err = f.service.SyncUpstreamModelCatalogWithOptions(context.Background(), f.repo.account, whitelistOnlyOptions(t)) + require.Error(t, err, "fresh BPS display cache must not mask this round's failure") + require.EqualValues(t, 2, f.bpsCalls.Load()) + require.Zero(t, f.nativeCalls.Load()) + require.Zero(t, f.repo.applyCalls) + f.assertNoAccountSideEffects(t, before) +} + +func TestBatchWhitelistBPSExpiredGrantDoesNotRefresh(t *testing.T) { + for _, rejected := range []bool{false, true} { + t.Run(map[bool]string{false: "expired", true: "rejected"}[rejected], func(t *testing.T) { + reauth, reader, grantRepo, updater, blocker := newExcelReauthTestService(t) + reader.account.Status = StatusActive + reader.account.Schedulable = true + reader.account.Extra = map[string]any{"openai_excel_bps": true, "openai_excel_bps_models": []string{"gpt-7-new"}} + expiry := time.Now().Add(-time.Hour) + if rejected { + expiry = time.Now().Add(time.Hour) + } + credentials := excelTestCredentials("read-only", expiry) + credentials[excelAccessTokenRejectedKey] = rejected + storeExcelTestCredentials(t, reauth, grantRepo, credentials) + refreshClient := &excelRefreshTestClient{credentials: excelTestCredentials("fresh", time.Now().Add(time.Hour))} + reauth.oauth.oauthClient = refreshClient + ciphertext := grantRepo.ciphertext + repo := &batchWhitelistReadOnlyRepo{upstreamModelMappingRepoStub: &upstreamModelMappingRepoStub{account: reader.account}} + gateway := newCodexModels401TestService(repo) + gateway.excelOAuthReauth = reauth + gateway.httpUpstream = &codexModelsHTTPUpstreamStub{do: func(*http.Request, string, int64, int) (*http.Response, error) { + t.Error("unready grant must not reach a catalog endpoint") + return nil, errors.New("synthetic catalog must not be called") + }} + svc := &AccountTestService{accountRepo: repo, openaiGatewayService: gateway} + _, err := svc.SyncUpstreamModelCatalogWithOptions(context.Background(), reader.account, whitelistOnlyOptions(t)) + require.Error(t, err) + require.Zero(t, refreshClient.calls, "whitelist reads must not refresh expired/rejected grants") + require.Equal(t, ciphertext, grantRepo.ciphertext) + require.False(t, grantRepo.applied) + require.False(t, updater.applied) + require.Zero(t, blocker.clearedAccountID) + require.Zero(t, repo.applyCalls) + }) + } +} + +func TestBatchWhitelistBPSExpiredLegacyTokenDoesNotDisable(t *testing.T) { + f := newBatchWhitelistReadFixture(t) + f.repo.account.Credentials["expires_at"] = time.Now().Add(-time.Hour).Format(time.RFC3339) + delete(f.repo.account.Credentials, "refresh_token") + f.gateway.openAITokenProvider = NewOpenAITokenProvider(f.repo, nil, nil) + f.gateway.openAITokenProvider.SetAccountRuntimeBlocker(f.gateway) + before, err := json.Marshal(f.repo.account) + require.NoError(t, err) + _, err = f.service.SyncUpstreamModelCatalogWithOptions(context.Background(), f.repo.account, whitelistOnlyOptions(t)) + require.Error(t, err) + require.Zero(t, f.bpsCalls.Load()) + require.Zero(t, f.nativeCalls.Load()) + require.Zero(t, f.repo.applyCalls) + f.assertNoAccountSideEffects(t, before) +} diff --git a/backend/internal/service/upstream_batch_whitelist_test.go b/backend/internal/service/upstream_batch_whitelist_test.go new file mode 100644 index 000000000000..488a0ee3a211 --- /dev/null +++ b/backend/internal/service/upstream_batch_whitelist_test.go @@ -0,0 +1,125 @@ +//go:build unit + +package service + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func whitelistOnlyOptions(t *testing.T) UpstreamModelSyncOptions { + t.Helper() + var options UpstreamModelSyncOptions + require.NoError(t, json.Unmarshal([]byte(`{"apply":true,"whitelist_only":true}`), &options)) + return options +} + +func TestBatchWhitelistSafety(t *testing.T) { + for _, tc := range []struct { + name, body string + platform, accountType string + expected map[string]string + skipped string + }{ + {"normalize and live alias", `{"data":[{"id":" new "},{"id":"new"},{"id":"*"},{"id":""}]}`, PlatformOpenAI, AccountTypeAPIKey, map[string]string{"new": "new", "public-new": "new"}, ""}, + {"unconsumed page", `{"data":[{"id":"new"}],"has_more":true}`, PlatformOpenAI, AccountTypeAPIKey, nil, "unverified_catalog"}, + {"unconsumed token", `{"data":[{"id":"new"}],"nextPageToken":"next"}`, PlatformOpenAI, AccountTypeAPIKey, nil, "unverified_catalog"}, + {"wildcards", `{"data":[{"id":"*"}]}`, PlatformOpenAI, AccountTypeAPIKey, nil, "empty"}, + {"subscription exclusions", `{"models":[{"slug":"gpt-5.5"},{"slug":"gpt-5.4-mini"},{"slug":"gpt-image-1.5"},{"slug":"new"}]}`, PlatformOpenAI, AccountTypeOAuth, map[string]string{"new": "new", "public-new": "new"}, ""}, + {"grok normalization", `{"data":[{"id":"new","name":"New label"}]}`, PlatformGrok, AccountTypeAPIKey, map[string]string{"new": "new", "public-new": "new"}, ""}, + } { + t.Run(tc.name, func(t *testing.T) { + svc, repo, up := modelMappingSyncFixture(http.StatusOK, tc.body) + repo.account.Platform, repo.account.Type = tc.platform, tc.accountType + repo.account.Credentials["access_token"] = "synthetic-oauth" + catalog, err := svc.SyncUpstreamModelCatalogWithOptions(context.Background(), repo.account, whitelistOnlyOptions(t)) + require.NoError(t, err) + require.Equal(t, tc.skipped, catalog.Skipped) + require.Equal(t, tc.expected, repo.mapping) + require.Len(t, up.requests, 1, "only one catalog read, no registry or generation requests") + require.Equal(t, http.MethodGet, up.requests[0].Method) + require.Zero(t, repo.claimCalls) + }) + } + t.Run("account policy before discovery", func(t *testing.T) { + svc, repo, up := modelMappingSyncFixture(200, `{"data":[{"id":"new"}]}`) + repo.account.Type = AccountTypeOAuth + repo.account.Credentials["model_mapping_mode"] = "aliases" + catalog, err := svc.SyncUpstreamModelCatalogWithOptions(context.Background(), repo.account, whitelistOnlyOptions(t)) + require.NoError(t, err) + require.Equal(t, "account_policy", catalog.Skipped) + require.Empty(t, up.requests) + require.Zero(t, repo.applyCalls) + }) + t.Run("concurrent credentials leave original mapping", func(t *testing.T) { + svc, repo, _ := modelMappingSyncFixture(200, `{"data":[{"id":"new"}]}`) + repo.changed = true + catalog, err := svc.SyncUpstreamModelCatalogWithOptions(context.Background(), repo.account, whitelistOnlyOptions(t)) + require.NoError(t, err) + require.Equal(t, "account_changed", catalog.Skipped) + require.Nil(t, repo.mapping) + }) +} + +func TestBatchWhitelistVertexPagination(t *testing.T) { + for _, fail := range []bool{false, true} { + t.Run(map[bool]string{false: "all pages", true: "partial failure"}[fail], func(t *testing.T) { + second := vertexModelsJSONResponse(200, `{"publisherModels":[{"name":"publishers/google/models/gemini-b"}]}`) + if fail { + second = vertexModelsJSONResponse(500, `{"error":{}}`) + } + up := &httpUpstreamRecorder{responses: []*http.Response{ + vertexModelsJSONResponse(200, `{"publisherModels":[{"name":"publishers/google/models/gemini-a"},{"name":"publishers/google/models/gemma-hidden"}],"nextPageToken":"next"}`), + second, + }} + svc := vertexModelSyncTestService(up) + account := vertexModelSyncTestAccount(map[string]any{"model_mapping": map[string]any{"old": "old"}}) + repo := &upstreamModelMappingRepoStub{account: account} + svc.accountRepo = repo + catalog, err := svc.SyncUpstreamModelCatalogWithOptions(context.Background(), account, whitelistOnlyOptions(t)) + if fail { + require.Error(t, err) + require.Nil(t, repo.mapping) + } else { + require.NoError(t, err) + require.True(t, catalog.Applied) + require.Equal(t, map[string]string{"gemini-a": "gemini-a", "gemini-b": "gemini-b"}, repo.mapping) + } + require.Len(t, up.requests, 2) + require.Equal(t, "next", up.requests[1].URL.Query().Get("pageToken")) + }) + } +} + +func TestBatchWhitelistBPSPartialCatalog(t *testing.T) { + for _, failedSource := range []string{"bps", "native"} { + t.Run(failedSource, func(t *testing.T) { + account := excelAccount() + account.Extra["openai_excel_bps_models"] = []string{"gpt-7-new"} + account.Credentials["model_mapping"] = map[string]any{"old": "old"} + gateway := openAIClientToolsTestService(nil) + gateway.httpUpstream = &codexModelsHTTPUpstreamStub{do: func(req *http.Request, _ string, _ int64, _ int) (*http.Response, error) { + require.Equal(t, http.MethodGet, req.Method) + isBPS := strings.Contains(req.URL.Path, "/access") + if (isBPS && failedSource == "bps") || (!isBPS && failedSource == "native") { + return nil, errors.New("synthetic unavailable source") + } + if isBPS { + return ordinaryModelsUpstreamResponse(bpsAccessFixture), nil + } + return ordinaryModelsUpstreamResponse(`{"models":[{"slug":"gpt-native"}]}`), nil + }} + repo := &upstreamModelMappingRepoStub{account: account} + svc := &AccountTestService{accountRepo: repo, openaiGatewayService: gateway} + _, err := svc.SyncUpstreamModelCatalogWithOptions(context.Background(), account, whitelistOnlyOptions(t)) + require.Error(t, err, "partial BPS/native catalogs cannot replace the entire whitelist") + require.Zero(t, repo.applyCalls) + }) + } +} diff --git a/backend/internal/service/upstream_model_mapping_sync.go b/backend/internal/service/upstream_model_mapping_sync.go index 0b18e8c973a9..03ed38c7f501 100644 --- a/backend/internal/service/upstream_model_mapping_sync.go +++ b/backend/internal/service/upstream_model_mapping_sync.go @@ -23,16 +23,29 @@ type UpstreamModelMappingSyncRepository interface { } type UpstreamModelSyncOptions struct { - Apply bool `json:"apply"` - Automatic bool `json:"automatic"` + Apply bool `json:"apply"` + Automatic bool `json:"automatic"` + WhitelistOnly bool `json:"whitelist_only"` +} + +// Validate rejects combinations that could silently turn a mapping-only edit +// into metadata discovery or an automatic refresh window. +func (options UpstreamModelSyncOptions) Validate() error { + if options.WhitelistOnly && (!options.Apply || options.Automatic) { + return newUpstreamModelSyncConfigError("Whitelist-only sync requires an explicit manual apply", nil) + } + if options.Automatic && !options.Apply { + return newUpstreamModelSyncConfigError("Automatic model sync requires apply=true", nil) + } + return nil } // SyncUpstreamModelCatalogWithOptions preserves the original discovery-only API // unless Apply is explicitly requested. An automatic attempt claims its window // before any upstream request; failures therefore cannot cause a polling storm. func (s *AccountTestService) SyncUpstreamModelCatalogWithOptions(ctx context.Context, account *Account, options UpstreamModelSyncOptions) (*UpstreamModelCatalog, error) { - if options.Automatic && !options.Apply { - return nil, newUpstreamModelSyncConfigError("Automatic model sync requires apply=true", nil) + if err := options.Validate(); err != nil { + return nil, err } if !options.Apply { return s.SyncUpstreamModelCatalog(ctx, account) @@ -83,7 +96,13 @@ func (s *AccountTestService) SyncUpstreamModelCatalogWithOptions(ctx context.Con ctx, cancel := context.WithTimeout(ctx, upstreamModelSyncTimeout) defer cancel() - catalog, err := s.SyncUpstreamModelCatalog(ctx, account) + var catalog *UpstreamModelCatalog + var err error + if options.WhitelistOnly { + catalog, err = s.fetchWhitelistCatalog(ctx, account) + } else { + catalog, err = s.SyncUpstreamModelCatalog(ctx, account) + } if err != nil { return nil, err } @@ -91,6 +110,9 @@ func (s *AccountTestService) SyncUpstreamModelCatalogWithOptions(ctx context.Con return skippedUpstreamModelSync("empty", nextSyncAt), nil } catalog.NextSyncAt = nextSyncAt + if catalog.Skipped != "" { + return catalog, nil + } if catalog.DiscoverySource != "upstream" { catalog.Skipped = "unverified_catalog" return catalog, nil @@ -122,6 +144,44 @@ func (s *AccountTestService) SyncUpstreamModelCatalogWithOptions(ctx context.Con return catalog, nil } +// Mapping-only edits never refresh capability snapshots, observations, quality +// state or the automatic cooldown. Use only the authenticated live listing. +func (s *AccountTestService) fetchWhitelistCatalog(ctx context.Context, account *Account) (*UpstreamModelCatalog, error) { + if account.IsOpenAIAgentIdentity() { + // The non-BPS request builder can register and persist an identity task. + // Whitelist-only edits never own that credential lifecycle. + return nil, newUpstreamModelSyncUnsupportedError("Read-only model discovery does not support agent identity accounts", nil) + } + var models []string + var body []byte + var err error + if account.IsExcelBPSEnabled() && s.openaiGatewayService != nil { + // The display catalog tolerates one source failing. A replacement + // whitelist needs both native and BPS sources when both routes apply. + response, fetchErr := s.openaiGatewayService.fetchExcelBPSAccountModelsWithPolicy(ctx, account, true) + if fetchErr != nil { + return nil, newUpstreamModelSyncUpstreamError("Excel BPS model discovery failed", fetchErr) + } + body = response.Body + models, _, err = extractUpstreamModelCatalog(body, false) + } else { + models, body, err = s.fetchUpstreamModelList(ctx, account) + } + if err != nil { + return nil, err + } + // Vertex's list reader consumes every page and returns no raw envelope. + // Do not replace an entire mapping with a partial single-page catalog. + var page struct { + HasMore bool `json:"has_more"` + NextPageToken string `json:"nextPageToken"` + } + if len(body) > 0 && json.Unmarshal(body, &page) == nil && (page.HasMore || page.NextPageToken != "") { + return skippedUpstreamModelSync("unverified_catalog", nil), nil + } + return &UpstreamModelCatalog{DiscoverySource: "upstream", Models: visibleUpstreamModelIDs(account, models)}, nil +} + func skippedUpstreamModelSync(reason string, next *time.Time) *UpstreamModelCatalog { return &UpstreamModelCatalog{Models: []string{}, Skipped: reason, NextSyncAt: next} } diff --git a/backend/internal/service/upstream_models.go b/backend/internal/service/upstream_models.go index 550e9ba32b95..624b20df1ce6 100644 --- a/backend/internal/service/upstream_models.go +++ b/backend/internal/service/upstream_models.go @@ -217,6 +217,16 @@ func (s *AccountTestService) FetchUpstreamSupportedModels(ctx context.Context, a return models, err } +func visibleUpstreamModelIDs(account *Account, models []string) []string { + filtered := make([]string, 0, len(models)) + for _, model := range models { + if !isHiddenOpenAISubscriptionModel(account, model) { + filtered = append(filtered, model) + } + } + return filtered +} + // SyncUpstreamModelCatalog fetches the account's live model list, enriches // missing capability fields from the provider registry used by the upstream, // and persists a normalized account snapshot when complete metadata is available. @@ -264,15 +274,7 @@ func (s *AccountTestService) SyncUpstreamModelCatalog(ctx context.Context, accou } else if saveErr := s.saveGrokCatalogObservation(ctx, account, grokObservation); saveErr != nil { return nil, newUpstreamModelSyncInternalError("Failed to save upstream model catalog", saveErr) } - if account != nil { - filtered := make([]string, 0, len(models)) - for _, model := range models { - if !isHiddenOpenAISubscriptionModel(account, model) { - filtered = append(filtered, model) - } - } - models = filtered - } + models = visibleUpstreamModelIDs(account, models) catalog := &UpstreamModelCatalog{Models: models, Metadata: make(map[string]UpstreamModelMetadata), DiscoverySource: "upstream"} if !liveListAvailable { // Every provider can use configured IDs when its list endpoint is absent. diff --git a/docs/features/account-batch-upstream-whitelist.md b/docs/features/account-batch-upstream-whitelist.md new file mode 100644 index 000000000000..f3acbc86787c --- /dev/null +++ b/docs/features/account-batch-upstream-whitelist.md @@ -0,0 +1,36 @@ +# 批量同步账号上游模型白名单 + +管理员在账号列表显式勾选账号,再打开“批量编辑”,可在模型白名单区域点击“批量同步上游模型”。跨页勾选的 ID 会一起冻结在弹窗打开时的选择中;后续列表刷新不会增删本轮账号。没有勾选账号的筛选结果预览不提供此操作。 + +该操作使用每个账号已保存的连接和凭据,立即保存各自的上游模型白名单。其他表单修改仍需单独提交。现有“同步最新支持模型”继续填充共同的手工编辑列表。启动上游同步会取消勾选共同模型限制,防止随后提交表单覆盖各账号刚同步的结果。 + +每个账号显示独立结果。最多同时提交两个账号;某个账号失败不阻止其他账号。成功时移除失效模型、保留仍指向有效上游模型的别名;空目录、未验证目录和受保护的账号策略保留旧白名单。失败账号可以单独重试。连接异常时可能无法确认服务端是否已经保存,重试前应核对账号。 + +“停止排队”及关闭弹窗会停止尚未启动的账号。已经提交的请求仍可能完成并保存,界面不会声称将其撤销。上一轮已提交请求结束前,新打开的弹窗不能开始另一轮同步。 + +## API 与安全合同 + +复用 POST /api/v1/admin/accounts/:id/models/sync-upstream,显式传入: + +```json +{"apply": true, "whitelist_only": true} +``` + +whitelist_only 要求 apply=true 且 automatic=false,省略该字段时保留原同步行为。请求经过管理员认证;observer 不得 apply。每个请求只处理 URL 中的正整数账号 ID。 + +白名单模式复用上游目录读取、代理、凭据、模型 ID 归一化、订阅隐藏模型策略及现有 model_mapping 条件更新。它不写能力元数据、目录观测、质量状态或自动同步冷却;只有 model_mapping、账号更新时间和既有调度失效通知会变化。发现期间账号凭据/代理/状态有变化时不覆盖并发编辑。 + +Vertex 复用现有有上限的分页读取;其他目录如果仍带未消费的分页标记,就保留旧白名单并报告未验证目录。混合 BPS/native 账号要求相关目录完整成功,避免部分来源失败导致错误删除。没有 generation、付费探针或自动业务重试。 + +无数据库迁移。旧后端不认识 whitelist_only,因此必须配套部署本 PR 的前后端。回滚代码不恢复已经保存的白名单。 + +## 验证边界 + +- BatchUpstreamWhitelist.spec.ts 挂载真实弹窗,使用 Axios adapter 的虚构响应,覆盖冻结选择、双并发、失败重试、停止、关闭与重入、普通表单互斥以及提示持久化。 +- AccountsView.bulkEdit.spec.ts 覆盖列表跨页勾选和筛选预览边界。 +- account_handler_batch_whitelist_test.go 使用真实 Gin handler/service 和管理员认证;存储/上游边界使用虚构实现。 +- upstream_batch_whitelist_test.go 覆盖目录归一化、别名、隐藏策略、分页、部分 BPS 来源失败和并发编辑。 +- account_handler_batch_whitelist_integration_test.go 在临时 PostgreSQL 上运行真实 handler → service → repository 写入,再通过 GET 逐字段核对两个不同成功目录、失败账号和未选中账号。 +- UI 取证脚本通过 Vite 渲染原组件与真实 CSS,浏览器禁用外部网络。截图和带 SHA/哈希的 manifest 作为 PR 附件与 CI artifacts 保存,不进入源码。 + +管理员新功能提示 ID 为 account-batch-upstream-whitelist-v1,仅展示于上述批量操作入口。关闭记录遵循当前浏览器、当前管理员的 localStorage 边界。 diff --git a/frontend/src/api/admin/accounts.ts b/frontend/src/api/admin/accounts.ts index 2e23f077428e..0cb05b331c68 100644 --- a/frontend/src/api/admin/accounts.ts +++ b/frontend/src/api/admin/accounts.ts @@ -648,6 +648,7 @@ export interface SyncUpstreamModelsResult { } export interface SyncUpstreamModelsOptions { + whitelist_only?: boolean apply?: boolean automatic?: boolean } @@ -678,7 +679,7 @@ export interface UpstreamModelMetadata { export async function syncUpstreamModels( id: number, options?: SyncUpstreamModelsOptions, - request?: { signal?: AbortSignal } + request?: { signal?: AbortSignal; timeout?: number } ): Promise { const { data } = await apiClient.post(`/admin/accounts/${id}/models/sync-upstream`, options, request) return data diff --git a/frontend/src/components/account/BatchUpstreamWhitelist.vue b/frontend/src/components/account/BatchUpstreamWhitelist.vue new file mode 100644 index 000000000000..477dc6edc959 --- /dev/null +++ b/frontend/src/components/account/BatchUpstreamWhitelist.vue @@ -0,0 +1,33 @@ + + diff --git a/frontend/src/components/account/BulkEditAccountModal.vue b/frontend/src/components/account/BulkEditAccountModal.vue index a43c1d2a9f01..4f58d311dde2 100644 --- a/frontend/src/components/account/BulkEditAccountModal.vue +++ b/frontend/src/components/account/BulkEditAccountModal.vue @@ -402,12 +402,20 @@ +
' + props: ['show', 'target', 'filteredPreview'], + template: '
' } const makeProbeAccount = (id: number, rate = 0.25) => ({ @@ -328,9 +328,28 @@ describe('admin AccountsView bulk edit scope', () => { expect(wrapper.get('[data-test="bulk-edit-modal"]').attributes('data-show')).toBe('true') expect(wrapper.get('[data-test="bulk-edit-modal"]').attributes('data-target-mode')).toBe('selected') + expect(wrapper.get('[data-test="bulk-edit-modal"]').attributes('data-filter-preview')).toBe('true') expect(wrapper.get('[data-test="bulk-edit-modal"]').attributes('data-target-ids')).toBe('[7,11]') }) + it('passes only the explicitly checked IDs across pages to the upstream action', async () => { + listAccounts.mockImplementation(async (page: number) => + page === 2 ? probePage([21, 22], 22, 2) : probePage([7, 8], 22, 1)) + const wrapper = mountProbeView({ BulkEditAccountModal: BulkEditAccountModalStub }) + await flushPromises() + await wrapper.get('[data-account-id="7"] [data-test="select-row"] input').trigger('change') + await wrapper.get('[data-test="next-page"]').trigger('click') + await flushPromises() + await wrapper.get('[data-account-id="22"] [data-test="select-row"] input').trigger('change') + await wrapper.get('[data-test="edit-filtered"]').trigger('click') + const modal = wrapper.get('[data-test="bulk-edit-modal"]') + expect(modal.attributes('data-filter-preview')).toBe('false') + expect(modal.attributes('data-target-ids')).toBe('[7,22]') + await wrapper.get('[data-account-id="21"] [data-test="select-row"] input').trigger('change') + expect(modal.attributes('data-target-ids')).toBe('[7,22]') + wrapper.unmount() + }) + it('keeps checked OAuth IDs when a stale filtered-edit action fires', async () => { const page = probePage([7, 11, 13]) page.items[0] = { ...page.items[0], type: 'oauth' }