Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AdminPathPatterns = []string{
"/admin",
"/management",
"/internal",
"/api/admin",
"/api/v*/admin",
"/debug",
"/metrics",
"/actuator",
"/console",
"/dashboard",
"/settings",
"/config",
"/system",
}
AdminPathPatterns are URL patterns that indicate admin endpoints.
var AuthMiddlewarePatterns = map[string][]string{
"gin": {
"authMiddleware", "AuthMiddleware", "jwt.Auth", "jwt.New",
"sessions.Sessions", "Auth()", "RequireAuth", "AuthRequired",
"gin-jwt", "gin-contrib/sessions",
},
"echo": {
"middleware.JWT", "middleware.KeyAuth", "middleware.BasicAuth",
"echojwt.JWT", "session.Middleware", "Auth()", "AuthMiddleware",
},
"chi": {
"jwtauth", "httpauth", "session", "Auth()", "AuthMiddleware",
},
"fiber": {
"jwtware", "basicauth", "session", "Auth()", "AuthMiddleware",
},
"fastapi": {
"Depends(get_current_user)", "Depends(get_current_active_user)",
"HTTPBearer", "OAuth2PasswordBearer", "Security(",
"api_key_header", "get_api_key", "verify_token",
},
"flask": {
"@login_required", "login_required", "@jwt_required",
"current_user", "flask_login", "flask_jwt", "flask_httpauth",
},
"django": {
"@login_required", "permission_required", "IsAuthenticated",
"IsAdminUser", "@permission_classes", "authentication_classes",
},
"express": {
"passport.authenticate", "jwt.verify", "express-jwt",
"isAuthenticated", "requireAuth", "authMiddleware",
},
"nestjs": {
"@UseGuards", "AuthGuard", "JwtAuthGuard", "@ApiBearerAuth",
"RolesGuard", "@Roles", "PermissionsGuard",
},
}
AuthMiddlewarePatterns contains patterns for auth middleware by framework.
var AuthzMiddlewarePatterns = map[string][]string{
"gin": {
"rbac", "casbin", "permission", "authorize", "checkRole",
"RequireRole", "HasPermission", "ACL", "CanAccess",
},
"echo": {
"casbin", "rbac", "authorize", "checkPermission",
},
"chi": {
"casbin", "rbac", "authorize", "permission",
},
"fiber": {
"casbin", "rbac", "authorize", "permission",
},
"fastapi": {
"RoleChecker", "PermissionChecker", "has_permission",
"check_permission", "require_role", "authorize",
},
"flask": {
"@roles_required", "@permission_required", "has_role",
"check_permission", "flask_principal",
},
"django": {
"@permission_required", "has_perm", "user_passes_test",
"PermissionRequiredMixin", "django-guardian",
},
"express": {
"checkRole", "hasPermission", "authorize", "rbac",
"accessControl", "acl",
},
"nestjs": {
"@Roles", "RolesGuard", "PermissionsGuard", "@SetMetadata",
"@UseGuards(RolesGuard)", "casl",
},
}
AuthzMiddlewarePatterns contains patterns for authorization middleware.
var MutationMethods = []string{"POST", "PUT", "PATCH", "DELETE"}
MutationMethods are HTTP methods that modify data.
var RoutePatterns = map[string]*RouteDetector{
"gin": {
ImportPattern: `"github.com/gin-gonic/gin"`,
RoutePattern: `\.(?:GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD)\s*\(\s*"([^"]+)"`,
GroupPattern: `\.Group\s*\(\s*"([^"]+)"`,
HandlerPattern: `\.(?:GET|POST|PUT|PATCH|DELETE)\s*\([^,]+,\s*(\w+)`,
},
"echo": {
ImportPattern: `"github.com/labstack/echo`,
RoutePattern: `\.(?:GET|POST|PUT|PATCH|DELETE)\s*\(\s*"([^"]+)"`,
GroupPattern: `\.Group\s*\(\s*"([^"]+)"`,
HandlerPattern: `\.(?:GET|POST|PUT|PATCH|DELETE)\s*\([^,]+,\s*(\w+)`,
},
"chi": {
ImportPattern: `"github.com/go-chi/chi`,
RoutePattern: `\.(?:Get|Post|Put|Patch|Delete)\s*\(\s*"([^"]+)"`,
GroupPattern: `\.Route\s*\(\s*"([^"]+)"`,
HandlerPattern: `\.(?:Get|Post|Put|Patch|Delete)\s*\([^,]+,\s*(\w+)`,
},
"fiber": {
ImportPattern: `"github.com/gofiber/fiber`,
RoutePattern: `\.(?:Get|Post|Put|Patch|Delete)\s*\(\s*"([^"]+)"`,
GroupPattern: `\.Group\s*\(\s*"([^"]+)"`,
HandlerPattern: `\.(?:Get|Post|Put|Patch|Delete)\s*\([^,]+,\s*(\w+)`,
},
"fastapi": {
ImportPattern: `from fastapi|import fastapi`,
RoutePattern: `@(?:app|router)\.(?:get|post|put|patch|delete)\s*\(\s*["']([^"']+)["']`,
GroupPattern: `APIRouter\s*\(\s*prefix\s*=\s*["']([^"']+)["']`,
HandlerPattern: `def\s+(\w+)\s*\(`,
},
"flask": {
ImportPattern: `from flask|import flask`,
RoutePattern: `@(?:app|bp)\.route\s*\(\s*["']([^"']+)["']`,
GroupPattern: `Blueprint\s*\(\s*\w+\s*,\s*\w+\s*,\s*url_prefix\s*=\s*["']([^"']+)["']`,
HandlerPattern: `def\s+(\w+)\s*\(`,
},
"express": {
ImportPattern: `require\s*\(\s*['"]express['"]|from\s+['"]express['"]`,
RoutePattern: `\.(?:get|post|put|patch|delete)\s*\(\s*['"]([^'"]+)['"]`,
GroupPattern: `Router\s*\(\s*\)`,
HandlerPattern: `\.(?:get|post|put|patch|delete)\s*\([^,]+,\s*(?:async\s+)?(?:function\s+)?(\w+)?`,
},
"nestjs": {
ImportPattern: `from\s+['"]@nestjs/`,
RoutePattern: `@(?:Get|Post|Put|Patch|Delete)\s*\(\s*['"]?([^'")\s]*)['"]?\s*\)`,
GroupPattern: `@Controller\s*\(\s*['"]([^'"]+)['"]`,
HandlerPattern: `(?:async\s+)?(\w+)\s*\(`,
},
}
RoutePatterns contains patterns for detecting routes by framework.
var SensitivePathPatterns = []string{
"/user",
"/users",
"/account",
"/profile",
"/password",
"/auth",
"/token",
"/session",
"/payment",
"/billing",
"/order",
"/cart",
}
SensitivePathPatterns are URL patterns that handle sensitive data.
Functions ¶
func IsAdminPath ¶
IsAdminPath checks if a path appears to be an admin endpoint.
func IsMutationMethod ¶
IsMutationMethod checks if method modifies data.
func IsSensitivePath ¶
IsSensitivePath checks if a path handles sensitive data.
Types ¶
type AuthCheckerImpl ¶
type AuthCheckerImpl struct {
// contains filtered or unexported fields
}
AuthCheckerImpl implements the safety.AuthChecker interface.
Description:
AuthCheckerImpl detects endpoints missing authentication or authorization middleware. It supports multiple web frameworks including Gin, Echo, FastAPI, Flask, NestJS, and Express.
Thread Safety:
AuthCheckerImpl is safe for concurrent use after initialization.
func NewAuthChecker ¶
func NewAuthChecker(g *graph.Graph, idx *index.SymbolIndex) *AuthCheckerImpl
NewAuthChecker creates a new auth checker.
Description:
Creates a checker with support for multiple web frameworks.
Inputs:
g - The code graph. idx - The symbol index.
Outputs:
*AuthCheckerImpl - The configured checker.
func (*AuthCheckerImpl) CheckAuthEnforcement ¶
func (c *AuthCheckerImpl) CheckAuthEnforcement( ctx context.Context, scope string, opts ...safety.AuthCheckOption, ) (*safety.AuthCheck, error)
CheckAuthEnforcement checks auth on endpoints.
Description:
Analyzes HTTP handlers and routes to verify they have proper authentication and authorization middleware. Detects: - Endpoints without authentication - Endpoints without authorization (especially admin/mutation) - Admin endpoints without any protection
Inputs:
ctx - Context for cancellation and timeout. scope - The scope to check (package path). opts - Optional configuration (framework hint, check type).
Outputs:
*safety.AuthCheck - The check result with endpoints and issues. error - Non-nil if scope not found or operation canceled.
Errors:
safety.ErrInvalidInput - Scope is empty. safety.ErrContextCanceled - Context was canceled.
Thread Safety:
This method is safe for concurrent use.
func (*AuthCheckerImpl) ClearFileCache ¶
func (c *AuthCheckerImpl) ClearFileCache()
ClearFileCache clears the file content cache.
func (*AuthCheckerImpl) SetFileContent ¶
func (c *AuthCheckerImpl) SetFileContent(filePath, content string)
SetFileContent sets file content for checking.
type DetectedRoute ¶
DetectedRoute represents a detected HTTP route.
type RouteDetector ¶
type RouteDetector struct {
ImportPattern string
RoutePattern string
GroupPattern string
HandlerPattern string
// contains filtered or unexported fields
}
RouteDetector contains compiled patterns for route detection.
func (*RouteDetector) Compile ¶
func (r *RouteDetector) Compile()
Compile compiles all patterns.
Description:
Compiles regex patterns for route detection. Invalid patterns will result in nil compiled regex and a warning is logged. Compilation is idempotent and safe to call multiple times.
func (*RouteDetector) DetectFramework ¶
func (r *RouteDetector) DetectFramework(content string) bool
DetectFramework detects the web framework used in content.
func (*RouteDetector) FindRoutes ¶
func (r *RouteDetector) FindRoutes(content string) []DetectedRoute
FindRoutes finds all routes in content.