Skip to content

Events spec says a bad signature answers invalid_state 403, but a signature-only tamper answers stale_state 409 #204

Closed

Description

@JuroOravec

What happens

The Events protocol error table says:

| invalid_state | 403 | The State token is malformed or fails signature verification. |
| stale_state | 409 | The token expired or was signed only by a rotated-out secret. |

(packages/protocol/events/v1/spec.md:659-660)

If someone changes only the signature of a well-formed signed token, the
server answers stale_state 409, not invalid_state 403. The code does this
on purpose, and its tests check for it.

Expected

The spec and the code give the same answer. Another implementation built from
the spec would answer 403 here, so a client would get a different status for the same request.

Repro

Citry 0.6.0:

from dataclasses import dataclass

from citry.ext.events.tokens import (
    InvalidStateError,
    StaleStateError,
    mint_state_token,
    verify_state_token,
)


@dataclass
class State:
    count: int = 0


class Counter:
    class_id = "Counter_x"


token = mint_state_token(
    State(count=1), class_id="Counter_x", secret="k", max_age=None, max_bytes=8192
)
prefix, payload, sig = token.split(".")
forged = f"{prefix}.{payload}.{'A' if sig[0] != 'A' else 'B'}{sig[1:]}"
try:
    verify_state_token(forged, cls=Counter, secrets=["k"])
except (InvalidStateError, StaleStateError) as err:
    print(err.status, err.code)
$ python repro3.py
409 stale_state

Cause

Line numbers are on origin/main (695bd31).

  • _verify_signed() (packages/py/citry/citry/ext/events/tokens.py:518-563)
    decides the code from the payload when no current secret matches the
    signature. A payload that still parses as a well-formed token answers
    stale_state, and anything less answers invalid_state. The comment at
    :546-555 explains why: a signature-only tamper looks exactly like an
    honest token signed by a rotated-out secret, so the server cannot tell them
    apart. The payload is parsed only to pick the error code, and none of its
    values are trusted.
  • The design doc states the same rule: docs/design/events.md:1793-1802.
  • Tests check for it: test_tampered_signature asserts 409 stale_state
    (packages/py/citry/tests/test_events_tokens.py:641-660), and
    test_tampered_payload (:494-511) asserts 403 only when the payload
    itself is broken.

Recommendation

Change the spec, not the code. The code, the design doc, and the tests agree,
and the rule follows from what the server can know. Suggested rows:

  • invalid_state 403: the token cannot be read as a well-formed token (bad
    base64 or JSON, wrong version, missing fields), or it was minted for another
    component.
  • stale_state 409: the token expired, its server-side entry is gone, or its
    signature matches no current secret while the payload is well-formed. This
    covers a rotated-out secret and also a changed signature, which looks the
    same.

The current invalid_state row also leaves out the case where a token was
minted for another component, which answers 403 (tokens.py:506-513). The
current stale_state row leaves out a server-stored token whose cache entry
is gone.

Found by

The error-prevention benchmark, issue #158.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions