27
The programmer intended for $birthday to be in a date format and $homepage to be a valid URL.
However, since the values are derived from an HTTP request, if an attacker can trick a victim into
clicking a crafted URL with <script> tags providing the values for birthday and / or homepage, then
the script will run on the client's browser when the web server echoes the content. Notice that even
if the programmer were to defend the $birthday variable by restricting input to integers and dashes,
it would still be possible for an attacker to provide a string of the form:
Example Language: (Attack)
2009-01-09--
If this data were used in a SQL statement, it would treat the remainder of the statement as a
comment. The comment could disable other security-related logic in the statement. In this case,
encoding combined with input validation would be a more useful protection mechanism.
Furthermore, an XSS (CWE-79) attack or SQL injection (CWE-89) are just a few of the potential
consequences when input validation is not used. Depending on the context of the code, CRLF
Injection (CWE-93), Argument Injection (CWE-88), or Command Injection (CWE-77) may also be
possible.
Example 4:
The following example takes a user-supplied value to allocate an array of objects and then
operates on the array.
Example Language: Java (Bad)
private void buildList ( int untrustedListSize ){
if ( 0 > untrustedListSize ){
die("Negative value supplied for list size, die evil hacker!");
}
Widget[] list = new Widget [ untrustedListSize ];
list[0] = new Widget();
}
This example attempts to build a list from a user-specified value, and even checks to ensure a nonnegative
value is supplied. If, however, a 0 value is provided, the code will build an array of size 0
and then try to store a new Widget in the first location, causing an exception to be thrown.
Example 5:
This Android application has registered to handle a URL when sent an intent:
Example Language: Java (Bad)
...
IntentFilter filter = new IntentFilter("com.example.URLHandler.openURL");
MyReceiver receiver = new MyReceiver();
registerReceiver(receiver, filter);
...
public class UrlHandlerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("com.example.URLHandler.openURL".equals(intent.getAction())) {
String URL = intent.getStringExtra("URLToOpen");
int length = URL.length();
...
}
}
}
The application assumes the URL will always be included in the intent. When the URL is not
present, the call to getStringExtra() will return null, thus causing a null pointer exception when
length() is called.
CWE Version 4.14
CWE-20: Improper Input Validation
CWE-20: Improper Input Validation