Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions backend/Controllers/AuthorizedAccessControllerA1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace backend.Controllers;

[Route("api-secure/[controller]")]
[ApiController]
public class AuthorizedAccessControllerA1 : ControllerBase
{
[HttpGet("GetSecretInfo")]
public IActionResult GetSecret()
{
return Ok("Something secret");
}

}

19 changes: 19 additions & 0 deletions backend/Controllers/AuthorizedAccessControllerA2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace backend.Controllers;

[Route("api-secure/[controller]")]
[ApiController]
[Authorize]
public class AuthorizedAccessControllerA2 : ControllerBase
{
[HttpGet("GetSecretInfo")]
public IActionResult GetSecret()
{
return Ok("Something secret");
}

}

53 changes: 53 additions & 0 deletions backend/Controllers/EncryptionController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography;
using System.Text;

namespace backend.Controllers;

[ApiController]
[Route("api/[controller]")]
public class EncryptionController : Controller
{
private readonly ILogger<HighScoreController> _logger;

public EncryptionController(ILogger<HighScoreController> logger)
{
_logger = logger;
}

[HttpGet("Encryption/EncryptString/{input}")]
public IActionResult EncryptString(string input)
{
var badEncryptionService = new BadEncryptionService();
return Ok(badEncryptionService.Encrypt(input));
}
}

public class BadEncryptionService
{
public byte[] Encrypt(string plainText)
{
var symmetricProvider = new DESCryptoServiceProvider();

Check failure

Code scanning / CodeQL

Weak encryption High

DES encryption uses keys of 56 bits only. Switch to AesCryptoServiceProvider or RijndaelManaged instead.

Copilot Autofix

AI about 1 year ago

To fix the problem, we need to replace the use of DESCryptoServiceProvider with AesCryptoServiceProvider, which implements the AES algorithm. AES is a widely accepted standard for encryption and provides a much higher level of security compared to DES.

  • General Fix: Replace DESCryptoServiceProvider with AesCryptoServiceProvider and ensure the key length is appropriate for AES (128, 192, or 256 bits).
  • Detailed Fix: Update the BadEncryptionService class to use AesCryptoServiceProvider. Generate a new key and initialization vector (IV) suitable for AES. Ensure the encryption and decryption processes are updated accordingly.
Suggested changeset 1
backend/Controllers/EncryptionController.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/Controllers/EncryptionController.cs b/backend/Controllers/EncryptionController.cs
--- a/backend/Controllers/EncryptionController.cs
+++ b/backend/Controllers/EncryptionController.cs
@@ -31,6 +31,6 @@
         {
-            var symmetricProvider = new DESCryptoServiceProvider();
-            byte[] key = { 14, 48, 157, 156, 42, 1, 240, 65 };
-            symmetricProvider.Key = key;
-            var encryptor = symmetricProvider.CreateEncryptor();
+            var symmetricProvider = new AesCryptoServiceProvider();
+            symmetricProvider.GenerateKey();
+            symmetricProvider.GenerateIV();
+            var encryptor = symmetricProvider.CreateEncryptor(symmetricProvider.Key, symmetricProvider.IV);
 
EOF
@@ -31,6 +31,6 @@
{
var symmetricProvider = new DESCryptoServiceProvider();
byte[] key = { 14, 48, 157, 156, 42, 1, 240, 65 };
symmetricProvider.Key = key;
var encryptor = symmetricProvider.CreateEncryptor();
var symmetricProvider = new AesCryptoServiceProvider();
symmetricProvider.GenerateKey();
symmetricProvider.GenerateIV();
var encryptor = symmetricProvider.CreateEncryptor(symmetricProvider.Key, symmetricProvider.IV);

Copilot is powered by AI and may make mistakes. Always verify output.
byte[] key = { 14, 48, 157, 156, 42, 1, 240, 65 };
symmetricProvider.Key = key;
var encryptor = symmetricProvider.CreateEncryptor();

var plainBytes = Encoding.UTF8.GetBytes(plainText);

var encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);

return encryptedBytes;
}

public string CreateMD5(string input)
{
using (var md5 = MD5.Create())
{
var hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
return Convert.ToHexString(hashBytes);
}
}
}

41 changes: 41 additions & 0 deletions backend/Controllers/SqlInjectionController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using backend.Entities;
using backend.Storage;
using Dapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace backend.Controllers;

[ApiController]
[Route("api/[controller]")]
public class SqlInjectionController : ControllerBase
{
private readonly ILogger<HighScoreController> _logger;
private readonly HighScoreDbContext _context;

public SqlInjectionController(ILogger<HighScoreController> logger, HighScoreDbContext context)
{
_logger = logger;
_context = context;
}

[HttpGet("SqlInjection/SearchPersonalHighScoreInsecure/{name}")]
public async Task<IActionResult> SearchPersonalHighScoreInsecure(string name)
{
var conn = _context.Database.GetDbConnection();
var query = "SELECT Id, PlayerName, Score FROM HighScoreEntry WHERE PlayerName Like '%" + name + "%'";
IEnumerable<HighScoreEntry> personalHighScore;

try
{
await conn.OpenAsync();
personalHighScore = await conn.QueryAsync<HighScoreEntry>(query);

Check failure

Code scanning / CodeQL

SQL query built from user-controlled sources High

This query depends on
this ASP.NET Core MVC action method parameter
.

Copilot Autofix

AI about 1 year ago

To fix the SQL injection vulnerability, we should use parameterized queries instead of string concatenation. This approach ensures that user input is treated as a parameter and not as part of the SQL command, thus preventing SQL injection attacks.

  • Replace the string concatenation with a parameterized query.
  • Use the Dapper library's parameterized query feature to safely include the user input in the SQL query.
  • Modify the query to use a parameter placeholder (@name) and pass the user input as a parameter.
Suggested changeset 1
backend/Controllers/SqlInjectionController.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/Controllers/SqlInjectionController.cs b/backend/Controllers/SqlInjectionController.cs
--- a/backend/Controllers/SqlInjectionController.cs
+++ b/backend/Controllers/SqlInjectionController.cs
@@ -25,3 +25,3 @@
         var conn = _context.Database.GetDbConnection();
-        var query = "SELECT Id, PlayerName, Score FROM HighScoreEntry WHERE PlayerName Like '%" + name + "%'";
+        var query = "SELECT Id, PlayerName, Score FROM HighScoreEntry WHERE PlayerName Like @name";
         IEnumerable<HighScoreEntry> personalHighScore;
@@ -31,3 +31,3 @@
             await conn.OpenAsync();
-            personalHighScore = await conn.QueryAsync<HighScoreEntry>(query);
+            personalHighScore = await conn.QueryAsync<HighScoreEntry>(query, new { name = "%" + name + "%" });
         }
EOF
@@ -25,3 +25,3 @@
var conn = _context.Database.GetDbConnection();
var query = "SELECT Id, PlayerName, Score FROM HighScoreEntry WHERE PlayerName Like '%" + name + "%'";
var query = "SELECT Id, PlayerName, Score FROM HighScoreEntry WHERE PlayerName Like @name";
IEnumerable<HighScoreEntry> personalHighScore;
@@ -31,3 +31,3 @@
await conn.OpenAsync();
personalHighScore = await conn.QueryAsync<HighScoreEntry>(query);
personalHighScore = await conn.QueryAsync<HighScoreEntry>(query, new { name = "%" + name + "%" });
}
Copilot is powered by AI and may make mistakes. Always verify output.
}

finally
{
conn.Close();
}
return Ok(personalHighScore);
}
}
4 changes: 4 additions & 0 deletions backend/backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
<PackageReference Include="Dapper" Version="2.1.24" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
</ItemGroup>

<!-- Pre-build event to run npm build and copy-to-backend in frontend folder -->
Expand Down
3 changes: 3 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"start": "http-server ./dist -p 5500",
"copy-to-backend": "cpy \"dist/**/*\" ../backend/wwwroot/"
},
"dependencies": {
"lodash": "4.17.20"
},
"devDependencies": {
"cpy-cli": "^5.0.0",
"http-server": "^14.1.1",
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pipeline-invaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function updatePowerUps() {
function activatePowerUp(type: string) {
powerUpActive = true;
powerUpEndTime = Date.now() + powerUpDuration;
(powerUpEffects as Record<string, () => void>)[type]();
eval(`(${(powerUpEffects as Record<string, () => void>)[type]})()`);
}

// Reset power-up effects
Expand All @@ -261,7 +261,7 @@ function autoShoot() {
// Display power-up messages
function displayMessage(message: string) {
const msgDiv = document.createElement('div');
msgDiv.textContent = message;
msgDiv.innerHTML = message; // Potential XSS if `message` contains malicious code
msgDiv.style.position = 'absolute';
msgDiv.style.top = '10px';
msgDiv.style.left = '50%';
Expand Down