Skip to content

[http-client-python] Query and header parameter wire names are interpolated into generated request builders without escaping #12473

Closed

Description

Package/versions: @typespec/http-client-python@0.39.0 (npm latest as of 2026-11-03, published 2026-10-14) — the same code is on main as of 2026-11-03. The Azure flavour @azure-tools/typespec-python@0.63.10 (npm latest) consumes this generator, so generated Azure SDK for Python clients get the same output. Monorepo path: packages/http-client-python.

Summary

The wire name an author writes in @query("...") or @header("...") is spliced into the generated request builder as a raw string literal with no escaping. A quote in the wire name closes the literal early and everything after it becomes a live Python expression in the generated module (in the dict-key position of the "Construct parameters" block), or breaks the file with a SyntaxError at import. In both outcomes the wire name fails to round-trip as data.

This is the same class of defect as #12405 (open) and PR #12342 (open), which fix the model-side literals — primitive_types.py StringType.get_declaration, discriminator values, docstrings, TypedDict keys. The request-builder parameter sites are not covered by that work: parameter_serializer.py is not among PR #12342's changed files (16 files, verified against the PR's files list on 2026-11-03).

Where it happens

packages/http-client-python/generator/pygen/codegen/serializers/parameter_serializer.py, method serialize_query_header (the raw .format around lines 166-170 at 0.39.0, unchanged on main):

set_parameter = "_{}['{}'] = {}".format(
    kwarg_name,
    param.wire_name,
    self.serialize_parameter(param, serializer_name),
)

param.wire_name is interpolated into a single-quoted literal with no escaping (the emitter's escape_str macro in templates/keywords.jinja2 is never wired here, and primitive_types.py's escape_dot escapes only the dot). kwarg_name is params for a query parameter and headers for a header, so one writer serves both channels. The call sites are serializers/builder_serializer.py around lines 517-534.

Two more raw wire-name interpolations live in the same file and look like the same gap: serialize_path (~131, ' "{}": {},'.format(path_parameter.wire_name, ...)) and pop_kwargs_from_signature (~234, f"_{kwarg_dict}.pop('{kwarg.wire_name}', {default_value})").

Minimal reproduction

Node 22+, Python 3.10+; nothing leaves the machine.

  1. Install the emitter:
mkdir tsp-repro && cd tsp-repro
npm init -y
npm install --save-dev @typespec/http-client-python@0.39.0 @typespec/compiler@1.17.0 @typespec/http@1.17.0 @typespec/rest@0.87.0
  1. Save as spec/main.tsp — an ordinary service whose only unusual feature is the query parameter's wire name (a benign computed expression, no imports, no I/O):
import "@typespec/http";
import "@typespec/rest";

using TypeSpec.Http;
using TypeSpec.Rest;

