19.09.2017 Views

the-web-application-hackers-handbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

154 Chapter 5 n Bypassing Client-Side Controls<br />

Handling Client-Side Data Securely<br />

As you have seen, <strong>the</strong> core security problem with <strong>web</strong> <strong>application</strong>s arises because<br />

client-side components and user input are outside <strong>the</strong> server’s direct control.<br />

The client, and all <strong>the</strong> data received from it, is inherently untrustworthy.<br />

Transmitting Data Via <strong>the</strong> Client<br />

Many <strong>application</strong>s leave <strong>the</strong>mselves exposed because <strong>the</strong>y transmit critical data<br />

such as product prices and discount rates via <strong>the</strong> client in an unsafe manner.<br />

If possible, <strong>application</strong>s should avoid transmitting this kind of data via <strong>the</strong><br />

client. In virtually any conceivable scenario, it is possible to hold such data on<br />

<strong>the</strong> server and reference it directly from server-side logic when needed. For<br />

example, an <strong>application</strong> that receives users’ orders for various products should<br />

allow users to submit a product code and quantity and look up <strong>the</strong> price of each<br />

requested product in a server-side database. There is no need for users to submit<br />

<strong>the</strong> prices of items back to <strong>the</strong> server. Even where an <strong>application</strong> offers different<br />

prices or discounts to different users, <strong>the</strong>re is no need to depart from this model.<br />

Prices can be held within <strong>the</strong> database on a per-user basis, and discount rates<br />

can be stored in user profiles or even session objects. The <strong>application</strong> already<br />

possesses, server-side, all <strong>the</strong> information it needs to calculate <strong>the</strong> price of a<br />

specific product for a specific user. It must. O<strong>the</strong>rwise, it would be unable, on<br />

<strong>the</strong> insecure model, to store this price in a hidden form field.<br />

If developers decide <strong>the</strong>y have no alternative but to transmit critical data via<br />

<strong>the</strong> client, <strong>the</strong> data should be signed and/or encrypted to prevent user tampering.<br />

If this course of action is taken, <strong>the</strong>re are two important pitfalls to avoid:<br />

n Some ways of using signed or encrypted data may be vulnerable to<br />

replay attacks. For example, if <strong>the</strong> product price is encrypted before<br />

being stored in a hidden field, it may be possible to copy <strong>the</strong> encrypted<br />

price of a cheaper product and submit it in place of <strong>the</strong> original price.<br />

To prevent this attack, <strong>the</strong> <strong>application</strong> needs to include sufficient context<br />

within <strong>the</strong> encrypted data to prevent it from being replayed in a different<br />

context. For example, <strong>the</strong> <strong>application</strong> could concatenate <strong>the</strong> product<br />

code and price, encrypt <strong>the</strong> result as a single item, and <strong>the</strong>n validate<br />

that <strong>the</strong> encrypted string submitted with an order actually matches <strong>the</strong><br />

product being ordered.<br />

n If users know and/or control <strong>the</strong> plaintext value of encrypted strings that<br />

are sent to <strong>the</strong>m, <strong>the</strong>y may be able to mount various cryptographic attacks<br />

to discover <strong>the</strong> encryption key <strong>the</strong> server is using. Having done this, <strong>the</strong>y<br />

can encrypt arbitrary values and fully circumvent <strong>the</strong> protection offered<br />

by <strong>the</strong> solution.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!