Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ here.**

## [Unreleased]

## [1.2.1] — Python only

_PyPI `1.2.1` only. npm, NuGet and Maven Central stay at `1.2.0` (`8.2.0`): none of them has a
product change. No vocabulary or `metamodelVersion` change._

### Security

- **The generated Python routes passed raw request input to the repository.** The FastAPI
create, PATCH and PUT handlers validated the body against `<Entity>Create` / `<Entity>Patch`
and then handed the repository the raw body, not what was validated. Pydantic ignores a key a
model does not declare, so a client could write fields it must never touch: a client-sent `id`
on create (or on update, changing the row's identity), a `@mutability: readOnly` field, and any
made-up field such as `isAdmin` all reached the repository. Found by an external review of
1.2.0. Every Python adopter serving the generated routes should upgrade and regenerate; an
ejected (owned) router needs the same change made by hand.

The repository now receives only the validated input, as the TypeScript routes already do.
Each handler validates the body, then keeps only the keys the input model validated, at every
depth (a nested value object included), before any repository call, in the plain and the TPH
per-subtype handlers. A field the entity does not declare, a server-generated id, a `readOnly`
or derived field on create, and the id, `readOnly` and `writeOnce` fields on update are all
dropped silently. None of them is answered with a 400, so a client that sends an extra harmless
field keeps working; a malformed value on a field the client may set is still
`400 {"error": "validation"}`. An assigned key is still the caller's on create. Values are the
body's own, so a repository receives the same value types as in 1.2.0.

Gated by `server/python/tests/integration/test_generated_router_validated_input.py`.

## [1.2.0] — 2026-10-30

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

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

**C#** — the generated route runs a per-field merge loop over the present JSON keys
(the app's configured `JsonSerializerOptions`), writing only the columns the body
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/.metaobjects/.gen-state/.hashes.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"generated/python/__init__.py": "daa04cec586768866d0996c779f62073e6773fe63750ff668327991fac2fd0f5",
"generated/python/subscriber_filter_allowlist.py": "b0414cc95ff3982c80a0e1a06839f090c0b423e41f5a94275df93426cea09df5",
"generated/python/subscriber_names.py": "269637a02bf9b46f55658b58cb42f83196c03a877e1e2101337847fd406c1561",
"generated/python/subscriber_router.py": "5a354e50b1aab48239a4249b670a6d2351c1816cbca6d710ea688f252e9612e5",
"generated/python/subscriber_router.py": "f3c81be3c07a10a94afb6a6a0f87eb84b9bca40ead03dd989ebd019f71b97dfc",
"generated/ts/Subscriber.queries.ts": "9f1a459c57fafc98bf074b4443b506d4a1e7023ed188f293021ae89c298d4562",
"generated/ts/Subscriber.routes.ts": "147d32ae658f985e024c3de6e8894ca7359f767eeaa765cec70756802e3bd300",
"generated/ts/Subscriber.ts": "a78c17f915222ee108290c28468313d77f77b5cff438f485324409302e389a96",
Expand Down
32 changes: 30 additions & 2 deletions examples/showcase/generated/python/subscriber_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ async def _json_body(request: Request) -> dict[str, Any] | None:
return None


def _validated_input(valid: BaseModel, raw: dict[str, Any]) -> dict[str, Any]:
"""GENERATED — *raw* cut down to the keys *valid* validated, at every depth. A key the
input model does not declare (an undeclared name, a server-generated id, a readOnly field)
is dropped silently and never reaches the repository; values stay the body's own."""
return {
_k: _validated_value(getattr(valid, _k), raw[_k])
for _k in valid.model_fields_set
}


def _validated_value(valid: Any, raw: Any) -> Any:
if isinstance(valid, BaseModel) and isinstance(raw, dict):
return _validated_input(valid, raw)
if isinstance(valid, list) and isinstance(raw, list):
return [_validated_value(v, r) for v, r in zip(valid, raw)]
if isinstance(valid, dict) and isinstance(raw, dict):
return {k: _validated_value(v, raw[k]) for k, v in valid.items() if k in raw}
return raw


_FROZEN_FIELDS: frozenset[str] = frozenset()


Expand Down Expand Up @@ -198,9 +218,13 @@ def create_subscriber(
# FR-036: run the generated field constraints (length / pattern /
# numeric bounds) before persisting; a violation is the cross-port 400.
try:
SubscriberCreate(**dto)
_valid = SubscriberCreate(**dto)
except ValidationError:
return JSONResponse(status_code=400, content={"error": "validation"})
# SECURITY (1.2.1): only what the input model validated reaches the repository. A key
# it does not declare (an undeclared name, a server-generated id, a readOnly or derived
# field, a frozen one on update) is dropped silently, never 400'd.
dto = _validated_input(_valid, dto)
# #203/ADR-0045: stamp @autoSet columns (server-owned; caller ignored).
_asnow = _dt.datetime.now(_dt.timezone.utc)
dto["createdAt"] = _asnow
Expand Down Expand Up @@ -239,9 +263,13 @@ def update_subscriber(
# FR-036: validate PRESENT, non-null values with the create rules (the
# all-optional patch model never fires required-checks on ABSENT fields).
try:
SubscriberPatch(**dto)
_valid = SubscriberPatch(**dto)
except ValidationError:
return JSONResponse(status_code=400, content={"error": "validation"})
# SECURITY (1.2.1): only what the input model validated reaches the repository. A key
# it does not declare (an undeclared name, a server-generated id, a readOnly or derived
# field, a frozen one on update) is dropped silently, never 400'd.
dto = _validated_input(_valid, dto)
dto.pop("createdAt", None) # onCreate @autoSet is write-once (server-owned)
try:
saved = repo.update(subscriber_id, dto)
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/site-payload.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"registries": {
"npm": "1.2.0",
"pypi": "1.2.0",
"pypi": "1.2.1",
"nuget": "1.2.0",
"maven": "8.2.0",
"metamodel": "1.2"
Expand Down
2 changes: 1 addition & 1 deletion server/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "metaobjects"
version = "1.2.0"
version = "1.2.1"
description = "Cross-language metadata standard: declare typed entities once, generate idiomatic drift-checked code across languages — Python port."
readme = "README.md"
requires-python = ">=3.11"
Expand Down
6 changes: 3 additions & 3 deletions server/python/src/metaobjects/codegen/KNOWN_GAPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ The generated router takes `dto: dict[str, Any]` for `POST` / `PATCH` /
(`from .<Entity> import <Entity>, <Entity>Patch`) and constructs them to enforce
field constraints over HTTP — POST validates `<Entity>(**dto)`, PATCH validates
the all-optional `<Entity>Patch(**dto)` (present values only) → `400 {"error":"validation"}`.
The repository seam still receives the original `dict` (preserving the FR-035
present-key tristate + wire values), so the strong-typing simplification below
remains for the repo/response types; only the validation import was added.
The repository seam receives only the keys the input model validated, as a `dict`
(the FR-035 present-key tristate + wire values are kept), so the strong-typing
simplification below remains for the repo/response types.

This keeps the router module decoupled from sibling generated files
(the entity-model generator emits one `@dataclass`/Pydantic model per
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,9 @@ def _frozen_names(entity: MetaObject) -> list[str]:
ADR-0045: the OUTERMOST generated write artifact enforces the mode, and for
FastAPI that is the ROUTER, not the ``<Entity>Patch`` model — the handler binds
``dto`` (the raw body as a dict) and passes THAT to the repository, so a
field merely absent from the patch model still reaches the row. @autoSet gets
away with it because the router OVERWRITES its key server-side; a writeOnce
field has no server value to overwrite, so it must be popped explicitly."""
``dto`` as a dict. Since 1.2.1 the router hands the repository only the keys the
patch model validated (:func:`_validated_input_lines`), which already drops these;
the explicit pop stays as the FR-037 seam an owned router edits."""
return [
f.name
for f in entity.fields()
Expand All @@ -461,6 +460,49 @@ def _frozen_strip_lines(entity: MetaObject, indent: str = " ") -> list[str]:
]


#: 1.2.1 security fix. The request body cut down to what the input model validated, emitted
#: once per writable router. 1.2.0 validated the body and then handed the RAW body to the
#: repository, so a client-sent server-generated id, a ``readOnly`` field or a name the entity
#: does not declare (an ``isAdmin``) reached the write. Pydantic ignores a key a model does not
#: declare, and the input models leave out every field a client may not set, so keeping only
#: the keys the model validated (at every depth: a nested value object included) drops all of
#: them silently, as the TypeScript routes do. The values stay the body's own (decimal-exact)
#: values, so a repository receives the same value types as in 1.2.0.
_VALIDATED_INPUT_LINES: list[str] = [
"def _validated_input(valid: BaseModel, raw: dict[str, Any]) -> dict[str, Any]:",
' """GENERATED — *raw* cut down to the keys *valid* validated, at every depth. A key the',
" input model does not declare (an undeclared name, a server-generated id, a readOnly field)",
' is dropped silently and never reaches the repository; values stay the body\'s own."""',
" return {_k: _validated_value(getattr(valid, _k), raw[_k]) for _k in valid.model_fields_set}",
"",
"",
"def _validated_value(valid: Any, raw: Any) -> Any:",
" if isinstance(valid, BaseModel) and isinstance(raw, dict):",
" return _validated_input(valid, raw)",
" if isinstance(valid, list) and isinstance(raw, list):",
" return [_validated_value(v, r) for v, r in zip(valid, raw)]",
" if isinstance(valid, dict) and isinstance(raw, dict):",
" return {k: _validated_value(v, raw[k]) for k, v in valid.items() if k in raw}",
" return raw",
]


def _validated_input_lines(model: str, *comment: str) -> list[str]:
"""Validate ``dto`` against the input *model*, then rebind it to what the model validated
(see :data:`_VALIDATED_INPUT_LINES`): the repository never sees any other key."""
return [
*(f" # {line}" for line in comment),
" try:",
f" _valid = {model}(**dto)",
" except ValidationError:",
' return JSONResponse(status_code=400, content={"error": "validation"})',
" # SECURITY (1.2.1): only what the input model validated reaches the repository. A key",
" # it does not declare (an undeclared name, a server-generated id, a readOnly or derived",
" # field, a frozen one on update) is dropped silently, never 400'd.",
" dto = _validated_input(_valid, dto)",
]


def _auto_set_split(entity: MetaObject) -> tuple[list[MetaField], list[MetaField]]:
"""The entity's ``@autoSet`` temporal fields split into ``(onCreate, onUpdate)``.
Expand Down Expand Up @@ -1083,12 +1125,11 @@ def _emit_route_handler(
") -> Any:",
*_DECIMAL_COERCE,
*denied_strip,
" # FR-036: run the generated field constraints (length / pattern /",
" # numeric bounds) before persisting; a violation is the cross-port 400.",
" try:",
f" {model_name}(**dto)",
" except ValidationError:",
' return JSONResponse(status_code=400, content={"error": "validation"})',
*_validated_input_lines(
model_name,
"FR-036: run the generated field constraints (length / pattern /",
"numeric bounds) before persisting; a violation is the cross-port 400.",
),
# #203/ADR-0045: the generated router (the API surface) stamps @autoSet columns
# ABOVE the consumer repo seam — onCreate + onUpdate both stamped from one instant
# (a fresh row's createdAt == updatedAt), ignoring any caller-supplied value.
Expand All @@ -1114,12 +1155,11 @@ def _emit_route_handler(
" for _k in _REQUIRED_FIELDS:",
" if _k in dto and dto[_k] is None:",
' return JSONResponse(status_code=400, content={"error": "validation"})',
" # FR-036: validate PRESENT, non-null values with the create rules (the",
" # all-optional patch model never fires required-checks on ABSENT fields).",
" try:",
f" {patch_model}(**dto)",
" except ValidationError:",
' return JSONResponse(status_code=400, content={"error": "validation"})',
*_validated_input_lines(
patch_model,
"FR-036: validate PRESENT, non-null values with the create rules (the",
"all-optional patch model never fires required-checks on ABSENT fields).",
),
# #203/ADR-0045: bump onUpdate @autoSet columns (server-owned) on EVERY patch,
# above the repo seam — a server-inserted present key (compatible with the PATCH
# present-key tristate); onCreate columns are never touched here.
Expand Down Expand Up @@ -1468,6 +1508,9 @@ def _render_tph_router(
parts.extend(_JSON_BODY_LINES)
parts.append("")
parts.append("")
parts.extend(_VALIDATED_INPUT_LINES)
parts.append("")
parts.append("")
# FR-037 R1 — the names a PATCH must never write. Emitted even when EMPTY so
# the generated module always carries the seam (an entity that later gains a
# writeOnce field changes one literal, not the module's shape).
Expand Down Expand Up @@ -1547,11 +1590,9 @@ def list_sig(fn: str, route: str) -> list[str]:
parts.append(") -> Any:")
parts.extend(_DECIMAL_COERCE)
parts.extend(denied_strip_tph)
parts.append(" # FR-036: run the subtype's field constraints before persisting.")
parts.append(" try:")
parts.append(f" {sub}Create(**dto)")
parts.append(" except ValidationError:")
parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})')
parts.extend(_validated_input_lines(
f"{sub}Create", "FR-036: run the subtype's field constraints before persisting."
))
# #203/ADR-0045: stamp @autoSet columns ABOVE the repo seam (empty for a
# non-@autoSet hierarchy — byte-identical output).
parts.extend(create_autoset)
Expand Down Expand Up @@ -1584,11 +1625,9 @@ def list_sig(fn: str, route: str) -> list[str]:
parts.append(" for _k in _REQUIRED_FIELDS:")
parts.append(" if _k in dto and dto[_k] is None:")
parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})')
parts.append(" # FR-036: validate PRESENT, non-null values with the subtype's create rules.")
parts.append(" try:")
parts.append(f" {sub}Patch(**dto)")
parts.append(" except ValidationError:")
parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})')
parts.extend(_validated_input_lines(
f"{sub}Patch", "FR-036: validate PRESENT, non-null values with the subtype's create rules."
))
# #203/ADR-0045: pop the write-once onCreate column(s), bump onUpdate — ABOVE
# the repo seam (empty for a non-@autoSet hierarchy — byte-identical output).
parts.extend(update_autoset)
Expand Down Expand Up @@ -1857,6 +1896,9 @@ def render_router(
parts.extend(_JSON_BODY_LINES)
parts.append("")
parts.append("")
parts.extend(_VALIDATED_INPUT_LINES)
parts.append("")
parts.append("")
# FR-037 R1 — the names a PATCH must never write. Emitted even when EMPTY so
# the generated module always carries the seam (an entity that later gains a
# writeOnce field changes one literal, not the module's shape).
Expand Down Expand Up @@ -1892,8 +1934,8 @@ def render_router(
create_autoset=create_autoset,
update_autoset=update_autoset,
# FR-037 R1 — ADR-0045: the ROUTER is the outermost write artifact for
# FastAPI (the handler binds the RAW body dict and hands THAT to the
# repository), so the strip has to live here, not in <Entity>Patch.
# FastAPI (the handler binds the body as a dict), so the strip lives here,
# not in <Entity>Patch.
frozen_strip=_frozen_strip_lines(entity),
denied_strip=_denied_strip_lines(denied_names),
)
Expand Down
Loading
Loading