Skip to content

fix(db): import JSON preserva estado das chaves e cifra credenciais (GHSA-p42h) - #16655

Merged
diegosouzapw merged 2 commits into
release/v3.8.52from
fix/ghsa-p42h-json-import-key-state
Oct 15, 2026
Merged

diegosouzapw merged 2 commits into
release/v3.8.52from
fix/ghsa-p42h-json-import-key-state

Conversation

@diegosouzapw

Copy link
Copy Markdown
Collaborator

Problema (GHSA-p42h-25v7-99v5)

Restaurar um backup JSON (POST /api/settings/import-json -> runJsonMigration()) regravava cada chave de API a partir de apenas 8 colunas. Todo o resto voltava ao default, então chaves revogadas, expiradas ou banidas voltavam a ser válidas e perdiam as restrições (scopes, ip_allowlist, conexões/combos permitidos, limites de taxa e de uso).

As conexões também eram gravadas com credenciais em texto puro (sem encryptConnectionFields(), mesmo com STORAGE_ENCRYPTION_KEY definida) e perdiam colunas de migrations posteriores.

Correção

  • Chaves: a lista de colunas agora é derivada do schema real de api_keys (PRAGMA table_info, novo src/lib/db/apiKeys/importRow.ts). Toda coluna presente no backup é restaurada (revoked_at, expires_at, is_active, is_banned, scopes, ip_allowlist, allowed_connections, allowed_combos, allowed_quotas, allowed_endpoints, max_requests_per_minute/day, max_sessions, throttle_delay_ms, rate_limits, access_schedule, usage_limit_enabled, daily/weekly_usage_limit_usd, key_hash, key_prefix, proxy_id etc.), inclusive colunas futuras. Campos ausentes em backups antigos mantêm o default da tabela; null em coluna NOT NULL cai no default em vez de abortar.
  • Conexões: apiKey, accessToken, refreshToken e idToken passam por encryptConnectionFields(); passam a ser preservadas group, max_concurrent, proxy_enabled, per_key_proxy_enabled, quota_visible e quota_window_thresholds_json.
  • Formato do export inalterado; SQL continua dentro de src/lib/db/.

Evidência (TDD)

  • RED (512acaf64c): tests/unit/settings-json-import-key-state-ghsa-p42h.test.ts -> 5 falhas, 1 passa (backup legado). Chaves revogada/expirada/banida validavam como true após o round trip; credenciais gravadas sem enc:v1:.
  • GREEN (b6f5332674): 6/6. Suítes relacionadas (json-migration-combos, rate-limit-overrides-reimport, settings-import-json-session-state, export-json-hide-paid-combos-6328, oidc-settings-reauth, credential-export-always-protected, api-manager-provider-permissions) todas verdes.
  • typecheck:core, eslint e prettier limpos nos arquivos alterados. check:file-size reporta 4 violações pré-existentes em arquivos não tocados (responseSanitizer, error.ts, dois testes).

Fora de escopo

IMPORT_STRIPPED_SETTINGS_KEYS (#16491) não foi tocado.

Co-authored-by: xiaoyaner0201 xiaoyaner0201@users.noreply.github.com

⚠️ base-red inherited: #15306

diegosouzapw and others added 2 commits October 14, 2026 23:27
…ngs import (GHSA-p42h)

The JSON importer rewrote every API key from 8 columns, so revoked, expired
and banned keys became valid again and their restrictions (scopes, allowlists,
connection/combo access, rate and usage limits) were dropped. The key column
list is now derived from the live api_keys schema, so every column present in
the backup is restored and future columns are not silently lost.

Connections were also written with plaintext credentials and lost group,
max_concurrent, proxy_enabled, per_key_proxy_enabled, quota_visible and
quota_window_thresholds. Credentials now go through encryptConnectionFields()
and those columns are restored.

Co-authored-by: xiaoyaner0201 <xiaoyaner0201@users.noreply.github.com>
@diegosouzapw
diegosouzapw merged commit 88c1e33 into release/v3.8.52 Oct 15, 2026
37 of 50 checks passed
@diegosouzapw
diegosouzapw deleted the fix/ghsa-p42h-json-import-key-state branch October 15, 2026 04:25
@github-actions

Copy link
Copy Markdown
Contributor

CI Coverage Report

  • Coverage job: skipped
  • PR test policy: success

Coverage artifact was not available for this run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant