deniz.in

Markets

Weather

Loading weather

· via dev.to (home feed)

Dictionary pattern matching in Python and Rust silently ignores extra keys

A dev.to write-up warns that dictionary patterns in languages like Python and Rust match mappings carrying extra keys and discard them, so code assuming strict shape validation can let malformed data through.

Dictionary pattern matching in Python and Rust silently ignores extra keys

What the mismatch is

Pattern matching lets developers pull values out of structured data in a single expression, and a recent article on dev.to by romdevin highlights a subtlety that can trip up even experienced programmers. In the languages the article cites, such as Python and Rust, patterns written against dictionaries do not enforce a strict shape. A sequence pattern succeeds only when the target holds exactly the expected elements; a dictionary pattern succeeds as long as the keys it names are present with the right values. Anything else in the dictionary goes unexamined.

The article's central example: write a pattern for the keys a and b, then hand it a dictionary that also contains c. The match succeeds, and c is discarded without any error or warning.

Why developers get caught out

According to the author, the trap is extrapolation. Programmers familiar with how sequence patterns demand an exact fit naturally assume dictionary patterns behave the same way, when the language design actually opts for flexibility. That flexibility is useful when payloads legitimately vary in shape, but hazardous when they must not. The article also argues that documentation rarely calls the behavior out clearly, so the incorrect assumption persists until something breaks.

Scenarios where it bites

The dev.to piece sketches several failure modes:

  • Financial processing: a system uses a pattern to extract amount and currency from a transaction record. An attacker slips in an extra key such as override_amount. The pattern matches, validation passes, and the surplus key travels downstream where other code may act on it.
  • Credential checks: a pattern covering username and password matches a dictionary that also carries an injected admin_access flag; if later logic trusts that key, privileges can escalate.
  • Nested data: because keys are ignored at every level of a nested structure, errors can compound and become significantly harder to trace.

The common thread, per the article, is silence. Nothing throws and nothing logs, so malformed or malicious data keeps flowing undetected.

How to close the gap

The recommended fix is to stop treating pattern matching as validation. When an exact shape matters, check the key set explicitly before matching; the article's Python example compares set(d.keys()) against the expected keys so that any surplus key fails fast. The author's rule of thumb: in security-sensitive or financial code, always pre-validate dictionary keys rather than relying on the pattern itself to reject unexpected input.

Why it matters

Pattern matching is increasingly the first tool developers reach for when parsing external input such as JSON payloads, API bodies and configuration, which is precisely the data an attacker or a buggy client controls. Because extra keys raise no exception, ordinary tests built on well-formed fixtures will never reveal the gap; the first visible symptom may be corrupted records or a privilege escalation. The remedy is cheap, since an explicit key-set comparison is all it takes, and the lesson is broader: understand which guarantees a language construct actually provides before using it as a gatekeeper, because features designed for convenience rarely double as validators.

  • #pattern-matching
  • #python
  • #rust
  • #data-validation
  • #debugging

Related posts