`to_key_string()` exposes the plaintext password from `SecretStr`

Aperta Adatta ai principianti
#2,879 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 4 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend, security

Direzione di ricerca

to_key_string() e il suo helper locale _normalize si trovano in core/wren/src/wren/model/__init__.py intorno alla riga 32; leggili entrambi, poi riproduci con lo snippet PostgresConnectionInfo del report. Sostituisci il ramo SecretStr che chiama get_secret_value() con un'impronta sha256 (importa hashlib), lasciando invariati gli altri tipi. Aggiungi un test di regressione nella suite core/test di wren che verifichi che la chiave non contenga testo in chiaro né un prefisso del secret e che due connessioni che differiscono solo per la password producano chiavi diverse.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Affected revision

  • Repository: Canner/WrenAI
  • Location: core/wren/src/wren/model/__init__.py:32

Summary

BaseConnectionInfo.to_key_string() is a public helper intended to produce a stable key string for a connection, but it unwraps every SecretStr field and embeds the plaintext credential in its return value. A password, access token, or private-key blob therefore appears verbatim in whatever the caller does with the key — logs, traces, cache keys, or error reports — even though the model deliberately stores it as a secret.

Expected behavior

The key string should distinguish connections without revealing their credentials: secret fields should be represented by a stable, non-reversible fingerprint (or omitted with a separate identity component), and the plaintext secret should never be returned by a method whose output is meant to be used as an opaque key.

Actual behavior

class PostgresConnectionInfo(BaseConnectionInfo):
    host: str
    port: str
    user: str
    password: SecretStr | None = None
    database: str | None = None

info = PostgresConnectionInfo(
    host="db.internal", port="5432", user="analyst",
    password=SecretStr("s3cr3t-P@ss"), database="analytics",
)
print(info.to_key_string())
$ python repro.py
to_key_string() -> {"database":"analytics","host":"db.internal","password":"s3cr3t-P@ss","port":"5432","user":"analyst"}
contains password: True
pydantic repr masks it: True

The model's own repr() masks the value (**********), but to_key_string() prints it in clear text.

Root cause

The local _normalize helper explicitly calls value.get_secret_value() for SecretStr instances before serializing the model:

if isinstance(value, SecretStr):
    return value.get_secret_value()

The result is then JSON-encoded into the key. Nothing hashes, masks, or otherwise protects the value, so the method defeats the purpose of using SecretStr in the model.

Suggested fix

Replace the unwrapping branch with a deterministic fingerprint, for example:

if isinstance(value, SecretStr):
    return "sha256:" + hashlib.sha256(value.get_secret_value().encode()).hexdigest()

This keeps the key stable and unique per secret without exposing it. If the key must never be correlatable across processes, use an HMAC with an application-held key instead. Add a regression test asserting that to_key_string() contains neither the raw secret nor any prefix of it, and that two connections differing only in password still produce different keys.

Lingua principale
Python
Stelle
17.8k
Fork
2k
Merge medio
5g 5h
PR unite (30g)
21

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Canner/WrenAI

Tutte le issue di Canner/WrenAI

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.