Skip to content

Commit 07b209f

Browse files
committed
Semgrep validation
1 parent 4ecb179 commit 07b209f

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
using System;
16+
using System.Security.Cryptography;
17+
using System.Text;
18+
19+
namespace MongoDB.Driver.Encryption
20+
{
21+
/// <summary>
22+
///
23+
/// </summary>
24+
public static class VulnerableCryptography
25+
{
26+
// Weak hashing - Semgrep should flag
27+
/// <summary>
28+
///
29+
/// </summary>
30+
/// <param name="password"></param>
31+
/// <returns></returns>
32+
public static string HashPassword(string password)
33+
{
34+
using (var md5 = MD5.Create()) // VULNERABLE - MD5 is weak
35+
{
36+
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
37+
return Convert.ToBase64String(hash);
38+
}
39+
}
40+
41+
// Hardcoded encryption key - Semgrep should flag
42+
private static readonly byte[] EncryptionKey = {
43+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
44+
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10
45+
};
46+
47+
/// <summary>
48+
///
49+
/// </summary>
50+
/// <param name="data"></param>
51+
/// <returns></returns>
52+
public static byte[] EncryptData(byte[] data)
53+
{
54+
using (var aes = Aes.Create())
55+
{
56+
aes.Key = EncryptionKey; // VULNERABLE - hardcoded key
57+
// ... encryption logic
58+
return data; // simplified
59+
}
60+
}
61+
}
62+
}

src/MongoDB.Driver/MongoClient.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,4 +751,33 @@ private async Task<TResult> UsingImplicitSessionAsync<TResult>(Func<IClientSessi
751751
}
752752
}
753753
}
754+
755+
/// <summary>
756+
///
757+
/// </summary>
758+
public class VulnerableConnectionExample
759+
{
760+
// Hardcoded credentials
761+
private const string DefaultConnectionString = "mongodb://admin:password123@prod-server:27017/sensitive_db";
762+
763+
/// <summary>
764+
///
765+
/// </summary>
766+
/// <returns></returns>
767+
public MongoClient CreateClient()
768+
{
769+
return new MongoClient(DefaultConnectionString);
770+
}
771+
772+
// Weak random session ID -
773+
/// <summary>
774+
///
775+
/// </summary>
776+
/// <returns></returns>
777+
public string GenerateSessionId()
778+
{
779+
var random = new Random(); // Cryptographically weak
780+
return random.Next().ToString();
781+
}
782+
}
754783
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using MongoDB.Bson;
19+
20+
namespace MongoDB.Driver
21+
{
22+
/// <summary>
23+
///
24+
/// </summary>
25+
public class VulnerableNullHandling
26+
{
27+
/// <summary>
28+
///
29+
/// </summary>
30+
/// <param name="document"></param>
31+
/// <returns></returns>
32+
public string ProcessDocument(BsonDocument document)
33+
{
34+
// Remove null checks - Semgrep should flag potential null reference
35+
var name = document["name"].AsString; // Could be null
36+
return name.ToUpper(); // VULNERABLE - potential null reference
37+
}
38+
39+
/// <summary>
40+
///
41+
/// </summary>
42+
/// <param name="documents"></param>
43+
public void ProcessCollection(List<BsonDocument> documents)
44+
{
45+
// Missing null check on collection
46+
foreach (var doc in documents) // VULNERABLE if documents is null
47+
{
48+
Console.WriteLine(doc["_id"]);
49+
}
50+
}
51+
}
52+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.Generic;
17+
using System.Threading.Tasks;
18+
using MongoDB.Bson;
19+
20+
namespace MongoDB.Driver
21+
{
22+
/// <summary>
23+
///
24+
/// </summary>
25+
public class VulnerableQueryBuilder
26+
{
27+
private readonly IMongoCollection<BsonDocument> _collection;
28+
29+
/// <summary>
30+
///
31+
/// </summary>
32+
/// <param name="collection"></param>
33+
public VulnerableQueryBuilder(IMongoCollection<BsonDocument> collection)
34+
{
35+
_collection = collection;
36+
}
37+
38+
// Vulnerable: String concatenation in query - Semgrep should flag
39+
/// <summary>
40+
///
41+
/// </summary>
42+
/// <param name="username"></param>
43+
/// <returns></returns>
44+
public async Task<List<BsonDocument>> FindUserByName(string username)
45+
{
46+
var queryJson = "{ 'username': '" + username + "' }"; // VULNERABLE
47+
var filter = BsonDocument.Parse(queryJson);
48+
return await _collection.Find(filter).ToListAsync().ConfigureAwait(false);
49+
}
50+
51+
// Another injection pattern
52+
/// <summary>
53+
///
54+
/// </summary>
55+
/// <param name="field"></param>
56+
/// <param name="value"></param>
57+
/// <returns></returns>
58+
public async Task<BsonDocument> FindByDynamicField(string field, string value)
59+
{
60+
var query = $"{{ {field}: '{value}' }}"; // VULNERABLE
61+
return await _collection.Find(BsonDocument.Parse(query)).FirstOrDefaultAsync().ConfigureAwait(false);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)