Skip to content

Security: pyfunc/load

Security

docs/security.md

🔒 Security Considerations

This document outlines important security considerations when using the load module.

Table of Contents

Installation Security

Package Verification

Always verify the authenticity of the package before installation:

# Verify package signature (if available)
pip install --require-hashes -r requirements.txt

Source Verification

When installing from source:

  1. Verify the repository URL
  2. Check commit hashes
  3. Review the source code before installation

Code Execution

Dynamic Imports

The load module performs dynamic imports, which can execute arbitrary code. Be cautious when:

  • Loading modules from untrusted sources
  • Using user-provided module names
  • Loading modules with side effects

Example of Safe Usage

# Safe: Using a whitelist of allowed modules
ALLOWED_MODULES = {'json', 'math', 'collections'}

def safe_load(module_name):
    if module_name not in ALLOWED_MODULES:
        raise ValueError(f"Module {module_name} is not allowed")
    return load(module_name)

Dependency Management

Version Pinning

Always pin your dependencies to specific versions:

# Good
load.install('numpy==1.21.0')

# Potentially unsafe
load.install('numpy')

Dependency Confusion

Be aware of dependency confusion attacks. Always:

  1. Use private package indexes when available
  2. Configure pip to prioritize your private index
  3. Use hashes in requirements files

Best Practices

Principle of Least Privilege

Run your application with the minimum required permissions:

  • Avoid running as root/administrator
  • Use virtual environments
  • Set appropriate file permissions

Input Validation

Always validate and sanitize inputs when they affect module loading:

import re

def is_valid_module_name(name):
    return bool(re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', name))

Logging and Monitoring

Enable logging to detect suspicious activities:

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('load.security')

# Log module loading
try:
    module = load(module_name)
except Exception as e:
    logger.warning(f"Failed to load module {module_name}: {e}")
    raise

Secure Configuration

Environment Variables

Use environment variables for sensitive configuration:

import os

# Load configuration from environment
MAX_MODULE_SIZE = int(os.getenv('MAX_MODULE_SIZE', '10485760'))  # 10MB default

Resource Limits

Set reasonable limits to prevent resource exhaustion:

import resource

# Set memory limit (in bytes)
resource.setrlimit(resource.RLIMIT_AS, (1_000_000_000, 1_000_000_000))  # 1GB

Reporting Vulnerabilities

If you discover a security vulnerability in this project:

  1. Do not create a public issue
  2. Email [email protected] with details
  3. Include steps to reproduce the issue
  4. We will respond within 48 hours

Security Checklist

Before deploying to production:

  • All dependencies are pinned to specific versions
  • Input validation is in place for dynamic imports
  • Appropriate file permissions are set
  • Logging is enabled for security events
  • Dependencies are regularly updated
  • Security headers are configured (for web applications)

Additional Resources

Changelog

Security Fixes

  • 1.0.0: Initial security guidelines

Last updated: June 2025

There aren’t any published security advisories