Skip to content

Commit 7340a12

Browse files
Fixing query & test
1 parent 8544356 commit 7340a12

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

csharp/ql/src/Security Features/InsecureRandomness.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ module Random {
6060
any(MethodCall mc |
6161
mc.getQualifier().getType().(RefType).hasQualifiedName("System", "Random")
6262
or
63-
// by using `% 87` on a `byte`, this function has a bias
63+
// by using `% 87` on a `byte`, `System.Web.Security.Membership.GeneratePassword` has a bias
6464
mc.getQualifier()
6565
.getType()
6666
.(RefType)
67-
.hasQualifiedName("System.Web.Security", "GeneratePassword")
67+
.hasQualifiedName("System.Web.Security", "Membership") and
68+
mc.getTarget().hasName("GeneratePassword")
6869
)
6970
}
7071
}

csharp/ql/test/query-tests/Security Features/CWE-338/InsecureRandomness.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,24 @@ public static string InsecureRandomStringFromIndexer(int length)
7373
}
7474
return result;
7575
}
76+
77+
public static string BiasPasswordGeneration()
78+
{
79+
// BAD: Membership.GeneratePassword is generates a password with a bias
80+
string password = System.Web.Security.Membership.GeneratePassword(12, 3);
81+
return password;
82+
}
83+
84+
}
85+
86+
namespace System.Web.Security
87+
{
88+
public static class Membership
89+
{
90+
public static string GeneratePassword(int length, int numberOfNonAlphanumericCharacters)
91+
{
92+
return "stub";
93+
}
94+
95+
}
7696
}

csharp/ql/test/query-tests/Security Features/CWE-338/InsecureRandomness.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ nodes
2929
| InsecureRandomness.cs:62:16:62:32 | call to method ToString : String | semmle.label | call to method ToString : String |
3030
| InsecureRandomness.cs:72:31:72:39 | call to method Next : Int32 | semmle.label | call to method Next : Int32 |
3131
| InsecureRandomness.cs:74:16:74:21 | access to local variable result : String | semmle.label | access to local variable result : String |
32+
| InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | semmle.label | call to method GeneratePassword |
3233
#select
3334
| InsecureRandomness.cs:12:27:12:50 | call to method InsecureRandomString | InsecureRandomness.cs:28:29:28:43 | call to method Next : Int32 | InsecureRandomness.cs:12:27:12:50 | call to method InsecureRandomString | Cryptographically insecure random number is generated at $@ and used here in a security context. | InsecureRandomness.cs:28:29:28:43 | call to method Next | call to method Next |
3435
| InsecureRandomness.cs:13:20:13:56 | call to method InsecureRandomStringFromSelection | InsecureRandomness.cs:60:31:60:39 | call to method Next : Int32 | InsecureRandomness.cs:13:20:13:56 | call to method InsecureRandomStringFromSelection | Cryptographically insecure random number is generated at $@ and used here in a security context. | InsecureRandomness.cs:60:31:60:39 | call to method Next | call to method Next |
3536
| InsecureRandomness.cs:14:20:14:54 | call to method InsecureRandomStringFromIndexer | InsecureRandomness.cs:72:31:72:39 | call to method Next : Int32 | InsecureRandomness.cs:14:20:14:54 | call to method InsecureRandomStringFromIndexer | Cryptographically insecure random number is generated at $@ and used here in a security context. | InsecureRandomness.cs:72:31:72:39 | call to method Next | call to method Next |
37+
| InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | Cryptographically insecure random number is generated at $@ and used here in a security context. | InsecureRandomness.cs:80:28:80:81 | call to method GeneratePassword | call to method GeneratePassword |

0 commit comments

Comments
 (0)