Home Security User Data Encryption in Play2

User Data Encryption in Play2

Author

Date

Category

Recent news shows the importance of data encryption.ย  For instance, the attack on MongoHQ showed how OAuth might be exploited if not properly handled. Buffer learnt from it the hard way. But so should all of us and thatโ€™s why we at Codacy would like to share how weโ€™ve protected our Infrastructure Secrets.

We too at Codacy use OAuth tokens, in our case to access Github and Googleโ€™s API. The leak of these tokens would mean granting an attacker our usersโ€™ code. Of course, this would be unacceptable. So, it would be better to follow Bufferโ€™s example and cipher it. This way if an attacker breaches our database and gets hold of the ciphered tokens theyโ€™d be useless.

Although necessary, security may be a burden. In spite of that, we wanted a way to secure data in the database that would be effortless to the rest of the team while developing. So that you know, as part of our stack weโ€™re using Play Framework for Scala and play-slick as the interface for PostgreSQL.

This was the solution we came up with. A simple case class named SecureString that stores a String and ciphers it. A SecureTable trait containing an implicit TypeMapper to take care of the conversion between String and SecureString. Then just combine the two by mixing the trait with the model and declare the columns we want to be ciphered as SecureString.

Take a look at this simple example where we have a LoveLetter and want to cipher the contents of the letter:

https://gist.github.com/mrfyda/7554222

For this change to be completely transparent to the rest of your code you can set implicit conversions between SecureString and String:

implicit def toSecureString(str: String): SecureString = SecureString(str)
 implicit def fromSecureString(str: SecureString): String = str.toString

This makes an automatic transformation of String into SecureString which means that we treat those as the same.
This in turn means that our code has minimal change cost to implement encrypted strings.

And thatโ€™s it. We hope this is useful in your projects.


Edit: We just published an ebook: โ€œThe Ultimate Guide to Code Reviewโ€ based on a survey of 680+ developers. Enjoy!


*This is a blog post of our Code Reading Wednesdays from Codacy (http://www.codacy.com): we make code reviews easier and automatic.

About Codacy

Codacy is used by thousands of developers to analyze billions of lines of code every day!

Getting started is easy โ€“ and free! Just use yourย ย GitHub, Bitbucket or Google account toย sign up.

GET STARTED

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Recent posts

How does code quality fit into your CI/CD pipeline?

Continuous Integration and Continuous Deployment (CI/CD) are key for organizations wanting to deliver software at scale. CI/CD allows developers to automate...

How Stim uses Codacy to achieve high-quality code

We spoke with Tobias Sjรถsten, Head of Software Engineering at Stim, about how Codacy helps them guarantee code quality and standardization...

6 things developers should do to ship more secure code

Writing better, more secure source code is fundamental to prevent potential exploits and attacks that could undermine your software applications. However,...

Best practices for security code reviews

In today's interconnected world, where data breaches and cyber threats are increasingly common, one of your top priorities should be to...

April Product Update ๐Ÿš€

Hi there ๐Ÿ‘‹ It's been a whirlwind month, and we have big news to share: