This document summarizes the implementation of Issue #28, which addresses the security gap where authentication was not properly enforced when enabled. The changes ensure that when authentication is enabled, only explicitly public endpoints are accessible without authentication.
File: src/auth_middleware.cpp
Changes:
initializeDefaultPaths() to only include truly essential endpoints as public by default/api/health - Health check endpoint/api/status - Basic server status/api/models, /api/models/types, /api/models/directories/api/samplers, /api/schedulers, /api/parametersSecurity Impact: Prevents unauthorized access to model information and system capabilities when authentication is enabled.
File: src/auth_middleware.cpp
Changes:
requiresAuthentication() to follow an "authentication-first" approachpublicPaths are accessible without authenticationBefore:
bool AuthMiddleware::requiresAuthentication(const std::string& path) const {
// Check if path is public
if (pathMatchesPattern(path, m_config.publicPaths)) {
return false;
}
// If authentication is enabled (not NONE), then all paths except public ones require authentication
return !isAuthenticationDisabled();
}
After:
bool AuthMiddleware::requiresAuthentication(const std::string& path) const {
// First check if authentication is completely disabled
if (isAuthenticationDisabled()) {
return false;
}
// Authentication is enabled, check if path is explicitly public
if (pathMatchesPattern(path, m_config.publicPaths)) {
return false;
}
// All other paths require authentication when auth is enabled
return true;
}
Files:
include/server_config.hsrc/auth_middleware.cppsrc/main.cppChanges:
customPublicPaths field to AuthConfig structure--public-paths command line optionUsage Examples:
# Default behavior (only health and status are public)
./stable-diffusion-rest --models-dir /data/SD_MODELS --auth jwt
# Custom public paths
./stable-diffusion-rest --models-dir /data/SD_MODELS --auth jwt --public-paths "/api/health,/api/status,/api/models"
# Make all endpoints public (not recommended for production)
./stable-diffusion-rest --models-dir /data/SD_MODELS --auth jwt --public-paths "/"
File: src/server.cpp
Changes:
/api/samplers, /api/schedulers, /api/parameters/api/models, /api/models/types, /api/models/directories/api/models/stats/api/queue/status, /api/queue/job/{id}Security Impact: Ensures all potentially sensitive endpoints require authentication when auth is enabled.
File: AUTHENTICATION_SECURITY_GUIDE.md
Changes:
Files:
test_auth_security_simple.cppsrc/CMakeLists.txtChanges:
--public-paths if needed to maintain compatibility./stable-diffusion-rest --models-dir /data/SD_MODELS --auth jwt --public-paths "/api/health,/api/status,/api/docs,/api/openapi.json"
./stable-diffusion-rest --models-dir /data/SD_MODELS --auth jwt --public-paths "/api/health,/api/status,/api/queue/status"
./stable-diffusion-rest --models-dir /data/SD_MODELS --auth jwt --public-paths "/api/health,/api/status,/api/models,/api/samplers"
# Configure build with tests enabled
cmake -DBUILD_AUTH_SECURITY_TEST=ON ..
# Build the test
cmake --build . --target test_auth_security_simple
# Run the tests
./build/bin/test_auth_security_simple
The test suite covers:
--public-paths <paths>: Comma-separated list of public paths that don't require authentication
/api/health,/api/status when auth is enabled--public-paths "/api/health,/api/status,/api/models"Authentication Options:
--auth <method> Authentication method (none, jwt, api-key, unix, pam, optional)
--auth-method <method> Authentication method (alias for --auth)
--jwt-secret <secret> JWT secret key (auto-generated if not provided)
--jwt-expiration <minutes> JWT token expiration time (default: 60)
--enable-guest-access Allow unauthenticated guest access
--pam-service-name <name> PAM service name (default: stable-diffusion-rest)
--auth-data-dir <dir> Directory for authentication data (default: ./auth)
--public-paths <paths> Comma-separated list of public paths (default: /api/health,/api/status)
The implementation of Issue #28 significantly improves the security posture of the stable-diffusion.cpp-rest server by ensuring that when authentication is enabled, login is truly forced and only explicitly configured endpoints remain publicly accessible. The changes provide administrators with fine-grained control over public endpoints while maintaining a secure default configuration.
The implementation is backward compatible through the --public-paths configuration option, allowing existing deployments to migrate at their own pace while immediately benefiting from the improved security defaults.