Skip to content

Security Guide

REDZ edited this page Aug 22, 2025 · 2 revisions

πŸ“‹ Overview

This guide covers security considerations, best practices, and recommendations for deploying the REDZAuth API in production environments.

⚠️ Security Notice

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!

πŸ”’ Current Security Features

βœ… Implemented Security Measures

  1. Password Security

    • SHA256 hashing for password storage
    • No plain-text passwords are stored in the database
    • Secure password comparison
  2. HWID Security

    • Hardware ID binding for device-specific access
    • HWID mismatch detection
    • Admin-controlled HWID reset functionality
  3. Blacklist System

    • Comprehensive ban/unban functionality
    • IP, username, and HWID-based banning
    • Audit trail for all ban operations
  4. Webhook Security

    • Configurable webhook URLs
    • Graceful error handling for webhook failures
    • No sensitive data in webhook payloads
  5. Configuration Security

    • No hardcoded credentials in source code
    • Environment-based configuration
    • Secure key management patterns

🚨 Security Considerations for Production

1. Authentication & Authorization

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"));
});

2. Rate Limiting

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)
            }));
});

3. CORS Configuration

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();
    });
});

4. Input Validation

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");
    }
}

5. HTTPS Enforcement

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();
});

πŸ” Advanced Security Measures

1. Secrets Management

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"];

2. Audit Logging

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);
}

3. Data Encryption

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());
}

4. Database Security

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);

πŸ“‹ Security Checklist

Pre-Production Checklist

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

Ongoing Security

Regular Tasks:

  • Update dependencies monthly
  • Review security logs weekly
  • Monitor for suspicious activity
  • Backup data regularly
  • Test disaster recovery procedures

🚨 Critical Security Warnings

  1. NEVER deploy without authentication - The current admin endpoints are unprotected
  2. ALWAYS use HTTPS in production - HTTP exposes sensitive data
  3. REGULARLY update dependencies - Keep packages updated for security patches
  4. MONITOR logs and access - Watch for suspicious activity
  5. BACKUP your database - Implement regular backup procedures
  6. TEST security measures - Verify all security controls work as expected

πŸ”§ Security Testing

Recommended Testing Tools

  1. OWASP ZAP - Web application security scanner
  2. Burp Suite - Web application security testing
  3. Nmap - Network security scanner
  4. MongoDB Compass - Database security testing

Testing Checklist

  • SQL injection testing
  • XSS vulnerability testing
  • CSRF protection testing
  • Authentication bypass testing
  • Rate limiting testing
  • Input validation testing

πŸ“ž Security Support

If you need help implementing any of these security measures:

  1. Review the Setup Guide for basic configuration
  2. Check the Configuration Guide for advanced settings
  3. Create an issue in the GitHub repository
  4. Consult security documentation for your chosen authentication method

πŸ”— Related Documentation


Remember: Security is an ongoing process, not a one-time implementation. Regularly review and update your security measures as threats evolve.