Courtesy: Yuri Samoilov, Flickr |
Grails comes packaged with several application security gems. However, by default some of these features are not enabled. This is especially the case in older versions (2.2.x) of the Framework.
In this blog post I will review six features that can help support your efforts to protect your application. If you have access to +lynda.com, I also highly recommend the Web Security course by +Kevin Skoglund.
Security Tips:
- Define allowed request types for all actions
- Explicitly specify domain properties to be updated
- Turn on HTML encoding by default (older versions)
- Define constraints for each field of a domain
- Use named or positional parameters in queries
- Use the Spring Security Plugin
Tip #1: Define allowed request types for all actions
Tip #2: Specify domain properties to be updated
def save() { def b = Book.get(params.id) b.properties['title', 'numPages'] = params b.save() }
def save() { def b = Book.get(params.id) b.properties = params b.save() }
Tip #3: Turn on HTML Encoding by default
grails.views.default.codec='none'
Then change it to:
grails.views.default.codec='html'
Tip #4: Define constraints for each field of a domain
I know this may seem obvious, but making sure you have the appropriate constraints defined will help protect your database. For example, if you are storing a US zip code in your database; you wouldn’t accept values that do not equal 5 characters. Further, you wouldn’t accept a negative value would you? Of course not, so by having the constraints in place you avoid having to go back and clean up data later. This is just one more layer of protection.
Tip #5: Use named or positional parameters in queries
This helps to protect your app from SQL Injection attempts. That’s where a nefarious person will attempt to gain access to information in your database that they have no rights to. Or they could possibly update/delete tables using this method. There is tons of delightful reading on the subject on the OWASP site.