Skip to content

Tighten extra-field policies on loose models#149

Merged
odesenfans merged 1 commit into
mainfrom
chore/tighten-extras-policies
Apr 22, 2026
Merged

Tighten extra-field policies on loose models#149
odesenfans merged 1 commit into
mainfrom
chore/tighten-extras-policies

Conversation

@odesenfans
Copy link
Copy Markdown
Contributor

  • 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.

Test plan

  • hatch -e testing run pytest (unchanged — same 4 pre-existing network-flaky tests fail on main)

Copy link
Copy Markdown

@foxpatch-aleph foxpatch-aleph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. StoreContent extra="ignore": Correctly drops unknown fields while accepting legacy messages. The comment explains the trade-off well.

  2. TrustedExecutionEnvironment & HostRequirements extra="forbid": Appropriate for fixed schemas with no need for extensibility.

  3. Subscription size limit: The validator is properly implemented:

    • Uses model_validator(mode="after") to access __pydantic_extra__
    • Handles the case where __pydantic_extra__ is None with or {}
    • Raises ValueError with a clear message

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.

@odesenfans odesenfans force-pushed the chore/tighten-extras-policies branch from 6f7a6c5 to 4db0ff3 Compare April 21, 2026 21:44
Copy link
Copy Markdown

@foxpatch-aleph foxpatch-aleph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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).

  2. Security: Tightening extra="forbid" on TrustedExecutionEnvironment and HostRequirements is appropriate security hardening for fixed schemas.

  3. Subscription validator: The implementation correctly uses mode="after" to access __pydantic_extra__, and the or {} pattern safely handles the case where extra is None.

  4. Documentation: Each change has clear inline comments explaining the rationale.

  5. 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.
@odesenfans odesenfans force-pushed the chore/tighten-extras-policies branch from 4db0ff3 to 281b0f9 Compare April 22, 2026 07:52
@odesenfans odesenfans merged commit 3c0de1f into main Apr 22, 2026
10 checks passed
@odesenfans odesenfans deleted the chore/tighten-extras-policies branch April 22, 2026 08:00
Copy link
Copy Markdown

@foxpatch-aleph foxpatch-aleph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants