This repository includes GitHub CodeQL SAST (Static Application Security Testing) analysis to detect security vulnerabilities. This application intentionally contains bad security practices for educational purposes.
CodeQL will identify the following intentionally bad security practices in this educational demo:
- Location:
BadSecurityService.cs - Detection: String concatenation in SQL queries
- Examples:
string sql = $"SELECT * FROM Users WHERE Username = '{username}'"; string sql = $"INSERT INTO Users VALUES ('{username}', '{password}')";
- Location:
BadSecurityService.csandUser.cs - Detection: MD5 hashing, exposed password fields
- Examples:
using var md5 = MD5.Create(); // Cryptographically broken public string Password { get; set; } // Exposed in model
- Location:
UserController.cs - Detection: Lack of input validation and sanitization
- Examples:
public IActionResult Register(string username, string password, string email) // No validation attributes or sanitization
- Location:
BadSecurityService.csandUserController.cs - Detection: Internal errors exposed to users
- Examples:
throw new Exception($"Database error: {ex.Message}"); ViewBag.ErrorMessage = $"Error: {ex.Message}";
- Location:
UserController.cs - Detection: Insecure session handling
- Examples:
HttpContext.Session.SetString("Username", username);
The repository includes custom CodeQL queries in .github/queries/csharp/ that specifically target the educational security demo patterns.
- On Push: CodeQL runs on every push to master
- On Pull Request: CodeQL runs on every PR to master
- Scheduled: CodeQL runs daily at 2 AM UTC
- security-extended: Comprehensive security analysis
- security-and-quality: Additional security and quality checks
- Custom queries: Educational demo specific patterns
When CodeQL analyzes this repository, it will likely find:
- SQL Injection: Multiple instances in
BadSecurityService.cs - Weak Cryptography: MD5 usage for password hashing
- Information Exposure: Password hashes exposed in UI
- Input Validation: Lack of proper validation
- Error Handling: Internal errors exposed
- Session Management: Poor session security
- Code Quality: Various code quality issues
- Best Practices: Deviations from security best practices
These findings are intentional and serve educational purposes:
- Demonstrate Common Vulnerabilities: Show real-world security issues
- CodeQL Detection: Demonstrate how SAST tools identify problems
- Security Awareness: Highlight the importance of secure coding
- Learning Tool: Provide examples for security training
In a production environment, these issues should be fixed:
// BAD (Current)
string sql = $"SELECT * FROM Users WHERE Username = '{username}'";
// GOOD (Fixed)
var command = new SqlCommand("SELECT * FROM Users WHERE Username = @username");
command.Parameters.AddWithValue("@username", username);// BAD (Current)
using var md5 = MD5.Create();
// GOOD (Fixed)
using var hasher = new Rfc2898DeriveBytes(password, salt, 10000);// BAD (Current)
public IActionResult Register(string username, string password)
// GOOD (Fixed)
public IActionResult Register([Required][StringLength(50)] string username,
[Required][MinLength(8)] string password)To view CodeQL analysis results:
- Go to the Security tab in your GitHub repository
- Click on Code scanning alerts
- Review the findings and their details
- Use the educational context to understand each vulnerability
When contributing to this educational demo:
- Maintain Educational Value: Keep the bad practices for demonstration
- Add Comments: Clearly mark intentional vulnerabilities
- Document: Explain why each bad practice exists
- Test CodeQL: Ensure new code triggers appropriate alerts