766
Example Language: Java (Bad)
response.addCookie( new Cookie("userAccountID", acctID);
Because the account ID is in plaintext, the user's account information is exposed if their computer
is compromised by an attacker.
Example 2:
This code writes a user's login information to a cookie so the user does not have to login again
later.
Example Language: PHP (Bad)
function persistLogin($username, $password){
$data = array("username" => $username, "password"=> $password);
setcookie ("userdata", $data);
}
The code stores the user's username and password in plaintext in a cookie on the user's machine.
This exposes the user's login information if their computer is compromised by an attacker. Even
if the user's machine is not compromised, this weakness combined with cross-site scripting
(CWE-79) could allow an attacker to remotely copy the cookie.
Also note this example code also exhibits Plaintext Storage in a Cookie (CWE-315).
Example 3:
The following code attempts to establish a connection, read in a password, then store it to a buffer.
Example Language: C (Bad)
server.sin_family = AF_INET; hp = gethostbyname(argv[1]);
if (hp==NULL) error("Unknown host");
memcpy( (char *)&server.sin_addr,(char *)hp->h_addr,hp->h_length);
if (argc < 3) port = 80;
else port = (unsigned short)atoi(argv[3]);
server.sin_port = htons(port);
if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0) error("Connecting");
...
while ((n=read(sock,buffer,BUFSIZE-1))!=-1) {
write(dfd,password_buffer,n);
...
While successful, the program does not encrypt the data before writing it to a buffer, possibly
exposing it to unauthorized actors.
Example 4:
The following examples show a portion of properties and configuration files for Java and ASP.NET
applications. The files include username and password information but they are stored in cleartext.
This Java example shows a properties file with a cleartext username / password pair.
Example Language: Java (Bad)
# Java Web App ResourceBundle properties file
...
webapp.ldap.username=secretUsername
webapp.ldap.password=secretPassword
...
The following example shows a portion of a configuration file for an ASP.Net application. This
configuration file includes username and password information for a connection to a database but
the pair is stored in cleartext.
CWE Version 4.14
CWE-312: Cleartext Storage of Sensitive Information
CWE-312: Cleartext Storage of Sensitive Information