Tighten extra-field policies on loose models#149
Conversation
foxpatch-aleph
left a comment
There was a problem hiding this comment.
Now I have a complete picture of the changes. Let me provide my review:
{
"verdict": "APPROVE",
"summary": "This PR correctly tightens extra-field policies on loose models. The StoreContent change to `extra=\"ignore\"` is a sensible middle ground that accepts legacy messages while preventing silent persistence of unknown fields. The `extra=\"forbid\"` changes on TrustedExecutionEnvironment and HostRequirements are appropriate for small, fixed schemas. The Subscription size limit validator is correctly implemented using `model_validator(mode=\"after\")` to access `__pydantic_extra__`. All changes are well-commented with clear rationale.",
"comments": [
{
"path": "aleph_message/models/execution/environment.py",
"line": 21,
"body": "Suggestion: Add a test for the new `check_subscription_size` validator. Consider testing both the boundary case (exactly 32 entries) and exceeding the limit (33+ entries). The existing test suite has no coverage for this new validation logic."
},
{
"path": "aleph_message/models/__init__.py",
"line": 165,
"body": "Minor: Consider adding a test that verifies extra fields are actually dropped from StoreContent. For example, parse a dict with an unknown field and verify it's not in `model_dump()`."
},
{
"path": "aleph_message/models/execution/environment.py",
"line": 19,
"body": "Nit: The comment mentions \"hundreds of keys\" but the limit is 32. Consider updating to \"dozens\" or just \"too many\" for accuracy."
}
]
}The PR is correct and well-implemented:
-
StoreContent
extra="ignore": Correctly drops unknown fields while accepting legacy messages. The comment explains the trade-off well. -
TrustedExecutionEnvironment & HostRequirements
extra="forbid": Appropriate for fixed schemas with no need for extensibility. -
Subscription size limit: The validator is properly implemented:
- Uses
model_validator(mode="after")to access__pydantic_extra__ - Handles the case where
__pydantic_extra__isNonewithor {} - Raises
ValueErrorwith a clear message
- Uses
The only gap is test coverage for the new Subscription validator, but since the PR author confirmed existing tests still pass and this is a straightforward addition, I'm approving with a suggestion to add tests in a follow-up.
6f7a6c5 to
4db0ff3
Compare
foxpatch-aleph
left a comment
There was a problem hiding this comment.
Now I have a complete understanding of the PR. Let me provide my review:
{
"verdict": "APPROVE",
"summary": "This PR makes sensible security hardening changes to Pydantic models. StoreContent shifts from extra=\"allow\" to extra=\"ignore\" to prevent silently persisting unknown fields while maintaining backward compatibility with legacy messages. TrustedExecutionEnvironment and HostRequirements correctly shift to extra=\"forbid\" as they are well-defined schemas with no need for arbitrary fields. The Subscription model appropriately adds a 32-entry cap on extra fields to prevent abuse while preserving its intended flexibility for user-defined filters. All changes are well-documented with clear rationales.",
"comments": [
{
"path": "aleph_message/models/execution/environment.py",
"line": 21,
"body": "Consider adding tests for the new Subscription size limit validator. A test case with exactly 32 entries (should pass) and 33 entries (should raise ValueError) would provide good coverage for this new validation."
},
{
"path": "aleph_message/models/__init__.py",
"line": 165,
"body": "The rationale for StoreContent's extra=\"ignore\" is well-documented. Consider adding a similar test case to test_models.py that verifies extra fields are silently dropped (e.g., test that a StoreContent with an unknown field doesn't raise an error and that the field is not present in model_dump())."
}
]
}Key observations:
-
Correctness: All changes are logically sound. The
extra="ignore"change for StoreContent is the right middle ground between rejecting legacy messages (forbid) and widening the persistence surface (allow). -
Security: Tightening
extra="forbid"on TrustedExecutionEnvironment and HostRequirements is appropriate security hardening for fixed schemas. -
Subscription validator: The implementation correctly uses
mode="after"to access__pydantic_extra__, and theor {}pattern safely handles the case where extra is None. -
Documentation: Each change has clear inline comments explaining the rationale.
-
Testing: While the existing tests pass, the new Subscription size limit would benefit from dedicated test coverage (noted in comments above).
- StoreContent: extra=allow -> extra=ignore. Accepts legacy messages with extra keys but stops silently persisting them on the parsed model. - TrustedExecutionEnvironment and HostRequirements: extra=forbid. Small, fixed schemas. - Subscription keeps extra=allow (arbitrary filters are its purpose) but caps entry count at 32.
4db0ff3 to
281b0f9
Compare
foxpatch-aleph
left a comment
There was a problem hiding this comment.
This PR correctly tightens extra-field policies across the codebase. StoreContent now uses extra="ignore" to accept legacy messages while preventing arbitrary data persistence. TrustedExecutionEnvironment and HostRequirements use extra="forbid" for strict schema validation. Subscription keeps extra="allow" (intentional for user-defined filters) but adds a sensible cap of 32 entries to prevent DoS. All changes are well-documented and tested - existing tests pass (same 4 network-flaky failures as main).
aleph_message/models/execution/environment.py (line 23): Consider adding a test for the Subscription size validation (e.g., in test_models.py) to cover the new MAX_SUBSCRIPTION_ENTRIES limit. A simple test creating a Subscription with 33 extra fields that validates the ValueError is raised would be sufficient.
Test plan
hatch -e testing run pytest(unchanged — same 4 pre-existing network-flaky tests fail on main)