python / python/cpython

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

未关闭
#151,532 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 Lib/xmlrpc/client.py,并使用提供的脚本在正常执行和使用 -O 执行两种情况下重现该行为。检查 dumps() 中的验证,然后为无效参数和多值响应添加回归覆盖,以确保在优化下行为保持一致。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
api
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。