@service(#{ title: "Widget Service" })
@server("https://example.contoso.com", "Widget Service endpoint")
namespace Contoso.WidgetService;

model Widget {
  name: string;
}

@route("/widgets")
interface Widgets {
  @get
  list(
    @query("x'+str(1024)+'")
    filter?: string,
  ): Widget[];
}
  1. Generate — the emitter accepts this wire name without a warning:
npx tsp compile spec/main.tsp --emit "@typespec/http-client-python" --output-dir out
echo "EXIT: $?"    # EXIT: 0

(Side note for anyone varying the payload: a wire name containing * is rejected earlier, by the compiler's own URI-template validation — @typespec/http/missing-uri-param — before the emitter runs; quote characters alone are not caught.)

  1. Open out/@typespec/http-client-python/contoso/widgetservice/operations/_operations.py, function build_widgets_list_request. The "Construct parameters" block contains a dict key that is an expression, not a literal (observed verbatim):
    if filter is not None:
        _params["x" + str(1024) + ""] = _SERIALIZER.query("filter", filter, "str")
  1. Install the generated package and run a consumer that builds one request:
python -m venv .venv
.venv/bin/pip install out/@typespec/http-client-python    # Windows: .venv\Scripts\pip
cat > consumer.py <<'PY'
from contoso.widgetservice.operations._operations import build_widgets_list_request
request = build_widgets_list_request(filter="v")
print("URL OBSERVED:", request.url)
PY
.venv/bin/python consumer.py    # Windows: .venv\Scripts\python

Observed output:

URL OBSERVED: /widgets?x1024=v

The key expression evaluates when the request is built — the emitted URL carries the computed value (x1024), showing the wire name was compiled into the module as code rather than carried as data. A wire name with an unmatched quote instead yields a generated file that fails at import:

    _params['x''] = _SERIALIZER.query("filter", filter, 'str')
SyntaxError: unterminated string literal (detected at line 38)
  1. Control: replace the wire name with @query("filter"), regenerate, re-run — the key is the literal "filter" and nothing is evaluated.

Observed vs expected

  • Observed: param.wire_name is spliced raw into a single-quoted Python literal; a quote in the name closes the literal and the remainder is emitted as a live expression in the generated request builder (evaluated when the application builds a request), or the generated file is syntactically invalid.
  • Expected: a specification string reaches generated code only through a proper string literal serializer, so any wire name the compiler accepts round-trips as data.

Suggested fix

Pass param.wire_name (and the serialize_path / pop_kwargs_from_signature sites) through the same literal serialization PR #12342 introduces for the model side — a repr() / json.dumps-style helper instead of raw .format()/f-string interpolation. Wire names containing quotes, backslashes, and control characters would make good additions to the new tests/unit/test_string_literals.py cases.

Framing

Specifications are ordinarily trusted inputs to SDK generation, and this report does not claim otherwise. The point is narrower: the generator currently turns a specification string into executable source in one position and into a syntax error in another, when it should always emit it as data. That makes this a correctness defect first and a defense-in-depth input-validation gap second — worth fixing whichever trust model a pipeline assumes.

Activity

  1. added
    bugSomething isn't working
    emitter:client:pythonIssue for the Python client emitter: @typespec/http-client-python
    on Nov 3, 2026
  2. github-actions commented on Nov 3, 2026

    @github-actions
    Contributor

    🎯 Issue Triage

    The Python client emitter inserts query and header wire names into generated request-builder string literals without escaping, which can change the emitted key expression or make the generated module invalid. The report includes a complete minimal reproduction and identifies the relevant serializer sites.

    Category: Bug

    Area: emitter:client:python

    🔗 [Playground Reproduction]((typespec.io/redacted)

    📋 Reproduction code
    import "@typespec/http";
    import "@typespec/rest";
    
    using TypeSpec.Http;
    using TypeSpec.Rest;
    
    @service(#{ title: "Widget Service" })
    @server("(example.contoso.com/redacted)", "Widget Service endpoint")
    namespace Contoso.WidgetService;
    
    model Widget {
      name: string;
    }
    
    @route("/widgets")
    interface Widgets {
      @get
      list(
        @query("x'+str(1024)+'")
        filter?: string,
      ): Widget[];
    }
    🔍 Analysis

    parameter_serializer.py interpolates param.wire_name directly into quoted Python source in serialize_query_header; similar raw interpolation is reported in serialize_path and pop_kwargs_from_signature. A quote can therefore terminate the generated literal, yielding either a different executable key expression or a SyntaxError. This affects request construction for the Python client emitter, including the Azure-flavoured package that consumes it.

    #12405 is closely related but not a duplicate: it tracks model-side string literals, discriminator values, and model wire names, whereas this issue covers separate request-parameter serializer sites.

    📝 Suggested next steps
    • Route all affected wire names through the shared Python string-literal serializer.
    • Add query, header, path, and kwargs regression tests for quotes, backslashes, and control characters.
    • Verify generated modules parse and preserve the original wire names exactly.

    Generated by Agentic Triage

  3. akhil9tiet commented on Nov 3, 2026

    @akhil9tiet

    I'd like to work on this — please assign it to me.

  4. msyyc commented on Nov 5, 2026

    @msyyc
    Contributor

    Akhil Gupta (@akhil9tiet) appreciate if you could create a PR and we will review it. CC Libba Lawrence (@l0lawrence)

  5. added a commit that references this issue on Nov 6, 2026
    d86f9c5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workingemitter:client:pythonIssue for the Python client emitter: @typespec/http-client-python

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions