2023-03-07 17:16:47 +01:00
|
|
|
== How to fix it in Laminas
|
|
|
|
|
|
|
|
=== Code examples
|
2022-11-08 13:51:45 +01:00
|
|
|
|
|
|
|
The following code is vulnerable to deserialization attacks because it
|
|
|
|
deserializes HTTP data without validating it first.
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
|
2023-08-10 15:57:24 +02:00
|
|
|
[source,php,diff-id=11,diff-type=noncompliant]
|
2022-11-08 13:51:45 +01:00
|
|
|
----
|
|
|
|
$serializer = new Adapter\PhpSerialize();
|
|
|
|
$session = $serializer->unserialize($_COOKIE['session']); // Noncompliant
|
|
|
|
return new ViewModel([
|
|
|
|
"auth" => $session->auth
|
|
|
|
]);
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
2023-08-10 15:57:24 +02:00
|
|
|
[source,php,diff-id=11,diff-type=compliant]
|
2022-11-08 13:51:45 +01:00
|
|
|
----
|
|
|
|
$serializer = new Adapter\Json();
|
|
|
|
$session = $serializer->unserialize($_COOKIE['session']);
|
|
|
|
return new ViewModel([
|
|
|
|
"auth" => $session['auth']
|
|
|
|
]);
|
|
|
|
----
|
|
|
|
|
|
|
|
=== How does this work?
|
|
|
|
|
|
|
|
include::../../common/fix/introduction.adoc[]
|
|
|
|
|
|
|
|
include::../../common/fix/safer-serialization.adoc[]
|
|
|
|
|
2023-08-10 15:57:24 +02:00
|
|
|
The example compliant code uses the `Json` adapter to perform the
|
2022-11-08 13:51:45 +01:00
|
|
|
deserialization. This one will not carry out any automatic object instantiation
|
|
|
|
and is thus safer than its `PhpSerialize` counterpart.
|
|
|
|
|
|
|
|
include::../../common/fix/integrity-check.adoc[]
|
|
|
|
|
|
|
|
include::../../common/fix/pre-approved-list.adoc[]
|
|
|
|
|
|
|
|
=== Pitfalls
|
|
|
|
|
|
|
|
include::../../common/pitfalls/constant-time.adoc[]
|