-
Notifications
You must be signed in to change notification settings - Fork 0
Security Guide
This guide covers security considerations, best practices, and recommendations for deploying the REDZAuth API in production environments.
IMPORTANT: This codebase has been prepared for open-source release, but please be aware of the following security considerations:
- No Authentication/Authorization: The admin endpoints currently have no authentication. In production, implement proper authentication (JWT, API keys, etc.).
- No Rate Limiting: No rate limiting is implemented. Consider adding rate limiting for production use.
- No CORS: CORS policies are not configured. Configure appropriate CORS policies for your domain.
- HTTPS: Ensure HTTPS is properly configured in production.
- Input Validation: While basic validation exists, consider implementing more robust input validation.
- Logging: Current logging uses Console.WriteLine. Implement proper structured logging for production.
Always review and test security measures before deploying to production!
-
Password Security
- SHA256 hashing for password storage
- No plain-text passwords are stored in the database
- Secure password comparison
-
HWID Security
- Hardware ID binding for device-specific access
- HWID mismatch detection
- Admin-controlled HWID reset functionality
-
Blacklist System
- Comprehensive ban/unban functionality
- IP, username, and HWID-based banning
- Audit trail for all ban operations
-
Webhook Security
- Configurable webhook URLs
- Graceful error handling for webhook failures
- No sensitive data in webhook payloads
-
Configuration Security
- No hardcoded credentials in source code
- Environment-based configuration
- Secure key management patterns
Current State: No authentication on admin endpoints
Recommendations:
- Implement JWT authentication
- Add API key authentication
- Implement role-based access control (RBAC)
- Add session management
Implementation Example:
// Add JWT authentication
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "your-issuer",
ValidAudience = "your-audience",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("your-secret-key"))
};
});
// Add authorization
builder.Services.AddAuthorization(options => {
options.AddPolicy("AdminOnly", policy =>
policy.RequireRole("Admin"));
});Current State: No rate limiting implemented
Recommendations:
- Implement rate limiting for all endpoints
- Use different limits for different user types
- Monitor and log rate limit violations
Implementation Example:
// Add rate limiting
builder.Services.AddRateLimiter(options => {
options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(context =>
RateLimitPartition.GetFixedWindowLimiter(
partitionKey: context.User.Identity?.Name ?? context.Request.Headers.Host.ToString(),
factory: partition => new FixedWindowRateLimiterOptions
{
AutoReplenishment = true,
PermitLimit = 100,
Window = TimeSpan.FromMinutes(1)
}));
});Current State: No CORS configuration
Recommendations:
- Configure appropriate CORS policies
- Restrict allowed origins to your domain
- Use specific methods and headers
Implementation Example:
// Add CORS
builder.Services.AddCors(options => {
options.AddPolicy("AllowedOrigins", policy => {
policy.WithOrigins("https://yourdomain.com", "https://www.yourdomain.com")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});Current State: Basic input validation
Recommendations:
- Implement comprehensive input validation
- Use FluentValidation or similar library
- Validate all user inputs
- Sanitize data before processing
Implementation Example:
// Add FluentValidation
builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddValidatorsFromAssemblyContaining<RegisterDTOValidator>();
// Example validator
public class RegisterDTOValidator : AbstractValidator<RegisterDTO>
{
public RegisterDTOValidator()
{
RuleFor(x => x.Username)
.NotEmpty()
.Length(3, 50)
.Matches("^[a-zA-Z0-9_]+$")
.WithMessage("Username must be 3-50 characters and contain only letters, numbers, and underscores");
RuleFor(x => x.Password)
.NotEmpty()
.MinimumLength(8)
.Matches("[A-Z]").WithMessage("Password must contain at least one uppercase letter")
.Matches("[a-z]").WithMessage("Password must contain at least one lowercase letter")
.Matches("[0-9]").WithMessage("Password must contain at least one number")
.Matches("[^a-zA-Z0-9]").WithMessage("Password must contain at least one special character");
}
}Current State: HTTPS available but not enforced
Recommendations:
- Enforce HTTPS in production
- Use valid SSL certificates
- Implement HSTS (HTTP Strict Transport Security)
- Redirect HTTP to HTTPS
Implementation Example:
// Enforce HTTPS
app.UseHttpsRedirection();
// Add HSTS
app.UseHsts();
// Redirect HTTP to HTTPS
app.Use(async (context, next) =>
{
if (!context.Request.IsHttps)
{
var httpsUrl = $"https://{context.Request.Host}{context.Request.Path}{context.Request.QueryString}";
context.Response.Redirect(httpsUrl, permanent: true);
return;
}
await next();
});Recommendations:
- Use Azure Key Vault, AWS Secrets Manager, or similar
- Never store secrets in configuration files
- Use environment variables for sensitive data
- Implement secret rotation
Implementation Example:
// Azure Key Vault integration
builder.Services.AddAzureKeyVault(
new Uri($"https://{builder.Configuration["KeyVault:Vault"]}.vault.azure.net/"),
new DefaultAzureCredential());
// Use secrets from Key Vault
var connectionString = builder.Configuration["ConnectionStrings:MongoDB"];
var encryptionKey = builder.Configuration["SecuritySettings:AesEncryptionKey"];Recommendations:
- Implement comprehensive audit trails
- Log all authentication attempts
- Log all admin actions
- Store logs securely
- Implement log retention policies
Implementation Example:
// Add structured logging
builder.Services.AddLogging(logging =>
{
logging.AddSerilog(new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs/redzauth-.log", rollingInterval: RollingInterval.Day)
.CreateLogger());
});
// Audit logging service
public interface IAuditLogger
{
void LogAuthenticationAttempt(string username, string ip, bool success, string reason);
void LogAdminAction(string admin, string action, string target, string details);
}Current State: AES encryption for sensitive data
Recommendations:
- Encrypt data at rest
- Use strong encryption algorithms
- Implement key rotation
- Secure key storage
Implementation Example:
// Encrypt sensitive data before storing
public string EncryptSensitiveData(string data)
{
using var aes = Aes.Create();
aes.Key = Convert.FromBase64String(_encryptionKey);
aes.GenerateIV();
using var encryptor = aes.CreateEncryptor();
using var msEncrypt = new MemoryStream();
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
using var swEncrypt = new StreamWriter(csEncrypt);
swEncrypt.Write(data);
swEncrypt.Close();
return Convert.ToBase64String(aes.IV.Concat(msEncrypt.ToArray()).ToArray());
}Recommendations:
- Use MongoDB authentication
- Implement network-level security
- Regular database backups
- Monitor database access
- Use connection pooling
Implementation Example:
// Secure MongoDB connection
var connectionString = builder.Configuration.GetConnectionString("MongoDB");
var settings = MongoClientSettings.FromConnectionString(connectionString);
settings.ServerSelectionTimeout = TimeSpan.FromSeconds(5);
settings.ConnectTimeout = TimeSpan.FromSeconds(10);
settings.MaxConnectionPoolSize = 100;
settings.MinConnectionPoolSize = 10;
var client = new MongoClient(settings);Configuration:
- Environment variables configured for all sensitive data
- Secrets management system in place
- Database connection with proper authentication
- HTTPS certificates configured
- CORS policies defined
Security:
- Authentication system implemented
- Authorization policies configured
- Rate limiting enabled
- Input validation enhanced
- Audit logging implemented
Monitoring:
- Structured logging configured
- Monitoring and alerting set up
- Health checks implemented
- Error tracking configured
Testing:
- Security testing completed
- Penetration testing performed
- Vulnerability scanning done
- Load testing completed
Regular Tasks:
- Update dependencies monthly
- Review security logs weekly
- Monitor for suspicious activity
- Backup data regularly
- Test disaster recovery procedures
- NEVER deploy without authentication - The current admin endpoints are unprotected
- ALWAYS use HTTPS in production - HTTP exposes sensitive data
- REGULARLY update dependencies - Keep packages updated for security patches
- MONITOR logs and access - Watch for suspicious activity
- BACKUP your database - Implement regular backup procedures
- TEST security measures - Verify all security controls work as expected
- OWASP ZAP - Web application security scanner
- Burp Suite - Web application security testing
- Nmap - Network security scanner
- MongoDB Compass - Database security testing
- SQL injection testing
- XSS vulnerability testing
- CSRF protection testing
- Authentication bypass testing
- Rate limiting testing
- Input validation testing
If you need help implementing any of these security measures:
- Review the Setup Guide for basic configuration
- Check the Configuration Guide for advanced settings
- Create an issue in the GitHub repository
- Consult security documentation for your chosen authentication method
Remember: Security is an ongoing process, not a one-time implementation. Regularly review and update your security measures as threats evolve.