Obfuscating ids in Rails app

Posted by fphilipe on Stack Overflow See other posts from Stack Overflow or by fphilipe
Published on 2010-05-11T22:21:09Z Indexed on 2010/05/11 22:24 UTC
Read the original article Hit count: 197

I'm trying to obfuscate all the ids that leave the server, i.e., ids appearing in URLs and in the HTML output.

I've written a simple Base62 lib that has the methods encode and decode. Defining—or better—overwriting the id method of an ActiveRecord to return the encoded version of the id and adjusting the controller to load the resource with the decoded params[:id] gives me the desired result. The ids now are base62 encoded in the urls and the response displays the correct resource.

Now I started to notice that subresources defined through has_many relationships aren't loading. e.g. I have a record called User that has_many Posts. Now User.find(1).posts is empty although there are posts with user_id = 1. My explanation is that ActiveRecord must be comparing the user_id of Post with the method id of User—which I've overwritten—instead of comparing with self[:id]. So basically this renders my approach useless.

What I would like to have is something like defining obfuscates_id in the model and that the rest would be taken care of, i.e., doing all the encoding/decoding at the appropriate locations and preventing ids to be returned by the server.

Is there any gem available or does somebody have a hint how to accomplish this? I bet I'm not the first trying this.

© Stack Overflow or respective owner

Related posts about base62

Related posts about obfuscation