-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCleartextStorage.cs
More file actions
78 lines (67 loc) · 2.13 KB
/
CleartextStorage.cs
File metadata and controls
78 lines (67 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs /r:System.Collections.Specialized.dll {testdir}/../../../../resources/stubs/System.Windows.cs
using System.Text;
using System.Web;
using System.Web.Security;
using System.Windows.Forms;
public class ClearTextStorageHandler : IHttpHandler
{
private string accountKey;
public void ProcessRequest(HttpContext ctx)
{
// BAD: Setting a cookie value or values with sensitive data.
ctx.Response.Cookies["MyCookie"].Value = accountKey;
ctx.Response.Cookies["MyOtherCookie"]["Sensitive"] = GetPassword();
ctx.Response.Cookies["MyOtherCookie"].Values["Sensitive"] = GetPassword();
ctx.Response.Cookies["MyCookie"].Value = GetAccountID();
// GOOD: Encoding the value before setting it.
ctx.Response.Cookies["MyCookie"].Value = Encode(accountKey, "Account key");
// OK: Account name is not considered to be secret data
ctx.Response.Cookies["MyCookie"].Value = GetAccountName();
ILogger logger = new ILogger();
// BAD: Logging sensitive data
logger.Warn(GetPassword());
// GOOD: Logging encrypted sensitive data
logger.Warn(Encode(GetPassword(), "Password"));
}
public string Encode(string value, string type)
{
return Encoding.UTF8.GetString(MachineKey.Protect(Encoding.UTF8.GetBytes(value), type));
}
public string GetPassword()
{
return "password";
}
public string GetAccountName()
{
return "accountName";
}
public string GetAccountID()
{
return "accountID";
}
public bool IsReusable
{
get
{
return true;
}
}
}
class ILogger
{
public void Warn(string message) { }
}
class MyForm : Form
{
TextBox password, box1, box2, box3;
ILogger logger;
public void OnButtonClicked()
{
box1.PasswordChar = '*';
box2.UseSystemPasswordChar = true;
logger.Warn(password.Text); // BAD
logger.Warn(box1.Text); // BAD
logger.Warn(box2.Text); // BAD
logger.Warn(box3.Text); // GOOD
}
}