Groovy's GString concept is pretty effective (see http://groovy.codehaus.org/Strings+and+GString ).
GStrings allows you to do things like:
world = "World" println "Hello ${world}" # Output: Hello World println "1+2 = ${1+2}" # Output: 1+2 = 3 println "${System.exit(-1)}" # Program terminated
I'm trying to find out if Groovy uses GString: s, which can lead to security problems in your code, similar to SQL injection attacks.
In the above example, the code was written by the author of the program, so the execution of the System.exit (-1) command cannot be considered a security flaw, as this was the author's stated intention.
Let's say I'm writing a Grails web application where user input is taken from form fields (reading POST / GET parameters) and database tables (using GORM). Suppose that an attacker controls what is sent to the POST / GET server and what is in the database.
The code in my application is as follows:
def str1 = params.someParameterControlledByTheAttacker def str2 = SomeGORMPersistedObject.get(1).somePropertyFieldControlledByTheAttacker render "Hello! Here is some text: ${str1} and ${str2}"
Is there a way that an attacker can execute code in the above scenario? What for? Why not? My initial hypothesis is that using GString is always safe. Please feel free to prove that I am wrong. Be very specific.
Update # 1: To focus on the discussion, please ignore any HTML-XSS problems in the code, as this issue is about the execution of the code on the server side and not on the client side.
Update # 2: Some people have indicated that "itβs generally a good idea to filter out unwanted lines." Although filtering out "potentially bad characters" can certainly save you some security problems, it would be even better to write code that would be safe even without filtering. You can compare it using PreparedStatements in the Java JDBC API. Proper Use of PreparedStatements Guaranteed to save you from certain classes of injection attacks. Filtering your SQL input is likely to give you the same result, but using PreparedStatements strongly dominates the IMHO filtering method.