python / python/cpython

xmlrpc.client.dumps() skips argument validation when Python is run with -O

Aperta
#151,532 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:

Bug description

xmlrpc.client.dumps() currently relies on assert statements to validate public API arguments.

As a result, invalid inputs are rejected in normal execution, but the validation is silently removed when Python is run with optimization (-O), causing different runtime behavior for the same API.

The documentation states that:

  • params must be a tuple or Fault instance.
  • methodresponse=True expects a singleton response tuple.

However, these constraints are enforced only through assert, which is removed when __debug__ is false.

Reproducer
import xmlrpc.client as xmlrpclib

for label, call in [
    ("list params", lambda: xmlrpclib.dumps(["x"])),
    ("dict params", lambda: xmlrpclib.dumps({"x": 1})),
    ("string params", lambda: xmlrpclib.dumps("abc")),
    ("multi-value response", lambda: xmlrpclib.dumps((1, 2), methodresponse=True)),
]:
    try:
        result = call()
    except BaseException as exc:
        print(label + ":", type(exc).__name__, str(exc))
    else:
        print(label + ": OK")
Normal execution
python repro.py

Output:

list params: AssertionError argument must be tuple or Fault instance
dict params: AssertionError argument must be tuple or Fault instance
string params: AssertionError argument must be tuple or Fault instance
multi-value response: AssertionError response tuple must be a singleton
Optimized execution
python -O repro.py

Output:

list params: OK
dict params: OK
string params: OK
multi-value response: OK
Root cause

Lib/xmlrpc/client.py currently performs argument validation using assertions:

assert isinstance(params, (tuple, Fault)), \
    "argument must be tuple or Fault instance"

assert len(params) == 1, \
    "response tuple must be a singleton"

When Python is executed with -O, these assertions are removed and execution proceeds into the marshalling logic, which serializes the supplied values instead of rejecting them.

Expected behavior

Public API validation should be enforced consistently regardless of optimization mode.

Invalid values should continue to be rejected when Python is run with -O.

Actual behavior

Running under -O disables validation and allows invalid inputs to be serialized.

Suggested fix

Replace the assertion-based validation with explicit runtime checks.

Possible exception types:

  • TypeError when params is neither a tuple nor a Fault instance.
  • ValueError when methodresponse=True is used with a tuple length other than 1.

Add regression tests to ensure behavior remains consistent under optimization.

Notes

I searched for existing issues and did not find an open report covering this specific -O behavior difference for xmlrpc.client.dumps().

This appears to be a public API consistency bug rather than a security issue.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs
  • gh-151533

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Leggi Lib/xmlrpc/client.py e riproduci il comportamento con lo script fornito sia durante l'esecuzione normale sia con -O. Esamina la validazione in dumps(), quindi aggiungi una copertura di regressione per i parametri non validi e le risposte con più valori, in modo che il comportamento rimanga coerente durante l'ottimizzazione.

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

Valutazione

Stack tecnologico
python
Ambito
api
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.