-
Notifications
You must be signed in to change notification settings - Fork 1
FUBAR #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
FUBAR #7
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
| } | ||
|
|
||
| } | ||
|
|
| 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"); | ||
| } | ||
|
|
||
| } | ||
|
|
| 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(); | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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 failureCode scanning / CodeQL SQL query built from user-controlled sources High
This query depends on
this ASP.NET Core MVC action method parameter Error loading related location Loading
Copilot AutofixAI 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.
Suggested changeset
1
backend/Controllers/SqlInjectionController.cs
Copilot is powered by AI and may make mistakes. Always verify output.
Positive FeedbackNegative Feedback
Refresh and try again.
|
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| finally | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| conn.Close(); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return Ok(personalHighScore); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
Check failure
Code scanning / CodeQL
Weak encryption High
Copilot Autofix
AI about 1 year ago
To fix the problem, we need to replace the use of
DESCryptoServiceProviderwithAesCryptoServiceProvider, which implements the AES algorithm. AES is a widely accepted standard for encryption and provides a much higher level of security compared to DES.DESCryptoServiceProviderwithAesCryptoServiceProviderand ensure the key length is appropriate for AES (128, 192, or 256 bits).BadEncryptionServiceclass to useAesCryptoServiceProvider. Generate a new key and initialization vector (IV) suitable for AES. Ensure the encryption and decryption processes are updated accordingly.