1

Currently I am creating a RESTful API for a mobile application. The RESTful API has a number of end points that allow users to exchange personal information between each other. I was testing how secure these endpoints were and quickly realized that if a third party managed to gain access to the API they could easily look up other user's information by guessing their user id or using an automated script to collect a wide range of personal information. This was due to the fact that I was using a primary key that was a simple auto-incremented integer which made it predictable and easy to determine other user's ids. I immediately began looking for something that didn't follow a distinct pattern. I came across UUIDs and decided to implement them with my existing rails app.

Was this a wise decision? I definitely see the upside to using UUIDs but upon further research I found that there were a number of negatives to this approach. Many sources claim that using UUIDs will cause performance issues with large tables. Are UUIDs right for my situation?

My second question is about implementing this in an existing Ruby on Rails application. I made the switch to UUIDs by following this article: http://rny.io/rails/postgresql/2013/07/27/use-uuids-in-rails-4-with-postgresql.html. I ran into an issue with enabling the uuid-ossp extension. I created a migration and put enable_extension 'uuid-ossp' inside the change function. I then changed the existing migrations to support UUIDs as their primary key and ran rake db:drop db:create db:migrate to recreate the database with the edited migrations. This failed with the error PG::UndefinedFunction: ERROR: function uuid_generate_v4() does not exist. I quickly realized that this was because I had created the migration that enabled the uuid-ossp extension after the migrations that I had edited to use UUIDs. When I changed the time stamp in the name of the migration to a date that preceded all migrations the db:migrate command completed with no errors. This felt very hack and defeated the purpose of having migrations. What is the correct way of adding this extension via a migration?

Edit in response to comments:

So a number of comments were made that suggested that I should just be properly authenticating users and checking their permissions before allowing them to view certain data. I have user authentication built into my application but will better explain my situation and why I needed something more than auto-incremented primary keys.

I have a number of users on this application and each user has the ability to create private and public contacts. Public contacts are viewable by everyone using the mobile application. Private contacts can only be viewed by the user who created them. However, a user can share their private contacts with other users by showing other users with the mobile application a QR code that has the contacts ID encoded into it. When the user decodes the contact ID a request is sent to the backend to notify the backend that the user is now an owner of that private contact. This allows the second user to now receive updates from that private contact. This is a large feature of my application. The aim here is to force people to have to exchange these contacts in person and to disallow others from seeing these contacts unless this process has happened.

Implementing this concept proved to be fairly tricky as all users could potentially share all private contacts with any other user on the system. I found this extremely hard to implement using permissions as which contacts a user can view is constantly changing.

Originally I implemented this with auto-incremented integers as my primary key for the contact IDs. It worked but forced me to create a very insecure API endpoint that essentially would take a user ID and a private contact ID as parameters and would add that user as an owner of that contact. Because auto-incremented IDs are so predictable a user with access to the API could essentially loop through a sequence of numbers calling the endpoint each time, pass the sequence number in as the contact ID and add themselves as owners to contacts that hadn't been shared with them. This would by pass the whole process of having to share the contact in person and in large defeats the purpose of having my mobile application.

I decided I needed something less predictable, completely random and unique to each private contact. I found UUIDs while doing research to solve this problem and changed the contact ID in my model to be of type UUID. Are UUIDs the best way to solve this? Should I use something else? Have I gone about solving this problem the wrong way?

ScottOBot
  • 839
  • 3
  • 16
  • 37
  • 1
    After implementing UUIDs, you still have the problem that a client that learns a UUID once could re-use to access data, it even after permissions or relationships change. UUIDs hide data from guesswork, but do not protect it otherwise. So I would hope your API also checks whether the current user has access permitted to the resource requested. – Neil Slater Feb 22 '15 at 12:56
  • @NeilSlater: You're right, and UUIDs hide data from guesswork (a little) only as a side effect. Their purpose isn't to hide data; their purpose is simply to be universally unique. OP needs to authenticate users and check their permissions. – Mike Sherrill 'Cat Recall' Feb 22 '15 at 17:52
  • @MikeSherrill'CatRecall' I authenticate users and check permissions thats not the issue. I will edit the original post to explain a bit about my backend architecture and the scenario that I ran into that led me to look for alternatives to auto-incremented primary keys. – ScottOBot Feb 22 '15 at 19:50
  • "...a user with access to the API could essentially loop through a sequence of numbers calling the endpoint each time, pass the sequence number in as the contact ID and add themselves as owners to contacts that hadn't been shared with them." Does that really sound like a good design? I have Google Docs. I could send you the URL to one of them, and when you tried to navigate to it, Google would ask you to sign in. But after you signed in, you *still* couldn't navigate to it, because I didn't give you permission to view it. – Mike Sherrill 'Cat Recall' Feb 23 '15 at 03:15
  • Awesome. The design is off sure. Now how do I go about implementing the feature outlined above properly in Ruby on Rails? Please tell me where to start? @MikeSherrill'CatRecall' – ScottOBot Feb 23 '15 at 06:55

1 Answers1

1

Are UUIDs the best way to solve this?

You could use them as a solution. If you do, you should build a new contacts table and model instead of trying to migrate the old model. As well as being tricky to implement, any migration would immediately make existing contact/invite emails invalid (since they contain the old id). Briefly support both models, and retire the old auto-incrementing id model once you are happy that traffic using it is no longer important to your application.

There is still a flaw - your contact share links will now be long-lasting, and if anyone gets access to a contact's id for any reason, and know enough to construct the URL for gaining that user as a contact, then they gain the ability to share it to themselves and anyone else completely outside of the control of your application. This because you are relying on knowledge of the id as the only thing preventing access to the contact details.

Should I use something else?

In my opinion, yes. Use a separate nonce or one-off code model (with UUIDs, or an indexed column containing a long random string - you could use SecureRandom for this) that can grant rights to complete the sharing. When someone wants to share a contact, create the nonce object with details about what is being shared - e.g. the contact_id - and use it to generate email link pointing to a route that will find the nonce and allow access to the resource.

The model doesn't need to be called "Nonce" or contain that as a column, this is just a common name for the pattern. Instead you might call the new model "ContactShare" and the secret property "link_code".

This will allow you to resolve access to contacts using your app's permissions model as normal, and block the possible misuse of sharing links. When the controller with the nonce id or code is invoked, create permissions at that point in order to grant access to the contacts. Then expire or delete the nonce, so it cannot be re-used. I prefer expiry, so you can track usage - this can be as simple as a used boolean column that you update once the sharing request has succeeded.

Note I am not referring to Rack::Auth::Digest nonce routine, which is specific to server authentication. I did not find a RoR pre-built nonce model, but it is possible it goes under a different name.

Neil Slater
  • 26,512
  • 6
  • 76
  • 94
  • Hey amazing answer! I am just getting around to implementing the solution to this problem now but have no idea where to start. Could you provide me with a starting point to implementing a nonce model in Ruby on Rails? i can't seem to find any documentation or tutorials on anything other then implementing such a model for verification emails. @Neil Slater – ScottOBot Mar 07 '15 at 02:52
  • @ScottOBott: Unfortunately most of the time, I work in Sinatra, not Rails. I could easily show a SequelORM version for instance. I suggest you start a new question. Show a model you want to use for your nonce (e.g. ContactShare), and explain where you are stuck. It really should be *very* similar to verification email system - the only difference is the *action* you perform when the link is activated. – Neil Slater Mar 07 '15 at 08:02
  • 1
    I used this wiki article: http://en.wikipedia.org/wiki/Cryptographic_nonce, and this php example: http://stackoverflow.com/questions/4145531/how-to-create-and-use-nonces, and have built something that I think will probably work. Haven't fully tested it yet but will create a new question if any problems arise. Thanks for the all the help! @Neil Slater – ScottOBot Mar 08 '15 at 03:12