You are here

README.txt in Secure Cookie Data 7.2

Same filename and directory in other branches
  1. 7 README.txt
Secure Data Cookie
==================

Introduction
------------

*Secure Data Cookie* is a module that implements the [Secure Cookie
Protocol](http://www.cs.utexas.edu/~gouda/papers/conference/cookie.pdf).
This protocol garantuees that cookies cannot be tampered.

Tamper resistance is achieved through the use of an HMAC that validates
the data stored in the cookie each time the data is retrieved. If the
validation fails an empty object is returned.

Design goals
------------

The goals we aim for are three:

1.  Have a way to store data in a cookie so that this data cannot be
    tampered with.

2.  Have data confidentiality. Note that at the moment that's not yet
    implemented.

3.  Implement an expirable token so that cookies are regenerated
    periodically and the token contains the time to live so that replay
    attacks can be better diffused.

Remarks on the Implementation
-----------------------------

The above cited paper prescribes *SSL* for the solution to be
considered really *secure*.

Otherwise you're exposed to
[MitM](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) attacks.
Note also that there's not any protection against [replay
attacks](https://en.wikipedia.org/wiki/Replay_attack). The only existing
functionality to address that type of attack in a small way is the
cookie expiration time. When closing the browser the cookie is
destroyed.

Currently the use of the cookie generated by the module is advisable
only for non critical things. One example is the order ID in [Drupal
Commerce](https://drupal.org/project/commerce), allowing you to
implement a cart that doesn't generate a session whenever you create a
cart.

Another example is storing data about some sort of dynamic page that is
unique to each client/user.

Target Audience
---------------

This is a module aimed at *developers* or to site builders that
*dabble* in development to put it bluntly.

Use Cases
---------

Use cases for the module are whenever *security* and/or
*performance* are at play.

It's common for module developers to use session variables to store some
particular settings that are specific to a given user. This practices
raises two issues:

1.  As soon as you store anything in a session, i.e., *write* to
    `$_SESSION`, Drupal starts a session, sending a cookie with a
    session token. This invalidates the vast majority of caching
    strategies.

2.  When setting a session variable you hit the persistence layer.
    Meaning the database in most cases. Therefore hurting performance.

This module addresses these issues by maintaining whatever it is you
need to store between the client and the server. The overhead in terms
of HTTP payload size is negligable relative to the common Drupal size
page.

How to Use
----------

The module provides two functions to be used in your module to store and
retrieve data from the cookie.

### Setting/modifying a cookie

Invoke `secure_data_cookie_put(<data>)` where `<data>` is the data you
want to store in the cookie. By default the cookie is called
`SecureCookieData`. The name can be altered through the
`SecureCookieBasic` class `$__cookie_name` attribute.

Usually you have the data in a serialized format. JSON is particularly
useful. Here's a complete example:

    secure_cookie_data_put(json_encode(array('arg' => 'property_one')));

this creates a cookie (named `SecureDataCookie` by default) with the
given name containing a base64 encoding of the data:

    eyJhcmciOiJ0ZXN0WVVZZ2hnaFVtZTM0NTUiLCJobWFjIjoiZThhNmVmNmNmNjFiMTMxZGZmMDcxMzhiZDcyYTdmNTkwM2I1YzY5NiJ9

Now to recover the value you do:

    secure_cookie_data_get();

that prints the initial array.

Cookie life time
----------------

By default the cookie is removed once you close the browser, i.e., the
life time is 0. To set it to a different value through the class
attribute `secureCookieBasic::$__cookie_duration`.

See the remarks above replay attacks on the consequences of messing with
cookie duration.

HMAC key
--------

There's a method `get_secret` that uses
[`drupal_get_hash_salt()`](https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_get_hash_salt/7)
to generate the key. This means that each Drupal has its *unique key*.
This avoids the cookie being vulnerable to client side modification by
recomputation of the HMAC.

This method can be overloaded by extending the class. So you can
implement your own HMAC key management scheme.

Complex data structures in cookies: storing unbalacend binary trees
-------------------------------------------------------------------

Besides the basic flat data structure that you can use non flat data
structures like trees. There's a submodule called
`secure_cookie_data_tree_two`.

This module allows you to store a list of unbalanced binary trees. It
was developed for a client project that needs to store a product
configuration that is specific to an user. Before it used a session
variable that stored that information. The result being that it created
a session voiding page caching.

Here's an example storing the configuration data:

      array('label_level0' => 'nid', 'nid' => 123,
            'label_level1' => 'support', 'value' = 'xxx')


                          nid => 123
                                 / \
                                /   \
                          support  format
                              /       \
                             /         \
                  (array) [xxx]    [big, small]

Let's got through it carefully:

1.  The tree has two levels.

2.  The first level has the value of `label_level1`.

3.  The second level has only values. The values can be whatever you
    want.

4.  All the trees are stored in a list. The list is implemented as an
    associative array with a key. The key is defined by `label_level0`.

The implementation of this data structure is done through the extension
of the `secureCookieBasic` class. It can be used as an example for
defining your own data structure to be stored in the cookie.

Final Remarks
-------------

Note that the secret generation could be done automatically upon
install. Nevertheless that would void one of the design goals which is
to avoid hitting the database at all. Since setting the value in install
would envolve using a variable, hence the database.

Even if it's a little less convenient, the gains in terms of simplicity
and performance justify it largely.

TODO
----

-   Implement cookie data confidentiality using crypto.

-   Implement a token that defines an expiration so that the cookies are
    regenerated periodically, to void replay attacks.

Acknowledgements
----------------

The development and maintenance of this module is sponsored by [Commerce
Guys](http://commerceguys.com).

File

README.txt
View source
  1. Secure Data Cookie
  2. ==================
  3. Introduction
  4. ------------
  5. *Secure Data Cookie* is a module that implements the [Secure Cookie
  6. Protocol](http://www.cs.utexas.edu/~gouda/papers/conference/cookie.pdf).
  7. This protocol garantuees that cookies cannot be tampered.
  8. Tamper resistance is achieved through the use of an HMAC that validates
  9. the data stored in the cookie each time the data is retrieved. If the
  10. validation fails an empty object is returned.
  11. Design goals
  12. ------------
  13. The goals we aim for are three:
  14. 1. Have a way to store data in a cookie so that this data cannot be
  15. tampered with.
  16. 2. Have data confidentiality. Note that at the moment that's not yet
  17. implemented.
  18. 3. Implement an expirable token so that cookies are regenerated
  19. periodically and the token contains the time to live so that replay
  20. attacks can be better diffused.
  21. Remarks on the Implementation
  22. -----------------------------
  23. The above cited paper prescribes *SSL* for the solution to be
  24. considered really *secure*.
  25. Otherwise you're exposed to
  26. [MitM](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) attacks.
  27. Note also that there's not any protection against [replay
  28. attacks](https://en.wikipedia.org/wiki/Replay_attack). The only existing
  29. functionality to address that type of attack in a small way is the
  30. cookie expiration time. When closing the browser the cookie is
  31. destroyed.
  32. Currently the use of the cookie generated by the module is advisable
  33. only for non critical things. One example is the order ID in [Drupal
  34. Commerce](https://drupal.org/project/commerce), allowing you to
  35. implement a cart that doesn't generate a session whenever you create a
  36. cart.
  37. Another example is storing data about some sort of dynamic page that is
  38. unique to each client/user.
  39. Target Audience
  40. ---------------
  41. This is a module aimed at *developers* or to site builders that
  42. *dabble* in development to put it bluntly.
  43. Use Cases
  44. ---------
  45. Use cases for the module are whenever *security* and/or
  46. *performance* are at play.
  47. It's common for module developers to use session variables to store some
  48. particular settings that are specific to a given user. This practices
  49. raises two issues:
  50. 1. As soon as you store anything in a session, i.e., *write* to
  51. `$_SESSION`, Drupal starts a session, sending a cookie with a
  52. session token. This invalidates the vast majority of caching
  53. strategies.
  54. 2. When setting a session variable you hit the persistence layer.
  55. Meaning the database in most cases. Therefore hurting performance.
  56. This module addresses these issues by maintaining whatever it is you
  57. need to store between the client and the server. The overhead in terms
  58. of HTTP payload size is negligable relative to the common Drupal size
  59. page.
  60. How to Use
  61. ----------
  62. The module provides two functions to be used in your module to store and
  63. retrieve data from the cookie.
  64. ### Setting/modifying a cookie
  65. Invoke `secure_data_cookie_put()` where `` is the data you
  66. want to store in the cookie. By default the cookie is called
  67. `SecureCookieData`. The name can be altered through the
  68. `SecureCookieBasic` class `$__cookie_name` attribute.
  69. Usually you have the data in a serialized format. JSON is particularly
  70. useful. Here's a complete example:
  71. secure_cookie_data_put(json_encode(array('arg' => 'property_one')));
  72. this creates a cookie (named `SecureDataCookie` by default) with the
  73. given name containing a base64 encoding of the data:
  74. eyJhcmciOiJ0ZXN0WVVZZ2hnaFVtZTM0NTUiLCJobWFjIjoiZThhNmVmNmNmNjFiMTMxZGZmMDcxMzhiZDcyYTdmNTkwM2I1YzY5NiJ9
  75. Now to recover the value you do:
  76. secure_cookie_data_get();
  77. that prints the initial array.
  78. Cookie life time
  79. ----------------
  80. By default the cookie is removed once you close the browser, i.e., the
  81. life time is 0. To set it to a different value through the class
  82. attribute `secureCookieBasic::$__cookie_duration`.
  83. See the remarks above replay attacks on the consequences of messing with
  84. cookie duration.
  85. HMAC key
  86. --------
  87. There's a method `get_secret` that uses
  88. [`drupal_get_hash_salt()`](https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_get_hash_salt/7)
  89. to generate the key. This means that each Drupal has its *unique key*.
  90. This avoids the cookie being vulnerable to client side modification by
  91. recomputation of the HMAC.
  92. This method can be overloaded by extending the class. So you can
  93. implement your own HMAC key management scheme.
  94. Complex data structures in cookies: storing unbalacend binary trees
  95. -------------------------------------------------------------------
  96. Besides the basic flat data structure that you can use non flat data
  97. structures like trees. There's a submodule called
  98. `secure_cookie_data_tree_two`.
  99. This module allows you to store a list of unbalanced binary trees. It
  100. was developed for a client project that needs to store a product
  101. configuration that is specific to an user. Before it used a session
  102. variable that stored that information. The result being that it created
  103. a session voiding page caching.
  104. Here's an example storing the configuration data:
  105. array('label_level0' => 'nid', 'nid' => 123,
  106. 'label_level1' => 'support', 'value' = 'xxx')
  107. nid => 123
  108. / \
  109. / \
  110. support format
  111. / \
  112. / \
  113. (array) [xxx] [big, small]
  114. Let's got through it carefully:
  115. 1. The tree has two levels.
  116. 2. The first level has the value of `label_level1`.
  117. 3. The second level has only values. The values can be whatever you
  118. want.
  119. 4. All the trees are stored in a list. The list is implemented as an
  120. associative array with a key. The key is defined by `label_level0`.
  121. The implementation of this data structure is done through the extension
  122. of the `secureCookieBasic` class. It can be used as an example for
  123. defining your own data structure to be stored in the cookie.
  124. Final Remarks
  125. -------------
  126. Note that the secret generation could be done automatically upon
  127. install. Nevertheless that would void one of the design goals which is
  128. to avoid hitting the database at all. Since setting the value in install
  129. would envolve using a variable, hence the database.
  130. Even if it's a little less convenient, the gains in terms of simplicity
  131. and performance justify it largely.
  132. TODO
  133. ----
  134. - Implement cookie data confidentiality using crypto.
  135. - Implement a token that defines an expiration so that the cookies are
  136. regenerated periodically, to void replay attacks.
  137. Acknowledgements
  138. ----------------
  139. The development and maintenance of this module is sponsored by [Commerce
  140. Guys](http://commerceguys.com).