Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Detecting View State Corruption


Detecting View State Corruption

The view state feature is essential to a large number of ASP.NET applications because it is a built-in way to maintain state across consecutive invocations of the same page within the same session. View state can be disabled, or at least restricted, in applications that make intensive use of controls and for which bandwidth is a serious problem. The view state, in fact, does not add power to the client but does fatten the page being downloaded. In some cases, the extra burden caused by the view state can easily reach 10 KB, more than doubling the normal size of the page.

Rather than being concerned with performance, however, programmers seem to be more sensitive to possible security issues related to the view state. Although not well explained by the MSDN documentation, the view state is absolutely safe to use as long as you don’t change the default settings. Reading the view state on the client is still possible, but that's only a security hole if you use the view state to store sensitive information such as credit-card numbers, passwords, or connection strings.

A rogue client can attempt to modify the view state, but once the page posts back to the server, the ASP.NET runtime would be able to detect the tampering and raise a security exception. Let's consider the security of the view state.

When the ASP.NET runtime creates the view state string, it recursively calls all the page controls to populate the page's state bag dictionary with their own view state properties. The resulting dictionary is serialized to a binary string using the .NET binary formatter. At this point, ASP.NET can calculate a hash value based on three inputs: the contents of the view state, the page type hash code, and the machine key. The resulting value is calculated using the SHA1 hashing algorithm and is normally 160 bits long. This sequence of bytes is appended to the binary stream. Finally, the binary stream is encoded as Base64 and inserted into the hidden field. In this architecture, it's clear that any changes performed on the client are detected on the server and the security exception is raised.

Notice, though, that the view state is not encrypted. So it is potentially readable on the client. If you want to maintain some sort of data confidentiality, use secure sockets instead.

The view state antitampering mechanism is set up by default and is controlled through the EnableViewStateMac attribute of the @Page directive. When the attribute is set to True (the undocumented default), the view state is protected using the hash value. If you change the default settings and disable the EnableViewStateMac feature, then the view state would simply be a Base64 string that can be deserialized to a .NET dictionary using the binary formatter.

Finally, notice that the machine key used to fuel the hash algorithm is stored in the local security authority of the Windows NT/2000 server machine (the LSA). However, in case of web farms, you have to synchronize all machine keys in all servers and must store the key in the configuration file. The key length can vary from a minimum of 40 characters (20 bytes) to a maximum of 128 characters (64 bytes).


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Magazine and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.