# How far do you bullet proof your code?

**URL:** <https://www.ruby-forum.com/t/how-far-do-you-bullet-proof-your-code/180001>\
**Category:** Rails\
**Created:** [December 9, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/how-far-do-you-bullet-proof-your-code/180001 "2009-12-09T14:59:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 9, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/how-far-do-you-bullet-proof-your-code/180001/1 "2009-12-09T14:59:10Z")

</div>

Im looking at my controllers and ive got loads of stuff like

user = User.find(params[:user\_id])

dotted around.

If for some unknown obscure reason an invalid user\_id is passed in the  
params then, judign by what happens in console, a exception is raised.  
In production mode what would happen? Would the whole site come  
crumbling down and require a restart from me?

I know I can rescue this exception perhaps just redirect to some splash  
page. Im just wondering what others do regarding this as nearly every  
tutorial ive seen just writes it as is. Or perhaps im just worrying to  
much.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 9, 2009, 3:10pm UTC](https://www.ruby-forum.com/t/how-far-do-you-bullet-proof-your-code/180001/2 "2009-12-09T15:10:04Z")

</div>

Adam A. wrote:

> Im looking at my controllers and ive got loads of stuff like
> 
> user = User.find(params[:user\_id])
> 
> dotted around.
> 
> If for some unknown obscure reason an invalid user\_id is passed in the  
> params then, judign by what happens in console, a exception is raised.  
> In production mode what would happen? Would the whole site come  
> crumbling down and require a restart from me?

I would rather make sure bad data never makes it that far.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 9, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/how-far-do-you-bullet-proof-your-code/180001/3 "2009-12-09T15:25:47Z")

</div>

Adam A. wrote:

> Im looking at my controllers and ive got loads of stuff like
> 
> user = User.find(params[:user\_id])
> 
> dotted around.
> 
> If for some unknown obscure reason an invalid user\_id is passed in the  
> params then, judign by what happens in console, a exception is raised.  
> In production mode what would happen? Would the whole site come  
> crumbling down and require a restart from me?
> 
> I know I can rescue this exception perhaps just redirect to some splash  
> page. Im just wondering what others do regarding this as nearly every  
> tutorial ive seen just writes it as is. Or perhaps im just worrying to  
> much.

Under what circumstance would user\_id be invalid? I think the question  
you should be asking is: Should there be validation in place for this  
before this stage?

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 9, 2009, 8:55pm UTC](https://www.ruby-forum.com/t/how-far-do-you-bullet-proof-your-code/180001/4 "2009-12-09T20:55:54Z")

</div>

Pale H. wrote:

> Under what circumstance would user\_id be invalid?

Think about what would happen in the following example:  
curl -X POST -F “user\_id=345” [http://example.com/users](http://example.com/users)

Obviously if no user\_id of 345 exists in the users table then AR will  
throw an ActiveRecord::RecordNotFound exception.

Is this a problem you need to worry about? Maybe yes, maybe no. If an  
unexpected exception occurs while running in production mode there is a  
simple “Something went wrong.” page returned to the user. Maybe that’s  
all you need. But, you might need something more.

I believe that it is common practice to return the standard 404 “Page  
not found.” page when ActiveRecord throws this exception. It’s possible  
that Rails will actually take care of doing that for you, but I’m not  
really sure about that.

> I think the question  
> you should be asking is: Should there be validation in place for this  
> before this stage?

I don’t actually see how validation would help here. Asking for a record  
with an id that does not exist can’t easily be validated.

Take for example that a client of a web service fetches user with id=5  
and caches the details client-side. Then suppose that before the client  
has a chance to modify and update the user with id=5 the user gets  
deleted. In this case the client is going to assume that user with id=5  
exists, and send the following request:

PUT: [http://myhost.com/users/5](http://myhost.com/users/5) (with the updated attributes)

The clients then needs to receive a response indicating that the user  
was not found. This response might not be HTML, but the situation would  
be the same as if it was. A logical response for an HTML client would be  
the 404 page “Resource not found.”

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 10, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/how-far-do-you-bullet-proof-your-code/180001/5 "2009-12-10T15:23:43Z")

</div>

Thanks everyone for your replies. Im going to make it so that even in a  
worst case scenario my app redirects appropriately. Ill stick the find  
call between a begin and rescue.

Thanks again.
