Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 38 ยท Integrity, Logging and SSRF

Signatures and Safe Deserialization

Protect data you hand to clients with HMAC signatures, understand why deserializing untrusted objects is dangerous, and choose safe formats like JSON with validation.

Applications often send data to the browser and expect it back: a cart, a preference, a "remember me" token, a one-time link. Anything the client holds, the client can edit. To trust it again you must either keep the truth on the server or sign the data so changes are detected. A second, related danger is deserialization, where a program rebuilds objects from bytes that someone else controls.

This lesson continues from Software and Data Integrity Failures. The examples are conceptual and harmless; they use JSON and an HMAC, and they never run attacker-supplied code.

Never trust data the client can edit

Imagine a cookie that stores role=user. A user changes it to role=admin in their browser's developer tools. If the server believes the cookie, access control has failed. Three safe options:

  1. Keep state on the server and give the client only a random session ID.
  2. Sign the data with a secret key so tampering is detected.
  3. Encrypt and authenticate it when the content must also be hidden.

HMAC signatures

An HMAC (hash-based message authentication code) combines a message with a secret key. Only holders of the key can produce a valid tag, so a client that edits the message cannot fix the tag. Verification must use a constant-time comparison.

Python Sign a cookie value and detect tampering

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A signed token is readable by the client (it is only Base64). Do not put secrets in it. Signing gives integrity, not confidentiality.

Signature pitfalls

  • Missing expiry: add an exp timestamp inside the signed data and check it, or a stolen token works forever.
  • Replay: a valid token can be sent again; bind it to a user and a purpose, and use one-time identifiers where it matters.
  • Algorithm confusion: with standards like JWT, never let the token choose its own algorithm. Reject none and accept only the algorithm your server expects.
  • Weak or leaked keys: use long random keys, keep them out of source control and plan rotation.
  • Signing the wrong thing: sign every field that matters, not just some.
Python Add expiry and a purpose to the signed data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Deserialization

Serialization turns an object into bytes (to store or send). Deserialization rebuilds the object. Some formats and libraries can rebuild arbitrary classes and, along the way, run code in constructors, destructors or magic methods. If an attacker controls those bytes, they may be able to make the application run unintended code, change logic or crash it. This class of flaw has caused severe breaches in Java, PHP, Python, .NET and Ruby applications.

Why it is dangerous: the data decides which types get created. The code that processes the object may then act on attacker-chosen properties.

Safe habits:

  • Do not deserialize untrusted data with native object formats. In Python avoid pickle for anything from outside; in PHP avoid unserialize() on user input; in Java avoid native ObjectInputStream on untrusted streams.
  • Use data-only formats such as JSON, with a schema, and map them to known types yourself.
  • Validate after parsing: types, ranges, allowed values, sizes.
  • Sign or authenticate serialized data you must round-trip through the client, and verify before parsing.
  • Allow-list the classes a deserializer may create, if you must use one.
  • Limit input size and nesting depth.
Python Validate parsed JSON instead of trusting it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Even JSON is not magic: the danger returns if you copy every field into an object blindly (mass assignment). Accept only the fields you list.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A cookie contains role=user and the server trusts it. What is the best fix?

    Anything the client holds can be edited. Server-side state or a verified signature stops silent changes. Base64 is not protection.
  2. 2. What does an HMAC signature provide?

    An HMAC proves data was not altered by someone without the key. It does not hide the content.
  3. 3. Why is deserializing untrusted native objects dangerous?

    Rebuilding arbitrary types lets attacker-chosen data drive code that runs during or after reconstruction.
  4. 4. Which pair of checks should a signed token carry in addition to the signature?

    Expiry limits stolen-token use and a purpose stops a token for one action being replayed for another.

Summary

  • Client-held data is editable; keep truth on the server or sign it with a secret key.
  • Verify HMACs in constant time and include expiry, purpose and every important field.
  • Avoid native deserialization of untrusted input; use data-only formats and validate strictly.
  • Accept only the fields you expect to prevent mass assignment.

Next: Security Logging and Monitoring Failures