badmoodle icon indicating copy to clipboard operation
badmoodle copied to clipboard

Badmoodle - Modernized

Open sumanrox opened this issue 1 month ago • 0 comments

Badmoodle Project Changes

Overview

This document summarizes all changes made to modernize and improve the badmoodle Moodle vulnerability scanner project.


1. Bug Fixes

1.1 Fixed ModuleNotFoundError

Issue: utils was not recognized as a package, causing import errors.

Fix:

  • Created/verified __init__.py files in:
    • utils/
    • lib/
    • vulns/

Impact: All imports now work correctly across the codebase.

1.2 Fixed ANSI Color Code Bug

File: utils/output.py

Issue: ORANGE color used incorrect ANSI escape sequence syntax.

Fix: Changed '\033[38:5:130m' to '\033[38;5;130m' (colon to semicolon)

Impact: Orange color now displays correctly in terminal output.


2. Code Modernization

2.1 Network Operations Improvements

Files: lib/update.py, lib/version.py

Changes:

  • Added timeout parameters (10-15 seconds) to all requests.get() calls
  • Implemented requests.raise_for_status() for better error detection
  • Added proper exception handling with requests.exceptions.RequestException
  • Used .json() method instead of json.loads(.text) for JSON parsing

Example:

# Before
r = requests.get(url)
data = json.loads(r.text)

# After
r = requests.get(url, timeout=10)
r.raise_for_status()
data = r.json()

2.2 File Operations Enhancement

Files: lib/update.py, lib/version.py

Changes:

  • Added explicit encoding='utf-8' to all open() calls

Example:

# Before
with open(filename, 'w') as f:

# After
with open(filename, 'w', encoding='utf-8') as f:

Impact: Ensures consistent behavior across different platforms and locales.

2.3 String Formatting Updates

Files: Multiple files throughout codebase

Changes:

  • Converted old-style string formatting to f-strings

Example:

# Before
print('Error: {}'.format(message))

# After
print(f'Error: {message}')

2.4 Code Style Consistency

Files: All Python files

Changes:

  • Converted all tabs to spaces (4-space indentation)
  • Fixed 1160+ linting errors
  • Resolved E701 violations (multiple statements on one line)
  • Changed bare except: to specific exception types
  • Fixed E402 import order issues

Example:

# Before
except:
    pass

# After
except Exception:
    pass

2.5 Import Structure Improvements

File: badmoodle.py

Changes:

  • Reorganized imports to follow PEP 8 standards:
    1. Standard library imports
    2. Third-party imports
    3. Local imports

3. Dependency Management

3.1 Updated requirements.txt

Changes:

requests>=2.31.0
beautifulsoup4>=4.12.2
pytest>=7.4.0
ruff>=0.1.0

Impact:

  • Pinned dependencies to modern, secure versions
  • Added minimum version constraints
  • Included development dependencies

4. Testing Infrastructure

4.1 Created Test Suite

New Files:

  • tests/__init__.py - Test package marker
  • tests/test_version.py - 5 tests for version checking logic
  • tests/test_update.py - 8 tests for parsing functions

Test Coverage:

  • Version range checking (check_in_range)
  • Version string parsing (parse_versions)
  • CVE extraction (parse_cves)
  • Edge case handling

Results: 13 tests with 100% pass rate


5. Code Quality & Linting

5.1 Configured Ruff Linter

New File: pyproject.toml

Configuration:

  • Rules: E (errors), F (pyflakes), W (warnings), I (import sorting)
  • Ignored: F403/F405 (star imports), E501 (long lines), E402 (imports after code)

Results:

  • Before: 1160+ linting errors
  • After: 0 linting errors

5.2 Updated .gitignore

Changes:

  • Added .ruff_cache/ to exclusions

6. Continuous Integration

6.1 Enhanced GitHub Actions Workflow

File: .github/workflows/ci.yml

Changes:

  • Python versions tested: 3.8, 3.9, 3.10, 3.11, 3.12, 3.13
  • Jobs:
    1. Test Job: Runs syntax checks and pytest suite on all Python versions
    2. Lint Job: Runs ruff linter to ensure code quality
  • Triggers: Push to main/master, pull requests

7. Project Configuration

7.1 Created pyproject.toml

Sections:

  • Build system: setuptools with modern backend
  • Pytest configuration: Test discovery in tests/ directory
  • Ruff configuration: Linting rules and exclusions
  • Python version: >=3.8

8. Documentation Updates

8.1 Enhanced README.md

New Sections:

  • Setup: Virtual environment setup instructions
  • Testing: Guide with multiple pytest examples
  • Code Quality: Standards documentation
  • Linting: Instructions for running ruff
  • CI/CD: Information about automated checks

9. Automatic Cache Cleanup

9.1 Implemented Auto-Cleanup Feature

File: badmoodle.py

Changes:

  • Added atexit, shutil, and pathlib.Path imports
  • Created cleanup_pycache() function
  • Registered cleanup with atexit.register()

Functionality:

  • Automatically removes on exit:
    • __pycache__/ directories
    • .pytest_cache/ directories
    • .ruff_cache/ directories
    • .pyc and .pyo compiled files
  • Preserves .venv virtual environment
  • Silently handles errors (non-critical operation)

Impact: Project directory stays clean after every run without manual intervention.


10. Cache Cleanup

10.1 Removed All Cache Files

Removed:

  • __pycache__/ directories (root, lib/, utils/, vulns/, tests/)
  • .pytest_cache/
  • .ruff_cache/
  • All .pyc and .pyo files

Preserved:

  • .venv/ virtual environment
  • .git/ repository data

Summary Statistics

Before Modernization

  • ❌ ModuleNotFoundError on import
  • ❌ No automated tests
  • ❌ 1160+ linting errors
  • ❌ Mixed tabs and spaces
  • ❌ Manual cache cleanup required

After Modernization

  • ✅ All imports working correctly
  • ✅ 13 tests passing (100% pass rate)
  • ✅ 0 linting errors
  • ✅ Consistent 4-space indentation
  • ✅ All syntax checks passing
  • ✅ Modern code practices throughout
  • ✅ Automatic cache cleanup on exit

Compatibility

  • Python Versions: 3.8, 3.9, 3.10, 3.11, 3.12, 3.13
  • Operating Systems: Linux, macOS, Windows
  • Dependencies: Modern, secure versions with minimum constraints

Verification Commands

To verify all improvements:

# Test all functionality
pytest tests/ -v

# Verify linting
ruff check .

# Syntax validation
python -m py_compile badmoodle.py lib/*.py utils/*.py vulns/*.py

# Run the tool
python badmoodle.py -h
python badmoodle.py -m

# Verify auto-cleanup (no cache should remain)
python badmoodle.py -h
find . -type d \( -name "__pycache__" -o -name ".pytest_cache" -o -name ".ruff_cache" \) ! -path "*/.venv/*"

Date

Changes completed: November 14, 2025

sumanrox avatar Nov 14 '25 18:11 sumanrox