...
Code Block | ||||
---|---|---|---|---|
| ||||
dummy\'); var bw = new JavaImporter(java.io.BufferedWriter); var fw = new JavaImporter(java.io.FileWriter); with(fw) with(bw) { bwr = new BufferedWriter(new FileWriter(\"c://somepath//somefile.txt\")); bwr.write(\"some text\"); bwr.close(); } // ; |
The script prints dummy
and writes some text
to somefile.txt
behind the scenes.
Compliant Solution
The best defense against code injection vulnerabilities is to avoid including executable user input in code. When dynamic code requires user input, that input must be sanitized. For example, a top-level method could ensure that the string firstName
contains only valid, whitelisted characters. Refer to IDS00-J. Sanitize untrusted data passed across a trust boundary for more details. If special characters are allowed in the name, they must be escaped and normalized before comparing with their equivalent forms for the purpose of input validation.
An alternative policy is to create a secure sandbox using a security manager. (See SEC60-JG. Create a secure sandbox using a Security Manager.) The application should not allow the script to execute arbitrary commands including, for example, querying the local file system. The two-argument form of doPrivileged()
can be used to lower privileges when the application must operate with higher privileges but the scripting engine must not. The RestrictedAccessControlContext
strips the permissions granted in the default policy file by reducing the permissions granted to the newly created protection domain. The effective permissions are the intersection of the permissions of the newly created protection domain and the systemwide security policy. Refer to SEC50-JG. Avoid granting excess privileges for more details on the two-argument form.
...