Nija

Security Hardening Guide

Overview

This document outlines the security hardening measures implemented in NIJA to protect the trading bot, user data, and API credentials.

Automated Security Scanning

CodeQL Analysis

NIJA uses GitHub’s CodeQL to perform automated security scanning of the codebase.

Workflow: .github/workflows/codeql.yml

Features:

View Results:

Dependency Vulnerability Scanning

Workflow: .github/workflows/security-scan.yml

Tools Used:

  1. Safety - Python dependency vulnerability scanner
  2. Bandit - Python security linting
  3. TruffleHog - Secret scanning in git history

Scanning Schedule:

Security Best Practices Checklist

✅ API Key Security

Required Environment Variables:

COINBASE_API_KEY=your_key_here
COINBASE_API_SECRET=your_secret_here
COINBASE_PEM_CONTENT=your_pem_content_here

✅ Input Validation

Example:

from pydantic import BaseModel, validator

class WebhookSignal(BaseModel):
    symbol: str
    action: str
    price: float

    @validator('action')
    def validate_action(cls, v):
        if v not in ['buy', 'sell']:
            raise ValueError('Invalid action')
        return v

✅ Network Security

✅ Error Handling

Example:

import logging

logger = logging.getLogger(__name__)

try:
    response = api_call()
except Exception as e:
    # DON'T: logger.error(f"API failed: {api_key}")
    # DO: logger.error(f"API call failed: {type(e).__name__}")

✅ Access Control

✅ Code Security

✅ Trading Security

Example:

from bot.risk_manager import RiskManager

risk_manager = RiskManager(
    max_position_size_pct=5.0,
    max_daily_loss_pct=5.0,
    max_drawdown_pct=12.0
)

# Validate trade before execution
if not risk_manager.validate_trade(size, price):
    logger.warning("Trade rejected by risk manager")
    return

Security Monitoring

Real-Time Alerts

Configure alerts for:

Security Logs

Monitor these log files:

Log Retention:

Incident Response

Security Incident Steps

  1. Immediate Actions:
    • Stop the trading bot: ./stop_bot.sh
    • Disable API keys in broker account
    • Assess the scope of the incident
  2. Investigation:
    • Review security logs
    • Check recent trade history
    • Identify compromised credentials
    • Document timeline of events
  3. Remediation:
    • Rotate all API keys and secrets
    • Update .env file with new credentials
    • Patch identified vulnerabilities
    • Review and update security policies
  4. Recovery:
    • Test bot with new credentials
    • Restart trading in paper mode first
    • Monitor closely for 24-48 hours
    • Document lessons learned

Emergency Contacts

Security Updates

Dependency Updates

Monthly Security Updates:

# Check for outdated packages
pip list --outdated

# Update specific vulnerable packages
pip install --upgrade package-name

# Update requirements.txt
pip freeze > requirements.txt

Security Patch Process

  1. Monitor GitHub Security Advisories
  2. Review Dependabot alerts weekly
  3. Test patches in development environment
  4. Deploy to production with monitoring

Compliance Considerations

Data Protection

Audit Trail

Regulatory Compliance

Security Review Schedule

Additional Resources

Questions or Issues?

If you discover a security vulnerability:

  1. DO NOT open a public issue
  2. Email security contact privately
  3. Provide detailed information
  4. Allow time for patch before disclosure

For general security questions, refer to SECURITY.md.