Skip to content

Commit 783ac21

Browse files
authored
fix(python): pass only validated input to the repository in generated routes (#566)
* fix(python): generated routes hand the repository only the validated input (1.2.1) The generated FastAPI create, PATCH and PUT handlers validated the body against <Entity>Create / <Entity>Patch and then passed the RAW body to the repository. Pydantic ignores undeclared keys and the input models leave out server-owned fields, so a client-sent id, a readOnly field and a made-up field such as isAdmin all reached the write. Security fix. Each handler (plain and TPH per-subtype) now rebinds dto to the keys the input model validated, at every depth, before any repository call, as the TypeScript routes do. Undeclared and non-settable keys are dropped silently, never 400'd, so a client sending an extra harmless field keeps working; values stay the body's own, so repositories see the same value types as in 1.2.0. Python package version 1.2.1 only; CHANGELOG entry under Security. Regression gate: tests/integration/test_generated_router_validated_input.py. * no-mistakes(document): python routes pass only validated input * no-mistakes(lint): regenerate showcase site payload for PyPI 1.2.1
1 parent 89a6774 commit 783ac21

10 files changed

Lines changed: 414 additions & 42 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@ here.**
1010

1111
## [Unreleased]
1212

13+
## [1.2.1] — Python only
14+
15+
_PyPI `1.2.1` only. npm, NuGet and Maven Central stay at `1.2.0` (`8.2.0`): none of them has a
16+
product change. No vocabulary or `metamodelVersion` change._
17+
18+
### Security
19+
20+
- **The generated Python routes passed raw request input to the repository.** The FastAPI
21+
create, PATCH and PUT handlers validated the body against `<Entity>Create` / `<Entity>Patch`
22+
and then handed the repository the raw body, not what was validated. Pydantic ignores a key a
23+
model does not declare, so a client could write fields it must never touch: a client-sent `id`
24+
on create (or on update, changing the row's identity), a `@mutability: readOnly` field, and any
25+
made-up field such as `isAdmin` all reached the repository. Found by an external review of
26+
1.2.0. Every Python adopter serving the generated routes should upgrade and regenerate; an
27+
ejected (owned) router needs the same change made by hand.
28+
29+
The repository now receives only the validated input, as the TypeScript routes already do.
30+
Each handler validates the body, then keeps only the keys the input model validated, at every
31+
depth (a nested value object included), before any repository call, in the plain and the TPH
32+
per-subtype handlers. A field the entity does not declare, a server-generated id, a `readOnly`
33+
or derived field on create, and the id, `readOnly` and `writeOnce` fields on update are all
34+
dropped silently. None of them is answered with a 400, so a client that sends an extra harmless
35+
field keeps working; a malformed value on a field the client may set is still
36+
`400 {"error": "validation"}`. An assigned key is still the caller's on create. Values are the
37+
body's own, so a repository receives the same value types as in 1.2.0.
38+
39+
Gated by `server/python/tests/integration/test_generated_router_validated_input.py`.
40+
1341
## [1.2.0] — 2026-10-30
1442

1543
_npm `1.2.0` (full lockstep across all 14 `@metaobjectsdev/*` publish candidates)._

‎docs/features/generated-mutations.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ but the semantics are one contract — call them **PATCH-1..7** (FR-035):
4141
simply omits every non-settable field, and Zod drops unknown keys — so
4242
`safeParse` succeeds and the key never reaches the `SET`. The TPH discriminator
4343
is additionally deleted by hand after parse, the same convention. A malformed
44-
*value* on a settable key is still `400 {"error":"validation"}`.
44+
*value* on a settable key is still `400 {"error":"validation"}`. A key the entity
45+
does not declare at all is dropped the same way. The Python routes hand the
46+
repository only the keys their input model validated.
4547
This matters for `writeOnce` specifically: the generated edit form submits EVERY
4648
registered field (`handleSubmit` passes all values, and 0.19.2 switched the
4749
resolver to `UpdateSchema` on edit rather than diff-and-omit), so rejecting a
@@ -109,10 +111,10 @@ repo.patch(id) { it[Authors.bio] = "…" } // touches only what you set
109111
the controller binds the partial JSON body via `<Entity>Patch.fromJson` (presence-tracked)
110112
and delegates to `repository.patch`, so an omitted key is never written.
111113

112-
**Python** — the generated FastAPI route binds the raw JSON body (the set of
113-
*present* keys — the tristate signal) and validates the present values against the
114-
generated `<Entity>Patch` model before delegating to the runtime `update`, which
115-
`SET`s only those keys.
114+
**Python** — the generated FastAPI route binds the JSON body (the set of
115+
*present* keys — the tristate signal), validates the present values against the
116+
generated `<Entity>Patch` model, and hands the repository only the keys that model
117+
validated. The runtime `update` then `SET`s only those keys.
116118

117119
**C#** — the generated route runs a per-field merge loop over the present JSON keys
118120
(the app's configured `JsonSerializerOptions`), writing only the columns the body

‎examples/showcase/.metaobjects/.gen-state/.hashes.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"generated/python/__init__.py": "daa04cec586768866d0996c779f62073e6773fe63750ff668327991fac2fd0f5",
1111
"generated/python/subscriber_filter_allowlist.py": "b0414cc95ff3982c80a0e1a06839f090c0b423e41f5a94275df93426cea09df5",
1212
"generated/python/subscriber_names.py": "269637a02bf9b46f55658b58cb42f83196c03a877e1e2101337847fd406c1561",
13-
"generated/python/subscriber_router.py": "5a354e50b1aab48239a4249b670a6d2351c1816cbca6d710ea688f252e9612e5",
13+
"generated/python/subscriber_router.py": "f3c81be3c07a10a94afb6a6a0f87eb84b9bca40ead03dd989ebd019f71b97dfc",
1414
"generated/ts/Subscriber.queries.ts": "9f1a459c57fafc98bf074b4443b506d4a1e7023ed188f293021ae89c298d4562",
1515
"generated/ts/Subscriber.routes.ts": "147d32ae658f985e024c3de6e8894ca7359f767eeaa765cec70756802e3bd300",
1616
"generated/ts/Subscriber.ts": "a78c17f915222ee108290c28468313d77f77b5cff438f485324409302e389a96",

‎examples/showcase/generated/python/subscriber_router.py‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ async def _json_body(request: Request) -> dict[str, Any] | None:
9191
return None
9292

9393

94+
def _validated_input(valid: BaseModel, raw: dict[str, Any]) -> dict[str, Any]:
95+
"""GENERATED — *raw* cut down to the keys *valid* validated, at every depth. A key the
96+
input model does not declare (an undeclared name, a server-generated id, a readOnly field)
97+
is dropped silently and never reaches the repository; values stay the body's own."""
98+
return {
99+
_k: _validated_value(getattr(valid, _k), raw[_k])
100+
for _k in valid.model_fields_set
101+
}
102+
103+
104+
def _validated_value(valid: Any, raw: Any) -> Any:
105+
if isinstance(valid, BaseModel) and isinstance(raw, dict):
106+
return _validated_input(valid, raw)
107+
if isinstance(valid, list) and isinstance(raw, list):
108+
return [_validated_value(v, r) for v, r in zip(valid, raw)]
109+
if isinstance(valid, dict) and isinstance(raw, dict):
110+
return {k: _validated_value(v, raw[k]) for k, v in valid.items() if k in raw}
111+
return raw
112+
113+
94114
_FROZEN_FIELDS: frozenset[str] = frozenset()
95115

96116

@@ -198,9 +218,13 @@ def create_subscriber(
198218
# FR-036: run the generated field constraints (length / pattern /
199219
# numeric bounds) before persisting; a violation is the cross-port 400.
200220
try:
201-
SubscriberCreate(**dto)
221+
_valid = SubscriberCreate(**dto)
202222
except ValidationError:
203223
return JSONResponse(status_code=400, content={"error": "validation"})
224+
# SECURITY (1.2.1): only what the input model validated reaches the repository. A key
225+
# it does not declare (an undeclared name, a server-generated id, a readOnly or derived
226+
# field, a frozen one on update) is dropped silently, never 400'd.
227+
dto = _validated_input(_valid, dto)
204228
# #203/ADR-0045: stamp @autoSet columns (server-owned; caller ignored).
205229
_asnow = _dt.datetime.now(_dt.timezone.utc)
206230
dto["createdAt"] = _asnow
@@ -239,9 +263,13 @@ def update_subscriber(
239263
# FR-036: validate PRESENT, non-null values with the create rules (the
240264
# all-optional patch model never fires required-checks on ABSENT fields).
241265
try:
242-
SubscriberPatch(**dto)
266+
_valid = SubscriberPatch(**dto)
243267
except ValidationError:
244268
return JSONResponse(status_code=400, content={"error": "validation"})
269+
# SECURITY (1.2.1): only what the input model validated reaches the repository. A key
270+
# it does not declare (an undeclared name, a server-generated id, a readOnly or derived
271+
# field, a frozen one on update) is dropped silently, never 400'd.
272+
dto = _validated_input(_valid, dto)
245273
dto.pop("createdAt", None) # onCreate @autoSet is write-once (server-owned)
246274
try:
247275
saved = repo.update(subscriber_id, dto)

‎examples/showcase/site-payload.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"registries": {
33
"npm": "1.2.0",
4-
"pypi": "1.2.0",
4+
"pypi": "1.2.1",
55
"nuget": "1.2.0",
66
"maven": "8.2.0",
77
"metamodel": "1.2"

‎server/python/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "metaobjects"
3-
version = "1.2.0"
3+
version = "1.2.1"
44
description = "Cross-language metadata standard: declare typed entities once, generate idiomatic drift-checked code across languages — Python port."
55
readme = "README.md"
66
requires-python = ">=3.11"

‎server/python/src/metaobjects/codegen/KNOWN_GAPS.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ The generated router takes `dto: dict[str, Any]` for `POST` / `PATCH` /
3636
(`from .<Entity> import <Entity>, <Entity>Patch`) and constructs them to enforce
3737
field constraints over HTTP — POST validates `<Entity>(**dto)`, PATCH validates
3838
the all-optional `<Entity>Patch(**dto)` (present values only) → `400 {"error":"validation"}`.
39-
The repository seam still receives the original `dict` (preserving the FR-035
40-
present-key tristate + wire values), so the strong-typing simplification below
41-
remains for the repo/response types; only the validation import was added.
39+
The repository seam receives only the keys the input model validated, as a `dict`
40+
(the FR-035 present-key tristate + wire values are kept), so the strong-typing
41+
simplification below remains for the repo/response types.
4242

4343
This keeps the router module decoupled from sibling generated files
4444
(the entity-model generator emits one `@dataclass`/Pydantic model per

‎server/python/src/metaobjects/codegen/generators/router_generator.py‎

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,9 @@ def _frozen_names(entity: MetaObject) -> list[str]:
434434
435435
ADR-0045: the OUTERMOST generated write artifact enforces the mode, and for
436436
FastAPI that is the ROUTER, not the ``<Entity>Patch`` model — the handler binds
437-
``dto`` (the raw body as a dict) and passes THAT to the repository, so a
438-
field merely absent from the patch model still reaches the row. @autoSet gets
439-
away with it because the router OVERWRITES its key server-side; a writeOnce
440-
field has no server value to overwrite, so it must be popped explicitly."""
437+
``dto`` as a dict. Since 1.2.1 the router hands the repository only the keys the
438+
patch model validated (:func:`_validated_input_lines`), which already drops these;
439+
the explicit pop stays as the FR-037 seam an owned router edits."""
441440
return [
442441
f.name
443442
for f in entity.fields()
@@ -461,6 +460,49 @@ def _frozen_strip_lines(entity: MetaObject, indent: str = " ") -> list[str]:
461460
]
462461

463462

463+
#: 1.2.1 security fix. The request body cut down to what the input model validated, emitted
464+
#: once per writable router. 1.2.0 validated the body and then handed the RAW body to the
465+
#: repository, so a client-sent server-generated id, a ``readOnly`` field or a name the entity
466+
#: does not declare (an ``isAdmin``) reached the write. Pydantic ignores a key a model does not
467+
#: declare, and the input models leave out every field a client may not set, so keeping only
468+
#: the keys the model validated (at every depth: a nested value object included) drops all of
469+
#: them silently, as the TypeScript routes do. The values stay the body's own (decimal-exact)
470+
#: values, so a repository receives the same value types as in 1.2.0.
471+
_VALIDATED_INPUT_LINES: list[str] = [
472+
"def _validated_input(valid: BaseModel, raw: dict[str, Any]) -> dict[str, Any]:",
473+
' """GENERATED — *raw* cut down to the keys *valid* validated, at every depth. A key the',
474+
" input model does not declare (an undeclared name, a server-generated id, a readOnly field)",
475+
' is dropped silently and never reaches the repository; values stay the body\'s own."""',
476+
" return {_k: _validated_value(getattr(valid, _k), raw[_k]) for _k in valid.model_fields_set}",
477+
"",
478+
"",
479+
"def _validated_value(valid: Any, raw: Any) -> Any:",
480+
" if isinstance(valid, BaseModel) and isinstance(raw, dict):",
481+
" return _validated_input(valid, raw)",
482+
" if isinstance(valid, list) and isinstance(raw, list):",
483+
" return [_validated_value(v, r) for v, r in zip(valid, raw)]",
484+
" if isinstance(valid, dict) and isinstance(raw, dict):",
485+
" return {k: _validated_value(v, raw[k]) for k, v in valid.items() if k in raw}",
486+
" return raw",
487+
]
488+
489+
490+
def _validated_input_lines(model: str, *comment: str) -> list[str]:
491+
"""Validate ``dto`` against the input *model*, then rebind it to what the model validated
492+
(see :data:`_VALIDATED_INPUT_LINES`): the repository never sees any other key."""
493+
return [
494+
*(f" # {line}" for line in comment),
495+
" try:",
496+
f" _valid = {model}(**dto)",
497+
" except ValidationError:",
498+
' return JSONResponse(status_code=400, content={"error": "validation"})',
499+
" # SECURITY (1.2.1): only what the input model validated reaches the repository. A key",
500+
" # it does not declare (an undeclared name, a server-generated id, a readOnly or derived",
501+
" # field, a frozen one on update) is dropped silently, never 400'd.",
502+
" dto = _validated_input(_valid, dto)",
503+
]
504+
505+
464506
def _auto_set_split(entity: MetaObject) -> tuple[list[MetaField], list[MetaField]]:
465507
"""The entity's ``@autoSet`` temporal fields split into ``(onCreate, onUpdate)``.
466508
@@ -1083,12 +1125,11 @@ def _emit_route_handler(
10831125
") -> Any:",
10841126
*_DECIMAL_COERCE,
10851127
*denied_strip,
1086-
" # FR-036: run the generated field constraints (length / pattern /",
1087-
" # numeric bounds) before persisting; a violation is the cross-port 400.",
1088-
" try:",
1089-
f" {model_name}(**dto)",
1090-
" except ValidationError:",
1091-
' return JSONResponse(status_code=400, content={"error": "validation"})',
1128+
*_validated_input_lines(
1129+
model_name,
1130+
"FR-036: run the generated field constraints (length / pattern /",
1131+
"numeric bounds) before persisting; a violation is the cross-port 400.",
1132+
),
10921133
# #203/ADR-0045: the generated router (the API surface) stamps @autoSet columns
10931134
# ABOVE the consumer repo seam — onCreate + onUpdate both stamped from one instant
10941135
# (a fresh row's createdAt == updatedAt), ignoring any caller-supplied value.
@@ -1114,12 +1155,11 @@ def _emit_route_handler(
11141155
" for _k in _REQUIRED_FIELDS:",
11151156
" if _k in dto and dto[_k] is None:",
11161157
' return JSONResponse(status_code=400, content={"error": "validation"})',
1117-
" # FR-036: validate PRESENT, non-null values with the create rules (the",
1118-
" # all-optional patch model never fires required-checks on ABSENT fields).",
1119-
" try:",
1120-
f" {patch_model}(**dto)",
1121-
" except ValidationError:",
1122-
' return JSONResponse(status_code=400, content={"error": "validation"})',
1158+
*_validated_input_lines(
1159+
patch_model,
1160+
"FR-036: validate PRESENT, non-null values with the create rules (the",
1161+
"all-optional patch model never fires required-checks on ABSENT fields).",
1162+
),
11231163
# #203/ADR-0045: bump onUpdate @autoSet columns (server-owned) on EVERY patch,
11241164
# above the repo seam — a server-inserted present key (compatible with the PATCH
11251165
# present-key tristate); onCreate columns are never touched here.
@@ -1468,6 +1508,9 @@ def _render_tph_router(
14681508
parts.extend(_JSON_BODY_LINES)
14691509
parts.append("")
14701510
parts.append("")
1511+
parts.extend(_VALIDATED_INPUT_LINES)
1512+
parts.append("")
1513+
parts.append("")
14711514
# FR-037 R1 — the names a PATCH must never write. Emitted even when EMPTY so
14721515
# the generated module always carries the seam (an entity that later gains a
14731516
# writeOnce field changes one literal, not the module's shape).
@@ -1547,11 +1590,9 @@ def list_sig(fn: str, route: str) -> list[str]:
15471590
parts.append(") -> Any:")
15481591
parts.extend(_DECIMAL_COERCE)
15491592
parts.extend(denied_strip_tph)
1550-
parts.append(" # FR-036: run the subtype's field constraints before persisting.")
1551-
parts.append(" try:")
1552-
parts.append(f" {sub}Create(**dto)")
1553-
parts.append(" except ValidationError:")
1554-
parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})')
1593+
parts.extend(_validated_input_lines(
1594+
f"{sub}Create", "FR-036: run the subtype's field constraints before persisting."
1595+
))
15551596
# #203/ADR-0045: stamp @autoSet columns ABOVE the repo seam (empty for a
15561597
# non-@autoSet hierarchy — byte-identical output).
15571598
parts.extend(create_autoset)
@@ -1584,11 +1625,9 @@ def list_sig(fn: str, route: str) -> list[str]:
15841625
parts.append(" for _k in _REQUIRED_FIELDS:")
15851626
parts.append(" if _k in dto and dto[_k] is None:")
15861627
parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})')
1587-
parts.append(" # FR-036: validate PRESENT, non-null values with the subtype's create rules.")
1588-
parts.append(" try:")
1589-
parts.append(f" {sub}Patch(**dto)")
1590-
parts.append(" except ValidationError:")
1591-
parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})')
1628+
parts.extend(_validated_input_lines(
1629+
f"{sub}Patch", "FR-036: validate PRESENT, non-null values with the subtype's create rules."
1630+
))
15921631
# #203/ADR-0045: pop the write-once onCreate column(s), bump onUpdate — ABOVE
15931632
# the repo seam (empty for a non-@autoSet hierarchy — byte-identical output).
15941633
parts.extend(update_autoset)
@@ -1857,6 +1896,9 @@ def render_router(
18571896
parts.extend(_JSON_BODY_LINES)
18581897
parts.append("")
18591898
parts.append("")
1899+
parts.extend(_VALIDATED_INPUT_LINES)
1900+
parts.append("")
1901+
parts.append("")
18601902
# FR-037 R1 — the names a PATCH must never write. Emitted even when EMPTY so
18611903
# the generated module always carries the seam (an entity that later gains a
18621904
# writeOnce field changes one literal, not the module's shape).
@@ -1892,8 +1934,8 @@ def render_router(
18921934
create_autoset=create_autoset,
18931935
update_autoset=update_autoset,
18941936
# FR-037 R1 — ADR-0045: the ROUTER is the outermost write artifact for
1895-
# FastAPI (the handler binds the RAW body dict and hands THAT to the
1896-
# repository), so the strip has to live here, not in <Entity>Patch.
1937+
# FastAPI (the handler binds the body as a dict), so the strip lives here,
1938+
# not in <Entity>Patch.
18971939
frozen_strip=_frozen_strip_lines(entity),
18981940
denied_strip=_denied_strip_lines(denied_names),
18991941
)

0 commit comments

Comments
 (0)