Secure development

Implementing safe structured data deserialisation

Serial interface: chip, wave, terminal arranged to illustrate implementing safe structured data deserialisation.

Implementing safe structured data deserialisation connects a security requirement to the behaviour of a real feature. The relevant controls need to remain effective across custom code, integrations and the production configuration. Understanding safe structured data deserialisation helps establish which security property the feature needs and where that property is actually enforced.

What does this involve?

Input validation defines the structures, sizes and meanings an application expects to receive. It must happen before information influences a sensitive operation. Validation does not replace safe interpreter APIs, output encoding or permission checks, because those controls address different boundaries.

Serialization is the process of turning some object into a data format that can be restored later. People often serialize objects in order to save them for storage, or to send as part of communications.

Deserialisation is the reverse of that process, taking data structured in some format, and rebuilding it into an object. Today, the most popular data format for serializing data is JSON. Before that, it was XML.

Why it matters

Consider a realistic failure: an application accepts a serialised object that can invoke behaviour rather than merely supply data.

Untrusted input can change application behaviour, consume excessive resources or reach data and execution paths outside the intended feature. The practical result depends on where the value is used and which privileges are available there.

Implementation guidance should be applied to a real feature and its deployment. Framework defaults, custom handlers and integrated services all influence whether the intended protection remains effective.

How penetration testers check for it

Trace the relevant feature from its entry point to the data or operation it protects, including custom code and alternate paths.

  • Trace input from every relevant entry point to its use.
  • Check types, lengths, ranges and ambiguous representations.
  • Compare parsing across components and alternate request formats.
  • Use controlled invalid inputs to establish effects rather than assumptions.

How to fix it

Use the appropriate maintained mechanisms and verify the result in the actual production configuration.

  • Check the use of unserialize function and review how the external parameters are accepted. Use a safe, standard data interchange format such as JSON (via json_decode and json_encode) if you need to pass serialized data to the user.
  • Uses of pickle.load or pickle.loads. Unpickling can execute arbitrary code; never unpickle untrusted data. pickle.loads expects bytes-like input, not a Python 3 string.
  • Uses of PyYAML's unsafe loaders, such as yaml.unsafe_load or yaml.load with Loader=yaml.Loader or Loader=yaml.UnsafeLoader. For untrusted YAML, use yaml.safe_load, and do not register custom constructors that allow arbitrary object creation. Current PyYAML requires an explicit Loader argument for yaml.load.
  • Configure serialisation filters with an application-specific class allowlist and resource limits before reading objects.
  • Reject unexpected structures before sensitive processing.
  • Apply resource bounds and predictable failure handling.

Blocking a few known attack strings is not a complete validation strategy. The intended data model and downstream interpretation determine which protections are needed.

Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.

Read about our methodology or contact pentested.com to discuss secure development and code review.

See your first test report this week.

Book a 30 minute demo. We will verify one of your targets with you and run a test against it, so you can judge the results on your own systems.

Book a demo